forked from andybalholm/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
748 lines (665 loc) · 20.5 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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
package main
import (
"bytes"
"compress/flate"
"context"
"errors"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/andybalholm/brotli"
"github.com/andybalholm/cascadia"
"github.com/andybalholm/dhash"
"github.com/golang/gddo/httputil"
"github.com/golang/gddo/httputil/header"
"github.com/klauspost/compress/gzip"
_ "golang.org/x/image/webp"
"golang.org/x/net/html"
"golang.org/x/net/html/charset"
)
type proxyHandler struct {
// TLS is whether this is an HTTPS connection.
TLS bool
// tlsFingerprint is the JA3 TLS fingerprint of the client (if available).
tlsFingerprint string
// connectPort is the server port that was specified in a CONNECT request.
connectPort string
// user is a user that has already been authenticated.
user string
// rt is the RoundTripper that will be used to fulfill the requests.
// If it is nil, a default Transport will be used.
rt http.RoundTripper
}
var ip6Loopback = net.ParseIP("::1")
// lanAddress returns whether addr is in one of the LAN address ranges.
func lanAddress(addr string) bool {
ip := net.ParseIP(addr)
if ip4 := ip.To4(); ip4 != nil {
switch ip4[0] {
case 10, 127:
return true
case 172:
return ip4[1]&0xf0 == 16
case 192:
return ip4[1] == 168
}
return false
}
// IPv6
switch {
case ip[0]&0xfe == 0xfc:
return true
case ip[0] == 0xfe && (ip[1]&0xfc) == 0x80:
return true
case ip.Equal(ip6Loopback):
return true
}
return false
}
var titleSelector = cascadia.MustCompile("title")
func (h proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
activeConnections.Add(1)
defer activeConnections.Done()
// If a request is directed to Redwood, rather than proxied or intercepted,
// it should be handled as an API request.
if !h.TLS && r.URL.Host == "" && strings.Contains(r.Host, ":") {
handleAPI(w, r)
return
}
conf := getConfig()
if len(r.URL.String()) > 10000 {
http.Error(w, "URL too long", http.StatusRequestURITooLong)
return
}
client := r.RemoteAddr
host, _, err := net.SplitHostPort(client)
if err == nil {
client = host
}
authUser := ""
switch {
case h.user != "":
authUser = h.user
case r.Header.Get("Proxy-Authorization") != "":
user, pass, ok := ProxyCredentials(r)
if ok {
if conf.ValidCredentials(user, pass) {
authUser = user
} else {
log.Printf("Incorrect username or password from %v: %s:%s", r.RemoteAddr, user, pass)
}
} else {
log.Printf("Invalid Proxy-Authorization header from %v: %q", r.RemoteAddr, r.Header.Get("Proxy-Authorization"))
}
}
// Reconstruct the URL if it is incomplete (i.e. on a transparent proxy).
if r.URL.Scheme == "" {
if h.TLS {
r.URL.Scheme = "https"
} else {
r.URL.Scheme = "http"
}
}
if r.URL.Host == "" {
if r.Host != "" {
r.URL.Host = r.Host
} else {
log.Printf("Request from %s has no host in URL: %v", client, r.URL)
// Delay a while since some programs really hammer us with this kind of request.
time.Sleep(time.Second)
http.Error(w, "No host in request URL, and no Host header.", http.StatusBadRequest)
return
}
}
if realHost, ok := conf.VirtualHosts[r.Host]; ok {
r.Host = realHost
r.URL.Host = realHost
}
user := client
if authUser != "" {
user = authUser
}
// Handle IPv6 hostname without brackets in CONNECT request.
if r.Method == "CONNECT" {
hostport := r.URL.Host
host, port, err := net.SplitHostPort(hostport)
if err, ok := err.(*net.AddrError); ok && err.Err == "too many colons in address" {
colon := strings.LastIndex(hostport, ":")
host, port = hostport[:colon], hostport[colon+1:]
if ip := net.ParseIP(host); ip != nil {
r.URL.Host = net.JoinHostPort(host, port)
}
}
}
// Some proxy interception programs send HTTP traffic as CONNECT requests
// for port 80.
if _, port, err := net.SplitHostPort(r.URL.Host); err == nil && port == "80" && r.Method == "CONNECT" {
conn, err := newHijackedConn(w)
if err != nil {
log.Println("Error hijacking connection for CONNECT request to %s: %v", r.URL.Host, err)
return
}
fmt.Fprint(conn, "HTTP/1.1 200 Connection Established\r\n\r\n")
server := &http.Server{
Handler: proxyHandler{
TLS: false,
connectPort: port,
user: authUser,
rt: h.rt,
},
IdleTimeout: conf.CloseIdleConnections,
}
conf = nil // Allow it to be garbage-collected, since we won't use it any more.
server.Serve(&singleListener{conn: conn})
return
}
if h.tlsFingerprint != "" {
r = r.WithContext(context.WithValue(r.Context(), tlsFingerprintKey{}, h.tlsFingerprint))
}
tally := conf.URLRules.MatchingRules(r.URL)
scores := conf.categoryScores(tally)
reqScores := scores
reqACLs := conf.ACLs.requestACLs(r, authUser)
possibleActions := []string{
"allow",
"block",
"block-invisible",
}
if authUser == "" && !h.TLS {
possibleActions = append(possibleActions, "require-auth")
}
if r.Method == "CONNECT" && conf.TLSReady {
possibleActions = append(possibleActions, "ssl-bump")
}
thisRule, ignored := conf.ChooseACLCategoryAction(reqACLs, scores, conf.Threshold, possibleActions...)
if r.Method == "CONNECT" && conf.TLSReady && thisRule.Action == "" {
// If the result is unclear, go ahead and start to bump the connection.
// The ACLs will be checked one more time anyway.
thisRule.Action = "ssl-bump"
}
switch thisRule.Action {
case "require-auth":
conf.send407(w)
log.Printf("Missing required proxy authentication from %v to %v", r.RemoteAddr, r.URL)
return
case "block":
conf.showBlockPage(w, r, nil, user, tally, scores, thisRule)
logAccess(r, nil, 0, false, user, tally, scores, thisRule, "", ignored)
return
case "block-invisible":
showInvisibleBlock(w)
logAccess(r, nil, 0, false, user, tally, scores, thisRule, "", ignored)
return
case "ssl-bump":
conn, err := newHijackedConn(w)
if err != nil {
log.Println("Error hijacking connection for CONNECT request to %s: %v", r.URL.Host, err)
return
}
fmt.Fprint(conn, "HTTP/1.1 200 Connection Established\r\n\r\n")
conf = nil // Allow it to be garbage-collected, since we won't use it any more.
SSLBump(conn, r.URL.Host, user, authUser, r)
return
}
if r.Host == localServer {
logAccess(r, nil, 0, false, user, tally, scores, thisRule, "", ignored)
conf.ServeMux.ServeHTTP(w, r)
return
}
if r.Method == "CONNECT" {
conn, err := newHijackedConn(w)
if err != nil {
log.Println("Error hijacking connection for CONNECT request to %s: %v", r.URL.Host, err)
return
}
fmt.Fprint(conn, "HTTP/1.1 200 Connection Established\r\n\r\n")
logAccess(r, nil, 0, false, user, tally, scores, thisRule, "", ignored)
conf = nil // Allow it to be garbage-collected, since we won't use it any more.
connectDirect(conn, r.URL.Host, nil)
return
}
if r.Header.Get("Upgrade") == "websocket" {
logAccess(r, nil, 0, false, user, tally, scores, thisRule, "", ignored)
h.makeWebsocketConnection(w, r)
return
}
if len(r.Header["X-Forwarded-For"]) >= 10 {
http.Error(w, "Proxy forwarding loop", http.StatusBadRequest)
log.Printf("Proxy forwarding loop from %s to %v", r.Header.Get("X-Forwarded-For"), r.URL)
return
}
headerRule, _ := conf.ChooseACLCategoryAction(reqACLs, scores, conf.Threshold, "disable-proxy-headers")
if headerRule.Action != "disable-proxy-headers" {
viaHosts := r.Header["Via"]
viaHosts = append(viaHosts, strings.TrimPrefix(r.Proto, "HTTP/")+" Redwood")
r.Header.Set("Via", strings.Join(viaHosts, ", "))
r.Header.Add("X-Forwarded-For", client)
}
// Limit Accept-Encoding header to encodings we can handle.
acceptEncoding := header.ParseAccept(r.Header, "Accept-Encoding")
filteredEncodings := make([]header.AcceptSpec, 0, len(acceptEncoding))
for _, a := range acceptEncoding {
switch a.Value {
case "br", "gzip", "deflate":
filteredEncodings = append(filteredEncodings, a)
}
}
switch {
case len(filteredEncodings) == 0:
r.Header.Del("Accept-Encoding")
case len(filteredEncodings) != len(acceptEncoding):
specs := make([]string, len(filteredEncodings))
for i, a := range filteredEncodings {
if a.Q == 1 {
specs[i] = a.Value
} else {
specs[i] = fmt.Sprintf("%s;q=%f", a.Value, a.Q)
}
}
r.Header.Set("Accept-Encoding", strings.Join(specs, ", "))
}
conf.changeQuery(r.URL)
var rt http.RoundTripper
if h.rt == nil {
rt = httpTransport
} else {
rt = h.rt
}
// Some HTTP/2 servers don't like having a body on a GET request, even if
// it is empty.
if r.ContentLength == 0 {
r.Body.Close()
r.Body = nil
}
removeHopByHopHeaders(r.Header)
resp, err := rt.RoundTrip(r)
if err != nil && strings.Contains(err.Error(), "trailer header without chunked transfer encoding") && r.Method == "GET" && r.URL.Scheme == "http" {
// Use a simpler transport that will ignore the error.
log.Printf("Using simpleTransport for %v", r.URL)
resp, err = simpleTransport{}.RoundTrip(r)
}
if err != nil {
conf.showErrorPage(w, r, err)
log.Printf("error fetching %s: %s", r.URL, err)
logAccess(r, nil, 0, false, user, tally, scores, thisRule, "", ignored)
return
}
defer resp.Body.Close()
// Prevent switching to QUIC.
resp.Header.Del("Alternate-Protocol")
resp.Header.Del("Alt-Svc")
removeHopByHopHeaders(resp.Header)
// Yet another workaround for https://github.com/golang/go/issues/31753
if resp.Header.Get("Content-Type") == "" && resp.Header.Get("Content-Encoding") == "gzip" && r.Method != "HEAD" {
gzr, err := gzip.NewReader(resp.Body)
if err != nil {
log.Printf("Error creating gzip reader for %v: %v", r.URL, err)
} else {
resp.Body = gzr
resp.Header.Del("Content-Encoding")
}
}
respACLs := conf.ACLs.responseACLs(resp)
acls := unionACLSets(reqACLs, respACLs)
headerRule, _ = conf.ChooseACLCategoryAction(acls, scores, conf.Threshold, "disable-proxy-headers")
if headerRule.Action != "disable-proxy-headers" {
viaHosts := resp.Header["Via"]
viaHosts = append(viaHosts, strings.TrimPrefix(resp.Proto, "HTTP/")+" Redwood")
resp.Header.Set("Via", strings.Join(viaHosts, ", "))
}
if r.Method == "HEAD" {
thisRule, ignored = conf.ChooseACLCategoryAction(acls, scores, conf.Threshold, "allow", "block", "block-invisible")
} else {
thisRule, ignored = conf.ChooseACLCategoryAction(acls, scores, conf.Threshold, "allow", "block", "block-invisible", "hash-image", "phrase-scan")
}
if thisRule.Action == "" {
thisRule.Action = "allow"
}
switch thisRule.Action {
case "allow":
if resp.ContentLength > 0 {
w.Header().Set("Content-Length", strconv.FormatInt(resp.ContentLength, 10))
}
copyResponseHeader(w, resp)
n, err := io.Copy(w, resp.Body)
if err != nil && err != context.Canceled {
log.Printf("error while copying response (URL: %s): %s", r.URL, err)
}
logAccess(r, resp, int(n), false, user, tally, scores, thisRule, "", ignored)
return
case "block":
conf.showBlockPage(w, r, resp, user, tally, scores, thisRule)
logAccess(r, resp, 0, false, user, tally, scores, thisRule, "", ignored)
return
case "block-invisible":
showInvisibleBlock(w)
logAccess(r, resp, 0, false, user, tally, scores, thisRule, "", ignored)
return
}
lr := &io.LimitedReader{
R: resp.Body,
N: int64(conf.MaxContentScanSize),
}
content, err := ioutil.ReadAll(lr)
if err != nil {
log.Printf("error while reading response body (URL: %s): %s", r.URL, err)
}
if lr.N == 0 {
log.Println("response body too long to filter:", r.URL)
if resp.ContentLength > 0 {
w.Header().Set("Content-Length", strconv.FormatInt(resp.ContentLength, 10))
}
copyResponseHeader(w, resp)
w.Write(content)
n, err := io.Copy(w, resp.Body)
if err != nil && err != context.Canceled {
log.Printf("error while copying response (URL: %s): %s", r.URL, err)
}
logAccess(r, resp, int(n)+len(content), false, user, tally, scores, ACLActionRule{Action: "allow", Needed: []string{"too-long-to-filter"}}, "", ignored)
return
}
modified := false
pageTitle := ""
var compressedContent []byte
if ce := resp.Header.Get("Content-Encoding"); ce != "" {
compressedContent = content
br := bytes.NewReader(compressedContent)
var decompressor io.Reader
switch ce {
case "br":
decompressor = brotli.NewReader(br)
case "deflate":
decompressor = flate.NewReader(br)
case "gzip":
decompressor, err = gzip.NewReader(br)
if err != nil {
log.Printf("Error creating gzip.Reader for %v: %v", r.URL, err)
decompressor = br
}
default:
log.Printf("Unrecognized Content-Encoding (%q) at %v", ce, r.URL)
decompressor = br
}
content, err = ioutil.ReadAll(decompressor)
if err != nil {
log.Printf("Error decompressing response body from %v: %v", r.URL, err)
}
}
switch thisRule.Action {
case "phrase-scan":
contentType := resp.Header.Get("Content-Type")
_, cs, _ := charset.DetermineEncoding(content, contentType)
var doc *html.Node
if strings.Contains(contentType, "html") {
if conf.LogTitle {
doc, err = parseHTML(content, cs)
if err != nil {
log.Printf("Error parsing HTML from %s: %s", r.URL, err)
} else {
t := titleSelector.MatchFirst(doc)
if t != nil {
if titleText := t.FirstChild; titleText != nil && titleText.Type == html.TextNode {
pageTitle = strings.Replace(strings.TrimSpace(titleText.Data), "\n", " ", -1)
}
}
}
}
modified = conf.pruneContent(r.URL, &content, cs, &doc)
if modified {
cs = "utf-8"
}
}
conf.scanContent(content, contentType, cs, tally)
if strings.Contains(contentType, "html") {
aclsWithCategories := copyACLSet(acls)
for name, score := range reqScores {
if conf.Categories[name].action == ACL && score > 0 {
aclsWithCategories[name] = true
}
}
modifiedAfterScan := conf.doFilteredPruning(r.URL, content, cs, aclsWithCategories, &doc)
censorRule, _ := conf.ChooseACLCategoryAction(acls, scores, conf.Threshold, "censor-words")
if censorRule.Action == "censor-words" {
if doc == nil {
doc, _ = parseHTML(content, cs)
}
if censorHTML(doc, conf.CensoredWords) {
modifiedAfterScan = true
}
}
if modifiedAfterScan {
b := new(bytes.Buffer)
if err := html.Render(b, doc); err != nil {
log.Printf("Error rendering modified content from %s: %v", r.URL, err)
} else {
content = b.Bytes()
modified = true
}
}
if modified {
resp.Header.Set("Content-Type", "text/html; charset=utf-8")
resp.Header.Del("Content-Encoding")
compressedContent = nil
}
}
if compressedContent == nil && len(content) > 1000 && resp.Header.Get("Content-Type") != "" {
// Either the content was not compressed from upstream,
// or we invalidated the original compressed content due to pruning.
// So we should probably compress the content now.
encoding := httputil.NegotiateContentEncoding(r, []string{"br", "gzip"})
buf := new(bytes.Buffer)
var compressor io.WriteCloser
switch encoding {
case "br":
compressor = brotli.NewWriterOptions(buf, brotli.WriterOptions{Quality: conf.BrotliLevel})
case "gzip":
compressor, err = gzip.NewWriterLevel(buf, conf.GZIPLevel)
if err != nil {
log.Println("Error creating gzip compressor:", err)
compressor = nil
}
}
if compressor != nil {
compressor.Write(content)
if err := compressor.Close(); err != nil {
log.Printf("Error compressing content of %v: %v", r.URL, err)
} else {
compressedContent = buf.Bytes()
resp.Header.Set("Content-Encoding", encoding)
}
}
}
case "hash-image":
img, _, err := image.Decode(bytes.NewReader(content))
if err != nil {
log.Printf("Error decoding image from %v: %v", r.URL, err)
break
}
hash := dhash.New(img)
for _, h := range conf.ImageHashes {
distance := dhash.Distance(hash, h.Hash)
if distance <= h.Threshold || h.Threshold == -1 && distance <= conf.DhashThreshold {
tally[rule{imageHash, h.String()}]++
}
}
}
scores = conf.categoryScores(tally)
contentRule, _ := conf.ChooseACLCategoryAction(acls, scores, 1, "log-content")
if contentRule.Action == "log-content" {
logContent(r.URL, content, scores)
}
thisRule, ignored = conf.ChooseACLCategoryAction(acls, scores, conf.Threshold, "allow", "block", "block-invisible")
if thisRule.Action == "" {
thisRule.Action = "allow"
}
switch thisRule.Action {
case "block":
conf.showBlockPage(w, r, resp, user, tally, scores, thisRule)
logAccess(r, resp, len(content), modified, user, tally, scores, thisRule, pageTitle, ignored)
return
case "block-invisible":
showInvisibleBlock(w)
logAccess(r, resp, len(content), modified, user, tally, scores, thisRule, pageTitle, ignored)
return
}
if compressedContent == nil {
w.Header().Set("Content-Length", strconv.Itoa(len(content)))
} else {
content = compressedContent
}
copyResponseHeader(w, resp)
w.Write(content)
logAccess(r, resp, len(content), modified, user, tally, scores, thisRule, pageTitle, ignored)
}
// copyResponseHeader writes resp's header and status code to w.
func copyResponseHeader(w http.ResponseWriter, resp *http.Response) {
newHeader := w.Header()
for key, values := range resp.Header {
if key == "Content-Length" {
continue
}
for _, v := range values {
newHeader.Add(key, v)
}
}
statusCode := resp.StatusCode
if statusCode < 100 || statusCode >= 600 {
statusCode = http.StatusBadGateway
}
w.WriteHeader(statusCode)
}
// A hijackedConn is a connection that has been hijacked (to fulfill a CONNECT
// request).
type hijackedConn struct {
net.Conn
io.Reader
}
func (hc *hijackedConn) Read(b []byte) (int, error) {
return hc.Reader.Read(b)
}
func newHijackedConn(w http.ResponseWriter) (*hijackedConn, error) {
hj, ok := w.(http.Hijacker)
if !ok {
return nil, errors.New("connection doesn't support hijacking")
}
conn, bufrw, err := hj.Hijack()
if err != nil {
return nil, err
}
err = bufrw.Flush()
if err != nil {
conn.Close()
return nil, err
}
return &hijackedConn{
Conn: conn,
Reader: bufrw.Reader,
}, nil
}
func (h proxyHandler) makeWebsocketConnection(w http.ResponseWriter, r *http.Request) {
addr := r.Host
if _, _, err := net.SplitHostPort(addr); err != nil {
// There is no port specified; we need to add it.
port := h.connectPort
if port == "" {
port = "80"
}
addr = net.JoinHostPort(addr, port)
}
var err error
var serverConn net.Conn
if h.TLS {
serverConn, err = dialWithExtraRootCerts("tcp", addr)
} else {
serverConn, err = net.Dial("tcp", addr)
}
if err != nil {
log.Printf("Error making websocket connection to %s: %v", addr, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Some servers are very particular about the
// capitalization of the special WebSocket headers.
for k, v := range r.Header {
if strings.HasPrefix(k, "Sec-Websocket-") {
newKey := "Sec-WebSocket-" + strings.TrimPrefix(k, "Sec-Websocket-")
delete(r.Header, k)
r.Header[newKey] = v
}
}
err = r.Write(serverConn)
if err != nil {
log.Printf("Error sending websocket request to %s: %v", addr, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
hj, ok := w.(http.Hijacker)
if !ok {
log.Printf("Couldn't hijack client connection for websocket to %s", addr)
http.Error(w, "Couldn't create a websocket connection", http.StatusInternalServerError)
return
}
conn, bufrw, err := hj.Hijack()
if err != nil {
log.Printf("Error hijacking client connection for websocket to %s: %v", addr, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
go func() {
io.Copy(conn, serverConn)
conn.Close()
}()
io.Copy(serverConn, bufrw)
serverConn.Close()
return
}
var hopByHop = []string{
"Connection",
"Keep-Alive",
"Proxy-Authenticate",
"Proxy-Authorization",
"Proxy-Connection",
"TE",
"Trailer",
"Transfer-Encoding",
"Upgrade",
}
// removeHopByHopHeaders removes header fields listed in
// http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-14#section-7.1.3.1
func removeHopByHopHeaders(h http.Header) {
toRemove := hopByHop
if c := h.Get("Connection"); c != "" {
for _, key := range strings.Split(c, ",") {
toRemove = append(toRemove, strings.TrimSpace(key))
}
}
for _, key := range toRemove {
h.Del(key)
}
}
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away. (Copied from net/http package)
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}