2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-06-29 19:28:06 +01:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-09 14:53:04 +01:00
|
|
|
"net"
|
2019-03-22 17:09:37 +00:00
|
|
|
"time"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2019-01-30 20:47:21 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2018-06-29 19:28:06 +01:00
|
|
|
)
|
|
|
|
|
2018-12-22 04:51:42 +00:00
|
|
|
// Observer implements the ConnSuccess and ConnFailure methods
|
|
|
|
// for Discovery and other services to use
|
|
|
|
type Observer interface {
|
|
|
|
ConnSuccess(ctx context.Context, node *pb.Node)
|
|
|
|
ConnFailure(ctx context.Context, node *pb.Node, err error)
|
|
|
|
}
|
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
// Client defines the interface to an transport client.
|
|
|
|
type Client interface {
|
|
|
|
DialNode(ctx context.Context, node *pb.Node, opts ...grpc.DialOption) (*grpc.ClientConn, error)
|
|
|
|
DialAddress(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error)
|
2019-07-17 19:14:44 +01:00
|
|
|
FetchPeerIdentity(ctx context.Context, node *pb.Node, opts ...grpc.DialOption) (*identity.PeerIdentity, error)
|
2019-01-30 20:47:21 +00:00
|
|
|
Identity() *identity.FullIdentity
|
2019-04-03 20:13:39 +01:00
|
|
|
WithObservers(obs ...Observer) Client
|
2019-06-26 14:16:46 +01:00
|
|
|
AlertSuccess(ctx context.Context, node *pb.Node)
|
|
|
|
AlertFail(ctx context.Context, node *pb.Node, err error)
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 12:26:25 +01:00
|
|
|
// Timeouts contains all of the timeouts configurable for a transport
|
|
|
|
type Timeouts struct {
|
|
|
|
Request time.Duration
|
|
|
|
Dial time.Duration
|
|
|
|
}
|
|
|
|
|
2018-06-29 19:28:06 +01:00
|
|
|
// Transport interface structure
|
|
|
|
type Transport struct {
|
2019-05-10 12:26:25 +01:00
|
|
|
tlsOpts *tlsopts.Options
|
|
|
|
observers []Observer
|
|
|
|
timeouts Timeouts
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
|
|
|
|
2019-03-22 17:09:37 +00:00
|
|
|
// NewClient returns a transport client with a default timeout for requests
|
2019-02-11 11:17:32 +00:00
|
|
|
func NewClient(tlsOpts *tlsopts.Options, obs ...Observer) Client {
|
2019-05-10 12:26:25 +01:00
|
|
|
return NewClientWithTimeouts(tlsOpts, Timeouts{}, obs...)
|
2019-03-22 17:09:37 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 12:26:25 +01:00
|
|
|
// NewClientWithTimeouts returns a transport client with a specified timeout for requests
|
|
|
|
func NewClientWithTimeouts(tlsOpts *tlsopts.Options, timeouts Timeouts, obs ...Observer) Client {
|
|
|
|
if timeouts.Request == 0 {
|
|
|
|
timeouts.Request = defaultRequestTimeout
|
|
|
|
}
|
|
|
|
if timeouts.Dial == 0 {
|
|
|
|
timeouts.Dial = defaultDialTimeout
|
|
|
|
}
|
|
|
|
|
2018-12-22 04:51:42 +00:00
|
|
|
return &Transport{
|
2019-05-10 12:26:25 +01:00
|
|
|
tlsOpts: tlsOpts,
|
|
|
|
timeouts: timeouts,
|
|
|
|
observers: obs,
|
2018-12-22 04:51:42 +00:00
|
|
|
}
|
2018-07-19 15:48:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:34:08 +00:00
|
|
|
// DialNode returns a grpc connection with tls to a node.
|
|
|
|
//
|
|
|
|
// Use this method for communicating with nodes as it is more secure than
|
|
|
|
// DialAddress. The connection will be established successfully only if the
|
|
|
|
// target node has the private key for the requested node ID.
|
2018-11-19 14:40:01 +00:00
|
|
|
func (transport *Transport) DialNode(ctx context.Context, node *pb.Node, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
|
2019-06-07 23:34:16 +01:00
|
|
|
defer mon.Task()(&ctx, "node: "+node.Id.String()[0:8])(&err)
|
2019-04-03 20:13:39 +01:00
|
|
|
|
2018-08-24 05:01:03 +01:00
|
|
|
if node.Address == nil || node.Address.Address == "" {
|
2018-06-29 19:28:06 +01:00
|
|
|
return nil, Error.New("no address")
|
|
|
|
}
|
2019-03-04 20:03:33 +00:00
|
|
|
dialOption, err := transport.tlsOpts.DialOption(node.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-26 18:35:16 +00:00
|
|
|
options := append([]grpc.DialOption{
|
2019-03-04 20:03:33 +00:00
|
|
|
dialOption,
|
2019-02-26 18:35:16 +00:00
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.FailOnNonTempDialError(true),
|
2019-05-09 14:53:04 +01:00
|
|
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-10 12:26:25 +01:00
|
|
|
return &timeoutConn{conn: conn, timeout: transport.timeouts.Request}, nil
|
2019-05-09 14:53:04 +01:00
|
|
|
}),
|
2019-02-26 18:35:16 +00:00
|
|
|
}, opts...)
|
2018-11-20 16:54:52 +00:00
|
|
|
|
2019-05-10 12:26:25 +01:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, transport.timeouts.Dial)
|
2019-03-20 10:11:07 +00:00
|
|
|
defer cancel()
|
2018-12-22 04:51:42 +00:00
|
|
|
|
2019-03-19 17:37:26 +00:00
|
|
|
conn, err = grpc.DialContext(timedCtx, node.GetAddress().Address, options...)
|
2018-12-22 04:51:42 +00:00
|
|
|
if err != nil {
|
2019-02-06 13:19:14 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-26 14:16:46 +01:00
|
|
|
transport.AlertFail(timedCtx, node, err)
|
2018-12-22 04:51:42 +00:00
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:46 +01:00
|
|
|
transport.AlertSuccess(timedCtx, node)
|
2018-12-22 04:51:42 +00:00
|
|
|
|
2019-01-08 08:22:54 +00:00
|
|
|
return conn, nil
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:34:08 +00:00
|
|
|
// DialAddress returns a grpc connection with tls to an IP address.
|
|
|
|
//
|
|
|
|
// Do not use this method unless having a good reason. In most cases DialNode
|
|
|
|
// should be used for communicating with nodes as it is more secure than
|
|
|
|
// DialAddress.
|
2018-11-19 14:40:01 +00:00
|
|
|
func (transport *Transport) DialAddress(ctx context.Context, address string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
|
2018-11-06 17:49:17 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-02-26 18:35:16 +00:00
|
|
|
options := append([]grpc.DialOption{
|
2019-03-04 20:03:33 +00:00
|
|
|
transport.tlsOpts.DialUnverifiedIDOption(),
|
2019-02-26 18:35:16 +00:00
|
|
|
grpc.WithBlock(),
|
|
|
|
grpc.FailOnNonTempDialError(true),
|
2019-05-09 14:53:04 +01:00
|
|
|
grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
|
|
|
|
conn, err := (&net.Dialer{}).DialContext(ctx, "tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-10 12:26:25 +01:00
|
|
|
return &timeoutConn{conn: conn, timeout: transport.timeouts.Request}, nil
|
2019-05-09 14:53:04 +01:00
|
|
|
}),
|
2019-02-26 18:35:16 +00:00
|
|
|
}, opts...)
|
2019-02-11 11:17:32 +00:00
|
|
|
|
2019-05-10 12:26:25 +01:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, transport.timeouts.Dial)
|
2019-03-20 10:11:07 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2019-06-26 14:16:46 +01:00
|
|
|
// TODO: this should also call alertFail or alertSuccess with the node id. We should be able
|
|
|
|
// to get gRPC to give us the node id after dialing?
|
2019-03-20 10:11:07 +00:00
|
|
|
conn, err = grpc.DialContext(timedCtx, address, options...)
|
2019-02-06 13:19:14 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-08 08:22:54 +00:00
|
|
|
return conn, Error.Wrap(err)
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
2018-11-06 17:49:17 +00:00
|
|
|
|
|
|
|
// Identity is a getter for the transport's identity
|
2019-01-30 20:47:21 +00:00
|
|
|
func (transport *Transport) Identity() *identity.FullIdentity {
|
2019-02-11 11:17:32 +00:00
|
|
|
return transport.tlsOpts.Ident
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 09:25:13 +00:00
|
|
|
// WithObservers returns a new transport including the listed observers.
|
2019-04-03 20:13:39 +01:00
|
|
|
func (transport *Transport) WithObservers(obs ...Observer) Client {
|
2019-05-10 12:26:25 +01:00
|
|
|
tr := &Transport{tlsOpts: transport.tlsOpts, timeouts: transport.timeouts}
|
2019-02-08 09:25:13 +00:00
|
|
|
tr.observers = append(tr.observers, transport.observers...)
|
|
|
|
tr.observers = append(tr.observers, obs...)
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:46 +01:00
|
|
|
// AlertFail alerts any subscribed observers of the failure 'err' for 'node'
|
|
|
|
func (transport *Transport) AlertFail(ctx context.Context, node *pb.Node, err error) {
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
for _, o := range transport.observers {
|
2018-12-22 04:51:42 +00:00
|
|
|
o.ConnFailure(ctx, node, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 14:16:46 +01:00
|
|
|
// AlertSuccess alerts any subscribed observers of success for 'node'
|
|
|
|
func (transport *Transport) AlertSuccess(ctx context.Context, node *pb.Node) {
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
for _, o := range transport.observers {
|
2018-12-22 04:51:42 +00:00
|
|
|
o.ConnSuccess(ctx, node)
|
|
|
|
}
|
|
|
|
}
|