From d29848572267ced6ae31190283cf2ddb964f3db8 Mon Sep 17 00:00:00 2001 From: d1slike Date: Wed, 22 Mar 2023 19:48:31 +0300 Subject: [PATCH] MSP-1470 implement websocket proxy --- proxy/ws.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 proxy/ws.go diff --git a/proxy/ws.go b/proxy/ws.go new file mode 100644 index 0000000..c822a01 --- /dev/null +++ b/proxy/ws.go @@ -0,0 +1,71 @@ +package proxy + +import ( + "fmt" + "net/http" + "net/url" + "time" + + "github.com/gorilla/websocket" + "github.com/pkg/errors" + "github.com/tomakado/websocketproxy" + "isp-gate-service/httperrors" + "isp-gate-service/request" +) + +type Ws struct { + hostManager HttpHostManager + skipAuth bool +} + +func NewWs(hostManager HttpHostManager, skipAuth bool) Ws { + return Ws{ + hostManager: hostManager, + skipAuth: skipAuth, + } +} + +//nolint:gomnd +func (ws Ws) Handle(ctx *request.Context) error { + host, err := ws.hostManager.Next() + if err != nil { + return errors.WithMessage(err, "ws: next host") + } + + rawUrl := fmt.Sprintf("ws://%s", host) + target, err := url.Parse(rawUrl) + if err != nil { + return errors.WithMessage(err, "ws: parse url") + } + + request := ctx.Request() + request.URL.Path = ctx.Endpoint() + + var resultError error + proxy := websocketproxy.NewProxy(target) + proxy.Director = func(incoming *http.Request, out http.Header) { + _ = setHttpHeaders(ctx, out, ws.skipAuth) + } + proxy.Upgrader = &websocket.Upgrader{ + HandshakeTimeout: 5 * time.Second, + ReadBufferSize: 1024, + WriteBufferSize: 1024, + WriteBufferPool: nil, + Subprotocols: nil, + Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) { + resultError = httperrors.New( + http.StatusServiceUnavailable, + "upstream is not available", + errors.WithMessagef(reason, "ws proxy to %s", host), + ) + }, + CheckOrigin: func(r *http.Request) bool { + return true + }, + EnableCompression: false, + } + + proxy.ServeHTTP(ctx.ResponseWriter(), ctx.Request()) + + return resultError +}