2022-01-10 18:43:13 +01:00
|
|
|
package peer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-08 12:06:14 +02:00
|
|
|
"math/rand"
|
2022-01-10 18:43:13 +01:00
|
|
|
"net"
|
2024-09-08 12:06:14 +02:00
|
|
|
"os"
|
2023-12-18 11:46:58 +01:00
|
|
|
"runtime"
|
2022-06-04 20:15:41 +02:00
|
|
|
"strings"
|
2022-01-10 18:43:13 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2022-02-16 20:00:21 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2023-12-20 23:02:42 +01:00
|
|
|
"github.com/pion/ice/v3"
|
2022-02-16 20:00:21 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-07-26 14:00:47 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2023-03-03 19:49:18 +01:00
|
|
|
|
2023-03-24 08:40:39 +01:00
|
|
|
"github.com/netbirdio/netbird/client/internal/stdnet"
|
2023-07-26 14:00:47 +02:00
|
|
|
"github.com/netbirdio/netbird/client/internal/wgproxy"
|
2023-03-03 19:49:18 +01:00
|
|
|
"github.com/netbirdio/netbird/iface"
|
2024-09-08 12:06:14 +02:00
|
|
|
relayClient "github.com/netbirdio/netbird/relay/client"
|
2024-06-03 17:31:37 +02:00
|
|
|
"github.com/netbirdio/netbird/route"
|
2024-04-08 18:56:52 +02:00
|
|
|
nbnet "github.com/netbirdio/netbird/util/net"
|
2022-01-10 18:43:13 +01:00
|
|
|
)
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
type ConnPriority int
|
2023-07-26 14:00:47 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
const (
|
2023-07-26 14:00:47 +02:00
|
|
|
defaultWgKeepAlive = 25 * time.Second
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
connPriorityRelay ConnPriority = 1
|
|
|
|
connPriorityICETurn ConnPriority = 1
|
|
|
|
connPriorityICEP2P ConnPriority = 2
|
2023-04-28 16:26:54 +02:00
|
|
|
)
|
|
|
|
|
2023-07-26 14:00:47 +02:00
|
|
|
type WgConfig struct {
|
|
|
|
WgListenPort int
|
|
|
|
RemoteKey string
|
2024-08-29 21:31:19 +02:00
|
|
|
WgInterface iface.IWGIface
|
2023-07-26 14:00:47 +02:00
|
|
|
AllowedIps string
|
|
|
|
PreSharedKey *wgtypes.Key
|
|
|
|
}
|
|
|
|
|
2024-09-04 11:14:58 +02:00
|
|
|
// ConnConfig is a peer Connection configuration
|
|
|
|
type ConnConfig struct {
|
|
|
|
// Key is a public key of a remote peer
|
|
|
|
Key string
|
|
|
|
// LocalKey is a public key of a local peer
|
|
|
|
LocalKey string
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
Timeout time.Duration
|
|
|
|
|
2023-07-26 14:00:47 +02:00
|
|
|
WgConfig WgConfig
|
2022-02-16 20:00:21 +01:00
|
|
|
|
2022-09-02 19:33:35 +02:00
|
|
|
LocalWgPort int
|
2022-11-23 08:42:12 +01:00
|
|
|
|
2024-01-08 12:25:35 +01:00
|
|
|
// RosenpassPubKey is this peer's Rosenpass public key
|
|
|
|
RosenpassPubKey []byte
|
|
|
|
// RosenpassPubKey is this peer's RosenpassAddr server address (IP:port)
|
|
|
|
RosenpassAddr string
|
2024-09-04 11:14:58 +02:00
|
|
|
|
|
|
|
// ICEConfig ICE protocol configuration
|
|
|
|
ICEConfig ICEConfig
|
2022-09-02 19:33:35 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
type WorkerCallbacks struct {
|
|
|
|
OnRelayReadyCallback func(info RelayConnInfo)
|
|
|
|
OnRelayStatusChanged func(ConnStatus)
|
2022-09-02 19:33:35 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
OnICEConnReadyCallback func(ConnPriority, ICEConnInfo)
|
|
|
|
OnICEStatusChanged func(ConnStatus)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Conn struct {
|
2024-09-08 12:06:14 +02:00
|
|
|
log *log.Entry
|
|
|
|
mu sync.Mutex
|
|
|
|
ctx context.Context
|
|
|
|
ctxCancel context.CancelFunc
|
|
|
|
config ConnConfig
|
2023-03-03 19:49:18 +01:00
|
|
|
statusRecorder *Status
|
2023-07-26 14:00:47 +02:00
|
|
|
wgProxyFactory *wgproxy.Factory
|
2024-09-08 12:06:14 +02:00
|
|
|
wgProxyICE wgproxy.Proxy
|
|
|
|
wgProxyRelay wgproxy.Proxy
|
|
|
|
signaler *Signaler
|
|
|
|
relayManager *relayClient.Manager
|
|
|
|
allowedIPsIP string
|
|
|
|
handshaker *Handshaker
|
|
|
|
|
|
|
|
onConnected func(remoteWireGuardKey string, remoteRosenpassPubKey []byte, wireGuardIP string, remoteRosenpassAddr string)
|
|
|
|
onDisconnected func(remotePeer string, wgIP string)
|
|
|
|
|
|
|
|
statusRelay ConnStatus
|
|
|
|
statusICE ConnStatus
|
|
|
|
currentConnPriority ConnPriority
|
|
|
|
opened bool // this flag is used to prevent close in case of not opened connection
|
|
|
|
|
|
|
|
workerICE *WorkerICE
|
|
|
|
workerRelay *WorkerRelay
|
|
|
|
|
|
|
|
connIDRelay nbnet.ConnectionID
|
|
|
|
connIDICE nbnet.ConnectionID
|
2024-06-17 09:47:17 +02:00
|
|
|
beforeAddPeerHooks []nbnet.AddHookFunc
|
|
|
|
afterRemovePeerHooks []nbnet.RemoveHookFunc
|
2023-03-16 16:46:17 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
endpointRelay *net.UDPAddr
|
2022-06-04 19:41:01 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// for reconnection operations
|
|
|
|
iCEDisconnected chan bool
|
|
|
|
relayDisconnected chan bool
|
2023-07-26 14:00:47 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
// NewConn creates a new not opened Conn to the remote peer.
|
|
|
|
// To establish a connection run Conn.Open
|
2024-09-08 12:06:14 +02:00
|
|
|
func NewConn(engineCtx context.Context, config ConnConfig, statusRecorder *Status, wgProxyFactory *wgproxy.Factory, signaler *Signaler, iFaceDiscover stdnet.ExternalIFaceDiscover, relayManager *relayClient.Manager) (*Conn, error) {
|
|
|
|
_, allowedIPsIP, err := net.ParseCIDR(config.WgConfig.AllowedIps)
|
2023-03-17 10:37:27 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
log.Errorf("failed to parse allowedIPS: %v", err)
|
|
|
|
return nil, err
|
2023-03-17 10:37:27 +01:00
|
|
|
}
|
2023-04-28 16:26:54 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
ctx, ctxCancel := context.WithCancel(engineCtx)
|
|
|
|
connLog := log.WithField("peer", config.Key)
|
2023-04-28 16:26:54 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
var conn = &Conn{
|
|
|
|
log: connLog,
|
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
|
|
|
config: config,
|
|
|
|
statusRecorder: statusRecorder,
|
|
|
|
wgProxyFactory: wgProxyFactory,
|
|
|
|
signaler: signaler,
|
|
|
|
relayManager: relayManager,
|
|
|
|
allowedIPsIP: allowedIPsIP.String(),
|
|
|
|
statusRelay: StatusDisconnected,
|
|
|
|
statusICE: StatusDisconnected,
|
|
|
|
iCEDisconnected: make(chan bool, 1),
|
|
|
|
relayDisconnected: make(chan bool, 1),
|
2022-11-23 11:03:29 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
rFns := WorkerRelayCallbacks{
|
|
|
|
OnConnReady: conn.relayConnectionIsReady,
|
|
|
|
OnDisconnected: conn.onWorkerRelayStateDisconnected,
|
2022-11-23 11:03:29 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
wFns := WorkerICECallbacks{
|
|
|
|
OnConnReady: conn.iCEConnectionIsReady,
|
|
|
|
OnStatusChanged: conn.onWorkerICEStateDisconnected,
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.workerRelay = NewWorkerRelay(connLog, config, relayManager, rFns)
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
relayIsSupportedLocally := conn.workerRelay.RelayIsSupportedLocally()
|
|
|
|
conn.workerICE, err = NewWorkerICE(ctx, connLog, config, signaler, iFaceDiscover, statusRecorder, relayIsSupportedLocally, wFns)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
return nil, err
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.handshaker = NewHandshaker(ctx, connLog, config, signaler, conn.workerICE, conn.workerRelay)
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.handshaker.AddOnNewOfferListener(conn.workerRelay.OnNewOffer)
|
|
|
|
if os.Getenv("NB_FORCE_RELAY") != "true" {
|
|
|
|
conn.handshaker.AddOnNewOfferListener(conn.workerICE.OnNewOffer)
|
2024-03-20 11:18:34 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
go conn.handshaker.Listen()
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
return conn, nil
|
2023-04-28 16:26:54 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// Open opens connection to the remote peer
|
|
|
|
// It will try to establish a connection using ICE and in parallel with relay. The higher priority connection type will
|
|
|
|
// be used.
|
|
|
|
func (conn *Conn) Open() {
|
|
|
|
conn.log.Debugf("open connection to peer")
|
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
conn.opened = true
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2023-07-26 14:00:47 +02:00
|
|
|
peerState := State{
|
|
|
|
PubKey: conn.config.Key,
|
|
|
|
IP: strings.Split(conn.config.WgConfig.AllowedIps, "/")[0],
|
|
|
|
ConnStatusUpdate: time.Now(),
|
2024-09-08 12:06:14 +02:00
|
|
|
ConnStatus: StatusDisconnected,
|
2024-04-11 22:12:23 +02:00
|
|
|
Mux: new(sync.RWMutex),
|
2023-07-26 14:00:47 +02:00
|
|
|
}
|
2022-07-02 12:02:17 +02:00
|
|
|
err := conn.statusRecorder.UpdatePeerState(peerState)
|
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Warnf("error while updating the state err: %v", err)
|
2022-07-02 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
go conn.startHandshakeAndReconnect()
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) startHandshakeAndReconnect() {
|
|
|
|
conn.waitInitialRandomSleepTime()
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
err := conn.handshaker.sendOffer()
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Errorf("failed to send initial offer: %v", err)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.workerRelay.IsController() {
|
|
|
|
conn.reconnectLoopWithRetry()
|
|
|
|
} else {
|
|
|
|
conn.reconnectLoopForOnDisconnectedEvent()
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// Close closes this peer Conn issuing a close event to the Conn closeCh
|
|
|
|
func (conn *Conn) Close() {
|
2022-01-10 18:43:13 +01:00
|
|
|
conn.mu.Lock()
|
2024-09-08 12:06:14 +02:00
|
|
|
defer conn.mu.Unlock()
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Infof("close peer connection")
|
|
|
|
conn.ctxCancel()
|
2022-07-02 12:02:17 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if !conn.opened {
|
|
|
|
conn.log.Debugf("ignore close connection to peer")
|
|
|
|
return
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.workerRelay.DisableWgWatcher()
|
|
|
|
conn.workerRelay.CloseConn()
|
|
|
|
conn.workerICE.Close()
|
|
|
|
|
|
|
|
if conn.wgProxyRelay != nil {
|
|
|
|
err := conn.wgProxyRelay.CloseConn()
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Errorf("failed to close wg proxy for relay: %v", err)
|
|
|
|
}
|
|
|
|
conn.wgProxyRelay = nil
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.wgProxyICE != nil {
|
|
|
|
err := conn.wgProxyICE.CloseConn()
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Errorf("failed to close wg proxy for ice: %v", err)
|
|
|
|
}
|
|
|
|
conn.wgProxyICE = nil
|
2022-09-02 19:33:35 +02:00
|
|
|
}
|
2024-03-20 11:18:34 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
err := conn.config.WgConfig.WgInterface.RemovePeer(conn.config.WgConfig.RemoteKey)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Errorf("failed to remove wg endpoint: %v", err)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.freeUpConnID()
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.evalStatus() == StatusConnected && conn.onDisconnected != nil {
|
|
|
|
conn.onDisconnected(conn.config.WgConfig.RemoteKey, conn.config.WgConfig.AllowedIps)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
conn.setStatusToDisconnected()
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// OnRemoteAnswer handles an offer from the remote peer and returns true if the message was accepted, false otherwise
|
|
|
|
// doesn't block, discards the message if connection wasn't ready
|
|
|
|
func (conn *Conn) OnRemoteAnswer(answer OfferAnswer) bool {
|
|
|
|
conn.log.Debugf("OnRemoteAnswer, status ICE: %s, status relay: %s", conn.statusICE, conn.statusRelay)
|
|
|
|
return conn.handshaker.OnRemoteAnswer(answer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnRemoteCandidate Handles ICE connection Candidate provided by the remote peer.
|
|
|
|
func (conn *Conn) OnRemoteCandidate(candidate ice.Candidate, haRoutes route.HAMap) {
|
|
|
|
conn.workerICE.OnRemoteCandidate(candidate, haRoutes)
|
2023-04-13 17:00:01 +02:00
|
|
|
}
|
|
|
|
|
2024-06-17 09:47:17 +02:00
|
|
|
func (conn *Conn) AddBeforeAddPeerHook(hook nbnet.AddHookFunc) {
|
2024-04-08 18:56:52 +02:00
|
|
|
conn.beforeAddPeerHooks = append(conn.beforeAddPeerHooks, hook)
|
|
|
|
}
|
2024-06-17 09:47:17 +02:00
|
|
|
func (conn *Conn) AddAfterRemovePeerHook(hook nbnet.RemoveHookFunc) {
|
2024-04-08 18:56:52 +02:00
|
|
|
conn.afterRemovePeerHooks = append(conn.afterRemovePeerHooks, hook)
|
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// SetOnConnected sets a handler function to be triggered by Conn when a new connection to a remote peer established
|
|
|
|
func (conn *Conn) SetOnConnected(handler func(remoteWireGuardKey string, remoteRosenpassPubKey []byte, wireGuardIP string, remoteRosenpassAddr string)) {
|
|
|
|
conn.onConnected = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOnDisconnected sets a handler function to be triggered by Conn when a connection to a remote disconnected
|
|
|
|
func (conn *Conn) SetOnDisconnected(handler func(remotePeer string, wgIP string)) {
|
|
|
|
conn.onDisconnected = handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) OnRemoteOffer(offer OfferAnswer) bool {
|
|
|
|
conn.log.Debugf("OnRemoteOffer, on status ICE: %s, status Relay: %s", conn.statusICE, conn.statusRelay)
|
|
|
|
return conn.handshaker.OnRemoteOffer(offer)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WgConfig returns the WireGuard config
|
|
|
|
func (conn *Conn) WgConfig() WgConfig {
|
|
|
|
return conn.config.WgConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// Status returns current status of the Conn
|
|
|
|
func (conn *Conn) Status() ConnStatus {
|
2022-01-10 18:43:13 +01:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
2024-09-08 12:06:14 +02:00
|
|
|
return conn.evalStatus()
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) GetKey() string {
|
|
|
|
return conn.config.Key
|
|
|
|
}
|
2022-03-01 14:07:33 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) reconnectLoopWithRetry() {
|
|
|
|
// Give chance to the peer to establish the initial connection.
|
|
|
|
// With it, we can decrease to send necessary offer
|
|
|
|
select {
|
|
|
|
case <-conn.ctx.Done():
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := conn.prepareExponentTicker()
|
|
|
|
defer ticker.Stop()
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case t := <-ticker.C:
|
|
|
|
if t.IsZero() {
|
|
|
|
// in case if the ticker has been canceled by context then avoid the temporary loop
|
|
|
|
return
|
|
|
|
}
|
2023-07-26 14:00:47 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.workerRelay.IsRelayConnectionSupportedWithPeer() {
|
|
|
|
if conn.statusRelay == StatusDisconnected || conn.statusICE == StatusDisconnected {
|
|
|
|
conn.log.Tracef("connectivity guard timedout, relay state: %s, ice state: %s", conn.statusRelay, conn.statusICE)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if conn.statusICE == StatusDisconnected {
|
|
|
|
conn.log.Tracef("connectivity guard timedout, ice state: %s", conn.statusICE)
|
|
|
|
}
|
|
|
|
}
|
2024-04-03 11:11:46 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// checks if there is peer connection is established via relay or ice
|
|
|
|
if conn.isConnected() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := conn.handshaker.sendOffer()
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Errorf("failed to do handshake: %v", err)
|
|
|
|
}
|
|
|
|
case changed := <-conn.relayDisconnected:
|
|
|
|
if !changed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
conn.log.Debugf("Relay state changed, reset reconnect timer")
|
|
|
|
ticker.Stop()
|
|
|
|
ticker = conn.prepareExponentTicker()
|
|
|
|
case changed := <-conn.iCEDisconnected:
|
|
|
|
if !changed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
conn.log.Debugf("ICE state changed, reset reconnect timer")
|
|
|
|
ticker.Stop()
|
|
|
|
ticker = conn.prepareExponentTicker()
|
|
|
|
case <-conn.ctx.Done():
|
|
|
|
conn.log.Debugf("context is done, stop reconnect loop")
|
|
|
|
return
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
}
|
2024-04-08 18:56:52 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) prepareExponentTicker() *backoff.Ticker {
|
|
|
|
bo := backoff.WithContext(&backoff.ExponentialBackOff{
|
|
|
|
InitialInterval: 800 * time.Millisecond,
|
|
|
|
RandomizationFactor: 0.01,
|
|
|
|
Multiplier: 2,
|
|
|
|
MaxInterval: conn.config.Timeout,
|
|
|
|
MaxElapsedTime: 0,
|
|
|
|
Stop: backoff.Stop,
|
|
|
|
Clock: backoff.SystemClock,
|
|
|
|
}, conn.ctx)
|
|
|
|
|
|
|
|
ticker := backoff.NewTicker(bo)
|
|
|
|
<-ticker.C // consume the initial tick what is happening right after the ticker has been created
|
|
|
|
|
|
|
|
return ticker
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconnectLoopForOnDisconnectedEvent is used when the peer is not a controller and it should reconnect to the peer
|
|
|
|
// when the connection is lost. It will try to establish a connection only once time if before the connection was established
|
|
|
|
// It track separately the ice and relay connection status. Just because a lover priority connection reestablished it does not
|
|
|
|
// mean that to switch to it. We always force to use the higher priority connection.
|
|
|
|
func (conn *Conn) reconnectLoopForOnDisconnectedEvent() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case changed := <-conn.relayDisconnected:
|
|
|
|
if !changed {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
conn.log.Debugf("Relay state changed, try to send new offer")
|
|
|
|
case changed := <-conn.iCEDisconnected:
|
|
|
|
if !changed {
|
|
|
|
continue
|
2024-05-07 18:50:34 +02:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Debugf("ICE state changed, try to send new offer")
|
|
|
|
case <-conn.ctx.Done():
|
|
|
|
conn.log.Debugf("context is done, stop reconnect loop")
|
|
|
|
return
|
2023-07-26 14:00:47 +02:00
|
|
|
}
|
2022-03-01 14:07:33 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
err := conn.handshaker.SendOffer()
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Errorf("failed to do handshake: %v", err)
|
|
|
|
}
|
2024-02-24 12:41:13 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// configureConnection starts proxying traffic from/to local Wireguard and sets connection status to StatusConnected
|
|
|
|
func (conn *Conn) iCEConnectionIsReady(priority ConnPriority, iceConnInfo ICEConnInfo) {
|
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
|
|
|
if conn.ctx.Err() != nil {
|
|
|
|
return
|
2022-07-02 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Debugf("ICE connection is ready")
|
|
|
|
|
|
|
|
conn.statusICE = StatusConnected
|
|
|
|
|
|
|
|
defer conn.updateIceState(iceConnInfo)
|
|
|
|
|
|
|
|
if conn.currentConnPriority > priority {
|
|
|
|
return
|
2022-07-02 12:02:17 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Infof("set ICE to active connection")
|
|
|
|
|
|
|
|
endpoint, wgProxy, err := conn.getEndpointForICEConnInfo(iceConnInfo)
|
2024-01-08 12:25:35 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
return
|
2024-01-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
endpointUdpAddr, _ := net.ResolveUDPAddr(endpoint.Network(), endpoint.String())
|
|
|
|
conn.log.Debugf("Conn resolved IP is %s for endopint %s", endpoint, endpointUdpAddr.IP)
|
2024-05-17 15:58:29 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.connIDICE = nbnet.GenerateConnID()
|
|
|
|
for _, hook := range conn.beforeAddPeerHooks {
|
|
|
|
if err := hook(conn.connIDICE, endpointUdpAddr.IP); err != nil {
|
|
|
|
conn.log.Errorf("Before add peer hook failed: %v", err)
|
|
|
|
}
|
2024-01-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.workerRelay.DisableWgWatcher()
|
2023-03-16 16:46:17 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
err = conn.configureWGEndpoint(endpointUdpAddr)
|
2023-03-16 16:46:17 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
if wgProxy != nil {
|
|
|
|
if err := wgProxy.CloseConn(); err != nil {
|
|
|
|
conn.log.Warnf("Failed to close turn connection: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn.log.Warnf("Failed to update wg peer configuration: %v", err)
|
2023-05-03 14:47:44 +02:00
|
|
|
return
|
2023-03-16 16:46:17 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
wgConfigWorkaround()
|
2023-03-16 16:46:17 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.wgProxyICE != nil {
|
|
|
|
if err := conn.wgProxyICE.CloseConn(); err != nil {
|
|
|
|
conn.log.Warnf("failed to close deprecated wg proxy conn: %v", err)
|
|
|
|
}
|
2023-03-16 16:46:17 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.wgProxyICE = wgProxy
|
|
|
|
|
|
|
|
conn.currentConnPriority = priority
|
|
|
|
|
|
|
|
conn.doOnConnected(iceConnInfo.RosenpassPubKey, iceConnInfo.RosenpassAddr)
|
2023-03-16 16:46:17 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// todo review to make sense to handle connecting and disconnected status also?
|
|
|
|
func (conn *Conn) onWorkerICEStateDisconnected(newState ConnStatus) {
|
2022-01-10 18:43:13 +01:00
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.ctx.Err() != nil {
|
|
|
|
return
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Tracef("ICE connection state changed to %s", newState)
|
2023-07-26 14:00:47 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// switch back to relay connection
|
|
|
|
if conn.endpointRelay != nil && conn.currentConnPriority != connPriorityRelay {
|
|
|
|
conn.log.Debugf("ICE disconnected, set Relay to active connection")
|
2024-09-12 19:18:02 +02:00
|
|
|
conn.workerRelay.EnableWgWatcher(conn.ctx)
|
2024-09-08 12:06:14 +02:00
|
|
|
err := conn.configureWGEndpoint(conn.endpointRelay)
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Errorf("failed to switch to relay conn: %v", err)
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.currentConnPriority = connPriorityRelay
|
2024-04-08 18:56:52 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
changed := conn.statusICE != newState && newState != StatusConnecting
|
|
|
|
conn.statusICE = newState
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
select {
|
|
|
|
case conn.iCEDisconnected <- changed:
|
|
|
|
default:
|
2024-01-08 12:25:35 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 14:00:47 +02:00
|
|
|
peerState := State{
|
|
|
|
PubKey: conn.config.Key,
|
2024-09-08 12:06:14 +02:00
|
|
|
ConnStatus: conn.evalStatus(),
|
|
|
|
Relayed: conn.isRelayed(),
|
2023-07-26 14:00:47 +02:00
|
|
|
ConnStatusUpdate: time.Now(),
|
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
err := conn.statusRecorder.UpdatePeerICEStateToDisconnected(peerState)
|
2022-07-02 12:02:17 +02:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Warnf("unable to set peer's state to disconnected ice, got error: %v", err)
|
2024-01-22 12:20:24 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) relayConnectionIsReady(rci RelayConnInfo) {
|
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
2022-07-02 12:02:17 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.ctx.Err() != nil {
|
|
|
|
return
|
2023-07-26 14:00:47 +02:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
conn.log.Debugf("Relay connection is ready to use")
|
|
|
|
conn.statusRelay = StatusConnected
|
|
|
|
|
|
|
|
wgProxy := conn.wgProxyFactory.GetProxy(conn.ctx)
|
|
|
|
endpoint, err := wgProxy.AddTurnConn(rci.relayedConn)
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Errorf("failed to add relayed net.Conn to local proxy: %v", err)
|
|
|
|
return
|
2023-07-26 14:00:47 +02:00
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
endpointUdpAddr, _ := net.ResolveUDPAddr(endpoint.Network(), endpoint.String())
|
|
|
|
conn.endpointRelay = endpointUdpAddr
|
|
|
|
conn.log.Debugf("conn resolved IP for %s: %s", endpoint, endpointUdpAddr.IP)
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
defer conn.updateRelayStatus(rci.relayedConn.RemoteAddr().String(), rci.rosenpassPubKey)
|
2024-01-08 12:25:35 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.currentConnPriority > connPriorityRelay {
|
|
|
|
if conn.statusICE == StatusConnected {
|
|
|
|
log.Debugf("do not switch to relay because current priority is: %v", conn.currentConnPriority)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-01-08 12:25:35 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.connIDRelay = nbnet.GenerateConnID()
|
|
|
|
for _, hook := range conn.beforeAddPeerHooks {
|
|
|
|
if err := hook(conn.connIDRelay, endpointUdpAddr.IP); err != nil {
|
|
|
|
conn.log.Errorf("Before add peer hook failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-12 19:18:02 +02:00
|
|
|
conn.workerRelay.EnableWgWatcher(conn.ctx)
|
2024-09-08 12:06:14 +02:00
|
|
|
err = conn.configureWGEndpoint(endpointUdpAddr)
|
|
|
|
if err != nil {
|
|
|
|
if err := wgProxy.CloseConn(); err != nil {
|
|
|
|
conn.log.Warnf("Failed to close relay connection: %v", err)
|
|
|
|
}
|
|
|
|
conn.log.Errorf("Failed to update wg peer configuration: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wgConfigWorkaround()
|
|
|
|
|
|
|
|
if conn.wgProxyRelay != nil {
|
|
|
|
if err := conn.wgProxyRelay.CloseConn(); err != nil {
|
|
|
|
conn.log.Warnf("failed to close deprecated wg proxy conn: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conn.wgProxyRelay = wgProxy
|
|
|
|
conn.currentConnPriority = connPriorityRelay
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Infof("start to communicate with peer via relay")
|
|
|
|
conn.doOnConnected(rci.rosenpassPubKey, rci.rosenpassAddr)
|
2023-03-16 16:46:17 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) onWorkerRelayStateDisconnected() {
|
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
|
|
|
|
|
|
|
if conn.ctx.Err() != nil {
|
2024-06-04 17:04:50 +02:00
|
|
|
return
|
|
|
|
}
|
2024-02-08 16:50:37 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.wgProxyRelay != nil {
|
|
|
|
log.Debugf("relayed connection is closed, clean up WireGuard config")
|
|
|
|
err := conn.config.WgConfig.WgInterface.RemovePeer(conn.config.WgConfig.RemoteKey)
|
2024-06-04 17:04:50 +02:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Errorf("failed to remove wg endpoint: %v", err)
|
2024-06-04 17:04:50 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.endpointRelay = nil
|
|
|
|
_ = conn.wgProxyRelay.CloseConn()
|
|
|
|
conn.wgProxyRelay = nil
|
2024-06-04 17:04:50 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
changed := conn.statusRelay != StatusDisconnected
|
|
|
|
conn.statusRelay = StatusDisconnected
|
|
|
|
|
|
|
|
select {
|
|
|
|
case conn.relayDisconnected <- changed:
|
|
|
|
default:
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-06-04 17:04:50 +02:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
peerState := State{
|
|
|
|
PubKey: conn.config.Key,
|
|
|
|
ConnStatus: conn.evalStatus(),
|
|
|
|
Relayed: conn.isRelayed(),
|
|
|
|
ConnStatusUpdate: time.Now(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := conn.statusRecorder.UpdatePeerRelayedStateToDisconnected(peerState)
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Warnf("unable to save peer's state to Relay disconnected, got error: %v", err)
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) configureWGEndpoint(addr *net.UDPAddr) error {
|
|
|
|
return conn.config.WgConfig.WgInterface.UpdatePeer(
|
|
|
|
conn.config.WgConfig.RemoteKey,
|
|
|
|
conn.config.WgConfig.AllowedIps,
|
|
|
|
defaultWgKeepAlive,
|
|
|
|
addr,
|
|
|
|
conn.config.WgConfig.PreSharedKey,
|
|
|
|
)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) updateRelayStatus(relayServerAddr string, rosenpassPubKey []byte) {
|
|
|
|
peerState := State{
|
|
|
|
PubKey: conn.config.Key,
|
|
|
|
ConnStatusUpdate: time.Now(),
|
|
|
|
ConnStatus: conn.evalStatus(),
|
|
|
|
Relayed: conn.isRelayed(),
|
|
|
|
RelayServerAddress: relayServerAddr,
|
|
|
|
RosenpassEnabled: isRosenpassEnabled(rosenpassPubKey),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := conn.statusRecorder.UpdatePeerRelayedState(peerState)
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Warnf("unable to save peer's Relay state, got error: %v", err)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) updateIceState(iceConnInfo ICEConnInfo) {
|
|
|
|
peerState := State{
|
|
|
|
PubKey: conn.config.Key,
|
|
|
|
ConnStatusUpdate: time.Now(),
|
|
|
|
ConnStatus: conn.evalStatus(),
|
|
|
|
Relayed: iceConnInfo.Relayed,
|
|
|
|
LocalIceCandidateType: iceConnInfo.LocalIceCandidateType,
|
|
|
|
RemoteIceCandidateType: iceConnInfo.RemoteIceCandidateType,
|
|
|
|
LocalIceCandidateEndpoint: iceConnInfo.LocalIceCandidateEndpoint,
|
|
|
|
RemoteIceCandidateEndpoint: iceConnInfo.RemoteIceCandidateEndpoint,
|
|
|
|
RosenpassEnabled: isRosenpassEnabled(iceConnInfo.RosenpassPubKey),
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
err := conn.statusRecorder.UpdatePeerICEState(peerState)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.log.Warnf("unable to save peer's ICE state, got error: %v", err)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) setStatusToDisconnected() {
|
|
|
|
conn.statusRelay = StatusDisconnected
|
|
|
|
conn.statusICE = StatusDisconnected
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
peerState := State{
|
|
|
|
PubKey: conn.config.Key,
|
|
|
|
ConnStatus: StatusDisconnected,
|
|
|
|
ConnStatusUpdate: time.Now(),
|
|
|
|
Mux: new(sync.RWMutex),
|
|
|
|
}
|
|
|
|
err := conn.statusRecorder.UpdatePeerState(peerState)
|
2022-01-10 18:43:13 +01:00
|
|
|
if err != nil {
|
2024-09-08 12:06:14 +02:00
|
|
|
// pretty common error because by that time Engine can already remove the peer and status won't be available.
|
|
|
|
// todo rethink status updates
|
|
|
|
conn.log.Debugf("error while updating peer's state, err: %v", err)
|
|
|
|
}
|
|
|
|
if err := conn.statusRecorder.UpdateWireGuardPeerState(conn.config.Key, iface.WGStats{}); err != nil {
|
|
|
|
conn.log.Debugf("failed to reset wireguard stats for peer: %s", err)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) doOnConnected(remoteRosenpassPubKey []byte, remoteRosenpassAddr string) {
|
|
|
|
if runtime.GOOS == "ios" {
|
|
|
|
runtime.GC()
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.onConnected != nil {
|
|
|
|
conn.onConnected(conn.config.Key, remoteRosenpassPubKey, conn.allowedIPsIP, remoteRosenpassAddr)
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) waitInitialRandomSleepTime() {
|
|
|
|
minWait := 100
|
|
|
|
maxWait := 800
|
|
|
|
duration := time.Duration(rand.Intn(maxWait-minWait)+minWait) * time.Millisecond
|
|
|
|
|
|
|
|
timeout := time.NewTimer(duration)
|
|
|
|
defer timeout.Stop()
|
|
|
|
|
2022-01-10 18:43:13 +01:00
|
|
|
select {
|
2024-09-08 12:06:14 +02:00
|
|
|
case <-conn.ctx.Done():
|
|
|
|
case <-timeout.C:
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) isRelayed() bool {
|
|
|
|
if conn.statusRelay == StatusDisconnected && (conn.statusICE == StatusDisconnected || conn.statusICE == StatusConnecting) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if conn.currentConnPriority == connPriorityICEP2P {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) evalStatus() ConnStatus {
|
|
|
|
if conn.statusRelay == StatusConnected || conn.statusICE == StatusConnected {
|
|
|
|
return StatusConnected
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.statusRelay == StatusConnecting || conn.statusICE == StatusConnecting {
|
|
|
|
return StatusConnecting
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
|
|
|
|
return StatusDisconnected
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) isConnected() bool {
|
|
|
|
conn.mu.Lock()
|
|
|
|
defer conn.mu.Unlock()
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.statusICE != StatusConnected && conn.statusICE != StatusConnecting {
|
2022-01-21 13:52:19 +01:00
|
|
|
return false
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.workerRelay.IsRelayConnectionSupportedWithPeer() {
|
|
|
|
if conn.statusRelay != StatusConnected {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (conn *Conn) freeUpConnID() {
|
|
|
|
if conn.connIDRelay != "" {
|
|
|
|
for _, hook := range conn.afterRemovePeerHooks {
|
|
|
|
if err := hook(conn.connIDRelay); err != nil {
|
|
|
|
conn.log.Errorf("After remove peer hook failed: %v", err)
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.connIDRelay = ""
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
if conn.connIDICE != "" {
|
|
|
|
for _, hook := range conn.afterRemovePeerHooks {
|
|
|
|
if err := hook(conn.connIDICE); err != nil {
|
|
|
|
conn.log.Errorf("After remove peer hook failed: %v", err)
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2024-09-08 12:06:14 +02:00
|
|
|
conn.connIDICE = ""
|
|
|
|
}
|
2022-01-10 18:43:13 +01:00
|
|
|
}
|
2022-01-18 16:44:58 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func (conn *Conn) getEndpointForICEConnInfo(iceConnInfo ICEConnInfo) (net.Addr, wgproxy.Proxy, error) {
|
|
|
|
if !iceConnInfo.RelayedOnLocal {
|
|
|
|
return iceConnInfo.RemoteConn.RemoteAddr(), nil, nil
|
|
|
|
}
|
|
|
|
conn.log.Debugf("setup ice turn connection")
|
|
|
|
wgProxy := conn.wgProxyFactory.GetProxy(conn.ctx)
|
|
|
|
ep, err := wgProxy.AddTurnConn(iceConnInfo.RemoteConn)
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Errorf("failed to add turn net.Conn to local proxy: %v", err)
|
|
|
|
err = wgProxy.CloseConn()
|
|
|
|
if err != nil {
|
|
|
|
conn.log.Warnf("failed to close turn proxy connection: %v", err)
|
|
|
|
}
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return ep, wgProxy, nil
|
2022-01-18 16:44:58 +01:00
|
|
|
}
|
2023-03-16 16:46:17 +01:00
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
func isRosenpassEnabled(remoteRosenpassPubKey []byte) bool {
|
|
|
|
return remoteRosenpassPubKey != nil
|
2024-06-04 17:04:50 +02:00
|
|
|
}
|
|
|
|
|
2024-09-08 12:06:14 +02:00
|
|
|
// wgConfigWorkaround is a workaround for the issue with WireGuard configuration update
|
|
|
|
// When update a peer configuration in near to each other time, the second update can be ignored by WireGuard
|
|
|
|
func wgConfigWorkaround() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2023-03-16 16:46:17 +01:00
|
|
|
}
|