Fix remote address in ws client

This commit is contained in:
Zoltán Papp 2024-06-19 18:16:23 +02:00
parent 0261e15aad
commit 81f2330d49
2 changed files with 23 additions and 9 deletions

View File

@ -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 {

View File

@ -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
}