-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
77 lines (65 loc) · 1.51 KB
/
proxy.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
package main
import (
"flag"
"io"
"net"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/net/proxy"
)
func pipe(src io.Reader, dst io.Writer) {
_, err := io.Copy(dst, src)
if err != nil {
log.WithError(err).Warn("error copying data")
}
}
func handleConnection(conn net.Conn, socks5, target string) {
defer conn.Close()
// Establish a SOCKS5 proxy connection
dialer, err := proxy.SOCKS5("tcp", socks5, nil, &net.Dialer{
Timeout: 60 * time.Second,
KeepAlive: 30 * time.Second,
})
if err != nil {
log.WithError(err).Warn("cannot initialize SOCKS5 proxy")
return
}
// Dial the target server
c, err := dialer.Dial("tcp", target)
if err != nil {
log.WithError(err).WithField("target", target).Warn("cannot dial target server")
return
}
defer c.Close()
// Set up data pipes
up, down := make(chan struct{}), make(chan struct{})
go func() {
pipe(conn, c)
close(up)
}()
go func() {
pipe(c, conn)
close(down)
}()
// Wait for data transfer to complete
<-up
<-down
}
func main() {
local := flag.String("local", "127.0.0.1:9001", "address to listen")
socks5 := flag.String("proxy", "127.0.0.1:1055", "SOCKS5 proxy")
target := flag.String("target", "100.112.221.133:9001", "forwarding target")
flag.Parse()
lis, err := net.Listen("tcp", *local)
if err != nil {
log.WithError(err).Fatal("cannot listen")
}
for {
conn, err := lis.Accept()
if err != nil {
log.WithError(err).Warn("cannot accept connection")
continue
}
go handleConnection(conn, *socks5, *target)
}
}