-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_socks.go
285 lines (242 loc) · 8.88 KB
/
handler_socks.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package main
import (
"context"
"fmt"
"io"
"net"
"os"
"runtime"
"slices"
"strconv"
"strings"
"text/template"
"github.com/jszwec/csvutil"
"github.com/phuslu/log"
"golang.org/x/crypto/bcrypt"
)
type SocksRequest struct {
RemoteAddr string
RemoteIP string
ServerAddr string
Version SocksVersion
ConnectType SocksCommand
SupportAuth bool
Username string
Password string
Host string
Port int
TraceID log.XID
}
type SocksHandler struct {
Config SocksConfig
ForwardLogger log.Logger
RegionResolver *RegionResolver
LocalDialer *LocalDialer
Upstreams map[string]Dialer
Functions template.FuncMap
PolicyTemplate *template.Template
UpstreamTemplate *template.Template
}
func (h *SocksHandler) Load() error {
var err error
if s := h.Config.Forward.Policy; s != "" {
if h.PolicyTemplate, err = template.New(s).Funcs(h.Functions).Parse(s); err != nil {
return err
}
}
if s := h.Config.Forward.Dialer; s != "" {
if h.UpstreamTemplate, err = template.New(s).Funcs(h.Functions).Parse(s); err != nil {
return err
}
}
if h.Config.Forward.BindInterface != "" {
if runtime.GOOS != "linux" {
log.Fatal().Strs("server_listen", h.Config.Listen).Msg("option bind_interface is only available on linux")
}
if h.Config.Forward.Dialer != "" {
log.Fatal().Strs("server_listen", h.Config.Listen).Msg("option bind_interface is confilict with option dialer")
}
dialer := new(LocalDialer)
*dialer = *h.LocalDialer
dialer.BindInterface = h.Config.Forward.BindInterface
dialer.PreferIPv6 = h.Config.Forward.PreferIpv6
h.LocalDialer = dialer
}
return nil
}
func (h *SocksHandler) ServeConn(conn net.Conn) {
defer conn.Close()
var req SocksRequest
req.RemoteAddr = conn.RemoteAddr().String()
req.RemoteIP, _, _ = net.SplitHostPort(req.RemoteAddr)
req.ServerAddr = conn.LocalAddr().String()
req.TraceID = log.NewXID()
var b [512]byte
n, err := io.ReadAtLeast(conn, b[:], 2)
if err != nil || n == 0 {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Msg("socks read handshake error")
return
}
req.Version = SocksVersion(b[0])
for i := 0; i < int(b[1]); i++ {
if b[i+2] == Socks5AuthMethodPassword {
req.SupportAuth = true
break
}
}
var ai ForwardAuthInfo
if h.Config.Forward.AuthTable != "" {
if !req.SupportAuth {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Msg("socks client not support auth")
return
}
conn.Write([]byte{VersionSocks5, byte(Socks5AuthMethodPassword)})
n, err = io.ReadAtLeast(conn, b[:], 4)
if err != nil || n == 0 {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Msg("socks read auth error")
return
}
// unpack username & password
req.Username = string(b[2 : 2+int(b[1])])
req.Password = string(b[3+int(b[1]) : 3+int(b[1])+int(b[2+int(b[1])])])
// auth plugin
ai, err = h.GetAuthInfo(req)
if err != nil || ai.Username != req.Username {
log.Warn().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Int("socks_version", int(req.Version)).Msg("auth error")
conn.Write([]byte{VersionSocks5, byte(Socks5StatusGeneralFailure)})
return
}
}
// auth ok
n, err = conn.Write([]byte{VersionSocks5, Socks5AuthMethodNone})
if err != nil || n == 0 {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Str("forward_policy", h.Config.Forward.Policy).Msg("socks write auth error")
return
}
n, err = io.ReadAtLeast(conn, b[:], 8)
if (err != nil && !strings.HasSuffix(err.Error(), " EOF")) || n == 0 {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Str("forward_policy", h.Config.Forward.Policy).Msg("socks read address error")
return
}
req.ConnectType = SocksCommand(b[1])
var addressType = Socks5AddressType(b[3])
switch addressType {
case Socks5IPv4Address:
req.Host = net.IP(b[4:8]).String()
case Socks5DomainName:
req.Host = string(b[5 : 5+int(b[4])]) //b[4]表示域名的长度
case Socks5IPv6Address:
req.Host = net.IP(b[4:20]).String()
}
req.Port = int(b[n-2])<<8 | int(b[n-1])
if ai.VIP == 0 {
if ai.SpeedLimit == 0 && h.Config.Forward.SpeedLimit > 0 {
ai.SpeedLimit = h.Config.Forward.SpeedLimit
}
}
var sb strings.Builder
if h.PolicyTemplate != nil {
sb.Reset()
err := h.PolicyTemplate.Execute(&sb, struct {
Request SocksRequest
ServerAddr string
}{req, req.ServerAddr})
if err != nil {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Str("forward_policy", h.Config.Forward.Policy).Msg("execute forward_policy error")
return
}
output := strings.TrimSpace(sb.String())
log.Debug().Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Interface("request", req).Str("forward_policy_output", output).Msg("execute forward_policy ok")
switch output {
case "reject", "deny":
WriteSocks5Status(conn, Socks5StatusConnectionNotAllowedByRuleset)
return
}
}
log.Info().Str("remote_ip", req.RemoteIP).Str("server_addr", req.ServerAddr).Int("socks_version", int(req.Version)).Str("username", req.Username).Str("socks_host", req.Host).Msg("forward socks request")
var dialerName = ""
dail := h.LocalDialer.DialContext
if h.UpstreamTemplate != nil {
sb.Reset()
err := h.UpstreamTemplate.Execute(&sb, struct {
Request SocksRequest
ServerAddr string
}{req, req.ServerAddr})
if err != nil {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Str("forward_dialer_name", h.Config.Forward.Dialer).Msg("execute forward_dialer error")
WriteSocks5Status(conn, Socks5StatusGeneralFailure)
return
}
if dialerName = strings.TrimSpace(sb.String()); dialerName != "" {
u, ok := h.Upstreams[dialerName]
if !ok {
log.Error().Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Str("forward_dialer_name", h.Config.Forward.Dialer).Str("dialer_name", dialerName).Msg("dialer not exists")
return
}
dail = u.DialContext
}
}
network := "tcp"
switch req.ConnectType {
case SocksCommandConnectUDP:
network = "udp"
}
log.Info().Str("server_addr", req.ServerAddr).Int("socks_version", int(req.Version)).Str("username", req.Username).Str("remote_ip", req.RemoteIP).Str("socks_network", network).Str("socks_host", req.Host).Int("socks_port", req.Port).Str("forward_dialer_name", dialerName).Msg("forward socks request")
rconn, err := dail(context.Background(), network, net.JoinHostPort(req.Host, strconv.Itoa(req.Port)))
if err != nil {
log.Error().Err(err).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Str("forward_dialer_name", h.Config.Forward.Dialer).Str("socks_host", req.Host).Int("socks_port", req.Port).Int("socks_version", int(req.Version)).Str("forward_dialer_name", dialerName).Msg("connect remote host failed")
WriteSocks5Status(conn, Socks5StatusNetworkUnreachable)
if rconn != nil {
rconn.Close()
}
return
}
defer rconn.Close()
WriteSocks5Status(conn, Socks5StatusRequestGranted)
go io.Copy(rconn, conn)
_, err = io.Copy(conn, NewRateLimitReader(rconn, ai.SpeedLimit))
if h.Config.Forward.Log {
var country, region, city string
if h.RegionResolver.MaxmindReader != nil {
country, region, city, _ = h.RegionResolver.LookupCity(context.Background(), net.ParseIP(req.RemoteIP))
}
h.ForwardLogger.Info().Stringer("trace_id", req.TraceID).Str("server_addr", req.ServerAddr).Str("remote_ip", req.RemoteIP).Str("remote_country", country).Str("remote_region", region).Str("remote_city", city).Str("forward_dialer_name", h.Config.Forward.Dialer).Str("socks_host", req.Host).Int("socks_port", req.Port).Int("socks_version", int(req.Version)).Str("forward_dialer_name", dialerName).Msg("forward socks request end")
}
return
}
func (h *SocksHandler) GetAuthInfo(req SocksRequest) (ForwardAuthInfo, error) {
username, password := req.Username, req.Password
var ai ForwardAuthInfo
if !strings.HasSuffix(h.Config.Forward.AuthTable, ".csv") {
return ai, fmt.Errorf("unsupported auth_table: %s", h.Config.Forward.AuthTable)
}
data, err := os.ReadFile(h.Config.Forward.AuthTable)
if err != nil {
return ai, err
}
var records []ForwardAuthInfo
err = csvutil.Unmarshal(data, &records)
if err != nil {
return ai, err
}
if i := slices.IndexFunc(records, func(r ForwardAuthInfo) bool {
if r.Username != username {
return false
}
switch {
case strings.HasPrefix(r.Password, "$2a$"):
return bcrypt.CompareHashAndPassword([]byte(r.Password), []byte(password)) == nil
default:
return r.Password == password
}
}); i >= 0 {
ai = records[i]
}
if ai.Username == "" {
return ai, fmt.Errorf("wrong username='%s' or password='%s'", username, password)
}
return ai, nil
}
func WriteSocks5Status(conn net.Conn, status Socks5Status) (int, error) {
return conn.Write([]byte{VersionSocks5, byte(status), 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
}