-
Notifications
You must be signed in to change notification settings - Fork 492
/
wspool.go
325 lines (287 loc) · 9.72 KB
/
wspool.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
/*
* Copyright (c) 2017, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package httpproxy
import (
stdctx "context"
"fmt"
"net"
"net/http"
"net/url"
"sync"
"github.com/megaease/easegress/v2/pkg/context"
"github.com/megaease/easegress/v2/pkg/filters/proxies"
"github.com/megaease/easegress/v2/pkg/logger"
"github.com/megaease/easegress/v2/pkg/protocols/httpprot"
"github.com/megaease/easegress/v2/pkg/protocols/httpprot/httpstat"
"github.com/megaease/easegress/v2/pkg/util/fasttime"
"nhooyr.io/websocket"
)
// WebSocketServerPool defines a server pool.
type WebSocketServerPool struct {
BaseServerPool
filter RequestMatcher
proxy *WebSocketProxy
spec *WebSocketServerPoolSpec
httpStat *httpstat.HTTPStat
healthChecker proxies.HealthChecker
}
// WebSocketServerPoolSpec is the spec for a server pool.
type WebSocketServerPoolSpec struct {
BaseServerPoolSpec `json:",inline"`
ClientMaxMsgSize int64 `json:"clientMaxMsgSize,omitempty"`
ServerMaxMsgSize int64 `json:"serverMaxMsgSize,omitempty"`
Filter *RequestMatcherSpec `json:"filter,omitempty"`
InsecureSkipVerify bool `json:"insecureSkipVerify,omitempty"`
OriginPatterns []string `json:"originPatterns,omitempty"`
HealthCheck *WSProxyHealthCheckSpec `json:"healthCheck,omitempty"`
}
// NewWebSocketServerPool creates a new server pool according to spec.
func NewWebSocketServerPool(proxy *WebSocketProxy, spec *WebSocketServerPoolSpec, name string) *WebSocketServerPool {
sp := &WebSocketServerPool{
proxy: proxy,
spec: spec,
httpStat: httpstat.New(),
healthChecker: NewWebSocketHealthChecker(spec.HealthCheck),
}
if spec.Filter != nil {
sp.filter = NewRequestMatcher(spec.Filter)
}
sp.Init(sp, proxy.super, name, &spec.BaseServerPoolSpec)
return sp
}
// CreateLoadBalancer creates a load balancer according to spec.
func (sp *WebSocketServerPool) CreateLoadBalancer(spec *LoadBalanceSpec, servers []*Server) LoadBalancer {
lb := proxies.NewGeneralLoadBalancer(spec, servers)
lb.Init(proxies.NewHTTPSessionSticker, sp.healthChecker, nil)
return lb
}
func (sp *WebSocketServerPool) buildFailureResponse(ctx *context.Context, statusCode int) {
resp, _ := ctx.GetOutputResponse().(*httpprot.Response)
if resp == nil {
resp, _ = httpprot.NewResponse(nil)
}
resp.SetStatusCode(statusCode)
ctx.SetOutputResponse(resp)
}
// buildSuccessResponse builds a success response for WebSocket.
// The response is from WebSocket dial process.
// Here we leave the response body alone, and only set the status code and headers.
// The response body will be handled by the WebSocket protocol.
func (sp *WebSocketServerPool) buildSuccessResponse(ctx *context.Context, resp *http.Response) {
newResp, _ := ctx.GetOutputResponse().(*httpprot.Response)
if newResp == nil {
newResp, _ = httpprot.NewResponse(nil)
}
if resp != nil {
newResp.SetStatusCode(resp.StatusCode)
newResp.Std().Header = resp.Header.Clone()
}
ctx.SetOutputResponse(newResp)
}
func buildServerURL(svr *Server, req *httpprot.Request) (string, error) {
u := *req.URL()
u1, err := url.ParseRequestURI(svr.URL)
if err != nil {
return "", err
}
u.Host = u1.Host
switch u1.Scheme {
case "ws", "wss":
u.Scheme = u1.Scheme
case "http":
u.Scheme = "ws"
case "https":
u.Scheme = "wss"
default:
return "", fmt.Errorf("invalid scheme %s", u1.Scheme)
}
return u.String(), nil
}
func (sp *WebSocketServerPool) dialServer(svr *Server, req *httpprot.Request) (*websocket.Conn, *http.Response, error) {
u, err := buildServerURL(svr, req)
if err != nil {
return nil, nil, err
}
opts := &websocket.DialOptions{
HTTPHeader: req.HTTPHeader().Clone(),
CompressionMode: websocket.CompressionDisabled,
}
// only set host when server address is not host name OR
// server is explicitly told to keep the host of the request.
if !svr.AddrIsHostName || svr.KeepHost {
opts.Host = req.Host()
}
opts.HTTPHeader.Del("Sec-WebSocket-Origin")
opts.HTTPHeader.Del("Sec-WebSocket-Protocol")
opts.HTTPHeader.Del("Sec-WebSocket-Accept")
opts.HTTPHeader.Del("Sec-WebSocket-Extensions")
// According to https://docs.oracle.com/en-us/iaas/Content/Balance/Reference/httpheaders.htm
// For load balancer, we add following key-value pairs to headers
// X-Forwarded-For: <original_client>, <proxy1>, <proxy2>
// X-Forwarded-Host: www.example.com:8080
// X-Forwarded-Proto: https
const xForwardedFor = "X-Forwarded-For"
xff := req.HTTPHeader().Get(xForwardedFor)
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
if xff == "" {
opts.HTTPHeader.Set(xForwardedFor, clientIP)
} else {
opts.HTTPHeader.Set(xForwardedFor, fmt.Sprintf("%s, %s", xff, clientIP))
}
}
const xForwardedHost = "X-Forwarded-Host"
xfh := req.HTTPHeader().Get(xForwardedHost)
if xfh == "" && req.Host() != "" {
opts.HTTPHeader.Set(xForwardedHost, req.Host())
}
const xForwardedProto = "X-Forwarded-Proto"
opts.HTTPHeader.Set(xForwardedProto, "http")
if req.TLS != nil {
opts.HTTPHeader.Set(xForwardedProto, "https")
}
conn, resp, err := websocket.Dial(stdctx.Background(), u, opts)
if err == nil && (sp.spec.ServerMaxMsgSize > 0 || sp.spec.ServerMaxMsgSize == -1) {
conn.SetReadLimit(sp.spec.ServerMaxMsgSize)
}
return conn, resp, err
}
func (sp *WebSocketServerPool) handle(ctx *context.Context) (result string) {
req := ctx.GetInputRequest().(*httpprot.Request)
svr := sp.LoadBalancer().ChooseServer(req)
metric := &httpstat.Metric{}
startTime := fasttime.Now()
defer func() {
metric.Duration = fasttime.Since(startTime)
sp.httpStat.Stat(metric)
}()
// if there's no available server.
if svr == nil {
logger.Errorf("%s: no available server", sp.Name)
sp.buildFailureResponse(ctx, http.StatusServiceUnavailable)
metric.StatusCode = http.StatusServiceUnavailable
return resultInternalError
}
stdw, _ := ctx.GetData("HTTP_RESPONSE_WRITER").(http.ResponseWriter)
if stdw == nil {
logger.Errorf("%s: cannot get response writer from context", sp.Name)
sp.buildFailureResponse(ctx, http.StatusInternalServerError)
metric.StatusCode = http.StatusInternalServerError
return resultInternalError
}
opts := &websocket.AcceptOptions{
InsecureSkipVerify: sp.spec.InsecureSkipVerify,
OriginPatterns: sp.spec.OriginPatterns,
}
subProtocol := req.HTTPHeader().Get("Sec-WebSocket-Protocol")
if subProtocol != "" {
opts.Subprotocols = []string{subProtocol}
}
clntConn, err := websocket.Accept(stdw, req.Std(), opts)
if err != nil {
logger.Errorf("%s: failed to establish client connection: %v", sp.Name, err)
sp.buildFailureResponse(ctx, http.StatusBadRequest)
metric.StatusCode = http.StatusBadRequest
return resultClientError
}
if sp.spec.ClientMaxMsgSize > 0 || sp.spec.ClientMaxMsgSize == -1 {
clntConn.SetReadLimit(sp.spec.ClientMaxMsgSize)
}
svrConn, resp, err := sp.dialServer(svr, req)
if err != nil {
logger.Errorf("%s: dial to %s failed: %v", sp.Name, svr.URL, err)
clntConn.Close(websocket.StatusGoingAway, "")
sp.buildFailureResponse(ctx, http.StatusServiceUnavailable)
metric.StatusCode = http.StatusServiceUnavailable
return resultServerError
}
var wg sync.WaitGroup
wg.Add(2)
stop := make(chan struct{})
// copy messages from client to server
go func() {
defer wg.Done()
for {
t, m, err := clntConn.Read(stdctx.Background())
if err != nil {
if cs := websocket.CloseStatus(err); cs == websocket.StatusNormalClosure {
svrConn.Close(websocket.StatusNormalClosure, "")
} else {
svrConn.Close(cs, err.Error())
logger.Errorf("%s: failed to read from client: %v", sp.Name, err)
}
break
}
err = svrConn.Write(stdctx.Background(), t, m)
if err != nil {
if cs := websocket.CloseStatus(err); cs == websocket.StatusNormalClosure {
clntConn.Close(websocket.StatusNormalClosure, "")
} else {
clntConn.Close(cs, err.Error())
logger.Errorf("%s: failed to write to server: %v", sp.Name, err)
}
break
}
metric.ReqSize += uint64(len(m))
}
}()
// copy messages from server to client
go func() {
defer wg.Done()
for {
t, m, err := svrConn.Read(stdctx.Background())
if err != nil {
if cs := websocket.CloseStatus(err); cs == websocket.StatusNormalClosure {
clntConn.Close(websocket.StatusNormalClosure, "")
} else {
clntConn.Close(cs, err.Error())
logger.Errorf("%s: failed to read from server: %v", sp.Name, err)
}
break
}
err = clntConn.Write(stdctx.Background(), t, m)
if err != nil {
if cs := websocket.CloseStatus(err); cs == websocket.StatusNormalClosure {
svrConn.Close(websocket.StatusNormalClosure, "")
} else {
svrConn.Close(cs, err.Error())
logger.Errorf("%s: failed to write to client: %v", sp.Name, err)
}
break
}
metric.RespSize += uint64(len(m))
}
}()
go func() {
select {
case <-stop:
case <-sp.Done():
}
svrConn.Close(websocket.StatusBadGateway, "")
clntConn.Close(websocket.StatusBadGateway, "")
}()
wg.Wait()
close(stop)
sp.buildSuccessResponse(ctx, resp)
metric.StatusCode = http.StatusSwitchingProtocols
ctx.SetData("HTTP_METRIC", metric)
return
}
func (sp *WebSocketServerPool) status() *ServerPoolStatus {
return &ServerPoolStatus{
Stat: sp.httpStat.Status(),
}
}