forked from mingcheng/socks5lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks5.go
56 lines (48 loc) · 1.17 KB
/
socks5.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
/**
* File: socks5.go
* Author: Ming Cheng<[email protected]>
*
* Created Date: Wednesday, July 6th 2022, 11:46:39 am
* Last Modified: Thursday, July 7th 2022, 6:31:37 pm
*
* http://www.opensource.org/licenses/MIT
*/
package socks5lb
import (
log "github.com/sirupsen/logrus"
"net"
)
// ListenSocks5 to listen on a specific address
func (s *Server) ListenSocks5(addr string) (err error) {
s.socks5Listener, err = net.Listen("tcp", addr)
if err != nil {
log.Error(err)
return
}
defer s.socks5Listener.Close()
for {
var socks5Conn net.Conn
socks5Conn, err = s.socks5Listener.Accept()
if err != nil {
log.Error(err)
return
}
go func() {
defer socks5Conn.Close()
backend := s.Pool.Next()
if backend == nil {
log.Error("sorry, we don't have healthy backend, so close the connection")
return
}
//log.Tracef("[socks5-tcp] %s -> %s", socks5Conn.RemoteAddr(), socks5Conn.LocalAddr())
backendConn, err := net.Dial("tcp", string(backend.Addr))
if err != nil {
log.Error(err)
return
}
defer backendConn.Close()
// transport the socket connection directly to the backend
s.Transport(socks5Conn, backendConn)
}()
}
}