generated from Soontao/go-project-template
/
server.go
369 lines (276 loc) · 8.01 KB
/
server.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package hsocks5
import (
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"github.com/pmezard/adblock/adblock"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/net/proxy"
)
// ProxyServer class
type ProxyServer struct {
m *adblock.RuleMatcher
priIPList *IPList
cnIPList *IPList
kvCache *KVCache
prom http.Handler
metric *ProxyServerMetrics
kl *KeyLock
option *ProxyServerOption
dialer proxy.Dialer
logger *log.Entry
}
// ProxyServerOption parameter
type ProxyServerOption struct {
ListenAddr string
RedisAddr string
SocksAddr string
ChinaSwitch bool
// provided dialer, will prefer use this programming dialer instead of socks5 service
Dialer proxy.Dialer
}
// NewProxyServer object
func NewProxyServer(option *ProxyServerOption) (*ProxyServer, error) {
pIPList := LoadIPListFrom("assets/private_ip_list.txt")
cnIPList := LoadIPListFrom("assets/china_ip_list.txt")
prom := promhttp.Handler()
logger := log.WithField("module", "hsocks5-server")
if option.ChinaSwitch {
logger.Info("enable smart traffic transfer for china")
}
return &ProxyServer{
option: option,
kvCache: NewKVCache(option.RedisAddr),
m: LoadGFWList(),
priIPList: pIPList,
cnIPList: cnIPList,
prom: prom,
metric: NewProxyServerMetrics(),
kl: NewKeyLock(),
logger: logger,
}, nil
}
// getDialer for establishing tcp connection
//
// this function will provide the flexibility of dynamic proxy provider
func (s *ProxyServer) getDialer() (rt proxy.Dialer, err error) {
rt = s.option.Dialer
if rt == nil {
rt, err = proxy.SOCKS5("tcp", s.option.SocksAddr, nil, proxy.Direct)
}
return
}
func (s *ProxyServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req.Method == "CONNECT" {
s.metric.connTotal.WithLabelValues("CONNECT").Inc()
s.handleConnect(res, req)
return
}
// handle direct http request
if len(req.URL.Host) == 0 {
s.handleRequest(res, req)
return
}
// handle proxy http request
s.metric.connTotal.WithLabelValues("REQUEST").Inc()
s.handleProxyRequest(res, req)
return
}
// isInGFWList url
func (s *ProxyServer) isInGFWList(url string) bool {
b, _, _ := s.m.Match(&adblock.Request{URL: url})
return b
}
// isWithoutProxy for the target
// 'true' means not require proxy
// 'false' means require proxy
func (s *ProxyServer) isWithoutProxy(hostnameOrURI string) (rt bool) {
// for other user, proxy all requests to socks5 proxy
if !s.option.ChinaSwitch {
return false
}
s.metric.cacheHitTotal.WithLabelValues("check").Inc()
reason := "FALLBACK"
rt = true
normalizeURI := hostnameOrURI
if !strings.HasPrefix(normalizeURI, "http") {
normalizeURI = fmt.Sprintf("https://%v", normalizeURI) // gfwlist must have protocol
}
url, err := url.Parse(normalizeURI)
hostname := url.Hostname()
// avoid the in-consistence for single hostname
// lock in `hostname` level
s.kl.Lock(hostname)
defer s.kl.Unlock(hostname)
// metric log
defer func() {
s.metric.routineResultTotal.WithLabelValues(hostname, strconv.FormatBool(rt), reason).Inc()
}()
if cachedValue, exist := s.kvCache.Get(hostname); exist { // use hostname as cache key
reason = "CACHE"
s.metric.cacheHitTotal.WithLabelValues("with_cache").Inc()
if rt, err = strconv.ParseBool(cachedValue); err != nil {
s.metric.AddErrorMetric("check", "parse_bool")
s.logger.Errorf("parse bool failed for '%v', please check your cahce", hostnameOrURI)
}
return
}
if err != nil {
s.metric.AddErrorMetric("check", "parse_bool")
s.logger.Errorf("parse url '%v' failed.", normalizeURI)
return
}
if s.priIPList.Contains(hostname) {
reason = "IN_PRIVATE_NETWORK"
rt = true // internal network
} else if s.isInGFWList(normalizeURI) {
reason = "IN_GFW_LIST"
rt = false // banned by gfw
} else if !s.cnIPList.Contains(hostname) {
reason = "NOT_IN_CN_IP_LIST"
rt = false // service server is not located in china
}
s.kvCache.Set(hostname, strconv.FormatBool(rt))
return
}
func (s *ProxyServer) handleConnect(w http.ResponseWriter, req *http.Request) {
host := req.Host // host & port
hostname := req.URL.Hostname()
s.logger.Infof("CONNECT %v", host)
hj, ok := w.(http.Hijacker)
if !ok {
s.logger.Error("hijacker http request failed")
s.metric.AddErrorMetric("connect", "hijacker failed")
s.sendError(w, fmt.Errorf("Proxt Server Internal Error"))
return // error break
}
conn, bufrw, err := hj.Hijack() // get TCp connection
if err != nil {
s.logger.Error(err)
s.metric.AddErrorMetric("connect", "hijacker conn failed")
s.sendError(w, err)
return // error break
}
if conn != nil {
defer conn.Close()
}
var remote net.Conn
if s.isWithoutProxy(hostname) {
remote, err = net.Dial("tcp", host)
} else {
if dial, err := s.getDialer(); err == nil {
remote, err = dial.Dial("tcp", host)
}
if err != nil {
s.metric.AddErrorMetric("connect", "dial proxy failed")
s.sendError(w, err)
}
}
if err != nil {
s.logger.Error(err)
conn.Close()
return // error break
}
if remote != nil {
defer remote.Close()
}
bufrw.WriteString("HTTP/1.1 200 Connection established\r\n\r\n") // connect accept
if err := bufrw.Flush(); err != nil {
s.metric.AddErrorMetric("connect", "buffer flush failed")
s.logger.Error(err)
return // error break
}
if remote != nil && conn != nil {
ch := make(chan error, 2)
go func() {
w, err := CopyBuffer(remote, conn, make([]byte, 32*1024))
s.metric.trafficSizeTotal.WithLabelValues("upload").Add(float64(w))
ch <- err
}()
go func() {
w, err := CopyBuffer(conn, remote, make([]byte, 32*1024))
s.metric.trafficSizeTotal.WithLabelValues("download").Add(float64(w))
ch <- err
}()
<-ch
<-ch
}
// all transfer finished
// close connections with 'defer'
}
func (s *ProxyServer) handleRequest(w http.ResponseWriter, req *http.Request) {
// >> Prometheus
if req.RequestURI == "/hsocks5/__/metric" {
s.prom.ServeHTTP(w, req)
return
}
// << Prometheus
// default 404 not exist
w.WriteHeader(http.StatusNotFound)
return
}
func (s *ProxyServer) handleProxyRequest(w http.ResponseWriter, req *http.Request) {
host := req.Host // host & port
s.logger.Infof("HTTP %v %v", req.Method, host)
var client http.Client
if s.isWithoutProxy(req.RequestURI) {
client = http.Client{}
} else {
dialer, err := s.getDialer()
if err != nil {
s.metric.AddErrorMetric("request", "create proxy failed")
s.sendError(w, err)
return
}
client = http.Client{
Transport: &http.Transport{Dial: dialer.Dial},
CheckRedirect: noRedirect,
}
}
// create a new http request from original inbound request
newReq, err := http.NewRequest(req.Method, req.URL.String(), req.Body)
newReq.Header = req.Header.Clone()
if err != nil {
s.logger.Error(err)
s.metric.AddErrorMetric("request", "copy original request failed")
s.sendError(w, err)
return
}
proxyResponse, err := client.Do(newReq) // if err happened, dont access proxyResponse
if err != nil {
s.logger.Error(err)
s.metric.AddErrorMetric("request", "send request")
s.sendError(w, err)
} else {
s.metric.requestStatusTotal.WithLabelValues(newReq.URL.Hostname(), fmt.Sprint(proxyResponse.StatusCode)).Inc()
s.pipeResponse(proxyResponse, w)
}
}
// sendError alias
func (s *ProxyServer) sendError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("http proxy internal error happened, %v", err)))
}
// pipeResponse for http request
func (s *ProxyServer) pipeResponse(from *http.Response, to http.ResponseWriter) {
h := to.Header()
for k, vs := range from.Header {
for _, v := range vs {
h.Set(k, v)
}
}
to.WriteHeader(from.StatusCode)
defer from.Body.Close()
w, _ := Copy(to, from.Body)
s.metric.trafficSizeTotal.WithLabelValues("download").Add(float64(w))
}
// Start server
func (s *ProxyServer) Start() error {
hs := http.Server{Addr: s.option.ListenAddr, Handler: s}
s.logger.Infof("start server at %v", s.option.ListenAddr)
return hs.ListenAndServe()
}