/
proxychain.go
202 lines (169 loc) · 5.06 KB
/
proxychain.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
package lib
import (
"io"
"net"
"sync"
"strings"
"time"
)
// ProxyChainConn a concrate inbound and outbound connection pair
type ProxyChainConn struct {
inConn net.Conn
InUDPRemoteAddr *net.UDPAddr
UDPData []byte
outConn net.Conn
IsClosed bool
}
// Exchange on connection-orintend or connectionless
// connection 1 : Client <-1-> ProxyServer
// connection 2 : ProxyDailer <-2-> UpstreamService
// connection-orintend: Duplex/Linked
// connectionless: Simplex
// | 1 | 2 | Things |
// |---------|---------|-------------|
// | Linked | Linked | Sync Break |
// | Linked | Simplex | Not Support |
// | Simplex | Linked | Time Break |
// | Simplex | Simplex | No Need |
// Exchange inbound and outbound connection data
func (c *ProxyChainConn) Exchange(to *ProxyProtoAddr) {
if c == nil {
return
}
dailer := AllDialerPools.GetDailer(to)
if dailer == nil {
log.Errorf("get a dailer to %s failed", to.Addr)
c.Close()
return
}
if dailer.SupportMultiplex() {
log.Infof("support multiplex")
}
if conn, err := dailer.GetConn(); err == nil {
c.outConn = conn
} else {
log.Errorf("connect %s failed: %s", to.Addr, err)
c.Close()
return
}
// on UDP Server Mode
if c.InUDPRemoteAddr != nil {
// send request to proxy service by dialer
conn := c.outConn
n, err := conn.Write(c.UDPData)
if n != len(c.UDPData) || err != nil {
log.Errorf("send %d bytes to %s error", len(c.UDPData), to.Addr)
c.Close()
return
}
// receive data response
conn.SetReadDeadline(time.Now().Add(time.Second * 3))
buf := make([]byte, 1500)
readSize, err := conn.Read(buf)
if err != nil {
if opErr, ok := err.(*net.OpError); ok && !opErr.Timeout() {
// error yet, shoule response back to client?
log.Errorf("read data(%d done) from %s error: %v", readSize, to.Addr, err)
}
}
// Write response back
writeSize, err := c.inConn.(*net.UDPConn).WriteToUDP(buf[:readSize], c.InUDPRemoteAddr)
if writeSize != readSize || err != nil {
log.Errorf("write %d bytes(%d done) to %s, error: %v", readSize, writeSize, c.InUDPRemoteAddr.String(), err)
}
c.Close()
return
}
wg := sync.WaitGroup{}
inConnClosed := false
outConnClosed := false
cp := func(src, dst net.Conn) {
defer wg.Done()
// buf := make([]byte, 16*1024) // 16 KB
if !c.IsClosed {
// On the paire connection any is Closed
// There is no need to wait recieve more data and copy to the other
timeoutCount := 0
var totalSize int64 = 0
for !inConnClosed && !outConnClosed {
src.SetReadDeadline(time.Now().Add(time.Second * 3))
// Reader From src, Write to dst
size, err := io.CopyBuffer(dst, src, nil)
totalSize += size
if err != nil {
if opErr, ok := err.(*net.OpError); ok {
if opErr.Timeout() {
timeoutCount++
} else {
log.Errorf("read data(%d done) from %s, opError: %v", totalSize, to.Addr, *opErr)
// If error, then close all connection
if !opErr.Temporary() {
inConnClosed = true
}
}
} else {
// error yet, shoule response back to client?
log.Errorf("read data(%d done) from %s, error: %v", totalSize, to.Addr, err)
inConnClosed = true
}
} else {
timeoutCount = 0
}
if timeoutCount == 0 {
if src == c.inConn {
inConnClosed = true
} else {
outConnClosed = true
}
}
// It should be long for some tcp using keep-alive and no heartbeat data
if timeoutCount >= 600 { // 30 minutes
log.Infof("check read from unix to tcp, limit unix data input time of %d minutes", 30)
break
}
}
log.Infof("transfor %d bytes from %s to %s", totalSize, src.RemoteAddr(), dst.RemoteAddr())
}
}
log.Infof("tunnel opened %s <-> [%s, %s] <-> %s", c.inConn.RemoteAddr(), c.inConn.LocalAddr(), c.outConn.LocalAddr(), c.outConn.RemoteAddr())
// transfer data
wg.Add(2)
// proxy request from inbound to outbound
go cp(c.inConn, c.outConn)
// proxy response from outbound to inbound
go cp(c.outConn, c.inConn)
// Block no timeout
wg.Wait()
if !c.IsClosed {
if c.inConn == nil {
log.Infof("closing connection pair: nil <-> [nil, %s] <-> %s", c.outConn.LocalAddr(), c.outConn.RemoteAddr())
} else if c.outConn == nil {
log.Infof("closing connection pair: %s <-> [%s, nil] <-> nil", c.inConn.RemoteAddr(), c.inConn.LocalAddr())
} else {
log.Infof("closing connection pair: %s <-> [%s, %s] <-> %s", c.inConn.RemoteAddr(), c.inConn.LocalAddr(), c.outConn.LocalAddr(), c.outConn.RemoteAddr())
}
}
// When to support multiplex, cannot close here
c.Close()
}
// Close connection pair
func (c *ProxyChainConn) Close() {
if c == nil {
return
}
if c.IsClosed {
return
}
// UDP Inbound Connection is a udp server, chould not be closed here
if c.inConn != nil && !strings.HasPrefix(c.inConn.LocalAddr().Network(), "udp") {
c.inConn.Close()
c.inConn = nil
}
// If want to build a connection for tcp <-> udp <-> udp <-> tcp
// Cannot close this connection, so not implements them
if c.outConn != nil {
c.outConn.Close()
c.outConn = nil
}
c.IsClosed = true
}