/
websocket.go
225 lines (181 loc) · 5.53 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
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
package mitmproxy
import (
"crypto/tls"
"fmt"
"log"
"net/http"
"time"
"github.com/gorilla/websocket"
)
var (
DefaultWSUpgrader = &websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
// Only the targetConn choose to CheckOrigin or not
CheckOrigin: func(r *http.Request) bool {
return true
},
}
DefaultWSDialer = &websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
HandshakeTimeout: 45 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true, NextProtos: []string{"http/1.1"}}, //nolint: gosec //ok
}
)
type Direction int32
const (
Inbound Direction = iota
Outbound
)
func (d Direction) String() string {
switch d {
case Inbound:
return "Inbound"
case Outbound:
return "Outbound"
default:
return ""
}
}
type WSMessage struct {
direction Direction
Type int
Msg []byte
}
func (m *WSMessage) Direction() Direction {
return m.direction
}
func (p *Proxy) serveWS(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()
outreq := req.Clone(ctx)
if req.ContentLength == 0 {
outreq.Body = nil
}
if outreq.Body != nil {
// Reading from the request body after returning from a handler is not
// allowed, and the RoundTrip goroutine that reads the Body can outlive
// this handler. This can lead to a crash if the handler panics (see
// Issue 46866). Although calling Close doesn't guarantee there isn't
// any Read in flight after the handle returns, in practice it's safe to
// read after closing it.
defer outreq.Body.Close()
}
if outreq.Header == nil {
outreq.Header = make(http.Header) // Issue 33142: historical behavior was to always allocate
}
if req.URL.Scheme == "" {
outreq.URL.Host = outreq.Host
outreq.URL.Scheme = "wss"
}
if p.director != nil {
p.director(outreq)
}
outreq.Close = false
removeConnectionHeaders(outreq.Header)
// Remove hop-by-hop headers to the backend. Especially
// important is "Connection" because we want a persistent
// connection, regardless of what the client sent to us.
for _, h := range hopHeaders {
outreq.Header.Del(h)
}
// Remove duplicatet websocket header
outreq.Header.Del("Sec-Websocket-Version")
outreq.Header.Del("Sec-Websocket-Key")
backConn, res, err := p.wsDialer.DialContext(ctx, outreq.URL.String(), outreq.Header)
if err != nil {
log.Printf("Cannot dial to remote backend url %s", err)
if res != nil {
// If the WebSocket handshake fails, ErrBadHandshake is returned
// along with a non-nil *http.Response so that callers can handle
// redirects, authentication, etcetera.
if err = p.copyResponse(rw, res.Body, p.getFlushInterval(res)); err != nil {
log.Printf("Cannot write response after failed remote backend handshake: %s", err)
}
} else {
p.errorHandler(rw, req, err)
}
return
}
backConnCloseCh := make(chan bool)
go func() {
// Ensure that the cancellation of a request closes the backend.
select {
case <-req.Context().Done():
case <-backConnCloseCh:
}
backConn.Close()
}()
defer close(backConnCloseCh)
// Only pass those headers to the upgrader.
upgradeHeader := http.Header{}
if hdr := res.Header.Get("Sec-Websocket-Protocol"); hdr != "" {
upgradeHeader.Set("Sec-Websocket-Protocol", hdr)
}
if hdr := res.Header.Get("Set-Cookie"); hdr != "" {
upgradeHeader.Set("Set-Cookie", hdr)
}
// Now upgrade the existing incoming request to a WebSocket connection.
// Also pass the header that we gathered from the Dial handshake.
// If the upgrade fails, then Upgrade replies to the client with an HTTP error
// response.
conn, err := p.wsUpgrader.Upgrade(rw, req, upgradeHeader)
if err != nil {
p.logErrorf("Cannot upgrade %s", err)
return
}
defer conn.Close()
errClient := make(chan error, 1)
errBackend := make(chan error, 1)
replicator := &websocketReplicator{modifier: p.wsMessageMofifier}
go replicator.copy(backConn, conn, Outbound, errBackend)
go replicator.copy(conn, backConn, Inbound, errClient)
var message string
select {
case err = <-errClient:
message = "Error when copying from backend to client: %v"
case err = <-errBackend:
message = "Error when copying from client to backend: %v"
}
if e, ok := err.(*websocket.CloseError); !ok || e.Code == websocket.CloseAbnormalClosure {
p.logErrorf(message, err)
}
}
type websocketReplicator struct {
modifier WSMessageModifierFunc
}
func (r *websocketReplicator) copy(dst, src *websocket.Conn, direction Direction, errc chan error) {
src.SetPingHandler(func(data string) error {
return dst.WriteControl(websocket.PingMessage, []byte(data), time.Time{})
})
src.SetPongHandler(func(data string) error {
return dst.WriteControl(websocket.PongMessage, []byte(data), time.Time{})
})
for {
msgType, msg, rerr := src.ReadMessage()
if rerr != nil {
m := websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%v", rerr))
if e, ok := rerr.(*websocket.CloseError); ok {
// Following codes are not valid on the wire so just close the
// underlying TCP connection without sending a close frame.
if e.Code == websocket.CloseAbnormalClosure || e.Code == websocket.CloseTLSHandshake {
errc <- rerr
return
}
if e.Code != websocket.CloseNoStatusReceived {
m = websocket.FormatCloseMessage(e.Code, e.Text)
}
}
errc <- rerr
_ = dst.WriteMessage(websocket.CloseMessage, m)
return
}
wsMSg := &WSMessage{Type: msgType, Msg: msg, direction: direction}
if r.modifier != nil {
r.modifier(wsMSg)
}
if werr := dst.WriteMessage(wsMSg.Type, wsMSg.Msg); werr != nil {
errc <- werr
return
}
}
}