/
options.go
116 lines (102 loc) · 3.11 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package npp
import (
"context"
"fmt"
"time"
"github.com/sonm-io/core/insonmnia/npp/relay"
"github.com/sonm-io/core/insonmnia/npp/rendezvous"
"go.uber.org/zap"
"google.golang.org/grpc/credentials"
)
// Option is a function that configures the listener or dialer.
type Option func(o *options) error
type options struct {
ctx context.Context
log *zap.Logger
puncher NATPuncher
puncherNew func() (NATPuncher, error)
nppBacklog int
nppMinBackoffInterval time.Duration
nppMaxBackoffInterval time.Duration
relayListener *relay.Listener
relayDialer *relay.Dialer
}
func newOptions(ctx context.Context) *options {
return &options{
ctx: ctx,
log: zap.NewNop(),
nppBacklog: 128,
nppMinBackoffInterval: 500 * time.Millisecond,
nppMaxBackoffInterval: 8000 * time.Millisecond,
}
}
// WithRendezvous is an option that specifies Rendezvous client settings.
//
// Without this option no intermediate server will be used for obtaining
// peer's endpoints and the entire connection establishment process will fall
// back to the old good plain TCP connection.
func WithRendezvous(cfg rendezvous.Config, credentials credentials.TransportCredentials) Option {
return func(o *options) error {
if len(cfg.Endpoints) == 0 {
return nil
}
o.puncherNew = func() (NATPuncher, error) {
for _, addr := range cfg.Endpoints {
client, err := newRendezvousClient(o.ctx, addr, credentials)
if err == nil {
return newNATPuncher(o.ctx, cfg, client)
}
}
return nil, fmt.Errorf("failed to connect to %+v", cfg.Endpoints)
}
return nil
}
}
// WithLogger is an option that specifies provided logger used for the internal
// logging.
// Nil value is supported and can be passed to deactivate the logging system
// entirely.
func WithLogger(log *zap.Logger) Option {
return func(o *options) error {
o.log = log
return nil
}
}
// WithNPPBacklog is an option that specifies NPP backlog size.
func WithNPPBacklog(backlog int) Option {
return func(o *options) error {
o.nppBacklog = backlog
return nil
}
}
// WithNPPBackoff is an option that specifies NPP timeouts.
func WithNPPBackoff(min, max time.Duration) Option {
return func(o *options) error {
o.nppMinBackoffInterval = min
o.nppMaxBackoffInterval = max
return nil
}
}
// WithRelayListener is an option that activates Relay fallback on a NPP
// listener.
//
// Without this option no intermediate server will be used for relaying
// TCP.
func WithRelayListener(listener *relay.Listener) Option {
return func(o *options) error {
o.relayListener = listener
return nil
}
}
// WithRelayDialer is an option that activates Relay fallback on a NPP dialer.
//
// One or more Relay TCP addresses must be specified in "addrs" argument.
// Hostname resolution is performed for each of them for environments with
// dynamic DNS addition/removal. Thus, a single Relay endpoint as a hostname
// should fit the best.
func WithRelayDialer(dialer *relay.Dialer) Option {
return func(o *options) error {
o.relayDialer = dialer
return nil
}
}