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

68 lines
1.2 KiB
Go
Raw Normal View History

package wsnhooyr
import (
"context"
"fmt"
"net"
"time"
"nhooyr.io/websocket"
)
type Conn struct {
ctx context.Context
2024-06-19 18:16:23 +02:00
*websocket.Conn
srvAddr *net.TCPAddr
}
2024-06-19 18:16:23 +02:00
func NewConn(wsConn *websocket.Conn, srvAddr *net.TCPAddr) net.Conn {
return &Conn{
2024-06-19 18:16:23 +02:00
ctx: context.Background(),
Conn: wsConn,
srvAddr: srvAddr,
}
}
func (c *Conn) Read(b []byte) (n int, err error) {
t, ioReader, err := c.Conn.Reader(c.ctx)
if err != nil {
return 0, err
}
if t != websocket.MessageBinary {
return 0, fmt.Errorf("unexpected message type")
}
return ioReader.Read(b)
}
func (c *Conn) Write(b []byte) (n int, err error) {
err = c.Conn.Write(c.ctx, websocket.MessageBinary, b)
return len(b), err
}
func (c *Conn) RemoteAddr() net.Addr {
2024-06-19 18:16:23 +02:00
return c.srvAddr
}
func (c *Conn) LocalAddr() net.Addr {
// todo: implement me
return nil
}
func (c *Conn) SetReadDeadline(t time.Time) error {
2024-06-26 16:23:50 +02:00
return fmt.Errorf("not implemented")
}
func (c *Conn) SetWriteDeadline(t time.Time) error {
2024-06-26 16:23:50 +02:00
return fmt.Errorf("not implemented")
}
func (c *Conn) SetDeadline(t time.Time) error {
2024-06-26 16:23:50 +02:00
return fmt.Errorf("not implemented")
}
func (c *Conn) Close() error {
2024-06-09 12:41:52 +02:00
return c.Conn.CloseNow()
}