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"
|
2018-11-20 16:54:52 +00:00
|
|
|
"time"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
2018-11-06 17:49:17 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-06-29 19:28:06 +01:00
|
|
|
"google.golang.org/grpc"
|
2019-01-08 08:22:54 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
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-11-06 17:49:17 +00:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
//Error is the errs class of standard Transport Client errors
|
|
|
|
Error = errs.Class("transport error")
|
2018-11-20 16:54:52 +00:00
|
|
|
// default time to wait for a connection to be established
|
|
|
|
timeout = 20 * time.Second
|
2018-11-06 17:49:17 +00: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-01-30 20:47:21 +00:00
|
|
|
Identity() *identity.FullIdentity
|
2019-02-08 09:25:13 +00:00
|
|
|
WithObservers(obs ...Observer) *Transport
|
2018-11-06 17:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 19:28:06 +01:00
|
|
|
// Transport interface structure
|
|
|
|
type Transport struct {
|
2019-02-11 11:17:32 +00:00
|
|
|
tlsOpts *tlsopts.Options
|
2018-12-22 04:51:42 +00:00
|
|
|
observers []Observer
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
|
|
|
|
2018-07-19 15:48:08 +01:00
|
|
|
// NewClient returns a newly instantiated Transport Client
|
2019-02-11 11:17:32 +00:00
|
|
|
func NewClient(tlsOpts *tlsopts.Options, obs ...Observer) Client {
|
2018-12-22 04:51:42 +00:00
|
|
|
return &Transport{
|
2019-02-11 11:17:32 +00:00
|
|
|
tlsOpts: tlsOpts,
|
2018-12-22 04:51:42 +00:00
|
|
|
observers: obs,
|
|
|
|
}
|
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) {
|
2018-06-29 19:28:06 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-02 18:47:34 +00:00
|
|
|
if node != nil {
|
|
|
|
node.Type.DPanicOnInvalid("transport dial node")
|
|
|
|
}
|
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")
|
|
|
|
}
|
2018-11-09 22:08:33 +00:00
|
|
|
|
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),
|
|
|
|
}, opts...)
|
2018-11-20 16:54:52 +00:00
|
|
|
|
|
|
|
ctx, cf := context.WithTimeout(ctx, timeout)
|
|
|
|
defer cf()
|
2018-12-22 04:51:42 +00:00
|
|
|
|
|
|
|
conn, err = grpc.DialContext(ctx, node.GetAddress().Address, options...)
|
|
|
|
if err != nil {
|
2019-02-06 13:19:14 +00:00
|
|
|
if err == context.Canceled {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-22 04:51:42 +00:00
|
|
|
alertFail(ctx, transport.observers, node, err)
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
alertSuccess(ctx, transport.observers, node)
|
|
|
|
|
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),
|
|
|
|
}, opts...)
|
2019-02-11 11:17:32 +00:00
|
|
|
|
2019-02-06 13:19:14 +00:00
|
|
|
conn, err = grpc.DialContext(ctx, address, options...)
|
|
|
|
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.
|
|
|
|
func (transport *Transport) WithObservers(obs ...Observer) *Transport {
|
2019-02-11 11:17:32 +00:00
|
|
|
tr := &Transport{tlsOpts: transport.tlsOpts}
|
2019-02-08 09:25:13 +00:00
|
|
|
tr.observers = append(tr.observers, transport.observers...)
|
|
|
|
tr.observers = append(tr.observers, obs...)
|
|
|
|
return tr
|
|
|
|
}
|
|
|
|
|
2018-12-22 04:51:42 +00:00
|
|
|
func alertFail(ctx context.Context, obs []Observer, node *pb.Node, err error) {
|
|
|
|
for _, o := range obs {
|
|
|
|
o.ConnFailure(ctx, node, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func alertSuccess(ctx context.Context, obs []Observer, node *pb.Node) {
|
|
|
|
for _, o := range obs {
|
|
|
|
o.ConnSuccess(ctx, node)
|
|
|
|
}
|
|
|
|
}
|