forked from rekby/lets-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
306 lines (267 loc) · 9.78 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"bytes"
"github.com/Sirupsen/logrus"
"io"
"net"
"strconv"
"sync"
)
const (
NETBUF_SIZE = 2048 // bytes
)
var (
poolNetBuffers sync.Pool
)
// var-constants
var (
HEAD_CONNECTION = []byte("CONNECTION")
HEAD_CONNECTION_KEEP_ALIVE = []byte("KEEP-ALIVE")
HEAD_CONTENT_LENGTH = []byte("CONTENT-LENGTH")
)
// Get or create network buffer for proxy
func netbufGet() (buf []byte) {
bufInterface := poolNetBuffers.Get()
if bufInterface == nil {
buf = make([]byte, NETBUF_SIZE)
} else {
buf = bufInterface.([]byte)
// prevent data leak
for i := range buf {
buf[i] = 0
}
}
return buf
}
func netbufPut(buf []byte) {
poolNetBuffers.Put(buf)
}
func proxyHTTPHeaders(cid ConnectionID, targetConn net.Conn, sourceConn net.Conn) (keepalive bool, contentLength int64) {
buf := netbufGet()
defer netbufPut(buf)
var totalReadBytes int
// Read lines
readHeaderLines:
for {
var i int
var headerStart []byte
for i = 0; i < len(buf); i++ {
readBytes, err := sourceConn.Read(buf[i : i+1])
totalReadBytes += readBytes
if err != nil {
logrus.Debugf("Error while read header from '%v' cid '%v': %v", sourceConn.RemoteAddr(), cid, err)
targetConn.Close()
sourceConn.Close()
return
}
if readBytes != 1 {
logrus.Infof("Can't read a byte from header from '%v' cid '%v'", sourceConn.RemoteAddr(), cid)
targetConn.Close()
sourceConn.Close()
return
}
if buf[i] == ':' || buf[i] == '\n' {
headerStart = buf[:i+1]
logrus.Debugf("Header Name '%v' -> '%v' cid '%v': '%s'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, buf[:i])
break
}
}
if len(headerStart) == 0 {
logrus.Infof("Header line longer then buffer (%v bytes). Force close connection. '%v' -> '%v' cid '%v'.", len(buf), sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid)
targetConn.Close()
sourceConn.Close()
return
}
// Empty line - end http headers
if bytes.Equal(headerStart, []byte("\n")) || bytes.Equal(headerStart, []byte("\r\n")) {
break readHeaderLines
}
headerName := headerStart[:len(headerStart)-1]
headerNameUpperCase := bytes.ToUpper(headerName) // Cut trailing colon from start
skipHeader := false
for _, ownHeader := range cutHeaders {
if bytes.Equal(ownHeader, headerNameUpperCase) {
skipHeader = true
break
}
ownHeaderS := string(ownHeader)
headerNameS := string(headerNameUpperCase)
skipHeader = ownHeaderS == headerNameS
}
if skipHeader {
logrus.Debugf("Skip header: '%v' -> '%v' cid '%v': '%s'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, headerNameUpperCase)
buf[0] = headerStart[len(headerStart)-1]
for buf[0] != '\n' {
_, err := sourceConn.Read(buf[:1])
if err != nil {
logrus.Infof("Error read header. Close connections. '%v' -> '%v' cid '%v': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, err)
sourceConn.Close()
targetConn.Close()
return
}
}
continue readHeaderLines
}
logrus.Debugf("Copy header: '%v' -> '%v' cid '%v': '%s'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, headerName)
// copy header without changes
_, err := targetConn.Write(headerStart)
if err != nil {
logrus.Infof("Write header start, from '%v' to '%v' cid '%v', headerStart='%s': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, headerStart, err)
sourceConn.Close()
targetConn.Close()
return
}
needHeaderContent := bytes.Equal(headerNameUpperCase, HEAD_CONTENT_LENGTH) || bytes.Equal(headerNameUpperCase, HEAD_CONNECTION)
headerContent := bytes.NewBuffer(buf[1:])
headerContent.Reset()
for buf[0] != '\n' {
readBytes, err := sourceConn.Read(buf[:1])
if err != nil {
logrus.Infof("Error read header to copy. Close connections. '%v' -> '%v' cid '%v': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, err)
sourceConn.Close()
targetConn.Close()
return
}
if readBytes != 1 {
logrus.Infof("Header copy read bytes != 1. Error. Close connections. '%v' -> '%v' cid '%v'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid)
sourceConn.Close()
targetConn.Close()
return
}
_, err = targetConn.Write(buf[:1])
if err != nil {
logrus.Infof("Error write header. Close connections. '%v' -> '%v' cid '%v': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, err)
sourceConn.Close()
targetConn.Close()
return
}
if needHeaderContent {
headerContent.WriteByte(buf[0])
}
}
if needHeaderContent {
switch {
case bytes.Equal(headerNameUpperCase, HEAD_CONNECTION):
keepalive = bytes.EqualFold(HEAD_CONNECTION_KEEP_ALIVE, bytes.TrimSpace(headerContent.Bytes()))
case bytes.Equal(headerNameUpperCase, HEAD_CONTENT_LENGTH):
contentLength, err = strconv.ParseInt(string(bytes.TrimSpace(headerContent.Bytes())), 10, 64)
if err == nil {
logrus.Debugf("Header content-length parsed from '%v' to '%v' cid '%v': %v", sourceConn.RemoteAddr(),
targetConn.RemoteAddr(), cid, contentLength)
} else {
logrus.Infof("Can't header content-length parsed from '%v' to '%v' cid '%v' content '%s': %v", sourceConn.RemoteAddr(),
targetConn.RemoteAddr(), cid, headerContent.Bytes(), err)
contentLength = 0
}
default:
logrus.Debugf("ERROR. Unknow why i need header content. Code error. From '%v' to '%v' cid '%v', header name '%s'",
sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, headerNameUpperCase,
)
}
}
}
remoteTcpAddr := sourceConn.RemoteAddr().(*net.TCPAddr)
remoteAddrString := remoteTcpAddr.IP.String()
headerBuf := bytes.NewBuffer(buf)
headerBuf.Reset()
// Write real IP
for _, header := range realIPHeaderNames {
headerBuf.Write(header)
headerBuf.WriteByte(':')
headerBuf.WriteString(remoteAddrString)
headerBuf.WriteString("\r\n")
}
// Write CID
if *connectionIDHeader != "" {
headerBuf.WriteString(*connectionIDHeader)
headerBuf.WriteString(": ")
headerBuf.WriteString(cid.String())
headerBuf.WriteString("\r\n")
}
headerBuf.Write(additionalHeaders)
headerBuf.Write([]byte("\r\n")) // end http headers
logrus.Debugf("Add headers. '%v' -> '%v': '%s'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), headerBuf.Bytes())
_, err := targetConn.Write(headerBuf.Bytes())
if err != nil {
logrus.Infof("Error while write real ip headers to target '%v' -> '%v': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), err)
}
return
}
func startProxy(cid ConnectionID, targetAddr net.TCPAddr, in net.Conn) {
targetConnCommon, err := net.DialTimeout("tcp", targetAddr.String(), *targetConnTimeout)
if err != nil {
logrus.Warnf("Can't connect to target '%v' cid '%v': %v", targetAddr.String(), cid, err)
return
}
targetConn, ok := targetConnCommon.(*net.TCPConn)
if !ok {
logrus.Errorf("Can't cast connection to tcp connection, target '%v' cid '%v'", targetAddr.String(), cid)
return
}
switch *proxyMode {
case "http":
startProxyHTTP(cid, targetConn, in)
case "tcp":
startProxyTCP(cid, targetConn, in)
default:
logrus.Panicf("Unknow proxy mode cid '%v': %v", cid, *proxyMode)
}
}
func startProxyHTTP(cid ConnectionID, targetConn net.Conn, sourceConn net.Conn) {
logrus.Infof("Start http-proxy connection from '%v' to '%v' cid '%v'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid)
// answer from server proxy without changes
go func() {
buf := netbufGet()
defer netbufPut(buf)
_, err := io.CopyBuffer(sourceConn, targetConn, buf)
logrus.Debugf("Connection closed with error1 '%v' -> '%v' cid '%v': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, err)
sourceConn.Close()
targetConn.Close()
}()
// proxy incoming traffic, parse every headers
go func() {
buf := netbufGet()
defer netbufPut(buf)
var summBytesCopied int64
for {
keepalive, contentLength := proxyHTTPHeaders(cid, targetConn, sourceConn)
if keepalive {
logrus.Debugf("Start keep-alieved proxy. '%v' -> '%v' cid '%v', content-length '%v'", sourceConn.RemoteAddr(),
targetConn.RemoteAddr(), cid, contentLength)
bytesCopied, err := io.CopyBuffer(targetConn, io.LimitReader(sourceConn, contentLength), buf)
summBytesCopied += bytesCopied
logrus.Debugf("Connection chunk copied '%v' -> '%v' cid '%v', bytes transferred '%v' (%v), error: %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, bytesCopied, summBytesCopied, err)
if err != nil {
logrus.Debugf("Connection closed '%v' -> '%v' cid '%v', bytes transferred '%v' (%v), error: %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, bytesCopied, summBytesCopied, err)
}
} else {
logrus.Debugf("Start proxy without support keepalive middle headers '%v' -> '%v' cid '%v'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid)
bytesCopied, err := io.CopyBuffer(targetConn, sourceConn, buf)
summBytesCopied += bytesCopied
logrus.Debugf("Connection closed '%v' -> '%v' cid '%v', bytes transferred '%v' (%v), error: %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, bytesCopied, summBytesCopied, err)
sourceConn.Close()
targetConn.Close()
return
}
}
}()
}
func startProxyTCP(cid ConnectionID, targetConn net.Conn, sourceConn net.Conn) {
logrus.Infof("Start tcp-proxy connection from '%v' to'%v' cid '%v'", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid)
go func() {
buf := netbufGet()
defer netbufPut(buf)
_, err := io.CopyBuffer(targetConn, sourceConn, buf)
logrus.Debugf("Connection closed with error2 '%v' -> '%v' cid '%v': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, err)
sourceConn.Close()
targetConn.Close()
}()
go func() {
buf := netbufGet()
defer netbufPut(buf)
_, err := io.CopyBuffer(sourceConn, targetConn, buf)
logrus.Debugf("Connection closed with error3 '%v' -> '%v' cid '%v': %v", sourceConn.RemoteAddr(), targetConn.RemoteAddr(), cid, err)
sourceConn.Close()
targetConn.Close()
}()
}