-
Notifications
You must be signed in to change notification settings - Fork 2
/
proxy.go
300 lines (251 loc) · 6.24 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
package proxy
import (
"context"
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"net"
"net/http"
"sync"
"time"
"github.com/acoshift/middleware"
)
type Proxy struct {
Logger *log.Logger
PrivateKey *ecdsa.PrivateKey
Certificate *x509.Certificate
TLSConfig *tls.Config
Transport *http.Transport
CacheStorage CacheStorage
BlacklistHosts []string
TunnelHosts []string
TunnelNotBrowser bool
RedirectHTTPS bool
Auth func(r *http.Request) bool
initOnce sync.Once
issuer *issuer
server *http.Server
httpsConn chan net.Conn
blacklistIndex index
tunnelIndex index
cache cacheBackend
}
// Init proxy now instead of lazy init
func (p *Proxy) Init() {
p.initOnce.Do(p.init)
}
func (p *Proxy) init() {
if p.Logger == nil {
p.Logger = log.New(ioutil.Discard, "", 0)
}
if p.CacheStorage == nil {
p.CacheStorage = noCache{}
}
p.cache.Store = p.CacheStorage
p.cache.MaxCacheItemSize = 16 * 1024 * 1024 // TODO: config from main
p.cache.Logger = p.Logger
go p.cache.AutoCleanup()
p.issuer = &issuer{
Logger: p.Logger,
PrivateKey: p.PrivateKey,
Certificate: p.Certificate,
Cache: p.CacheStorage,
}
p.issuer.Init()
p.blacklistIndex = loadIndex(p.BlacklistHosts)
p.tunnelIndex = loadIndex(p.TunnelHosts)
if p.Transport == nil {
p.Transport = &http.Transport{
DialContext: (&RetryDialer{
Dialer: net.Dialer{
Timeout: 15 * time.Second,
KeepAlive: 30 * time.Second,
},
MaxRetries: 2,
}).DialContext,
MaxIdleConnsPerHost: 8,
IdleConnTimeout: 5 * time.Minute,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
},
}
}
if p.TLSConfig == nil {
p.TLSConfig = &tls.Config{}
}
p.TLSConfig.GetCertificate = p.issuer.GetCertificate
mw := middleware.Chain(
middleware.Compress(middleware.BrCompressor),
)
p.server = &http.Server{
ErrorLog: p.Logger,
Handler: mw(http.HandlerFunc(p.proxyHTTPS)),
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 3 * time.Minute,
TLSConfig: p.TLSConfig,
}
p.httpsConn = make(chan net.Conn)
go p.server.ServeTLS(newConnPipe(p.httpsConn), "", "")
}
func (p *Proxy) useTunnel(r *http.Request) bool {
if p.TunnelNotBrowser && !isBrowser(r) {
return true
}
host, _, _ := net.SplitHostPort(r.Host)
if host == "" {
host = r.Host
}
if ip := net.ParseIP(host); ip != nil {
return true
}
if matchHost(p.tunnelIndex, host) {
return true
}
return false
}
func (p *Proxy) auth(r *http.Request) bool {
if p.Auth == nil {
return true
}
return p.Auth(r)
}
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.initOnce.Do(p.init)
if !p.auth(r) {
w.Header().Set("Proxy-Authenticate", "Basic")
http.Error(w, "Proxy Authentication Required", http.StatusProxyAuthRequired)
return
}
r.Header.Del("Proxy-Authorization")
// blacklist
if matchHost(p.blacklistIndex, r.Host) {
p.Logger.Printf("%s; blocked", r.Host)
http.Error(w, "Forbidden", http.StatusForbidden)
return
}
if r.Method == http.MethodConnect {
p.tunnelHTTPS(w, r)
return
}
// p.RedirectHTTPS && (p.TunnelNotBrowser -> isBrowser(r))
if p.RedirectHTTPS && (!p.TunnelNotBrowser || isBrowser(r)) {
p.Logger.Printf("%s; redirect to https", r.Host)
r.URL.Scheme = "https"
http.Redirect(w, r, r.URL.String(), http.StatusFound)
return
}
p.proxyHTTP(w, r)
}
func (p *Proxy) proxyHTTP(w http.ResponseWriter, r *http.Request) {
r.URL.Host = r.Host
if r.Header.Get("Connection") == "Upgrade" {
host, port, _ := net.SplitHostPort(r.Host)
if port == "" {
if r.URL.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}
if host == "" {
host = r.Host
}
var (
upstream net.Conn
err error
)
if r.URL.Scheme == "https" {
upstream, err = tls.Dial("tcp", host+":"+port, nil)
} else {
upstream, err = net.Dial("tcp", host+":"+port)
}
if err != nil {
p.Logger.Printf("%s; dial error; %v", r.Host, err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer upstream.Close()
downstream, _, err := w.(http.Hijacker).Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer downstream.Close()
r.Write(upstream)
stream(downstream, upstream)
return
}
if resp := p.cache.Get(r); resp != nil {
w.Header().Set("X-Proxy-Cache-Status", "HIT")
copyResponse(resp, w)
resp.Body.Close()
return
}
req := *r
req.Header = make(http.Header)
copyHeaders(req.Header, r.Header)
req.Header.Set("Connection", "keep-alive")
req.Header.Del("Accept-Encoding")
if req.ContentLength == 0 {
req.Body = nil
}
resp, err := p.Transport.RoundTrip(&req)
if err == context.Canceled {
return
}
if err != nil {
p.Logger.Printf("%s; round trip error; %v", r.Host, err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer resp.Body.Close()
resp.Header.Del("Keep-Alive")
resp.Header.Del("Alt-Svc")
if cw := p.cache.NewWriter(resp); cw != nil {
w.Header().Set("X-Proxy-Cache-Status", "MISS")
cw.CloseWithError(copyResponse(resp, w, cw))
return
}
w.Header().Set("X-Proxy-Cache-Status", "DISABLE")
copyResponse(resp, w)
}
func (p *Proxy) proxyHTTPS(w http.ResponseWriter, r *http.Request) {
r.URL.Scheme = "https"
p.proxyHTTP(w, r)
}
func (p *Proxy) tunnelHTTPS(w http.ResponseWriter, r *http.Request) {
if p.useTunnel(r) {
p.Logger.Printf("%s; tunneled", r.Host)
upstream, err := net.Dial("tcp", r.Host)
if err != nil {
p.Logger.Printf("%s; dial error; %v", r.Host, err)
http.Error(w, err.Error(), http.StatusBadGateway)
return
}
defer upstream.Close()
downstream, wr, err := w.(http.Hijacker).Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer downstream.Close()
wr.WriteString(proxyConnect)
wr.Flush()
stream(upstream, downstream)
return
}
p.Logger.Printf("%s: proxied", r.Host)
downstream, wr, err := w.(http.Hijacker).Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
wr.WriteString(proxyConnect)
wr.Flush()
p.httpsConn <- downstream
}