forked from go-gost/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
173 lines (147 loc) · 3.55 KB
/
route.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package chain
import (
"context"
"errors"
"fmt"
"net"
"time"
"github.com/dolfly/core/common/net/dialer"
"github.com/dolfly/core/common/net/udp"
"github.com/dolfly/core/logger"
)
var (
ErrEmptyRoute = errors.New("empty route")
)
var (
DefaultRoute Route = &route{}
)
type Route interface {
Dial(ctx context.Context, network, address string, opts ...DialOption) (net.Conn, error)
Bind(ctx context.Context, network, address string, opts ...BindOption) (net.Listener, error)
Nodes() []*Node
}
// route is a Route without nodes.
type route struct{}
func (*route) Dial(ctx context.Context, network, address string, opts ...DialOption) (net.Conn, error) {
var options DialOptions
for _, opt := range opts {
opt(&options)
}
netd := dialer.NetDialer{
Timeout: options.Timeout,
Interface: options.Interface,
Logger: options.Logger,
}
if options.SockOpts != nil {
netd.Mark = options.SockOpts.Mark
}
return netd.Dial(ctx, network, address)
}
func (*route) Bind(ctx context.Context, network, address string, opts ...BindOption) (net.Listener, error) {
var options BindOptions
for _, opt := range opts {
opt(&options)
}
switch network {
case "tcp", "tcp4", "tcp6":
addr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, err
}
return net.ListenTCP(network, addr)
case "udp", "udp4", "udp6":
addr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, err
}
conn, err := net.ListenUDP(network, addr)
if err != nil {
return nil, err
}
logger := logger.Default().WithFields(map[string]any{
"network": network,
"address": address,
})
ln := udp.NewListener(conn, &udp.ListenConfig{
Backlog: options.Backlog,
ReadQueueSize: options.UDPDataQueueSize,
ReadBufferSize: options.UDPDataBufferSize,
TTL: options.UDPConnTTL,
KeepAlive: true,
Logger: logger,
})
return ln, err
default:
err := fmt.Errorf("network %s unsupported", network)
return nil, err
}
}
func (r *route) Nodes() []*Node {
return nil
}
type DialOptions struct {
Timeout time.Duration
Interface string
SockOpts *SockOpts
Logger logger.Logger
}
type DialOption func(opts *DialOptions)
func TimeoutDialOption(d time.Duration) DialOption {
return func(opts *DialOptions) {
opts.Timeout = d
}
}
func InterfaceDialOption(ifName string) DialOption {
return func(opts *DialOptions) {
opts.Interface = ifName
}
}
func SockOptsDialOption(so *SockOpts) DialOption {
return func(opts *DialOptions) {
opts.SockOpts = so
}
}
func LoggerDialOption(logger logger.Logger) DialOption {
return func(opts *DialOptions) {
opts.Logger = logger
}
}
type BindOptions struct {
Mux bool
Backlog int
UDPDataQueueSize int
UDPDataBufferSize int
UDPConnTTL time.Duration
Logger logger.Logger
}
type BindOption func(opts *BindOptions)
func MuxBindOption(mux bool) BindOption {
return func(opts *BindOptions) {
opts.Mux = mux
}
}
func BacklogBindOption(backlog int) BindOption {
return func(opts *BindOptions) {
opts.Backlog = backlog
}
}
func UDPDataQueueSizeBindOption(size int) BindOption {
return func(opts *BindOptions) {
opts.UDPDataQueueSize = size
}
}
func UDPDataBufferSizeBindOption(size int) BindOption {
return func(opts *BindOptions) {
opts.UDPDataBufferSize = size
}
}
func UDPConnTTLBindOption(ttl time.Duration) BindOption {
return func(opts *BindOptions) {
opts.UDPConnTTL = ttl
}
}
func LoggerBindOption(logger logger.Logger) BindOption {
return func(opts *BindOptions) {
opts.Logger = logger
}
}