-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
367 lines (301 loc) · 7.77 KB
/
main.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"bufio"
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"github.com/armon/go-socks5"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
)
type ProxyConfig struct {
proxyURL *url.URL
isDirect bool // Indicates if this is a direct connection
}
type ProxyManager struct {
proxies []*ProxyConfig
currentIdx int
mu sync.Mutex
enableEdge bool
}
func NewProxyManager(enableEdge bool) *ProxyManager {
pm := &ProxyManager{
proxies: make([]*ProxyConfig, 0),
enableEdge: enableEdge,
}
// If edge mode is enabled, add direct connection as first proxy
if enableEdge {
pm.proxies = append(pm.proxies, &ProxyConfig{isDirect: true})
}
return pm
}
func (pm *ProxyManager) LoadProxies(filename string) error {
file, err := os.Open(filename)
if err != nil {
if pm.enableEdge {
// If edge mode is enabled and no proxy file, that's okay - we still have direct
return nil
}
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
proxyURL, err := url.Parse(line)
if err != nil {
return fmt.Errorf("invalid proxy URL: %s", err)
}
pm.proxies = append(pm.proxies, &ProxyConfig{
proxyURL: proxyURL,
isDirect: false,
})
}
if err := scanner.Err(); err != nil {
return err
}
if len(pm.proxies) == 0 && !pm.enableEdge {
return fmt.Errorf("no proxies loaded from configuration and edge mode is disabled")
}
return nil
}
func (pm *ProxyManager) GetNextProxy() (*ProxyConfig, error) {
pm.mu.Lock()
defer pm.mu.Unlock()
if len(pm.proxies) == 0 {
return nil, fmt.Errorf("no proxies available")
}
proxy := pm.proxies[pm.currentIdx]
pm.currentIdx = (pm.currentIdx + 1) % len(pm.proxies)
return proxy, nil
}
type ProxyDialer struct {
manager *ProxyManager
}
func (d *ProxyDialer) Dial(ctx context.Context, network, addr string) (net.Conn, error) {
proxy, err := d.manager.GetNextProxy()
if err != nil {
return nil, err
}
// Handle direct connection
if proxy.isDirect {
return net.Dial(network, addr)
}
// Handle proxy connection
return d.dialWithProxy(proxy, network, addr)
}
func (d *ProxyDialer) dialWithProxy(proxy *ProxyConfig, network, addr string) (net.Conn, error) {
switch proxy.proxyURL.Scheme {
case "socks5", "socks5h":
return d.dialSocks5(proxy, addr)
case "http", "https":
return d.dialHTTP(proxy, network, addr)
default:
return nil, fmt.Errorf("unsupported proxy scheme: %s", proxy.proxyURL.Scheme)
}
}
func (d *ProxyDialer) dialSocks5(proxy *ProxyConfig, addr string) (net.Conn, error) {
conn, err := net.Dial("tcp", proxy.proxyURL.Host)
if err != nil {
return nil, err
}
if proxy.proxyURL.User != nil {
err = performSocks5Handshake(conn, proxy.proxyURL)
if err != nil {
_ = conn.Close()
return nil, err
}
}
if err := sendSocks5Connect(conn, addr); err != nil {
_ = conn.Close()
return nil, err
}
return conn, nil
}
func (d *ProxyDialer) dialHTTP(proxy *ProxyConfig, network, addr string) (net.Conn, error) {
conn, err := net.Dial("tcp", proxy.proxyURL.Host)
if err != nil {
return nil, err
}
if proxy.proxyURL.Scheme == "https" {
tlsConn := tls.Client(conn, &tls.Config{
InsecureSkipVerify: true,
})
if err := tlsConn.Handshake(); err != nil {
_ = conn.Close()
return nil, err
}
conn = tlsConn
}
req := &http.Request{
Method: "CONNECT",
URL: &url.URL{Opaque: addr},
Host: addr,
Header: make(http.Header),
}
if proxy.proxyURL.User != nil {
basicAuth := base64.StdEncoding.EncodeToString([]byte(proxy.proxyURL.User.String()))
req.Header.Set("Proxy-Authorization", "Basic "+basicAuth)
}
if err := req.Write(conn); err != nil {
_ = conn.Close()
return nil, err
}
resp, err := http.ReadResponse(bufio.NewReader(conn), req)
if err != nil {
_ = conn.Close()
return nil, err
}
if resp.StatusCode != 200 {
_ = conn.Close()
return nil, fmt.Errorf("proxy error: %s", resp.Status)
}
return conn, nil
}
func loadUserCredentials(filename string) (socks5.StaticCredentials, error) {
credentials := make(socks5.StaticCredentials)
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer func(file *os.File) {
_ = file.Close()
}(file)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
parts := strings.Split(line, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("invalid credentials format in users.conf: %s", line)
}
credentials[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
}
if err := scanner.Err(); err != nil {
return nil, err
}
if len(credentials) == 0 {
return nil, fmt.Errorf("no valid credentials found in users.conf")
}
return credentials, nil
}
func main() {
// Load user credentials from file
credentials, err := loadUserCredentials("users.conf")
if err != nil {
log.Fatal(err)
}
// Check if edge mode is enabled
enableEdge := os.Getenv("ENABLE_EDGE_MODE") == "true"
// Initialize proxy manager
proxyManager := NewProxyManager(enableEdge)
if err := proxyManager.LoadProxies("proxies.conf"); err != nil {
log.Fatal(err)
}
dialer := &ProxyDialer{manager: proxyManager}
// Create SOCKS5 server configuration with authentication
conf := &socks5.Config{
Dial: dialer.Dial,
Credentials: credentials,
AuthMethods: []socks5.Authenticator{socks5.UserPassAuthenticator{
Credentials: credentials,
}},
}
server, err := socks5.New(conf)
if err != nil {
log.Fatal(err)
}
log.Printf("SOCKS5 server running on :1080 (Edge Mode: %v, Users: %d, Proxies: %d)\n",
enableEdge, len(credentials), len(proxyManager.proxies))
// Always listen on port 1080 inside container
if err := server.ListenAndServe("tcp", ":1080"); err != nil {
log.Fatal(err)
}
}
func performSocks5Handshake(conn net.Conn, proxyURL *url.URL) error {
_, err := conn.Write([]byte{0x05, 0x01, 0x02})
if err != nil {
return err
}
resp := make([]byte, 2)
if _, err := io.ReadFull(conn, resp); err != nil {
return err
}
if resp[0] != 0x05 || resp[1] != 0x02 {
return fmt.Errorf("unsupported auth method")
}
username := proxyURL.User.Username()
password, _ := proxyURL.User.Password()
auth := []byte{0x01}
auth = append(auth, byte(len(username)))
auth = append(auth, []byte(username)...)
auth = append(auth, byte(len(password)))
auth = append(auth, []byte(password)...)
if _, err := conn.Write(auth); err != nil {
return err
}
authResp := make([]byte, 2)
if _, err := io.ReadFull(conn, authResp); err != nil {
return err
}
if authResp[1] != 0x00 {
return fmt.Errorf("authentication failed")
}
return nil
}
func sendSocks5Connect(conn net.Conn, addr string) error {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
req := []byte{0x05, 0x01, 0x00}
ip := net.ParseIP(host)
if ip == nil {
req = append(req, 0x03, byte(len(host)))
req = append(req, []byte(host)...)
} else if ip4 := ip.To4(); ip4 != nil {
req = append(req, 0x01)
req = append(req, ip4...)
} else {
req = append(req, 0x04)
req = append(req, ip.To16()...)
}
portNum := uint16(0)
_, _ = fmt.Sscanf(port, "%d", &portNum)
req = append(req, byte(portNum>>8), byte(portNum&0xff))
if _, err := conn.Write(req); err != nil {
return err
}
resp := make([]byte, 4)
if _, err := io.ReadFull(conn, resp); err != nil {
return err
}
if resp[1] != 0x00 {
return fmt.Errorf("connect failed: %d", resp[1])
}
switch resp[3] {
case 0x01:
_, err = io.ReadFull(conn, make([]byte, 4+2))
case 0x03:
size := make([]byte, 1)
_, err = io.ReadFull(conn, size)
if err == nil {
_, err = io.ReadFull(conn, make([]byte, int(size[0])+2))
}
case 0x04:
_, err = io.ReadFull(conn, make([]byte, 16+2))
}
return err
}