forked from TuanKiri/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
222 lines (182 loc) Β· 3.89 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
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
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.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
}
}
func WithPublicIP(val net.IP) Option {
return func(o *options) {
o.publicIP = val
}
}
func WithReadTimeout(val time.Duration) Option {
return func(o *options) {
o.readTimeout = val
}
}
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
}
}