-
Notifications
You must be signed in to change notification settings - Fork 17
/
websocket_handler.go
150 lines (131 loc) · 3.33 KB
/
websocket_handler.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
package mps
import (
"bufio"
"context"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/telanflow/mps/pool"
)
// WebsocketHandler The websocket proxy type. Implements http.Handler.
type WebsocketHandler struct {
Ctx *Context
BufferPool httputil.BufferPool
}
// NewWebsocketHandler Create a websocket handler
func NewWebsocketHandler() *WebsocketHandler {
return &WebsocketHandler{
Ctx: NewContext(),
BufferPool: pool.DefaultBuffer,
}
}
// NewWebsocketHandlerWithContext Create a tunnel handler with Context
func NewWebsocketHandlerWithContext(ctx *Context) *WebsocketHandler {
return &WebsocketHandler{
Ctx: ctx,
BufferPool: pool.DefaultBuffer,
}
}
// Standard net/http function. You can use it alone
func (ws *WebsocketHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
// Whether to upgrade to Websocket
if !isWebSocketRequest(req) {
return
}
// hijacker connection
clientConn, err := hijacker(rw)
if err != nil {
http.Error(rw, err.Error(), 502)
return
}
var (
u *url.URL
targetAddr = hostAndPort(req.URL.Host)
)
if ws.Ctx.Transport != nil && ws.Ctx.Transport.Proxy != nil {
u, err = ws.Ctx.Transport.Proxy(req)
if err != nil {
ConnError(clientConn)
return
}
if u != nil {
// connect addr eg. "localhost:443"
targetAddr = hostAndPort(u.Host)
}
}
targetConn, err := ws.ConnectDial("tcp", targetAddr)
if err != nil {
return
}
defer targetConn.Close()
// Perform handshake
// write handshake request to target
err = req.Write(targetConn)
if err != nil {
return
}
// Read handshake response from target
targetReader := bufio.NewReader(targetConn)
resp, err := http.ReadResponse(targetReader, req)
if err != nil {
return
}
// Proxy handshake back to client
err = resp.Write(clientConn)
if err != nil {
return
}
// Proxy ws connection
go func() {
buf := ws.buffer().Get()
_, _ = io.CopyBuffer(targetConn, clientConn, buf)
ws.buffer().Put(buf)
_ = clientConn.Close()
}()
buf := ws.buffer().Get()
_, _ = io.CopyBuffer(clientConn, targetConn, buf)
ws.buffer().Put(buf)
}
func (ws *WebsocketHandler) ConnectDial(network, addr string) (net.Conn, error) {
if ws.Ctx.Transport != nil && ws.Ctx.Transport.DialContext != nil {
return ws.Ctx.Transport.DialContext(ws.context(), network, addr)
}
return net.DialTimeout(network, addr, 30*time.Second)
}
// Transport get http.Transport instance
func (ws *WebsocketHandler) Transport() *http.Transport {
return ws.Ctx.Transport
}
// context returned a context.Context
func (ws *WebsocketHandler) context() context.Context {
if ws.Ctx.Context != nil {
return ws.Ctx.Context
}
return context.Background()
}
// buffer returned a httputil.BufferPool
func (ws *WebsocketHandler) buffer() httputil.BufferPool {
if ws.BufferPool != nil {
return ws.BufferPool
}
return pool.DefaultBuffer
}
// isWebSocketRequest to upgrade to a Websocket request
func isWebSocketRequest(req *http.Request) bool {
return headerContains(req.Header, "Connection", "upgrade") &&
headerContains(req.Header, "Upgrade", "websocket")
}
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
}