micro/tunnel/options.go

136 lines
2.5 KiB
Go
Raw Normal View History

package tunnel
import (
"time"
"github.com/google/uuid"
"github.com/micro/go-micro/transport"
2019-08-07 18:44:33 +01:00
"github.com/micro/go-micro/transport/quic"
)
var (
// DefaultAddress is default tunnel bind address
2019-08-21 12:55:10 +01:00
DefaultAddress = ":0"
2019-08-30 20:05:00 +01:00
// The shared default token
2019-11-29 17:27:29 +00:00
DefaultToken = "go.micro.tunnel"
)
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 20:05:00 +01:00
// The shared auth token
Token string
// Transport listens to incoming connections
Transport transport.Transport
}
type DialOption func(*DialOptions)
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
// the dial timeout
Timeout time.Duration
}
2019-10-15 15:40:04 +01:00
type ListenOption func(*ListenOptions)
type ListenOptions struct {
// specify mode of the session
Mode Mode
2019-12-07 23:28:39 +00:00
// The read timeout
Timeout time.Duration
2019-10-15 15:40:04 +01:00
}
// The tunnel id
func Id(id string) Option {
return func(o *Options) {
o.Id = id
}
}
// The tunnel address
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 {
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
}
}
// Transport listens for incoming connections
func Transport(t transport.Transport) Option {
return func(o *Options) {
o.Transport = t
}
}
2019-10-15 15:40:04 +01:00
// Listen options
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
}
}
2019-12-07 23:28:39 +00:00
// Timeout for reads and writes on the listener session
func ListenTimeout(t time.Duration) ListenOption {
return func(o *ListenOptions) {
o.Timeout = t
}
}
// Dial options
// Dial multicast sets the multicast option to send only to those mapped
2019-10-15 16:08:38 +01:00
func DialMode(m Mode) DialOption {
return func(o *DialOptions) {
2019-10-15 16:08:38 +01:00
o.Mode = m
}
}
2019-09-11 11:57:41 -07:00
// DialTimeout sets the dial timeout of the connection
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
// DefaultOptions returns router default options
func DefaultOptions() Options {
return Options{
Id: uuid.New().String(),
Address: DefaultAddress,
Token: DefaultToken,
Transport: quic.NewTransport(),
}
}