netbird/relay/client/client.go

359 lines
8.1 KiB
Go
Raw Normal View History

2024-05-17 17:43:28 +02:00
package client
import (
2024-05-26 22:14:33 +02:00
"context"
2024-05-17 17:43:28 +02:00
"fmt"
"io"
"net"
"sync"
2024-05-17 17:43:28 +02:00
"time"
log "github.com/sirupsen/logrus"
"github.com/netbirdio/netbird/relay/client/dialer/ws"
2024-05-17 17:43:28 +02:00
"github.com/netbirdio/netbird/relay/messages"
)
const (
bufferSize = 1500 // optimise the buffer size
serverResponseTimeout = 8 * time.Second
2024-05-17 17:43:28 +02:00
)
2024-05-23 13:24:02 +02:00
type Msg struct {
buf []byte
}
2024-05-17 17:43:28 +02:00
type connContainer struct {
conn *Conn
2024-05-23 13:24:02 +02:00
messages chan Msg
2024-05-17 17:43:28 +02:00
}
2024-06-03 11:22:16 +02:00
// Client is a client for the relay server. It is responsible for establishing a connection to the relay server and
// managing connections to other peers. All exported functions are safe to call concurrently. After close the connection,
// the client can be reused by calling Connect again. When the client is closed, all connections are closed too.
// While the Connect is in progress, the OpenConn function will block until the connection is established.
2024-05-17 17:43:28 +02:00
type Client struct {
2024-05-23 13:24:02 +02:00
log *log.Entry
2024-06-03 00:29:08 +02:00
parentCtx context.Context
2024-05-26 22:14:33 +02:00
ctx context.Context
ctxCancel context.CancelFunc
2024-05-17 17:43:28 +02:00
serverAddress string
2024-05-23 13:24:02 +02:00
hashedID []byte
2024-05-17 17:43:28 +02:00
2024-06-03 00:29:08 +02:00
relayConn net.Conn
2024-05-28 01:27:53 +02:00
conns map[string]*connContainer
2024-06-03 00:29:08 +02:00
serviceIsRunning bool
mu sync.Mutex
readLoopMutex sync.Mutex
wgReadLoop sync.WaitGroup
2024-05-29 16:40:26 +02:00
remoteAddr net.Addr
onDisconnectListener func()
listenerMutex sync.Mutex
2024-05-17 17:43:28 +02:00
}
2024-06-03 11:22:16 +02:00
// NewClient creates a new client for the relay server. The client is not connected to the server until the Connect
2024-05-26 22:14:33 +02:00
func NewClient(ctx context.Context, serverAddress, peerID string) *Client {
2024-05-23 13:24:02 +02:00
hashedID, hashedStringId := messages.HashID(peerID)
2024-05-17 17:43:28 +02:00
return &Client{
log: log.WithField("client_id", hashedStringId),
2024-06-03 00:29:08 +02:00
parentCtx: ctx,
ctxCancel: func() {},
serverAddress: serverAddress,
hashedID: hashedID,
conns: make(map[string]*connContainer),
2024-05-17 17:43:28 +02:00
}
}
2024-06-03 11:22:16 +02:00
// SetOnDisconnectListener sets a function that will be called when the connection to the relay server is closed.
func (c *Client) SetOnDisconnectListener(fn func()) {
c.listenerMutex.Lock()
defer c.listenerMutex.Unlock()
c.onDisconnectListener = fn
2024-05-17 17:43:28 +02:00
}
2024-06-03 11:22:16 +02:00
// Connect establishes a connection to the relay server. It blocks until the connection is established or an error occurs.
func (c *Client) Connect() error {
2024-06-03 00:29:08 +02:00
c.readLoopMutex.Lock()
defer c.readLoopMutex.Unlock()
c.mu.Lock()
2024-06-03 20:14:39 +02:00
defer c.mu.Unlock()
2024-05-29 16:40:26 +02:00
if c.serviceIsRunning {
return nil
}
2024-05-28 01:00:25 +02:00
2024-05-29 16:40:26 +02:00
err := c.connect()
if err != nil {
return err
}
2024-05-28 01:00:25 +02:00
2024-05-29 16:40:26 +02:00
c.serviceIsRunning = true
2024-05-28 01:00:25 +02:00
2024-06-03 00:29:08 +02:00
c.ctx, c.ctxCancel = context.WithCancel(c.parentCtx)
context.AfterFunc(c.ctx, func() {
cErr := c.Close()
2024-05-29 16:40:26 +02:00
if cErr != nil {
log.Errorf("failed to close relay connection: %s", cErr)
}
2024-06-03 00:29:08 +02:00
})
c.wgReadLoop.Add(1)
go c.readLoop(c.relayConn)
2024-05-29 16:40:26 +02:00
return nil
2024-05-28 01:00:25 +02:00
}
2024-06-03 00:29:08 +02:00
// todo: what should happen of call with the same peerID?
2024-05-23 13:24:02 +02:00
func (c *Client) OpenConn(dstPeerID string) (net.Conn, error) {
2024-06-03 00:29:08 +02:00
c.mu.Lock()
defer c.mu.Unlock()
2024-05-28 01:00:25 +02:00
2024-06-03 00:29:08 +02:00
if !c.serviceIsRunning {
2024-05-26 22:14:33 +02:00
return nil, fmt.Errorf("relay connection is not established")
}
2024-05-23 13:24:02 +02:00
hashedID, hashedStringID := messages.HashID(dstPeerID)
log.Infof("open connection to peer: %s", hashedStringID)
messageBuffer := make(chan Msg, 2)
2024-05-27 10:25:08 +02:00
conn := NewConn(c, hashedID, hashedStringID, c.generateConnReaderFN(messageBuffer))
2024-05-23 13:24:02 +02:00
c.conns[hashedStringID] = &connContainer{
conn,
messageBuffer,
2024-05-17 17:43:28 +02:00
}
2024-05-23 13:24:02 +02:00
return conn, nil
2024-05-17 17:43:28 +02:00
}
2024-06-03 11:22:16 +02:00
// RelayRemoteAddress returns the IP address of the relay server. It could change after the close and reopen the connection.
2024-05-29 16:40:26 +02:00
func (c *Client) RelayRemoteAddress() (net.Addr, error) {
2024-06-03 00:29:08 +02:00
c.mu.Lock()
defer c.mu.Unlock()
2024-05-29 16:40:26 +02:00
if c.remoteAddr == nil {
return nil, fmt.Errorf("relay connection is not established")
}
return c.remoteAddr, nil
}
2024-06-03 11:22:16 +02:00
// Close closes the connection to the relay server and all connections to other peers.
2024-05-17 17:43:28 +02:00
func (c *Client) Close() error {
2024-06-03 00:29:08 +02:00
c.readLoopMutex.Lock()
defer c.readLoopMutex.Unlock()
c.mu.Lock()
var err error
if c.serviceIsRunning {
c.serviceIsRunning = false
err = c.relayConn.Close()
2024-05-28 01:27:53 +02:00
}
2024-06-03 00:29:08 +02:00
c.closeAllConns()
c.mu.Unlock()
2024-05-28 01:27:53 +02:00
2024-06-03 00:29:08 +02:00
c.wgReadLoop.Wait()
2024-05-26 22:14:33 +02:00
c.ctxCancel()
2024-06-03 00:29:08 +02:00
return err
2024-05-26 22:14:33 +02:00
}
2024-05-28 01:00:25 +02:00
func (c *Client) connect() error {
conn, err := ws.Dial(c.serverAddress)
2024-05-28 01:00:25 +02:00
if err != nil {
return err
}
c.relayConn = conn
err = c.handShake()
if err != nil {
cErr := conn.Close()
if cErr != nil {
log.Errorf("failed to close connection: %s", cErr)
}
c.relayConn = nil
return err
}
2024-05-29 16:40:26 +02:00
c.remoteAddr = conn.RemoteAddr()
2024-05-28 01:00:25 +02:00
return nil
}
2024-05-17 17:43:28 +02:00
func (c *Client) handShake() error {
2024-05-26 22:14:33 +02:00
defer func() {
err := c.relayConn.SetReadDeadline(time.Time{})
if err != nil {
log.Errorf("failed to reset read deadline: %s", err)
}
}()
2024-05-23 13:24:02 +02:00
msg, err := messages.MarshalHelloMsg(c.hashedID)
2024-05-17 17:43:28 +02:00
if err != nil {
2024-05-23 13:24:02 +02:00
log.Errorf("failed to marshal hello message: %s", err)
2024-05-17 17:43:28 +02:00
return err
}
_, err = c.relayConn.Write(msg)
if err != nil {
log.Errorf("failed to send hello message: %s", err)
return err
}
err = c.relayConn.SetReadDeadline(time.Now().Add(serverResponseTimeout))
if err != nil {
log.Errorf("failed to set read deadline: %s", err)
return err
}
buf := make([]byte, 1500) // todo: optimise buffer size
n, err := c.relayConn.Read(buf)
if err != nil {
log.Errorf("failed to read hello response: %s", err)
return err
}
msgType, err := messages.DetermineServerMsgType(buf[:n])
if err != nil {
log.Errorf("failed to determine message type: %s", err)
return err
}
if msgType != messages.MsgTypeHelloResponse {
log.Errorf("unexpected message type: %s", msgType)
return fmt.Errorf("unexpected message type")
}
2024-05-17 17:43:28 +02:00
return nil
}
2024-06-03 00:29:08 +02:00
func (c *Client) readLoop(relayConn net.Conn) {
2024-05-17 17:43:28 +02:00
var errExit error
var n int
for {
2024-05-23 13:24:02 +02:00
buf := make([]byte, bufferSize)
2024-06-03 00:29:08 +02:00
n, errExit = relayConn.Read(buf)
2024-05-17 17:43:28 +02:00
if errExit != nil {
2024-06-03 00:29:08 +02:00
c.mu.Lock()
2024-05-28 01:00:25 +02:00
if c.serviceIsRunning {
2024-05-23 13:24:02 +02:00
c.log.Debugf("failed to read message from relay server: %s", errExit)
}
2024-06-03 00:29:08 +02:00
c.mu.Unlock()
2024-05-17 17:43:28 +02:00
break
}
msgType, err := messages.DetermineServerMsgType(buf[:n])
if err != nil {
2024-05-23 13:24:02 +02:00
c.log.Errorf("failed to determine message type: %s", err)
2024-05-17 17:43:28 +02:00
continue
}
switch msgType {
case messages.MsgTypeTransport:
2024-05-23 13:24:02 +02:00
peerID, err := messages.UnmarshalTransportID(buf[:n])
2024-05-17 17:43:28 +02:00
if err != nil {
2024-05-23 13:24:02 +02:00
c.log.Errorf("failed to parse transport message: %v", err)
2024-05-17 17:43:28 +02:00
continue
}
2024-05-23 13:24:02 +02:00
stringID := messages.HashIDToString(peerID)
2024-06-03 00:29:08 +02:00
c.mu.Lock()
if !c.serviceIsRunning {
c.mu.Unlock()
break
}
2024-05-23 13:24:02 +02:00
container, ok := c.conns[stringID]
2024-06-03 00:29:08 +02:00
c.mu.Unlock()
2024-05-21 16:21:29 +02:00
if !ok {
2024-05-23 13:24:02 +02:00
c.log.Errorf("peer not found: %s", stringID)
continue
2024-05-21 16:21:29 +02:00
}
2024-06-03 00:29:08 +02:00
container.messages <- Msg{buf[:n]}
2024-05-17 17:43:28 +02:00
}
}
c.notifyDisconnected()
2024-05-28 01:00:25 +02:00
c.log.Tracef("exit from read loop")
2024-05-28 01:27:53 +02:00
c.wgReadLoop.Done()
2024-06-03 00:29:08 +02:00
c.Close()
2024-05-17 17:43:28 +02:00
}
2024-06-03 00:29:08 +02:00
// todo check by reference too, the id is not enought because the id come from the outer conn
2024-05-27 10:25:08 +02:00
func (c *Client) writeTo(id string, dstID []byte, payload []byte) (int, error) {
2024-06-03 00:29:08 +02:00
c.mu.Lock()
// conn, ok := c.conns[id]
2024-05-27 10:25:08 +02:00
_, ok := c.conns[id]
2024-06-03 00:29:08 +02:00
c.mu.Unlock()
2024-05-27 10:25:08 +02:00
if !ok {
return 0, io.EOF
}
2024-06-03 00:29:08 +02:00
/*
if conn != clientRef {
return 0, io.EOF
}
*/
2024-05-23 13:24:02 +02:00
msg := messages.MarshalTransportMsg(dstID, payload)
2024-05-17 17:43:28 +02:00
n, err := c.relayConn.Write(msg)
if err != nil {
log.Errorf("failed to write transport message: %s", err)
}
return n, err
}
2024-05-23 13:24:02 +02:00
func (c *Client) generateConnReaderFN(msgChannel chan Msg) func(b []byte) (n int, err error) {
2024-05-17 17:43:28 +02:00
return func(b []byte) (n int, err error) {
2024-05-23 13:24:02 +02:00
msg, ok := <-msgChannel
if !ok {
return 0, io.EOF
}
2024-05-17 23:29:47 +02:00
2024-05-23 13:24:02 +02:00
payload, err := messages.UnmarshalTransportPayload(msg.buf)
if err != nil {
return 0, err
2024-05-17 17:43:28 +02:00
}
2024-05-23 13:24:02 +02:00
n = copy(b, payload)
2024-05-17 17:43:28 +02:00
return n, nil
}
}
2024-05-27 10:25:08 +02:00
2024-06-03 00:29:08 +02:00
func (c *Client) closeAllConns() {
for _, container := range c.conns {
close(container.messages)
}
c.conns = make(map[string]*connContainer)
}
// todo check by reference too, the id is not enought because the id come from the outer conn
2024-05-27 10:25:08 +02:00
func (c *Client) closeConn(id string) error {
2024-06-03 00:29:08 +02:00
c.mu.Lock()
defer c.mu.Unlock()
2024-05-28 01:00:25 +02:00
2024-05-27 10:25:08 +02:00
conn, ok := c.conns[id]
if !ok {
return fmt.Errorf("connection already closed")
}
close(conn.messages)
delete(c.conns, id)
return nil
}
func (c *Client) onDisconnect() {
c.listenerMutex.Lock()
defer c.listenerMutex.Unlock()
if c.onDisconnectListener == nil {
return
}
c.onDisconnectListener()
}
func (c *Client) notifyDisconnected() {
c.listenerMutex.Lock()
defer c.listenerMutex.Unlock()
if c.onDisconnectListener == nil {
return
}
go c.onDisconnectListener()
}