forked from domosekai/turnout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
255 lines (245 loc) · 6.39 KB
/
http.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
// Released under MIT License
// Copyright (c) 2020 domosekai
// HTTP proxy
package main
import (
"bufio"
"errors"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"strings"
"time"
)
const httpPipeline = 20
func doHTTP(total *int) {
defer wg.Done()
addr, err := net.ResolveTCPAddr("tcp", *httpAddr)
if err != nil {
log.Fatal(err)
}
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
logger.Printf("HTTP proxy started on TCP %s", addr)
for {
var lo localConn
var err error
lo.conn, err = listener.Accept()
if err != nil {
logger.Printf("Failed to accept new connection. Error: %s", err)
continue
}
lo.buf = bufio.NewReader(lo.conn)
mu.Lock()
*total++
open[0]++
lo.total = *total
mu.Unlock()
go lo.handleHTTP()
}
}
func (lo *localConn) handleHTTP() {
defer func() {
lo.conn.Close()
mu.Lock()
open[0]--
mu.Unlock()
}()
newConn := true
var totalBytes int64
if *verbose {
logger.Printf("H %5d: * New %s %s -> %s", lo.total, lo.conn.LocalAddr().Network(), lo.conn.RemoteAddr(), lo.conn.LocalAddr())
}
lo.mode = "H"
lo.network = "tcp"
re := new(remoteConn)
for {
req, err := http.ReadRequest(lo.buf)
if err != nil {
if re.sent > 0 {
mu.Lock()
sent[re.route] += re.sent
mu.Unlock()
totalBytes += re.sent
}
if errors.Is(err, io.EOF) || strings.Contains(err.Error(), "closed") {
if *verbose {
logger.Printf("H %5d: * Local connection closed. Sent %d bytes.", lo.total, totalBytes)
}
} else if strings.Contains(err.Error(), "reset") {
if *verbose {
logger.Printf("H %5d: * Local connection reset. Sent %d bytes.", lo.total, totalBytes)
}
if re.conn != nil {
if tcp, ok := (*re.conn).(*net.TCPConn); ok {
tcp.SetLinger(0)
}
}
} else if strings.Contains(err.Error(), "malformed") {
if *verbose {
logger.Printf("H %5d: ERR Local connection closed due to bad HTTP request. Sent %d bytes. Error: %s", lo.total, totalBytes, err)
} else {
logger.Printf("H %5d: ERR Bad HTTP request from client. Error: %s", lo.total, err)
}
} else {
if *verbose {
logger.Printf("H %5d: * Local connection closed. Sent %d bytes. Error: %s", lo.total, totalBytes, err)
}
}
break
}
if *verbose {
logger.Printf("H %5d: * HTTP %s Host %s Content-length %d", lo.total, req.Method, req.Host, req.ContentLength)
}
host, port := normalizeHostname(req.Host, "80")
if host == "" {
logger.Printf("H %5d: ERR Invalid HTTP host: %s", lo.total, req.Host)
rejectHTTP(lo.conn)
break
}
if host != lo.dest || port != lo.dport {
lo.dest = host
lo.key = net.JoinHostPort(host, port)
if net.ParseIP(host) == nil {
lo.host = host
} else {
lo.host = ""
}
lo.dport = port
newConn = true
}
if req.Method == "CONNECT" {
if re.conn != nil {
(*re.conn).Close()
}
resp := &http.Response{
Status: "200 Connection established",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
respBytes, _ := httputil.DumpResponse(resp, false)
if _, err := lo.conn.Write(respBytes); err != nil {
break
}
lo.getFirstByte()
break
} else {
req.RequestURI = strings.TrimPrefix(req.RequestURI, "http://")
req.RequestURI = strings.TrimPrefix(req.RequestURI, net.JoinHostPort(host, port))
req.RequestURI = strings.TrimPrefix(req.RequestURI, req.Host)
req.RequestURI = strings.TrimPrefix(req.RequestURI, host)
var connection bool
if req.Header.Get("Connection") == "" {
if value := req.Header.Get("Proxy-Connection"); value != "" {
req.Header.Add("Connection", value)
req.Close = shouldClose(req.ProtoMajor, req.ProtoMinor, req.Header)
}
connection = false
} else {
connection = true
}
req.Header.Del("Proxy-Connection")
header, _ := httputil.DumpRequest(req, false)
if !newConn && re.conn != nil {
re.hasConnection = connection
re.reqs <- req
if n, err := (*re.conn).Write(header); err == nil {
re.sent += int64(n)
} else {
newConn = true
}
} else {
newConn = true
}
if newConn {
if re.conn != nil {
(*re.conn).Close()
}
if re.sent > 0 {
mu.Lock()
sent[re.route] += re.sent
mu.Unlock()
totalBytes += re.sent
}
re = &remoteConn{
hasConnection: connection,
reqs: make(chan *http.Request, httpPipeline),
firstReq: req,
first: header,
firstIsFull: req.ContentLength != 0,
}
if re.getRouteFor(*lo) {
newConn = false
re.sent += int64(len(header))
} else {
lo.buf.Discard(lo.buf.Buffered())
rejectHTTP(lo.conn)
continue
}
} else {
re.lastReq = time.Now()
}
if req.ContentLength == -1 && len(req.TransferEncoding) > 0 && req.TransferEncoding[0] == "chunked" {
cr := newChunkedReader(lo.buf)
n, bytes, err := cr.copy(*re.conn)
re.sent += bytes
if errors.Is(err, io.EOF) {
if *verbose {
logger.Printf("H %5d: * Parsed %d chunks and %d bytes", lo.total, n, bytes)
}
} else {
if *verbose {
logger.Printf("H %5d: ERR Parsed %d chunks and %d bytes but failed to sent to server. Error: %s", lo.total, n, bytes, err)
}
lo.buf.Discard(lo.buf.Buffered())
continue
}
// Write trailer
var trailer []byte
for {
line, err := lo.buf.ReadSlice('\n')
trailer = append(trailer, line...)
if len(line) == 2 || err != nil {
n, _ := (*re.conn).Write(trailer)
re.sent += int64(n)
break
}
}
re.lastReq = time.Now()
} else if req.ContentLength != 0 {
bytes, err := io.Copy(*re.conn, req.Body)
re.sent += bytes
req.Body.Close()
if err != nil {
if *verbose {
logger.Printf("H %5d: ERR Failed to send HTTP body to server. Error: %s", lo.total, err)
}
lo.buf.Discard(lo.buf.Buffered())
continue
}
re.lastReq = time.Now()
}
}
}
if re.conn != nil {
(*re.conn).Close()
}
}
func rejectHTTP(conn net.Conn) {
resp := &http.Response{
Status: "502 Bad Gateway", // Squid sends 502 on unreachable hosts, 503 on failed DNS, 200 on CONNECT method
StatusCode: 502,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
}
respBytes, _ := httputil.DumpResponse(resp, false)
conn.Write(respBytes)
}