-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
192 lines (169 loc) · 3.91 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
package main
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"sync"
"time"
"github.com/Andrew-Morozko/SOCKSForce/roconn"
"golang.org/x/net/proxy"
)
func doProxying(clientConn net.Conn, route *RouteConfig, destAddr string, buf *bytes.Buffer) (err error) {
var localWG sync.WaitGroup
proxyDialer, err := proxy.SOCKS5("tcp", route.Socks, nil, &net.Dialer{Timeout: route.ConnectTimeout})
if err != nil {
return fmt.Errorf("failed to connect to SOCKS server: %s", err)
}
destConn, err := proxyDialer.Dial("tcp", destAddr)
if err != nil {
return fmt.Errorf("failed to connect through SOCKS server: %s", err)
}
defer destConn.Close()
localWG.Add(2)
go func() {
defer func() {
_ = clientConn.(*net.TCPConn).CloseWrite()
localWG.Done()
}()
_, _ = io.Copy(clientConn, destConn)
}()
go func() {
defer func() {
_ = destConn.(*net.TCPConn).CloseWrite()
localWG.Done()
}()
if buf != nil {
_, err := io.Copy(destConn, buf)
buf = nil
if err != nil {
return
}
}
_, _ = io.Copy(destConn, clientConn)
}()
localWG.Wait()
return
}
func handleConnection(wg *sync.WaitGroup, route *RouteConfig, conn net.Conn) {
defer wg.Done()
var destAddr string
// close connection after timeout
var timer *time.Timer
if route.ProxyTimeout != 0 {
timer = time.AfterFunc(route.ProxyTimeout, func() {
if DEBUG {
log.Printf("Finished (timeout): %s", route.toString(destAddr))
}
conn.Close()
})
}
defer func() {
if timer == nil || timer.Stop() {
// exiting function before the connection timed-out, closing it
if DEBUG {
log.Printf("Finished: %s", route.toString(destAddr))
}
conn.Close()
}
}()
roConn := &roconn.RoConn{
Conn: conn,
}
var err error
destAddr, err = route.Destination.GetAddr(roConn)
if err != nil {
if DEBUG {
log.Printf("%s\nFailed to determine destination address: %s", route, err)
} else {
log.Printf("Route #%d: failed to determine destination address: %s", route.Num, err)
}
return
}
var buf *bytes.Buffer
if roConn.Buf.Len() != 0 {
buf = &roConn.Buf
}
roConn = nil
if DEBUG {
log.Printf("Started: %s", route.toString(destAddr))
}
err = doProxying(conn, route, destAddr, buf)
if err != nil {
if DEBUG {
log.Printf("%s failed: %s", route.toString(destAddr), err)
} else {
log.Printf("Route #%d: proxying failed: %s", route.Num, err)
}
}
}
func acceptAndHandle(wg *sync.WaitGroup, l net.Listener, route *RouteConfig) {
defer wg.Done()
for {
conn, err := l.Accept()
if err != nil {
// probably closed listener sock
return
}
wg.Add(1)
go handleConnection(wg, route, conn)
}
}
var forcedShutdown = errors.New("forced shutdown")
func multiProxyServer(routes []*RouteConfig) (err error) {
signalC := make(chan os.Signal, 2)
signal.Notify(signalC, os.Interrupt)
wg := new(sync.WaitGroup)
listeners := make([]net.Listener, 0, len(routes))
defer func() {
for _, l := range listeners {
l.Close()
}
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
case <-signalC:
err = forcedShutdown
}
}()
for _, route := range routes {
listener, err := net.Listen("tcp", route.ListenAddr)
if err != nil {
return fmt.Errorf("route #%d: failed to start listening on %s: %s", route.Num, route.ListenAddr, err)
}
listeners = append(listeners, listener)
wg.Add(1)
go acceptAndHandle(wg, listener, route)
}
<-signalC
log.Println("Shutdown requested, finalizing connections")
err = nil
return
}
func main() {
if DEBUG {
log.Println("Parsing config...")
}
routes, err := parseConfig()
if err != nil {
log.Fatalf("Configuration error: %s", err)
}
log.Println("Starting proxy server...")
err = multiProxyServer(routes)
switch err {
case nil:
log.Println("Proxy server finished")
case forcedShutdown:
log.Fatal("Forced server shutdown, dropping client connections")
default:
log.Fatalf("Proxy error: %s", err)
}
}