Skip to content

Commit 15a8055

Browse files
joy4egTwiN
andauthored
fix(client): Switch websocket library (#1423)
* fix(websocket): switch to gorilla/websocket * fix(client): add missing t.Parallel() in tests --------- Co-authored-by: TwiN <[email protected]>
1 parent 1318423 commit 15a8055

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

client/client.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import (
2121
"github.com/TwiN/gocache/v2"
2222
"github.com/TwiN/logr"
2323
"github.com/TwiN/whois"
24+
"github.com/gorilla/websocket"
2425
"github.com/ishidawataru/sctp"
2526
"github.com/miekg/dns"
2627
ping "github.com/prometheus-community/pro-bing"
2728
"github.com/registrobr/rdap"
2829
"github.com/registrobr/rdap/protocol"
2930
"golang.org/x/crypto/ssh"
30-
"golang.org/x/net/websocket"
3131
)
3232

3333
const (
@@ -394,48 +394,53 @@ func ShouldRunPingerAsPrivileged() bool {
394394
// QueryWebSocket opens a websocket connection, write `body` and return a message from the server
395395
func QueryWebSocket(address, body string, headers map[string]string, config *Config) (bool, []byte, error) {
396396
const (
397-
Origin = "http://localhost/"
398-
MaximumMessageSize = 1024 // in bytes
397+
Origin = "http://localhost/"
399398
)
400-
wsConfig, err := websocket.NewConfig(address, Origin)
401-
if err != nil {
402-
return false, nil, fmt.Errorf("error configuring websocket connection: %w", err)
403-
}
404-
if headers != nil {
405-
if wsConfig.Header == nil {
406-
wsConfig.Header = make(http.Header)
407-
}
408-
for name, value := range headers {
409-
wsConfig.Header.Set(name, value)
399+
var (
400+
dialer = websocket.Dialer{
401+
EnableCompression: true,
410402
}
403+
wsHeaders = make(http.Header)
404+
)
405+
406+
wsHeaders.Set("Origin", Origin)
407+
for name, value := range headers {
408+
wsHeaders.Set(name, value)
411409
}
410+
411+
ctx := context.Background()
412412
if config != nil {
413-
wsConfig.Dialer = &net.Dialer{Timeout: config.Timeout}
414-
wsConfig.TlsConfig = &tls.Config{
413+
if config.Timeout > 0 {
414+
var cancel context.CancelFunc
415+
ctx, cancel = context.WithTimeout(ctx, config.Timeout)
416+
defer cancel()
417+
}
418+
dialer.TLSClientConfig = &tls.Config{
415419
InsecureSkipVerify: config.Insecure,
416420
}
417421
if config.HasTLSConfig() && config.TLS.isValid() == nil {
418-
wsConfig.TlsConfig = configureTLS(wsConfig.TlsConfig, *config.TLS)
422+
dialer.TLSClientConfig = configureTLS(dialer.TLSClientConfig, *config.TLS)
419423
}
420424
}
421425
// Dial URL
422-
ws, err := websocket.DialConfig(wsConfig)
426+
ws, _, err := dialer.DialContext(ctx, address, wsHeaders)
423427
if err != nil {
424428
return false, nil, fmt.Errorf("error dialing websocket: %w", err)
425429
}
426430
defer ws.Close()
427431
body = parseLocalAddressPlaceholder(body, ws.LocalAddr())
428432
// Write message
429-
if _, err := ws.Write([]byte(body)); err != nil {
433+
if err := ws.WriteMessage(websocket.TextMessage, []byte(body)); err != nil {
430434
return false, nil, fmt.Errorf("error writing websocket body: %w", err)
431435
}
432436
// Read message
433-
var n int
434-
msg := make([]byte, MaximumMessageSize)
435-
if n, err = ws.Read(msg); err != nil {
437+
msgType, msg, err := ws.ReadMessage()
438+
if err != nil {
436439
return false, nil, fmt.Errorf("error reading websocket message: %w", err)
440+
} else if msgType != websocket.TextMessage && msgType != websocket.BinaryMessage {
441+
return false, nil, fmt.Errorf("unexpected websocket message type: %d, expected %d or %d", msgType, websocket.TextMessage, websocket.BinaryMessage)
437442
}
438-
return true, msg[:n], nil
443+
return true, msg, nil
439444
}
440445

441446
func QueryDNS(queryType, queryName, url string) (connected bool, dnsRcode string, body []byte, err error) {

client/client_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
func TestGetHTTPClient(t *testing.T) {
20+
t.Parallel()
2021
cfg := &Config{
2122
Insecure: false,
2223
IgnoreRedirect: false,
@@ -42,6 +43,7 @@ func TestGetHTTPClient(t *testing.T) {
4243
}
4344

4445
func TestRdapQuery(t *testing.T) {
46+
t.Parallel()
4547
if _, err := rdapQuery("1.1.1.1"); err == nil {
4648
t.Error("expected an error due to the invalid domain type")
4749
}
@@ -288,6 +290,7 @@ func TestCanPerformTLS(t *testing.T) {
288290
}
289291

290292
func TestCanCreateConnection(t *testing.T) {
293+
t.Parallel()
291294
connected, _ := CanCreateNetworkConnection("tcp", "127.0.0.1", "", &Config{Timeout: 5 * time.Second})
292295
if connected {
293296
t.Error("should've failed, because there's no port in the address")
@@ -302,6 +305,7 @@ func TestCanCreateConnection(t *testing.T) {
302305
// performs a Client Credentials OAuth2 flow and adds the obtained token as a `Authorization`
303306
// header to all outgoing HTTP calls.
304307
func TestHttpClientProvidesOAuth2BearerToken(t *testing.T) {
308+
t.Parallel()
305309
defer InjectHTTPClient(nil)
306310
oAuth2Config := &OAuth2Config{
307311
ClientID: "00000000-0000-0000-0000-000000000000",
@@ -357,6 +361,7 @@ func TestHttpClientProvidesOAuth2BearerToken(t *testing.T) {
357361
}
358362

359363
func TestQueryWebSocket(t *testing.T) {
364+
t.Parallel()
360365
_, _, err := QueryWebSocket("", "body", nil, &Config{Timeout: 2 * time.Second})
361366
if err == nil {
362367
t.Error("expected an error due to the address being invalid")
@@ -368,6 +373,7 @@ func TestQueryWebSocket(t *testing.T) {
368373
}
369374

370375
func TestTlsRenegotiation(t *testing.T) {
376+
t.Parallel()
371377
scenarios := []struct {
372378
name string
373379
cfg TLSConfig
@@ -411,6 +417,7 @@ func TestTlsRenegotiation(t *testing.T) {
411417
}
412418

413419
func TestQueryDNS(t *testing.T) {
420+
t.Parallel()
414421
scenarios := []struct {
415422
name string
416423
inputDNS dns.Config
@@ -540,6 +547,7 @@ func TestQueryDNS(t *testing.T) {
540547
}
541548

542549
func TestCheckSSHBanner(t *testing.T) {
550+
t.Parallel()
543551
cfg := &Config{Timeout: 3}
544552
t.Run("no-auth-ssh", func(t *testing.T) {
545553
connected, status, err := CheckSSHBanner("tty.sdf.org", cfg)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/gofiber/fiber/v2 v2.52.9
2121
github.com/google/go-github/v48 v48.2.0
2222
github.com/google/uuid v1.6.0
23+
github.com/gorilla/websocket v1.5.3
2324
github.com/ishidawataru/sctp v0.0.0-20230406120618-7ff4192f6ff2
2425
github.com/lib/pq v1.10.9
2526
github.com/miekg/dns v1.1.68
@@ -29,7 +30,6 @@ require (
2930
github.com/valyala/fasthttp v1.67.0
3031
github.com/wcharczuk/go-chart/v2 v2.1.2
3132
golang.org/x/crypto v0.45.0
32-
golang.org/x/net v0.47.0
3333
golang.org/x/oauth2 v0.32.0
3434
golang.org/x/sync v0.18.0
3535
google.golang.org/api v0.252.0
@@ -93,6 +93,7 @@ require (
9393
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
9494
golang.org/x/image v0.18.0 // indirect
9595
golang.org/x/mod v0.29.0 // indirect
96+
golang.org/x/net v0.47.0 // indirect
9697
golang.org/x/sys v0.38.0 // indirect
9798
golang.org/x/text v0.31.0 // indirect
9899
golang.org/x/tools v0.38.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU
101101
github.com/googleapis/enterprise-certificate-proxy v0.3.6/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA=
102102
github.com/googleapis/gax-go/v2 v2.15.0 h1:SyjDc1mGgZU5LncH8gimWo9lW1DtIfPibOG81vgd/bo=
103103
github.com/googleapis/gax-go/v2 v2.15.0/go.mod h1:zVVkkxAQHa1RQpg9z2AUCMnKhi0Qld9rcmyfL1OZhoc=
104+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
105+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
104106
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=
105107
github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
106108
github.com/ishidawataru/sctp v0.0.0-20230406120618-7ff4192f6ff2 h1:i2fYnDurfLlJH8AyyMOnkLHnHeP8Ff/DDpuZA/D3bPo=

0 commit comments

Comments
 (0)