-
Notifications
You must be signed in to change notification settings - Fork 76
/
option.go
169 lines (145 loc) · 4.6 KB
/
option.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
package socks5
import (
"context"
"io"
"net"
"github.com/things-go/go-socks5/bufferpool"
)
// Option user's option
type Option func(s *Server)
// WithBufferPool can be provided to implement custom buffer pool
// By default, buffer pool use size is 32k
func WithBufferPool(bufferPool bufferpool.BufPool) Option {
return func(s *Server) {
s.bufferPool = bufferPool
}
}
// WithAuthMethods can be provided to implement custom authentication
// By default, "auth-less" mode is enabled.
// For password-based auth use UserPassAuthenticator.
func WithAuthMethods(authMethods []Authenticator) Option {
return func(s *Server) {
s.authMethods = append(s.authMethods, authMethods...)
}
}
// WithCredential If provided, username/password authentication is enabled,
// by appending a UserPassAuthenticator to AuthMethods. If not provided,
// and AUthMethods is nil, then "auth-less" mode is enabled.
func WithCredential(cs CredentialStore) Option {
return func(s *Server) {
s.credentials = cs
}
}
// WithResolver can be provided to do custom name resolution.
// Defaults to DNSResolver if not provided.
func WithResolver(res NameResolver) Option {
return func(s *Server) {
s.resolver = res
}
}
// WithRule is provided to enable custom logic around permitting
// various commands. If not provided, NewPermitAll is used.
func WithRule(rule RuleSet) Option {
return func(s *Server) {
s.rules = rule
}
}
// WithRewriter can be used to transparently rewrite addresses.
// This is invoked before the RuleSet is invoked.
// Defaults to NoRewrite.
func WithRewriter(rew AddressRewriter) Option {
return func(s *Server) {
s.rewriter = rew
}
}
// WithBindIP is used for bind or udp associate
func WithBindIP(ip net.IP) Option {
return func(s *Server) {
if len(ip) != 0 {
s.bindIP = make(net.IP, 0, len(ip))
s.bindIP = append(s.bindIP, ip...)
}
}
}
// WithLogger can be used to provide a custom log target.
// Defaults to io.Discard.
func WithLogger(l Logger) Option {
return func(s *Server) {
s.logger = l
}
}
// WithDial Optional function for dialing out.
// The callback set by WithDialAndRequest will be called first.
func WithDial(dial func(ctx context.Context, network, addr string) (net.Conn, error)) Option {
return func(s *Server) {
s.dial = dial
}
}
// WithDialAndRequest Optional function for dialing out with the access of request detail.
func WithDialAndRequest(
dial func(ctx context.Context, network, addr string, request *Request) (net.Conn, error),
) Option {
return func(s *Server) {
s.dialWithRequest = dial
}
}
// WithGPool can be provided to do custom goroutine pool.
func WithGPool(pool GPool) Option {
return func(s *Server) {
s.gPool = pool
}
}
// WithConnectHandle is used to handle a user's connect command
func WithConnectHandle(h func(ctx context.Context, writer io.Writer, request *Request) error) Option {
return func(s *Server) {
s.userConnectHandle = h
}
}
// WithBindHandle is used to handle a user's bind command
func WithBindHandle(h func(ctx context.Context, writer io.Writer, request *Request) error) Option {
return func(s *Server) {
s.userBindHandle = h
}
}
// WithAssociateHandle is used to handle a user's associate command
func WithAssociateHandle(h func(ctx context.Context, writer io.Writer, request *Request) error) Option {
return func(s *Server) {
s.userAssociateHandle = h
}
}
// Handler is used to handle a user's commands
type Handler func(ctx context.Context, writer io.Writer, request *Request) error
// WithMiddleware is used to add interceptors in chain
type Middleware func(ctx context.Context, writer io.Writer, request *Request) error
// MiddlewareChain is used to add interceptors in chain
type MiddlewareChain []Middleware
// Execute is used to add interceptors in chain
func (m MiddlewareChain) Execute(ctx context.Context, writer io.Writer, request *Request, last Handler) error {
if len(m) == 0 {
return nil
}
for i := 0; i < len(m); i++ {
if err := m[i](ctx, writer, request); err != nil {
return err
}
}
return last(ctx, writer, request)
}
// WithConnectMiddleware is used to add interceptors in chain
func WithConnectMiddleware(m Middleware) Option {
return func(s *Server) {
s.userConnectMiddlewares = append(s.userConnectMiddlewares, m)
}
}
// WithBindMiddleware is used to add interceptors in chain
func WithBindMiddleware(m Middleware) Option {
return func(s *Server) {
s.userBindMiddlewares = append(s.userBindMiddlewares, m)
}
}
// WithAssociateMiddleware is used to add interceptors in chain
func WithAssociateMiddleware(m Middleware) Option {
return func(s *Server) {
s.userAssociateMiddlewares = append(s.userAssociateMiddlewares, m)
}
}