2019-08-05 19:52:57 +03:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2019-09-04 11:48:05 +03:00
|
|
|
"time"
|
|
|
|
|
2019-08-05 19:52:57 +03:00
|
|
|
"github.com/google/uuid"
|
2020-08-29 17:44:49 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
2020-09-20 16:57:54 +03:00
|
|
|
"github.com/unistack-org/micro/v3/network/transport"
|
2019-08-05 19:52:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultAddress is default tunnel bind address
|
2019-08-21 14:55:10 +03:00
|
|
|
DefaultAddress = ":0"
|
2019-08-30 22:05:00 +03:00
|
|
|
// The shared default token
|
2019-11-29 20:27:29 +03:00
|
|
|
DefaultToken = "go.micro.tunnel"
|
2019-08-05 19:52:57 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
// Options provides network configuration options
|
|
|
|
type Options struct {
|
|
|
|
// Id is tunnel id
|
|
|
|
Id string
|
|
|
|
// Address is tunnel address
|
|
|
|
Address string
|
|
|
|
// Nodes are remote nodes
|
|
|
|
Nodes []string
|
2019-08-30 22:05:00 +03:00
|
|
|
// The shared auth token
|
|
|
|
Token string
|
2019-08-05 19:52:57 +03:00
|
|
|
// Transport listens to incoming connections
|
|
|
|
Transport transport.Transport
|
2020-08-29 17:44:49 +03:00
|
|
|
// Logger
|
|
|
|
Logger logger.Logger
|
2019-08-05 19:52:57 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 11:48:05 +03:00
|
|
|
type DialOption func(*DialOptions)
|
|
|
|
|
|
|
|
type DialOptions struct {
|
2019-09-11 21:57:41 +03:00
|
|
|
// Link specifies the link to use
|
|
|
|
Link string
|
2019-10-15 17:40:04 +03:00
|
|
|
// specify mode of the session
|
|
|
|
Mode Mode
|
2019-12-11 17:37:03 +03:00
|
|
|
// Wait for connection to be accepted
|
|
|
|
Wait bool
|
2019-09-04 11:48:05 +03:00
|
|
|
// the dial timeout
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2019-10-15 17:40:04 +03:00
|
|
|
type ListenOption func(*ListenOptions)
|
|
|
|
|
|
|
|
type ListenOptions struct {
|
|
|
|
// specify mode of the session
|
|
|
|
Mode Mode
|
2019-12-08 02:28:39 +03:00
|
|
|
// The read timeout
|
|
|
|
Timeout time.Duration
|
2019-10-15 17:40:04 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 19:52:57 +03:00
|
|
|
// The tunnel id
|
|
|
|
func Id(id string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Id = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 19:52:57 +03:00
|
|
|
// The tunnel address
|
|
|
|
func Address(a string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Address = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes specify remote network nodes
|
2019-08-07 20:44:33 +03:00
|
|
|
func Nodes(n ...string) Option {
|
2019-08-05 19:52:57 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Nodes = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 22:05:00 +03:00
|
|
|
// Token sets the shared token for auth
|
|
|
|
func Token(t string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Token = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 19:52:57 +03:00
|
|
|
// Transport listens for incoming connections
|
|
|
|
func Transport(t transport.Transport) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Transport = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 17:40:04 +03:00
|
|
|
// Listen options
|
2019-10-15 18:08:38 +03:00
|
|
|
func ListenMode(m Mode) ListenOption {
|
2019-10-15 17:40:04 +03:00
|
|
|
return func(o *ListenOptions) {
|
2019-10-15 18:08:38 +03:00
|
|
|
o.Mode = m
|
2019-10-15 17:40:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 02:28:39 +03:00
|
|
|
// Timeout for reads and writes on the listener session
|
|
|
|
func ListenTimeout(t time.Duration) ListenOption {
|
|
|
|
return func(o *ListenOptions) {
|
|
|
|
o.Timeout = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 11:48:05 +03:00
|
|
|
// Dial options
|
|
|
|
|
|
|
|
// Dial multicast sets the multicast option to send only to those mapped
|
2019-10-15 18:08:38 +03:00
|
|
|
func DialMode(m Mode) DialOption {
|
2019-09-04 11:48:05 +03:00
|
|
|
return func(o *DialOptions) {
|
2019-10-15 18:08:38 +03:00
|
|
|
o.Mode = m
|
2019-09-04 11:48:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 21:57:41 +03:00
|
|
|
// DialTimeout sets the dial timeout of the connection
|
2019-09-04 11:48:05 +03:00
|
|
|
func DialTimeout(t time.Duration) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Timeout = t
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 21:57:41 +03: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-08 02:28:39 +03:00
|
|
|
|
2019-12-11 17:37:03 +03: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 02:28:39 +03:00
|
|
|
// DefaultOptions returns router default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
return Options{
|
2020-08-25 13:44:41 +03:00
|
|
|
Id: uuid.New().String(),
|
|
|
|
Address: DefaultAddress,
|
|
|
|
Token: DefaultToken,
|
2019-12-08 02:28:39 +03:00
|
|
|
}
|
|
|
|
}
|