diff --git a/relay/client/dialer/wsnhooyr/client_conn.go b/relay/client/dialer/wsnhooyr/client_conn.go index 8f02b6701..0aa995286 100644 --- a/relay/client/dialer/wsnhooyr/client_conn.go +++ b/relay/client/dialer/wsnhooyr/client_conn.go @@ -10,14 +10,16 @@ import ( ) type Conn struct { - *websocket.Conn ctx context.Context + *websocket.Conn + srvAddr *net.TCPAddr } -func NewConn(wsConn *websocket.Conn) net.Conn { +func NewConn(wsConn *websocket.Conn, srvAddr *net.TCPAddr) net.Conn { return &Conn{ - Conn: wsConn, - ctx: context.Background(), + ctx: context.Background(), + Conn: wsConn, + srvAddr: srvAddr, } } @@ -40,8 +42,7 @@ func (c *Conn) Write(b []byte) (n int, err error) { } func (c *Conn) RemoteAddr() net.Addr { - // todo: implement me - return nil + return c.srvAddr } func (c *Conn) LocalAddr() net.Addr { diff --git a/relay/client/dialer/wsnhooyr/ws.go b/relay/client/dialer/wsnhooyr/ws.go index ccec97402..cdb056bce 100644 --- a/relay/client/dialer/wsnhooyr/ws.go +++ b/relay/client/dialer/wsnhooyr/ws.go @@ -5,18 +5,31 @@ import ( "fmt" "net" + log "github.com/sirupsen/logrus" "nhooyr.io/websocket" ) func Dial(address string) (net.Conn, error) { - addr := fmt.Sprintf("ws://" + address) - wsConn, _, err := websocket.Dial(context.Background(), addr, nil) + hostName, _, err := net.SplitHostPort(address) + + addr, err := net.ResolveTCPAddr("tcp", address) + if err != nil { + log.Errorf("failed to resolve address of Relay server: %s", address) + return nil, err + } + + url := fmt.Sprintf("ws://%s:%d"+addr.IP.String(), addr.Port) + opts := &websocket.DialOptions{ + Host: hostName, + } + + wsConn, _, err := websocket.Dial(context.Background(), url, opts) if err != nil { return nil, err } - conn := NewConn(wsConn) + conn := NewConn(wsConn, addr) return conn, nil }