2024-09-26 09:00:05 +00:00
|
|
|
package httputil
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/tls"
|
2020-11-06 08:26:03 +00:00
|
|
|
"errors"
|
2019-03-14 08:30:04 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gorillawebsocket "github.com/gorilla/websocket"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2024-09-26 09:00:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/testhelpers"
|
2019-03-14 08:30:04 +00:00
|
|
|
"golang.org/x/net/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWebSocketTCPClose(t *testing.T) {
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
upgrader := gorillawebsocket.Upgrader{}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
2024-09-26 09:00:05 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
for {
|
|
|
|
_, _, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
errChan <- err
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
proxyAddr := proxy.Listener.Addr().String()
|
|
|
|
_, conn, err := newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
).open()
|
|
|
|
require.NoError(t, err)
|
2020-11-06 08:26:03 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
conn.Close()
|
|
|
|
|
|
|
|
serverErr := <-errChan
|
|
|
|
|
2020-11-06 08:26:03 +00:00
|
|
|
var wsErr *gorillawebsocket.CloseError
|
2023-11-17 00:50:06 +00:00
|
|
|
require.ErrorAs(t, serverErr, &wsErr)
|
2019-03-14 08:30:04 +00:00
|
|
|
assert.Equal(t, 1006, wsErr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketPingPong(t *testing.T) {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{
|
2019-03-14 08:30:04 +00:00
|
|
|
HandshakeTimeout: 10 * time.Second,
|
|
|
|
CheckOrigin: func(*http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/ws", func(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
ws, err := upgrader.Upgrade(writer, request, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ws.SetPingHandler(func(appData string) error {
|
|
|
|
err = ws.WriteMessage(gorillawebsocket.PongMessage, []byte(appData+"Pong"))
|
|
|
|
require.NoError(t, err)
|
2024-09-26 09:00:05 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
_, _, _ = ws.ReadMessage()
|
|
|
|
})
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
srv := httptest.NewServer(mux)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer srv.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
serverAddr := proxy.Listener.Addr().String()
|
|
|
|
|
|
|
|
headers := http.Header{}
|
|
|
|
webSocketURL := "ws://" + serverAddr + "/ws"
|
|
|
|
headers.Add("Origin", webSocketURL)
|
|
|
|
|
|
|
|
conn, resp, err := gorillawebsocket.DefaultDialer.Dial(webSocketURL, headers)
|
|
|
|
require.NoError(t, err, "Error during Dial with response: %+v", resp)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
goodErr := fmt.Errorf("signal: %s", "Good data")
|
|
|
|
badErr := fmt.Errorf("signal: %s", "Bad data")
|
|
|
|
conn.SetPongHandler(func(data string) error {
|
|
|
|
if data == "PingPong" {
|
|
|
|
return goodErr
|
|
|
|
}
|
2024-09-26 09:00:05 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
return badErr
|
|
|
|
})
|
|
|
|
|
|
|
|
err = conn.WriteControl(gorillawebsocket.PingMessage, []byte("Ping"), time.Now().Add(time.Second))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, _, err = conn.ReadMessage()
|
2020-11-06 08:26:03 +00:00
|
|
|
if !errors.Is(err, goodErr) {
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketEcho(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/ws", websocket.Handler(func(conn *websocket.Conn) {
|
|
|
|
msg := make([]byte, 4)
|
2024-09-26 09:00:05 +00:00
|
|
|
n, err := conn.Read(msg)
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
_, err = conn.Write(msg[:n])
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = conn.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
srv := httptest.NewServer(mux)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer srv.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
serverAddr := proxy.Listener.Addr().String()
|
|
|
|
|
|
|
|
headers := http.Header{}
|
|
|
|
webSocketURL := "ws://" + serverAddr + "/ws"
|
|
|
|
headers.Add("Origin", webSocketURL)
|
|
|
|
|
|
|
|
conn, resp, err := gorillawebsocket.DefaultDialer.Dial(webSocketURL, headers)
|
|
|
|
require.NoError(t, err, "Error during Dial with response: %+v", resp)
|
|
|
|
|
|
|
|
err = conn.WriteMessage(gorillawebsocket.TextMessage, []byte("OK"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
_, msg, err := conn.ReadMessage()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "OK", string(msg))
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
err = conn.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketPassHost(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
passHost bool
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "PassHost false",
|
|
|
|
passHost: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "PassHost true",
|
|
|
|
passHost: true,
|
|
|
|
expected: "example.com",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/ws", websocket.Handler(func(conn *websocket.Conn) {
|
|
|
|
req := conn.Request()
|
|
|
|
|
|
|
|
if test.passHost {
|
|
|
|
require.Equal(t, test.expected, req.Host)
|
|
|
|
} else {
|
|
|
|
require.NotEqual(t, test.expected, req.Host)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := make([]byte, 4)
|
2024-09-26 09:00:05 +00:00
|
|
|
n, err := conn.Read(msg)
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
_, err = conn.Write(msg[:n])
|
2019-03-14 08:30:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = conn.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
srv := httptest.NewServer(mux)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer srv.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
serverAddr := proxy.Listener.Addr().String()
|
|
|
|
|
|
|
|
headers := http.Header{}
|
|
|
|
webSocketURL := "ws://" + serverAddr + "/ws"
|
|
|
|
headers.Add("Origin", webSocketURL)
|
|
|
|
headers.Add("Host", "example.com")
|
|
|
|
|
|
|
|
conn, resp, err := gorillawebsocket.DefaultDialer.Dial(webSocketURL, headers)
|
|
|
|
require.NoError(t, err, "Error during Dial with response: %+v", resp)
|
|
|
|
|
|
|
|
err = conn.WriteMessage(gorillawebsocket.TextMessage, []byte("OK"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
_, msg, err := conn.ReadMessage()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "OK", string(msg))
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
err = conn.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketServerWithoutCheckOrigin(t *testing.T) {
|
2024-09-26 09:00:05 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{CheckOrigin: func(*http.Request) bool { return true }}
|
|
|
|
srv := createServer(t, upgrader, func(*http.Request) {})
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
proxyAddr := proxy.Listener.Addr().String()
|
|
|
|
resp, err := newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
withData("ok"),
|
|
|
|
withOrigin("http://127.0.0.2"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "ok", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketRequestWithOrigin(t *testing.T) {
|
2024-09-26 09:00:05 +00:00
|
|
|
srv := createServer(t, gorillawebsocket.Upgrader{}, func(*http.Request) {})
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
proxyAddr := proxy.Listener.Addr().String()
|
2022-11-16 10:38:07 +00:00
|
|
|
_, err := newWebsocketRequest(
|
2019-03-14 08:30:04 +00:00
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
withData("echo"),
|
|
|
|
withOrigin("http://127.0.0.2"),
|
|
|
|
).send()
|
|
|
|
require.EqualError(t, err, "bad status")
|
|
|
|
|
|
|
|
resp, err := newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
withData("ok"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "ok", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketRequestWithQueryParams(t *testing.T) {
|
2024-09-26 09:00:05 +00:00
|
|
|
srv := createServer(t, gorillawebsocket.Upgrader{}, func(r *http.Request) {
|
2019-03-14 08:30:04 +00:00
|
|
|
assert.Equal(t, "test", r.URL.Query().Get("query"))
|
2024-09-26 09:00:05 +00:00
|
|
|
})
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
proxyAddr := proxy.Listener.Addr().String()
|
|
|
|
|
|
|
|
resp, err := newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws?query=test"),
|
|
|
|
withData("ok"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "ok", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketRequestWithHeadersInResponseWriter(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/ws", websocket.Handler(func(conn *websocket.Conn) {
|
|
|
|
conn.Close()
|
|
|
|
}))
|
2022-11-16 10:38:07 +00:00
|
|
|
srv := httptest.NewServer(mux)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer srv.Close()
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
transportManager := &transportManagerMock{
|
|
|
|
roundTrippers: map[string]http.RoundTripper{
|
|
|
|
"default@internal": &http.Transport{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewProxyBuilder(transportManager, nil).Build("default@internal", testhelpers.MustParseURL(srv.URL), false, true, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2024-09-26 09:00:05 +00:00
|
|
|
req.URL = testhelpers.MustParseURL(srv.URL)
|
2019-03-14 08:30:04 +00:00
|
|
|
w.Header().Set("HEADER-KEY", "HEADER-VALUE")
|
2024-09-26 09:00:05 +00:00
|
|
|
p.ServeHTTP(w, req)
|
2019-03-14 08:30:04 +00:00
|
|
|
}))
|
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
serverAddr := proxy.Listener.Addr().String()
|
|
|
|
|
|
|
|
headers := http.Header{}
|
|
|
|
webSocketURL := "ws://" + serverAddr + "/ws"
|
|
|
|
headers.Add("Origin", webSocketURL)
|
2022-11-16 10:38:07 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
conn, resp, err := gorillawebsocket.DefaultDialer.Dial(webSocketURL, headers)
|
|
|
|
require.NoError(t, err, "Error during Dial with response: %+v", err, resp)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
assert.Equal(t, "HEADER-VALUE", resp.Header.Get("HEADER-KEY"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketRequestWithEncodedChar(t *testing.T) {
|
2024-09-26 09:00:05 +00:00
|
|
|
srv := createServer(t, gorillawebsocket.Upgrader{}, func(r *http.Request) {
|
2019-03-14 08:30:04 +00:00
|
|
|
assert.Equal(t, "/%3A%2F%2F", r.URL.EscapedPath())
|
2024-09-26 09:00:05 +00:00
|
|
|
})
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
proxyAddr := proxy.Listener.Addr().String()
|
|
|
|
|
|
|
|
resp, err := newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/%3A%2F%2F"),
|
|
|
|
withData("ok"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "ok", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketUpgradeFailed(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/ws", func(w http.ResponseWriter, req *http.Request) {
|
2022-08-09 15:36:08 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2019-03-14 08:30:04 +00:00
|
|
|
})
|
2022-11-16 10:38:07 +00:00
|
|
|
srv := httptest.NewServer(mux)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer srv.Close()
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
transportManager := &transportManagerMock{
|
|
|
|
roundTrippers: map[string]http.RoundTripper{
|
|
|
|
"default@internal": &http.Transport{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewProxyBuilder(transportManager, nil).Build("default@internal", testhelpers.MustParseURL(srv.URL), false, true, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
path := req.URL.Path // keep the original path
|
|
|
|
|
|
|
|
if path == "/ws" {
|
|
|
|
// Set new backend URL
|
2024-09-26 09:00:05 +00:00
|
|
|
req.URL = testhelpers.MustParseURL(srv.URL)
|
2019-03-14 08:30:04 +00:00
|
|
|
req.URL.Path = path
|
2024-09-26 09:00:05 +00:00
|
|
|
p.ServeHTTP(w, req)
|
2019-03-14 08:30:04 +00:00
|
|
|
} else {
|
2022-08-09 15:36:08 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
proxyAddr := proxy.Listener.Addr().String()
|
|
|
|
conn, err := net.DialTimeout("tcp", proxyAddr, dialTimeout)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "ws://127.0.0.1/ws", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req.Header.Add("upgrade", "websocket")
|
|
|
|
req.Header.Add("Connection", "upgrade")
|
|
|
|
|
|
|
|
err = req.Write(conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// First request works with 400
|
|
|
|
br := bufio.NewReader(conn)
|
|
|
|
resp, err := http.ReadResponse(br, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 400, resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestForwardsWebsocketTraffic(t *testing.T) {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/ws", websocket.Handler(func(conn *websocket.Conn) {
|
|
|
|
_, err := conn.Write([]byte("ok"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = conn.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}))
|
2022-11-16 10:38:07 +00:00
|
|
|
srv := httptest.NewServer(mux)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer srv.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxy := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxy.Close()
|
|
|
|
|
|
|
|
proxyAddr := proxy.Listener.Addr().String()
|
|
|
|
resp, err := newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
withData("echo"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "ok", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTLSWebsocketServer() *httptest.Server {
|
|
|
|
upgrader := gorillawebsocket.Upgrader{}
|
|
|
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
for {
|
|
|
|
mt, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = conn.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebSocketTransferTLSConfig(t *testing.T) {
|
|
|
|
srv := createTLSWebsocketServer()
|
|
|
|
defer srv.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxyWithoutTLSConfig := createProxyWithForwarder(t, srv.URL, http.DefaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxyWithoutTLSConfig.Close()
|
|
|
|
|
|
|
|
proxyAddr := proxyWithoutTLSConfig.Listener.Addr().String()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
_, err := newWebsocketRequest(
|
2019-03-14 08:30:04 +00:00
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
withData("ok"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.EqualError(t, err, "bad status")
|
|
|
|
|
|
|
|
transport := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxyWithTLSConfig := createProxyWithForwarder(t, srv.URL, transport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxyWithTLSConfig.Close()
|
|
|
|
|
|
|
|
proxyAddr = proxyWithTLSConfig.Listener.Addr().String()
|
|
|
|
|
|
|
|
resp, err := newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
withData("ok"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "ok", resp)
|
|
|
|
|
2022-10-24 08:52:04 +00:00
|
|
|
// Don't alter default transport to prevent side effects on other tests.
|
|
|
|
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
|
|
|
|
defaultTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
proxyWithTLSConfigFromDefaultTransport := createProxyWithForwarder(t, srv.URL, defaultTransport)
|
2019-03-14 08:30:04 +00:00
|
|
|
defer proxyWithTLSConfig.Close()
|
|
|
|
|
|
|
|
proxyAddr = proxyWithTLSConfigFromDefaultTransport.Listener.Addr().String()
|
|
|
|
|
|
|
|
resp, err = newWebsocketRequest(
|
|
|
|
withServer(proxyAddr),
|
|
|
|
withPath("/ws"),
|
|
|
|
withData("ok"),
|
|
|
|
).send()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "ok", resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
const dialTimeout = time.Second
|
|
|
|
|
|
|
|
type websocketRequestOpt func(w *websocketRequest)
|
|
|
|
|
|
|
|
func withServer(server string) websocketRequestOpt {
|
|
|
|
return func(w *websocketRequest) {
|
|
|
|
w.ServerAddr = server
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withPath(path string) websocketRequestOpt {
|
|
|
|
return func(w *websocketRequest) {
|
|
|
|
w.Path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withData(data string) websocketRequestOpt {
|
|
|
|
return func(w *websocketRequest) {
|
|
|
|
w.Data = data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func withOrigin(origin string) websocketRequestOpt {
|
|
|
|
return func(w *websocketRequest) {
|
|
|
|
w.Origin = origin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWebsocketRequest(opts ...websocketRequestOpt) *websocketRequest {
|
|
|
|
wsrequest := &websocketRequest{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(wsrequest)
|
|
|
|
}
|
|
|
|
if wsrequest.Origin == "" {
|
|
|
|
wsrequest.Origin = "http://" + wsrequest.ServerAddr
|
|
|
|
}
|
|
|
|
if wsrequest.Config == nil {
|
|
|
|
wsrequest.Config, _ = websocket.NewConfig(fmt.Sprintf("ws://%s%s", wsrequest.ServerAddr, wsrequest.Path), wsrequest.Origin)
|
|
|
|
}
|
|
|
|
return wsrequest
|
|
|
|
}
|
|
|
|
|
|
|
|
type websocketRequest struct {
|
|
|
|
ServerAddr string
|
|
|
|
Path string
|
|
|
|
Data string
|
|
|
|
Origin string
|
|
|
|
Config *websocket.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *websocketRequest) send() (string, error) {
|
|
|
|
conn, _, err := w.open()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
if _, err := conn.Write([]byte(w.Data)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-07-07 12:42:03 +00:00
|
|
|
msg := make([]byte, 512)
|
2019-03-14 08:30:04 +00:00
|
|
|
var n int
|
|
|
|
n, err = conn.Read(msg)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
received := string(msg[:n])
|
|
|
|
return received, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *websocketRequest) open() (*websocket.Conn, net.Conn, error) {
|
|
|
|
client, err := net.DialTimeout("tcp", w.ServerAddr, dialTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
conn, err := websocket.NewClient(w.Config, client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return conn, client, err
|
|
|
|
}
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
func createProxyWithForwarder(t *testing.T, uri string, transport http.RoundTripper) *httptest.Server {
|
2020-12-29 09:54:03 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
u := testhelpers.MustParseURL(uri)
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
transportManager := &transportManagerMock{
|
|
|
|
roundTrippers: map[string]http.RoundTripper{"fwd": transport},
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := NewProxyBuilder(transportManager, nil).Build("fwd", u, false, true, 0)
|
|
|
|
require.NoError(t, err)
|
2020-12-29 09:54:03 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2024-09-26 09:00:05 +00:00
|
|
|
// keep the original path
|
|
|
|
path := req.URL.Path
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
// Set new backend URL
|
2022-11-16 10:38:07 +00:00
|
|
|
req.URL = u
|
2019-03-14 08:30:04 +00:00
|
|
|
req.URL.Path = path
|
|
|
|
|
2024-09-26 09:00:05 +00:00
|
|
|
p.ServeHTTP(w, req)
|
|
|
|
}))
|
|
|
|
t.Cleanup(srv.Close)
|
|
|
|
|
|
|
|
return srv
|
|
|
|
}
|
|
|
|
|
|
|
|
func createServer(t *testing.T, upgrader gorillawebsocket.Upgrader, check func(*http.Request)) *httptest.Server {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Error during upgrade: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
check(r)
|
|
|
|
for {
|
|
|
|
mt, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Error during read: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
err = conn.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("Error during write: %v", err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
}))
|
2022-11-16 10:38:07 +00:00
|
|
|
t.Cleanup(srv.Close)
|
2024-09-26 09:00:05 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
return srv
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|