Add compression and better error handling

This commit is contained in:
SALLEYRON Julien 2018-01-12 17:52:03 +01:00 committed by Traefiker
parent 8239e04a19
commit b5ee5c34f2
3 changed files with 35 additions and 11 deletions

2
Gopkg.lock generated
View file

@ -1026,7 +1026,7 @@
"roundrobin",
"utils"
]
revision = "812cebb8c764f2a78cb806267648b8728b4599ad"
revision = "fd0f370c961f6aa304379f4106e76ffe5ed7e97a"
source = "https://github.com/containous/oxy.git"
[[projects]]

View file

@ -5,6 +5,7 @@ package forward
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
@ -281,6 +282,9 @@ func (f *httpForwarder) serveWebSocket(w http.ResponseWriter, req *http.Request,
outReq := f.copyWebSocketRequest(req)
dialer := websocket.DefaultDialer
dialer.EnableCompression = strings.Contains(req.Header.Get("Sec-Websocket-Extensions"), "permessage-deflate")
if outReq.URL.Scheme == "wss" && f.tlsClientConfig != nil {
dialer.TLSClientConfig = f.tlsClientConfig.Clone()
// WebSocket is only in http/1.1
@ -322,7 +326,10 @@ func (f *httpForwarder) serveWebSocket(w http.ResponseWriter, req *http.Request,
return true
}}
upgrader.EnableCompression = strings.Contains(resp.Header.Get("Sec-Websocket-Extensions"), "permessage-deflate")
utils.RemoveHeaders(resp.Header, WebsocketUpgradeHeaders...)
underlyingConn, err := upgrader.Upgrade(w, req, resp.Header)
if err != nil {
log.Errorf("vulcand/oxy/forward/websocket: Error while upgrading connection : %v", err)
@ -331,29 +338,45 @@ func (f *httpForwarder) serveWebSocket(w http.ResponseWriter, req *http.Request,
defer underlyingConn.Close()
defer targetConn.Close()
errc := make(chan error, 2)
replicateWebsocketConn := func(dst, src *websocket.Conn, dstName, srcName string) {
var err error
errClient := make(chan error)
errBackend := make(chan error)
replicateWebsocketConn := func(dst, src *websocket.Conn, errc chan error) {
for {
msgType, msg, err := src.ReadMessage()
if err != nil {
f.log.Errorf("vulcand/oxy/forward/websocket: Error when copying from %s to %s using ReadMessage: %v", srcName, dstName, err)
m := websocket.FormatCloseMessage(websocket.CloseNormalClosure, fmt.Sprintf("%v", err))
if e, ok := err.(*websocket.CloseError); ok {
if e.Code != websocket.CloseNoStatusReceived {
m = websocket.FormatCloseMessage(e.Code, e.Text)
}
}
errc <- err
dst.WriteMessage(websocket.CloseMessage, m)
break
}
err = dst.WriteMessage(msgType, msg)
if err != nil {
f.log.Errorf("vulcand/oxy/forward/websocket: Error when copying from %s to %s using WriteMessage: %v", srcName, dstName, err)
errc <- err
break
}
}
errc <- err
}
go replicateWebsocketConn(underlyingConn, targetConn, "client", "backend")
go replicateWebsocketConn(targetConn, underlyingConn, "backend", "client")
go replicateWebsocketConn(underlyingConn, targetConn, errClient)
go replicateWebsocketConn(targetConn, underlyingConn, errBackend)
<-errc
var message string
select {
case err = <-errClient:
message = "vulcand/oxy/forward/websocket: Error when copying from backend to client: %v"
case err = <-errBackend:
message = "vulcand/oxy/forward/websocket: Error when copying from client to backend: %v"
}
if e, ok := err.(*websocket.CloseError); !ok || e.Code == websocket.CloseAbnormalClosure {
f.log.Errorf(message, err)
}
}
// copyWebsocketRequest makes a copy of the specified request.

View file

@ -49,6 +49,7 @@ var WebsocketUpgradeHeaders = []string{
Upgrade,
Connection,
SecWebsocketAccept,
SecWebsocketExtensions,
}
var XHeaders = []string{