-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.go
268 lines (223 loc) Β· 5.42 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package socks5
import (
"fmt"
"log"
"net"
"os"
"time"
)
const (
Connect Command = iota 1
Bind
UDPAssociate
)
type Command int
type Option func(*options)
type options struct {
host string
port int
publicIP net.IP
readTimeout time.Duration
writeTimeout time.Duration
dialTimeout time.Duration
getPasswordTimeout time.Duration
passwordAuthentication bool
staticCredentials map[string]string
allowCommands map[byte]struct{}
blockListHosts map[string]struct{}
allowIPs []net.IP
maxPacketSize int
packetWriteTimeout time.Duration
ttlPacket time.Duration
natCleanupPeriod time.Duration
logger Logger
store Store
driver Driver
metrics Metrics
rules Rules
}
func (o options) authMethods() map[byte]struct{} {
methods := make(map[byte]struct{})
switch {
case o.passwordAuthentication:
methods[usernamePasswordAuthentication] = struct{}{}
default:
methods[noAuthenticationRequired] = struct{}{}
}
return methods
}
func (o options) listenAddress() string {
return fmt.Sprintf("%s:%d", o.host, o.port)
}
func optsWithDefaults(opts *options) *options {
if opts.port <= 0 {
opts.port = 1080
}
if opts.maxPacketSize <= 0 {
opts.maxPacketSize = 1500
}
if opts.publicIP == nil {
opts.publicIP = net.ParseIP("127.0.0.1")
}
if opts.logger == nil {
opts.logger = &stdoutLogger{
log: log.New(os.Stdout, "[socks5] - ", log.Ldate|log.Ltime),
}
}
if opts.store == nil {
if opts.staticCredentials == nil {
opts.staticCredentials = map[string]string{
"root": "password",
}
}
opts.store = &mapStore{
db: opts.staticCredentials,
}
}
if opts.driver == nil {
opts.driver = &netDriver{
timeout: opts.dialTimeout,
}
}
if opts.metrics == nil {
opts.metrics = &nopMetrics{}
}
if opts.rules == nil {
if opts.allowCommands == nil {
opts.allowCommands = permitAllCommands()
}
opts.rules = &serverRules{
allowCommands: opts.allowCommands,
blockListHosts: opts.blockListHosts,
allowIPs: opts.allowIPs,
}
}
return opts
}
func WithHost(val string) Option {
return func(o *options) {
o.host = val
}
}
func WithPort(val int) Option {
return func(o *options) {
o.port = val
}
}
// WithPublicIP sets an IP address that is visible on the external Internet,
// accessible to users outside the local network and will be sent to clients in
// response to a connection request.
func WithPublicIP(val net.IP) Option {
return func(o *options) {
o.publicIP = val
}
}
// WithReadTimeout sets the read timeout for tcp connection.
func WithReadTimeout(val time.Duration) Option {
return func(o *options) {
o.readTimeout = val
}
}
// WithWriteTimeout sets the write timeout for tcp connection.
func WithWriteTimeout(val time.Duration) Option {
return func(o *options) {
o.writeTimeout = val
}
}
func WithDialTimeout(val time.Duration) Option {
return func(o *options) {
o.dialTimeout = val
}
}
func WithGetPasswordTimeout(val time.Duration) Option {
return func(o *options) {
o.getPasswordTimeout = val
}
}
func WithPasswordAuthentication() Option {
return func(o *options) {
o.passwordAuthentication = true
}
}
func WithStaticCredentials(val map[string]string) Option {
return func(o *options) {
o.staticCredentials = val
}
}
func WithLogger(val Logger) Option {
return func(o *options) {
o.logger = val
}
}
func WithStore(val Store) Option {
return func(o *options) {
o.store = val
}
}
func WithDriver(val Driver) Option {
return func(o *options) {
o.driver = val
}
}
func WithMetrics(val Metrics) Option {
return func(o *options) {
o.metrics = val
}
}
func WithRules(val Rules) Option {
return func(o *options) {
o.rules = val
}
}
func WithAllowCommands(commands ...Command) Option {
allowCommands := map[byte]struct{}{}
for _, command := range commands {
allowCommands[byte(command)] = struct{}{}
}
return func(o *options) {
o.allowCommands = allowCommands
}
}
func WithWhiteListIPs(IPs ...net.IP) Option {
return func(o *options) {
o.allowIPs = IPs
}
}
func WithBlockListHosts(hosts ...string) Option {
blockListHosts := map[string]struct{}{}
for _, host := range hosts {
blockListHosts[host] = struct{}{}
}
return func(o *options) {
o.blockListHosts = blockListHosts
}
}
// WithPacketWriteTimeout sets the timeout for waiting to write a packet to the remote host.
func WithPacketWriteTimeout(val time.Duration) Option {
return func(o *options) {
o.packetWriteTimeout = val
}
}
// WithMaxPacketSize sets the maximum size in bytes for the datagram to be read from the socket.
func WithMaxPacketSize(val int) Option {
return func(o *options) {
o.maxPacketSize = val
}
}
// WithTTLPacket sets how long the packet will stay in the table
// that links the sender of the packet to the remote host it was meant for.
// Nat cleanup period must be greater than 0.
func WithTTLPacket(val time.Duration) Option {
return func(o *options) {
o.ttlPacket = val
}
}
// WithNatCleanupPeriod sets the period when the table that links the
// packets from the sender to the remote host will be cleaned.
// It makes sense if there's no time limit on the TCP connection.
// TTL of the packet must be greater than 0.
func WithNatCleanupPeriod(val time.Duration) Option {
return func(o *options) {
o.natCleanupPeriod = val
}
}