2017-08-01 17:32:44 +00:00
|
|
|
package integration
|
2017-07-25 14:56:46 +00:00
|
|
|
|
|
|
|
import (
|
2017-09-06 07:36:02 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2017-09-29 19:04:03 +00:00
|
|
|
"encoding/base64"
|
2017-08-01 13:24:08 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-07-25 14:56:46 +00:00
|
|
|
"os"
|
2024-01-09 16:00:07 +00:00
|
|
|
"testing"
|
2017-08-01 13:24:08 +00:00
|
|
|
"time"
|
2017-07-25 14:56:46 +00:00
|
|
|
|
2017-08-01 13:24:08 +00:00
|
|
|
gorillawebsocket "github.com/gorilla/websocket"
|
2024-01-09 16:00:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/integration/try"
|
2017-08-01 13:24:08 +00:00
|
|
|
"golang.org/x/net/websocket"
|
2017-07-25 14:56:46 +00:00
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// WebsocketSuite tests suite.
|
2017-07-25 14:56:46 +00:00
|
|
|
type WebsocketSuite struct{ BaseSuite }
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func TestWebsocketSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(WebsocketSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WebsocketSuite) TestBase() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{} // use default options
|
2017-07-25 14:56:46 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-07-25 14:56:46 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-07-25 14:56:46 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-07-25 14:56:46 +00:00
|
|
|
|
2017-08-01 13:24:08 +00:00
|
|
|
conn, _, err := gorillawebsocket.DefaultDialer.Dial("ws://127.0.0.1:8000/ws", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-07-25 14:56:46 +00:00
|
|
|
|
2017-08-18 00:18:02 +00:00
|
|
|
err = conn.WriteMessage(gorillawebsocket.TextMessage, []byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-07-25 14:56:46 +00:00
|
|
|
|
|
|
|
_, msg, err := conn.ReadMessage()
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-07-25 14:56:46 +00:00
|
|
|
}
|
2017-08-01 13:24:08 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestWrongOrigin() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{} // use default options
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-08-01 13:24:08 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
config, err := websocket.NewConfig("ws://127.0.0.1:8000/ws", "ws://127.0.0.1:800")
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout("tcp", "127.0.0.1:8000", time.Second)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
_, err = websocket.NewClient(config, conn)
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.ErrorContains(s.T(), err, "bad status")
|
2017-08-01 13:24:08 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestOrigin() {
|
2017-08-18 00:18:02 +00:00
|
|
|
// use default options
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{}
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-08-01 13:24:08 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
config, err := websocket.NewConfig("ws://127.0.0.1:8000/ws", "ws://127.0.0.1:8000")
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout("tcp", "127.0.0.1:8000", time.Second)
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
client, err := websocket.NewClient(config, conn)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
n, err := client.Write([]byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), 2, n)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
msg := make([]byte, 2)
|
|
|
|
n, err = client.Read(msg)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), 2, n)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-08-01 13:24:08 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestWrongOriginIgnoredByServer() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{CheckOrigin: func(r *http.Request) bool {
|
2017-08-01 13:24:08 +00:00
|
|
|
return true
|
|
|
|
}}
|
|
|
|
|
|
|
|
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()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-08-01 13:24:08 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
config, err := websocket.NewConfig("ws://127.0.0.1:8000/ws", "ws://127.0.0.1:80")
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout("tcp", "127.0.0.1:8000", time.Second)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
client, err := websocket.NewClient(config, conn)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
n, err := client.Write([]byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), 2, n)
|
2017-08-01 13:24:08 +00:00
|
|
|
|
|
|
|
msg := make([]byte, 2)
|
|
|
|
n, err = client.Read(msg)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), 2, n)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-08-01 13:24:08 +00:00
|
|
|
}
|
2017-09-06 07:36:02 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestSSLTermination() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{} // use default options
|
2017-09-06 07:36:02 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config_https.toml", struct {
|
2017-09-06 07:36:02 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-09-06 07:36:02 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-09-06 07:36:02 +00:00
|
|
|
|
2018-07-03 08:02:03 +00:00
|
|
|
// Add client self-signed cert
|
2017-09-06 07:36:02 +00:00
|
|
|
roots := x509.NewCertPool()
|
2021-03-04 19:08:03 +00:00
|
|
|
certContent, err := os.ReadFile("./resources/tls/local.cert")
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-09-06 07:36:02 +00:00
|
|
|
roots.AppendCertsFromPEM(certContent)
|
|
|
|
gorillawebsocket.DefaultDialer.TLSClientConfig = &tls.Config{
|
|
|
|
RootCAs: roots,
|
|
|
|
}
|
|
|
|
conn, _, err := gorillawebsocket.DefaultDialer.Dial("wss://127.0.0.1:8000/ws", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-09-06 07:36:02 +00:00
|
|
|
|
|
|
|
err = conn.WriteMessage(gorillawebsocket.TextMessage, []byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-09-06 07:36:02 +00:00
|
|
|
|
|
|
|
_, msg, err := conn.ReadMessage()
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-09-06 07:36:02 +00:00
|
|
|
}
|
2017-09-29 19:04:03 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestBasicAuth() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{} // use default options
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
user, password, _ := r.BasicAuth()
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.Equal(s.T(), "traefiker", user)
|
|
|
|
assert.Equal(s.T(), "secret", password)
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
mt, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = conn.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-09-29 19:04:03 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
config, err := websocket.NewConfig("ws://127.0.0.1:8000/ws", "ws://127.0.0.1:8000")
|
|
|
|
auth := "traefiker:secret"
|
|
|
|
config.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(auth)))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.NoError(s.T(), err)
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
conn, err := net.DialTimeout("tcp", "127.0.0.1:8000", time.Second)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-09-29 19:04:03 +00:00
|
|
|
client, err := websocket.NewClient(config, conn)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
n, err := client.Write([]byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), 2, n)
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
msg := make([]byte, 2)
|
|
|
|
n, err = client.Read(msg)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), 2, n)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-09-29 19:04:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestSpecificResponseFromBackend() {
|
2017-09-29 19:04:03 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2017-11-20 08:40:03 +00:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2017-09-29 19:04:03 +00:00
|
|
|
}))
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-09-29 19:04:03 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-09-29 19:04:03 +00:00
|
|
|
|
|
|
|
_, resp, err := gorillawebsocket.DefaultDialer.Dial("ws://127.0.0.1:8000/ws", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.Error(s.T(), err)
|
|
|
|
assert.Equal(s.T(), http.StatusUnauthorized, resp.StatusCode)
|
2017-09-29 19:04:03 +00:00
|
|
|
}
|
2017-10-02 13:36:02 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestURLWithURLEncodedChar() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{} // use default options
|
2017-10-02 13:36:02 +00:00
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.Equal(s.T(), "/ws/http%3A%2F%2Ftest", r.URL.EscapedPath())
|
2017-10-02 13:36:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-10-02 13:36:02 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-10-02 13:36:02 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-10-02 13:36:02 +00:00
|
|
|
|
|
|
|
conn, _, err := gorillawebsocket.DefaultDialer.Dial("ws://127.0.0.1:8000/ws/http%3A%2F%2Ftest", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-10-02 13:36:02 +00:00
|
|
|
|
|
|
|
err = conn.WriteMessage(gorillawebsocket.TextMessage, []byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-10-02 13:36:02 +00:00
|
|
|
|
|
|
|
_, msg, err := conn.ReadMessage()
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-10-02 13:36:02 +00:00
|
|
|
}
|
2017-10-20 15:38:04 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestSSLhttp2() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{} // use default options
|
2017-10-20 15:38:04 +00:00
|
|
|
|
|
|
|
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
ts.TLS = &tls.Config{}
|
2021-03-04 08:02:03 +00:00
|
|
|
ts.TLS.NextProtos = append(ts.TLS.NextProtos, `h2`, `http/1.1`)
|
2017-10-20 15:38:04 +00:00
|
|
|
ts.StartTLS()
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config_https.toml", struct {
|
2017-10-20 15:38:04 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: ts.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG", "--accesslog")
|
2017-10-20 15:38:04 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-10-20 15:38:04 +00:00
|
|
|
|
2018-07-03 08:02:03 +00:00
|
|
|
// Add client self-signed cert
|
2017-10-20 15:38:04 +00:00
|
|
|
roots := x509.NewCertPool()
|
2021-03-04 19:08:03 +00:00
|
|
|
certContent, err := os.ReadFile("./resources/tls/local.cert")
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-10-20 15:38:04 +00:00
|
|
|
roots.AppendCertsFromPEM(certContent)
|
|
|
|
gorillawebsocket.DefaultDialer.TLSClientConfig = &tls.Config{
|
|
|
|
RootCAs: roots,
|
|
|
|
}
|
|
|
|
conn, _, err := gorillawebsocket.DefaultDialer.Dial("wss://127.0.0.1:8000/echo", nil)
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-10-20 15:38:04 +00:00
|
|
|
|
|
|
|
err = conn.WriteMessage(gorillawebsocket.TextMessage, []byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-10-20 15:38:04 +00:00
|
|
|
|
|
|
|
_, msg, err := conn.ReadMessage()
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-10-20 15:38:04 +00:00
|
|
|
}
|
2017-11-09 09:04:03 +00:00
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
func (s *WebsocketSuite) TestHeaderAreForwarded() {
|
2020-07-07 12:42:03 +00:00
|
|
|
upgrader := gorillawebsocket.Upgrader{} // use default options
|
2017-11-09 09:04:03 +00:00
|
|
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-01-09 16:00:07 +00:00
|
|
|
assert.Equal(s.T(), "my-token", r.Header.Get("X-Token"))
|
2017-11-09 09:04:03 +00:00
|
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
for {
|
|
|
|
mt, message, err := c.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = c.WriteMessage(mt, message)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
file := s.adaptFile("fixtures/websocket/config.toml", struct {
|
2017-11-09 09:04:03 +00:00
|
|
|
WebsocketServer string
|
|
|
|
}{
|
|
|
|
WebsocketServer: srv.URL,
|
|
|
|
})
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
s.traefikCmd(withConfigFile(file), "--log.level=DEBUG")
|
2017-11-09 09:04:03 +00:00
|
|
|
|
|
|
|
// wait for traefik
|
2024-01-09 16:00:07 +00:00
|
|
|
err := try.GetRequest("http://127.0.0.1:8080/api/rawdata", 10*time.Second, try.BodyContains("127.0.0.1"))
|
|
|
|
require.NoError(s.T(), err)
|
2017-11-09 09:04:03 +00:00
|
|
|
|
|
|
|
headers := http.Header{}
|
|
|
|
headers.Add("X-Token", "my-token")
|
|
|
|
conn, _, err := gorillawebsocket.DefaultDialer.Dial("ws://127.0.0.1:8000/ws", headers)
|
|
|
|
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-11-09 09:04:03 +00:00
|
|
|
err = conn.WriteMessage(gorillawebsocket.TextMessage, []byte("OK"))
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
2017-11-09 09:04:03 +00:00
|
|
|
|
|
|
|
_, msg, err := conn.ReadMessage()
|
2024-01-09 16:00:07 +00:00
|
|
|
require.NoError(s.T(), err)
|
|
|
|
assert.Equal(s.T(), "OK", string(msg))
|
2017-11-09 09:04:03 +00:00
|
|
|
}
|