-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.go
80 lines (73 loc) · 1.77 KB
/
websocket.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
// Copyright (c) 2017, Boise State University All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"io"
"log"
"net"
"net/http"
"strings"
)
// isWebsocket inspects an incoming request and determines if it is for a
// websocket.
func isWebsocket(r *http.Request) bool {
upgrade := false
for _, h := range r.Header["Connection"] {
if strings.Contains(strings.ToLower(h), "upgrade") {
upgrade = true
break
}
}
if !upgrade {
return false
}
// FIXME(kyle): Can we just check for websocket in 'Upgrade'?
for _, h := range r.Header["Upgrade"] {
if strings.Contains(strings.ToLower(h), "websocket") {
return true
}
}
return false
}
// websocketProxy handles websocket requests for reverse proxy calls.
// Written by @bradfitz. See:
//
// https://groups.google.com/forum/#!msg/golang-nuts/KBx9pDlvFOc/QC5v-uC5UOgJ
func websocketProxy(target string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
d, err := net.Dial("tcp", target)
if err != nil {
http.Error(w, "Error contacting backend server.", 500)
log.Printf("Error dialing websocket backend %s: %v", target, err)
return
}
defer d.Close()
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, "Not a hijacker?", 500)
return
}
nc, _, err := hj.Hijack()
if err != nil {
log.Printf("Hijack error: %v", err)
return
}
defer nc.Close()
err = r.Write(d)
if err != nil {
log.Printf("Error copying request to target: %v", err)
return
}
errc := make(chan error, 2)
cp := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src)
errc <- err
}
go cp(d, nc)
go cp(nc, d)
if err = <-errc; err != nil {
log.Print(err)
}
})
}