mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-24 17:13:30 +01:00
0c039274a4
This update adds new relay integration for NetBird clients. The new relay is based on web sockets and listens on a single port. - Adds new relay implementation with websocket with single port relaying mechanism - refactor peer connection logic, allowing upgrade and downgrade from/to P2P connection - peer connections are faster since it connects first to relay and then upgrades to P2P - maintains compatibility with old clients by not using the new relay - updates infrastructure scripts with new relay service
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package ws
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"nhooyr.io/websocket"
|
|
)
|
|
|
|
type Conn struct {
|
|
ctx context.Context
|
|
*websocket.Conn
|
|
remoteAddr WebsocketAddr
|
|
}
|
|
|
|
func NewConn(wsConn *websocket.Conn, serverAddress string) net.Conn {
|
|
return &Conn{
|
|
ctx: context.Background(),
|
|
Conn: wsConn,
|
|
remoteAddr: WebsocketAddr{serverAddress},
|
|
}
|
|
}
|
|
|
|
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 0, err
|
|
}
|
|
|
|
func (c *Conn) RemoteAddr() net.Addr {
|
|
return c.remoteAddr
|
|
}
|
|
|
|
func (c *Conn) LocalAddr() net.Addr {
|
|
return WebsocketAddr{addr: "unknown"}
|
|
}
|
|
|
|
func (c *Conn) SetReadDeadline(t time.Time) error {
|
|
return fmt.Errorf("SetReadDeadline is not implemented")
|
|
}
|
|
|
|
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
|
return fmt.Errorf("SetWriteDeadline is not implemented")
|
|
}
|
|
|
|
func (c *Conn) SetDeadline(t time.Time) error {
|
|
return fmt.Errorf("SetDeadline is not implemented")
|
|
}
|
|
|
|
func (c *Conn) Close() error {
|
|
return c.Conn.CloseNow()
|
|
}
|