-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathwebsocket.go
56 lines (49 loc) · 1.35 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
package goproxy
import (
"io"
"net"
"net/http"
"strings"
)
func headerContains(header http.Header, name string, value string) bool {
for _, v := range header[name] {
for _, s := range strings.Split(v, ",") {
if strings.EqualFold(value, strings.TrimSpace(s)) {
return true
}
}
}
return false
}
func isWebSocketHandshake(header http.Header) bool {
return headerContains(header, "Connection", "Upgrade") &&
headerContains(header, "Upgrade", "websocket")
}
func (proxy *ProxyHttpServer) hijackConnection(ctx *ProxyCtx, w http.ResponseWriter) (net.Conn, error) {
// Connect to Client
hj, ok := w.(http.Hijacker)
if !ok {
panic("httpserver does not support hijacking")
}
clientConn, _, err := hj.Hijack()
if err != nil {
ctx.Warnf("Hijack error: %v", err)
return nil, err
}
return clientConn, nil
}
func (proxy *ProxyHttpServer) proxyWebsocket(ctx *ProxyCtx, remoteConn io.ReadWriter, proxyClient io.ReadWriter) {
// 2 is the number of goroutines, this code is implemented according to
// https://stackoverflow.com/questions/52031332/wait-for-one-goroutine-to-finish
waitChan := make(chan struct{}, 2)
go func() {
_ = copyOrWarn(ctx, remoteConn, proxyClient)
waitChan <- struct{}{}
}()
go func() {
_ = copyOrWarn(ctx, proxyClient, remoteConn)
waitChan <- struct{}{}
}()
// Wait until one end closes the connection
<-waitChan
}