2019-08-05 17:52:57 +01:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
2019-09-04 09:48:05 +01:00
|
|
|
"time"
|
|
|
|
|
2019-08-05 17:52:57 +01:00
|
|
|
"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"
|
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"
|
2019-08-30 20:05:00 +01:00
|
|
|
// The shared default token
|
|
|
|
DefaultToken = "micro"
|
2019-08-05 17:52:57 +01: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 20:05:00 +01:00
|
|
|
// The shared auth token
|
|
|
|
Token string
|
2019-08-05 17:52:57 +01:00
|
|
|
// Transport listens to incoming connections
|
|
|
|
Transport transport.Transport
|
|
|
|
}
|
|
|
|
|
2019-09-04 09:48:05 +01:00
|
|
|
type DialOption func(*DialOptions)
|
|
|
|
|
|
|
|
type DialOptions struct {
|
|
|
|
// specify a multicast connection
|
|
|
|
Multicast bool
|
|
|
|
// the dial timeout
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
2019-08-05 17:52:57 +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 {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultOptions returns router default options
|
|
|
|
func DefaultOptions() Options {
|
|
|
|
return Options{
|
|
|
|
Id: uuid.New().String(),
|
|
|
|
Address: DefaultAddress,
|
2019-08-30 20:05:00 +01:00
|
|
|
Token: DefaultToken,
|
2019-08-07 18:44:33 +01:00
|
|
|
Transport: quic.NewTransport(),
|
2019-08-05 17:52:57 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-04 09:48:05 +01:00
|
|
|
|
|
|
|
// Dial options
|
|
|
|
|
|
|
|
// Dial multicast sets the multicast option to send only to those mapped
|
|
|
|
func DialMulticast() DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Multicast = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DialTimeout(t time.Duration) DialOption {
|
|
|
|
return func(o *DialOptions) {
|
|
|
|
o.Timeout = t
|
|
|
|
}
|
|
|
|
}
|