Skip to content

Commit ad6304f

Browse files
authored
Update main.go
1 parent be0e505 commit ad6304f

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

main.go

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919

2020
type ProxyConfig struct {
2121
proxyURL *url.URL
22-
isDirect bool // Indicates if this is a direct connection
22+
isDirect bool
2323
}
2424

2525
type ProxyManager struct {
@@ -35,7 +35,6 @@ func NewProxyManager(enableEdge bool) *ProxyManager {
3535
enableEdge: enableEdge,
3636
}
3737

38-
// If edge mode is enabled, add direct connection as first proxy
3938
if enableEdge {
4039
pm.proxies = append(pm.proxies, &ProxyConfig{isDirect: true})
4140
}
@@ -47,7 +46,6 @@ func (pm *ProxyManager) LoadProxies(filename string) error {
4746
file, err := os.Open(filename)
4847
if err != nil {
4948
if pm.enableEdge {
50-
// If edge mode is enabled and no proxy file, that's okay - we still have direct
5149
return nil
5250
}
5351
return err
@@ -77,7 +75,7 @@ func (pm *ProxyManager) LoadProxies(filename string) error {
7775
}
7876

7977
if len(pm.proxies) == 0 && !pm.enableEdge {
80-
return fmt.Errorf("no proxies loaded from configuration and edge mode is disabled")
78+
return fmt.Errorf("no proxies loaded and edge mode disabled")
8179
}
8280

8381
return nil
@@ -93,7 +91,6 @@ func (pm *ProxyManager) GetNextProxy() (*ProxyConfig, error) {
9391

9492
proxy := pm.proxies[pm.currentIdx]
9593
pm.currentIdx = (pm.currentIdx + 1) % len(pm.proxies)
96-
9794
return proxy, nil
9895
}
9996

@@ -107,12 +104,10 @@ func (d *ProxyDialer) Dial(ctx context.Context, network, addr string) (net.Conn,
107104
return nil, err
108105
}
109106

110-
// Handle direct connection
111107
if proxy.isDirect {
112108
return net.Dial(network, addr)
113109
}
114110

115-
// Handle proxy connection
116111
return d.dialWithProxy(proxy, network, addr)
117112
}
118113

@@ -134,8 +129,7 @@ func (d *ProxyDialer) dialSocks5(proxy *ProxyConfig, addr string) (net.Conn, err
134129
}
135130

136131
if proxy.proxyURL.User != nil {
137-
err = performSocks5Handshake(conn, proxy.proxyURL)
138-
if err != nil {
132+
if err := performSocks5Handshake(conn, proxy.proxyURL); err != nil {
139133
_ = conn.Close()
140134
return nil, err
141135
}
@@ -156,9 +150,7 @@ func (d *ProxyDialer) dialHTTP(proxy *ProxyConfig, network, addr string) (net.Co
156150
}
157151

158152
if proxy.proxyURL.Scheme == "https" {
159-
tlsConn := tls.Client(conn, &tls.Config{
160-
InsecureSkipVerify: true,
161-
})
153+
tlsConn := tls.Client(conn, &tls.Config{InsecureSkipVerify: true})
162154
if err := tlsConn.Handshake(); err != nil {
163155
_ = conn.Close()
164156
return nil, err
@@ -201,12 +193,9 @@ func loadUserCredentials(filename string) (socks5.StaticCredentials, error) {
201193

202194
file, err := os.Open(filename)
203195
if err != nil {
204-
// If the file is empty or doesn't exist, return empty credentials
205-
return credentials, nil
196+
return credentials, nil // Return empty credentials if file missing
206197
}
207-
defer func(file *os.File) {
208-
_ = file.Close()
209-
}(file)
198+
defer file.Close()
210199

211200
scanner := bufio.NewScanner(file)
212201
for scanner.Scan() {
@@ -217,7 +206,7 @@ func loadUserCredentials(filename string) (socks5.StaticCredentials, error) {
217206

218207
parts := strings.Split(line, ":")
219208
if len(parts) != 2 {
220-
return nil, fmt.Errorf("invalid credentials format in users.conf: %s", line)
209+
return nil, fmt.Errorf("invalid credential format: %s", line)
221210
}
222211
credentials[strings.TrimSpace(parts[0])] = strings.TrimSpace(parts[1])
223212
}
@@ -230,53 +219,44 @@ func loadUserCredentials(filename string) (socks5.StaticCredentials, error) {
230219
}
231220

232221
func main() {
233-
// Load user credentials from file
234222
credentials, err := loadUserCredentials("users.conf")
235223
if err != nil {
236224
log.Fatal(err)
237225
}
238226

239-
// Check if edge mode is enabled
240227
enableEdge := os.Getenv("ENABLE_EDGE_MODE") == "true"
241228

242-
// Initialize proxy manager
243229
proxyManager := NewProxyManager(enableEdge)
244230
if err := proxyManager.LoadProxies("proxies.conf"); err != nil {
245231
log.Fatal(err)
246232
}
247233

248-
dialer := &ProxyDialer{manager: proxyManager}
249-
250-
// Create SOCKS5 server configuration with or without authentication
251234
conf := &socks5.Config{
252-
Dial: dialer.Dial,
235+
Dial: (&ProxyDialer{manager: proxyManager}).Dial,
253236
}
254237

255-
// Only set credentials and auth methods if there are any credentials
256238
if len(credentials) > 0 {
257239
conf.Credentials = credentials
258-
conf.AuthMethods = []socks5.Authenticator{socks5.UserPassAuthenticator{
259-
Credentials: credentials,
260-
}}
240+
conf.AuthMethods = []socks5.Authenticator{
241+
socks5.UserPassAuthenticator{Credentials: credentials},
242+
}
261243
}
262244

263245
server, err := socks5.New(conf)
264246
if err != nil {
265247
log.Fatal(err)
266248
}
267249

268-
log.Printf("SOCKS5 server running on :1080 (Edge Mode: %v, Users: %d, Proxies: %d)\n",
269-
enableEdge, len(credentials), len(proxyManager.proxies))
250+
log.Printf("SOCKS5 server running on :1080 (Auth: %t, Edge Mode: %t, Proxies: %d)",
251+
len(credentials) > 0, enableEdge, len(proxyManager.proxies))
270252

271-
// Always listen on port 1080 inside container
272253
if err := server.ListenAndServe("tcp", ":1080"); err != nil {
273254
log.Fatal(err)
274255
}
275256
}
276257

277258
func performSocks5Handshake(conn net.Conn, proxyURL *url.URL) error {
278-
_, err := conn.Write([]byte{0x05, 0x01, 0x02})
279-
if err != nil {
259+
if _, err := conn.Write([]byte{0x05, 0x01, 0x02}); err != nil {
280260
return err
281261
}
282262

@@ -294,9 +274,9 @@ func performSocks5Handshake(conn net.Conn, proxyURL *url.URL) error {
294274

295275
auth := []byte{0x01}
296276
auth = append(auth, byte(len(username)))
297-
auth = append(auth, []byte(username)...)
277+
auth = append(auth, username...)
298278
auth = append(auth, byte(len(password)))
299-
auth = append(auth, []byte(password)...)
279+
auth = append(auth, password...)
300280

301281
if _, err := conn.Write(auth); err != nil {
302282
return err
@@ -323,19 +303,20 @@ func sendSocks5Connect(conn net.Conn, addr string) error {
323303
req := []byte{0x05, 0x01, 0x00}
324304
ip := net.ParseIP(host)
325305

326-
if ip == nil {
306+
switch {
307+
case ip == nil:
327308
req = append(req, 0x03, byte(len(host)))
328-
req = append(req, []byte(host)...)
329-
} else if ip4 := ip.To4(); ip4 != nil {
309+
req = append(req, host...)
310+
case ip.To4() != nil:
330311
req = append(req, 0x01)
331-
req = append(req, ip4...)
332-
} else {
312+
req = append(req, ip.To4()...)
313+
default:
333314
req = append(req, 0x04)
334315
req = append(req, ip.To16()...)
335316
}
336317

337318
portNum := uint16(0)
338-
_, _ = fmt.Sscanf(port, "%d", &portNum)
319+
fmt.Sscanf(port, "%d", &portNum)
339320
req = append(req, byte(portNum>>8), byte(portNum&0xff))
340321

341322
if _, err := conn.Write(req); err != nil {
@@ -353,15 +334,14 @@ func sendSocks5Connect(conn net.Conn, addr string) error {
353334

354335
switch resp[3] {
355336
case 0x01:
356-
_, err = io.ReadFull(conn, make([]byte, 4+2))
337+
_, err = io.ReadFull(conn, make([]byte, 6))
357338
case 0x03:
358339
size := make([]byte, 1)
359-
_, err = io.ReadFull(conn, size)
360-
if err == nil {
340+
if _, err = io.ReadFull(conn, size); err == nil {
361341
_, err = io.ReadFull(conn, make([]byte, int(size[0])+2))
362342
}
363343
case 0x04:
364-
_, err = io.ReadFull(conn, make([]byte, 16+2))
344+
_, err = io.ReadFull(conn, make([]byte, 18))
365345
}
366346

367347
return err

0 commit comments

Comments
 (0)