github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/network/tunnel/transport/transport.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/network/tunnel/mucp/transport/transport.go
    14  
    15  // Package transport provides a tunnel transport
    16  package transport
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/tickoalcantara12/micro/v3/service/network/transport"
    22  	"github.com/tickoalcantara12/micro/v3/service/network/tunnel"
    23  	"github.com/tickoalcantara12/micro/v3/service/network/tunnel/mucp"
    24  )
    25  
    26  type tunTransport struct {
    27  	options transport.Options
    28  
    29  	tunnel tunnel.Tunnel
    30  }
    31  
    32  type tunnelKey struct{}
    33  
    34  type transportKey struct{}
    35  
    36  func (t *tunTransport) Init(opts ...transport.Option) error {
    37  	for _, o := range opts {
    38  		o(&t.options)
    39  	}
    40  
    41  	// close the existing tunnel
    42  	if t.tunnel != nil {
    43  		t.tunnel.Close()
    44  	}
    45  
    46  	// get the tunnel
    47  	tun, ok := t.options.Context.Value(tunnelKey{}).(tunnel.Tunnel)
    48  	if !ok {
    49  		tun = mucp.NewTunnel()
    50  	}
    51  
    52  	// get the transport
    53  	tr, ok := t.options.Context.Value(transportKey{}).(transport.Transport)
    54  	if ok {
    55  		tun.Init(tunnel.Transport(tr))
    56  	}
    57  
    58  	// set the tunnel
    59  	t.tunnel = tun
    60  
    61  	return nil
    62  }
    63  
    64  func (t *tunTransport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
    65  	if err := t.tunnel.Connect(); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	c, err := t.tunnel.Dial(addr)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	return c, nil
    75  }
    76  
    77  func (t *tunTransport) Listen(addr string, opts ...transport.ListenOption) (transport.Listener, error) {
    78  	if err := t.tunnel.Connect(); err != nil {
    79  		return nil, err
    80  	}
    81  
    82  	l, err := t.tunnel.Listen(addr)
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return &tunListener{l}, nil
    88  }
    89  
    90  func (t *tunTransport) Options() transport.Options {
    91  	return t.options
    92  }
    93  
    94  func (t *tunTransport) String() string {
    95  	return "tunnel"
    96  }
    97  
    98  // NewTransport honours the initialiser used in
    99  func NewTransport(opts ...transport.Option) transport.Transport {
   100  	t := &tunTransport{
   101  		options: transport.Options{},
   102  	}
   103  
   104  	// initialise
   105  	t.Init(opts...)
   106  
   107  	return t
   108  }
   109  
   110  // WithTransport sets the internal tunnel
   111  func WithTunnel(t tunnel.Tunnel) transport.Option {
   112  	return func(o *transport.Options) {
   113  		if o.Context == nil {
   114  			o.Context = context.Background()
   115  		}
   116  		o.Context = context.WithValue(o.Context, tunnelKey{}, t)
   117  	}
   118  }
   119  
   120  // WithTransport sets the internal transport
   121  func WithTransport(t transport.Transport) transport.Option {
   122  	return func(o *transport.Options) {
   123  		if o.Context == nil {
   124  			o.Context = context.Background()
   125  		}
   126  		o.Context = context.WithValue(o.Context, transportKey{}, t)
   127  	}
   128  }