netbird/relay/client/dialer/wsnhooyr/ws.go

36 lines
644 B
Go
Raw Normal View History

package wsnhooyr
import (
"context"
"fmt"
"net"
2024-06-19 18:16:23 +02:00
log "github.com/sirupsen/logrus"
"nhooyr.io/websocket"
)
func Dial(address string) (net.Conn, error) {
2024-06-19 18:16:23 +02:00
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
}
2024-06-19 18:17:52 +02:00
url := fmt.Sprintf("ws://%s:%d", addr.IP.String(), addr.Port)
2024-06-19 18:16:23 +02:00
opts := &websocket.DialOptions{
Host: hostName,
}
wsConn, _, err := websocket.Dial(context.Background(), url, opts)
if err != nil {
return nil, err
}
2024-06-19 18:16:23 +02:00
conn := NewConn(wsConn, addr)
return conn, nil
}