2019-08-05 17:52:57 +01:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2019-09-04 09:48:05 +01:00
|
|
|
"time"
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
2020-09-20 16:57:54 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/transport"
|
2021-01-22 23:32:33 +03:00
|
|
|
"github.com/unistack-org/micro/v3/tracer"
|
2021-08-20 22:40:48 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/id"
|
2019-08-05 17:52:57 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultAddress is default tunnel bind address
|
2019-08-21 12:55:10 +01:00
|
|
|
DefaultAddress = ":0"
|
2021-02-14 16:16:01 +03:00
|
|
|
// DefaultToken the shared default token
|
2019-11-29 17:27:29 +00:00
|
|
|
DefaultToken = "go.micro.tunnel"
|
2019-08-05 17:52:57 +01:00
|
|
|
)
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// Option func signature
|
2019-08-05 17:52:57 +01:00
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// Options provides network configuration options
|
|
|
|
type Options struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Logger used for logging
|
|
|
|
Logger logger.Logger
|
|
|
|
// Meter used for metrics
|
|
|
|
Meter meter.Meter
|
|
|
|
// Tracer used for tracing
|
|
|
|
Tracer tracer.Tracer
|
|
|
|
// Transport used for communication
|
|
|
|
Transport transport.Transport
|
|
|
|
// Token the shared auth token
|
|
|
|
Token string
|
|
|
|
// Name holds the tunnel name
|
2021-01-29 14:07:35 +03:00
|
|
|
Name string
|
2021-04-27 08:32:47 +03:00
|
|
|
// ID holds the tunnel id
|
|
|
|
ID string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Address holds the tunnel address
|
2019-08-05 17:52:57 +01:00
|
|
|
Address string
|
2021-03-06 19:45:13 +03:00
|
|
|
// Nodes holds the tunnel nodes
|
2019-08-05 17:52:57 +01:00
|
|
|
Nodes []string
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// DialOption func
|
2019-09-04 09:48:05 +01:00
|
|
|
type DialOption func(*DialOptions)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// DialOptions provides dial options
|
2019-09-04 09:48:05 +01:00
|
|
|
type DialOptions struct {
|
2019-09-11 11:57:41 -07:00
|
|
|
// Link specifies the link to use
|
|
|
|
Link string
|
2019-10-15 15:40:04 +01:00
|
|
|
// specify mode of the session
|
|
|
|
Mode Mode
|
2019-12-11 14:37:03 +00:00
|
|
|
// Wait for connection to be accepted
|
|
|
|
Wait bool
|
2019-09-04 09:48:05 +01:00
|
|
|
// the dial timeout
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// ListenOption func
|
2019-10-15 15:40:04 +01:00
|
|
|
type ListenOption func(*ListenOptions)
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// ListenOptions provides listen options
|
2019-10-15 15:40:04 +01:00
|
|
|
type ListenOptions struct {
|
2021-03-06 19:45:13 +03:00
|
|
|
// Mode specify mode of the session
|
2019-10-15 15:40:04 +01:00
|
|
|
Mode Mode
|
2021-03-06 19:45:13 +03:00
|
|
|
// Timeout the read timeout
|
2019-12-07 23:28:39 +00:00
|
|
|
Timeout time.Duration
|
2019-10-15 15:40:04 +01:00
|
|
|
}
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
// ID sets the tunnel id
|
|
|
|
func ID(id string) Option {
|
2019-08-05 17:52:57 +01:00
|
|
|
return func(o *Options) {
|
2021-04-27 08:32:47 +03:00
|
|
|
o.ID = id
|
2019-08-05 17:52:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger sets the logger
|
|
|
|
func Logger(l logger.Logger) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Logger = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 23:32:33 +03:00
|
|
|
// Meter sets the meter
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Address sets the tunnel address
|
2019-08-05 17:52:57 +01:00
|
|
|
func Address(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Address = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes specify remote network nodes
|
2019-08-07 18:44:33 +01:00
|
|
|
func Nodes(n ...string) Option {
|
2019-08-05 17:52:57 +01:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Nodes = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 20:05:00 +01:00
|
|
|
// Token sets the shared token for auth
|
|
|
|
func Token(t string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Token = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 17:52:57 +01:00
|
|
|
// Transport listens for incoming connections
|
|
|
|
func Transport(t transport.Transport) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Transport = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// ListenMode option
|
2019-10-15 16:08:38 +01:00
|
|
|
func ListenMode(m Mode) ListenOption {
|
2019-10-15 15:40:04 +01:00
|
|
|
return func(o *ListenOptions) {
|
2019-10-15 16:08:38 +01:00
|
|
|
o.Mode = m
|
2019-10-15 15:40:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// ListenTimeout for reads and writes on the listener session
|
2019-12-07 23:28:39 +00:00
|
|
|
func ListenTimeout(t time.Duration) ListenOption {
|
|
|
|
return func(o *ListenOptions) {
|
|
|
|
o.Timeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// DialMode multicast sets the multicast option to send only to those mapped
|
2019-10-15 16:08:38 +01:00
|
|
|
func DialMode(m Mode) DialOption {
|
2019-09-04 09:48:05 +01:00
|
|
|
return func(o *DialOptions) {
|
2019-10-15 16:08:38 +01:00
|
|
|
o.Mode = m
|
2019-09-04 09:48:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 11:57:41 -07:00
|
|
|
// DialTimeout sets the dial timeout of the connection
|
2019-09-04 09:48:05 +01:00
|
|
|
func DialTimeout(t time.Duration) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Timeout = t
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 11:57:41 -07:00
|
|
|
|
|
|
|
// DialLink specifies the link to pin this connection to.
|
|
|
|
// This is not applicable if the multicast option is set.
|
|
|
|
func DialLink(id string) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Link = id
|
|
|
|
}
|
|
|
|
}
|
2019-12-07 23:28:39 +00:00
|
|
|
|
2019-12-11 14:37:03 +00:00
|
|
|
// DialWait specifies whether to wait for the connection
|
|
|
|
// to be accepted before returning the session
|
|
|
|
func DialWait(b bool) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Wait = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 16:16:01 +03:00
|
|
|
// NewOptions returns router default options with filled values
|
2020-11-03 01:08:23 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
|
|
|
options := Options{
|
2021-08-20 22:40:48 +03:00
|
|
|
ID: id.Must(),
|
2020-08-25 13:44:41 +03:00
|
|
|
Address: DefaultAddress,
|
|
|
|
Token: DefaultToken,
|
2021-01-22 23:32:33 +03:00
|
|
|
Logger: logger.DefaultLogger,
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
Tracer: tracer.DefaultTracer,
|
2019-12-07 23:28:39 +00:00
|
|
|
}
|
2020-11-03 01:08:23 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
2019-12-07 23:28:39 +00:00
|
|
|
}
|
2021-01-22 23:32:33 +03:00
|
|
|
|
|
|
|
// Tracer to be used for tracing
|
|
|
|
func Tracer(t tracer.Tracer) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Tracer = t
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 14:07:35 +03:00
|
|
|
|
|
|
|
// Name sets the name
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = n
|
|
|
|
}
|
|
|
|
}
|