2019-08-07 23:58:25 +03:00
|
|
|
// Package transport provides a tunnel transport
|
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-25 13:44:41 +03:00
|
|
|
"fmt"
|
2019-08-07 23:58:25 +03:00
|
|
|
|
2020-09-20 16:57:54 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/transport"
|
|
|
|
"github.com/unistack-org/micro/v3/network/tunnel"
|
2019-08-07 23:58:25 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type tunTransport struct {
|
|
|
|
options transport.Options
|
|
|
|
|
|
|
|
tunnel tunnel.Tunnel
|
|
|
|
}
|
|
|
|
|
|
|
|
type tunnelKey struct{}
|
|
|
|
|
|
|
|
type transportKey struct{}
|
|
|
|
|
|
|
|
func (t *tunTransport) Init(opts ...transport.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&t.options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// close the existing tunnel
|
|
|
|
if t.tunnel != nil {
|
2020-11-03 02:02:32 +03:00
|
|
|
t.tunnel.Close(context.TODO())
|
2019-08-07 23:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the tunnel
|
|
|
|
tun, ok := t.options.Context.Value(tunnelKey{}).(tunnel.Tunnel)
|
|
|
|
if !ok {
|
2020-08-25 13:44:41 +03:00
|
|
|
return fmt.Errorf("tunnel not set")
|
2019-08-07 23:58:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the transport
|
|
|
|
tr, ok := t.options.Context.Value(transportKey{}).(transport.Transport)
|
|
|
|
if ok {
|
|
|
|
tun.Init(tunnel.Transport(tr))
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the tunnel
|
|
|
|
t.tunnel = tun
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
func (t *tunTransport) Dial(ctx context.Context, addr string, opts ...transport.DialOption) (transport.Client, error) {
|
|
|
|
if err := t.tunnel.Connect(ctx); err != nil {
|
2019-08-07 23:58:25 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
c, err := t.tunnel.Dial(ctx, addr)
|
2019-08-07 23:58:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
func (t *tunTransport) Listen(ctx context.Context, addr string, opts ...transport.ListenOption) (transport.Listener, error) {
|
|
|
|
if err := t.tunnel.Connect(ctx); err != nil {
|
2019-08-07 23:58:25 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:02:32 +03:00
|
|
|
l, err := t.tunnel.Listen(ctx, addr)
|
2019-08-07 23:58:25 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tunListener{l}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunTransport) Options() transport.Options {
|
|
|
|
return t.options
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tunTransport) String() string {
|
|
|
|
return "tunnel"
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTransport honours the initialiser used in
|
|
|
|
func NewTransport(opts ...transport.Option) transport.Transport {
|
|
|
|
t := &tunTransport{
|
|
|
|
options: transport.Options{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialise
|
|
|
|
t.Init(opts...)
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// WithTunnel sets the internal tunnel
|
2019-08-07 23:58:25 +03:00
|
|
|
func WithTunnel(t tunnel.Tunnel) transport.Option {
|
|
|
|
return func(o *transport.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, tunnelKey{}, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTransport sets the internal transport
|
|
|
|
func WithTransport(t transport.Transport) transport.Option {
|
|
|
|
return func(o *transport.Options) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, transportKey{}, t)
|
|
|
|
}
|
|
|
|
}
|