mirror of
https://github.com/netbirdio/netbird.git
synced 2026-05-05 16:46:39 +00:00
Compare commits
2 Commits
fix/debug-
...
fix/relay-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abccbdee9e | ||
|
|
a409678fe5 |
@@ -9,7 +9,6 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@@ -34,13 +33,7 @@ func (d Dialer) Dial(ctx context.Context, address string) (net.Conn, error) {
|
|||||||
|
|
||||||
opts := createDialOptions()
|
opts := createDialOptions()
|
||||||
|
|
||||||
parsedURL, err := url.Parse(wsURL)
|
wsConn, resp, err := websocket.Dial(ctx, wsURL, opts)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
parsedURL.Path = relay.WebSocketURLPath
|
|
||||||
|
|
||||||
wsConn, resp, err := websocket.Dial(ctx, parsedURL.String(), opts)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, context.Canceled) {
|
if errors.Is(err, context.Canceled) {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -56,12 +49,24 @@ func (d Dialer) Dial(ctx context.Context, address string) (net.Conn, error) {
|
|||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// prepareURL rewrites a rel://host[:port] or rels://host[:port] address into a
|
||||||
|
// ws://host[:port]/relay or wss://host[:port]/relay URL, preserving any
|
||||||
|
// non-standard port from the input.
|
||||||
func prepareURL(address string) (string, error) {
|
func prepareURL(address string) (string, error) {
|
||||||
if !strings.HasPrefix(address, "rel:") && !strings.HasPrefix(address, "rels:") {
|
parsed, err := url.Parse(address)
|
||||||
return "", fmt.Errorf("unsupported scheme: %s", address)
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("parse relay address %q: %w", address, err)
|
||||||
}
|
}
|
||||||
|
switch parsed.Scheme {
|
||||||
return strings.Replace(address, "rel", "ws", 1), nil
|
case "rel":
|
||||||
|
parsed.Scheme = "ws"
|
||||||
|
case "rels":
|
||||||
|
parsed.Scheme = "wss"
|
||||||
|
default:
|
||||||
|
return "", fmt.Errorf("unsupported scheme: %s", parsed.Scheme)
|
||||||
|
}
|
||||||
|
parsed.Path = relay.WebSocketURLPath
|
||||||
|
return parsed.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func httpClientNbDialer() *http.Client {
|
func httpClientNbDialer() *http.Client {
|
||||||
|
|||||||
76
shared/relay/client/dialer/ws/ws_test.go
Normal file
76
shared/relay/client/dialer/ws/ws_test.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package ws
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPrepareURL(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "rel scheme with non-standard port",
|
||||||
|
input: "rel://test-domain-2:45678",
|
||||||
|
want: "ws://test-domain-2:45678/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rels scheme with non-standard port",
|
||||||
|
input: "rels://test-domain-2:45678",
|
||||||
|
want: "wss://test-domain-2:45678/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rel scheme without port",
|
||||||
|
input: "rel://test-domain-2",
|
||||||
|
want: "ws://test-domain-2/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rels scheme without port",
|
||||||
|
input: "rels://test-domain-2",
|
||||||
|
want: "wss://test-domain-2/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rel scheme with IP and port",
|
||||||
|
input: "rel://1.2.3.4:45678",
|
||||||
|
want: "ws://1.2.3.4:45678/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rel scheme with hostname starting with rel",
|
||||||
|
input: "rel://relay.example.com:45678",
|
||||||
|
want: "ws://relay.example.com:45678/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rel scheme with IPv6 and port",
|
||||||
|
input: "rel://[2001:db8::1]:45678",
|
||||||
|
want: "ws://[2001:db8::1]:45678/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rels scheme with IPv6 loopback and port",
|
||||||
|
input: "rels://[::1]:45678",
|
||||||
|
want: "wss://[::1]:45678/relay",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "unsupported scheme",
|
||||||
|
input: "http://test-domain-2:45678",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "no scheme",
|
||||||
|
input: "test-domain-2:45678",
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := prepareURL(tt.input)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Fatalf("prepareURL(%q) err = %v, wantErr %v", tt.input, err, tt.wantErr)
|
||||||
|
}
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("prepareURL(%q) = %q, want %q", tt.input, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user