mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-26 12:16:39 +00:00
- Implement remote addr for conn
- Eliminate cached offeranswer arguments - Fix exponent reset in conn reconnect loop - Fix on disconnected callback for permanent server - Add peer relay status for status details command
This commit is contained in:
13
relay/client/addr.go
Normal file
13
relay/client/addr.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package client
|
||||
|
||||
type RelayAddr struct {
|
||||
addr string
|
||||
}
|
||||
|
||||
func (a RelayAddr) Network() string {
|
||||
return "relay"
|
||||
}
|
||||
|
||||
func (a RelayAddr) String() string {
|
||||
return a.addr
|
||||
}
|
||||
@@ -110,7 +110,7 @@ type Client struct {
|
||||
mu sync.Mutex // protect serviceIsRunning and conns
|
||||
readLoopMutex sync.Mutex
|
||||
wgReadLoop sync.WaitGroup
|
||||
instanceURL string
|
||||
instanceURL *RelayAddr
|
||||
muInstanceURL sync.Mutex
|
||||
|
||||
onDisconnectListener func()
|
||||
@@ -183,7 +183,7 @@ func (c *Client) OpenConn(dstPeerID string) (net.Conn, error) {
|
||||
|
||||
log.Infof("open connection to peer: %s", hashedStringID)
|
||||
msgChannel := make(chan Msg, 2)
|
||||
conn := NewConn(c, hashedID, hashedStringID, msgChannel)
|
||||
conn := NewConn(c, hashedID, hashedStringID, msgChannel, c.instanceURL)
|
||||
|
||||
c.conns[hashedStringID] = newConnContainer(conn, msgChannel)
|
||||
return conn, nil
|
||||
@@ -193,10 +193,10 @@ func (c *Client) OpenConn(dstPeerID string) (net.Conn, error) {
|
||||
func (c *Client) ServerInstanceURL() (string, error) {
|
||||
c.muInstanceURL.Lock()
|
||||
defer c.muInstanceURL.Unlock()
|
||||
if c.instanceURL == "" {
|
||||
if c.instanceURL == nil {
|
||||
return "", fmt.Errorf("relay connection is not established")
|
||||
}
|
||||
return c.instanceURL, nil
|
||||
return c.instanceURL.String(), nil
|
||||
}
|
||||
|
||||
// SetOnDisconnectListener sets a function that will be called when the connection to the relay server is closed.
|
||||
@@ -274,7 +274,7 @@ func (c *Client) handShake() error {
|
||||
return err
|
||||
}
|
||||
c.muInstanceURL.Lock()
|
||||
c.instanceURL = ia
|
||||
c.instanceURL = &RelayAddr{addr: ia}
|
||||
c.muInstanceURL.Unlock()
|
||||
return nil
|
||||
}
|
||||
@@ -315,7 +315,7 @@ func (c *Client) readLoop(relayConn net.Conn) {
|
||||
hc.Stop()
|
||||
|
||||
c.muInstanceURL.Lock()
|
||||
c.instanceURL = ""
|
||||
c.instanceURL = nil
|
||||
c.muInstanceURL.Unlock()
|
||||
|
||||
c.notifyDisconnected()
|
||||
|
||||
@@ -11,14 +11,16 @@ type Conn struct {
|
||||
dstID []byte
|
||||
dstStringID string
|
||||
messageChan chan Msg
|
||||
instanceURL *RelayAddr
|
||||
}
|
||||
|
||||
func NewConn(client *Client, dstID []byte, dstStringID string, messageChan chan Msg) *Conn {
|
||||
func NewConn(client *Client, dstID []byte, dstStringID string, messageChan chan Msg, instanceURL *RelayAddr) *Conn {
|
||||
c := &Conn{
|
||||
client: client,
|
||||
dstID: dstID,
|
||||
dstStringID: dstStringID,
|
||||
messageChan: messageChan,
|
||||
instanceURL: instanceURL,
|
||||
}
|
||||
|
||||
return c
|
||||
@@ -48,7 +50,7 @@ func (c *Conn) LocalAddr() net.Addr {
|
||||
}
|
||||
|
||||
func (c *Conn) RemoteAddr() net.Addr {
|
||||
return c.client.relayConn.RemoteAddr()
|
||||
return c.instanceURL
|
||||
}
|
||||
|
||||
func (c *Conn) SetDeadline(t time.Time) error {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ws
|
||||
|
||||
type WebsocketAddr struct {
|
||||
addr string
|
||||
}
|
||||
|
||||
func (a WebsocketAddr) Network() string {
|
||||
@@ -8,5 +9,5 @@ func (a WebsocketAddr) Network() string {
|
||||
}
|
||||
|
||||
func (a WebsocketAddr) String() string {
|
||||
return "websocket/unknown-addr"
|
||||
return a.addr
|
||||
}
|
||||
|
||||
@@ -12,12 +12,14 @@ import (
|
||||
type Conn struct {
|
||||
ctx context.Context
|
||||
*websocket.Conn
|
||||
remoteAddr WebsocketAddr
|
||||
}
|
||||
|
||||
func NewConn(wsConn *websocket.Conn) net.Conn {
|
||||
func NewConn(wsConn *websocket.Conn, serverAddress string) net.Conn {
|
||||
return &Conn{
|
||||
ctx: context.Background(),
|
||||
Conn: wsConn,
|
||||
ctx: context.Background(),
|
||||
Conn: wsConn,
|
||||
remoteAddr: WebsocketAddr{serverAddress},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,11 +42,11 @@ func (c *Conn) Write(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (c *Conn) RemoteAddr() net.Addr {
|
||||
return WebsocketAddr{}
|
||||
return c.remoteAddr
|
||||
}
|
||||
|
||||
func (c *Conn) LocalAddr() net.Addr {
|
||||
return WebsocketAddr{}
|
||||
return WebsocketAddr{addr: "unknown"}
|
||||
}
|
||||
|
||||
func (c *Conn) SetReadDeadline(t time.Time) error {
|
||||
|
||||
@@ -32,7 +32,7 @@ func Dial(address string) (net.Conn, error) {
|
||||
_ = resp.Body.Close()
|
||||
}
|
||||
|
||||
conn := NewConn(wsConn)
|
||||
conn := NewConn(wsConn, address)
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ func NewGuard(context context.Context, relayClient *Client) *Guard {
|
||||
}
|
||||
|
||||
func (g *Guard) OnDisconnected() {
|
||||
// todo prevent multiple reconnect
|
||||
ticker := time.NewTicker(reconnectingTimeout)
|
||||
defer ticker.Stop()
|
||||
|
||||
|
||||
@@ -108,13 +108,20 @@ func (m *Manager) OpenConn(serverAddress, peerKey string, onClosedListener func(
|
||||
log.Debugf("open peer connection via foreign server: %s", serverAddress)
|
||||
netConn, err = m.openConnVia(serverAddress, peerKey)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if onClosedListener != nil {
|
||||
m.addListener(serverAddress, onClosedListener)
|
||||
var listenerAddr string
|
||||
if foreign {
|
||||
m.addListener(serverAddress, onClosedListener)
|
||||
listenerAddr = serverAddress
|
||||
} else {
|
||||
listenerAddr = m.serverURL
|
||||
}
|
||||
m.addListener(listenerAddr, onClosedListener)
|
||||
|
||||
}
|
||||
|
||||
return netConn, err
|
||||
@@ -196,7 +203,7 @@ func (m *Manager) openConnVia(serverAddress, peerKey string) (net.Conn, error) {
|
||||
|
||||
func (m *Manager) onServerDisconnected(serverAddress string) {
|
||||
if serverAddress == m.serverURL {
|
||||
m.reconnectGuard.OnDisconnected()
|
||||
go m.reconnectGuard.OnDisconnected()
|
||||
}
|
||||
|
||||
m.notifyOnDisconnectListeners(serverAddress)
|
||||
@@ -251,17 +258,19 @@ func (m *Manager) cleanUpUnusedRelays() {
|
||||
|
||||
func (m *Manager) addListener(serverAddress string, onClosedListener func()) {
|
||||
m.listenerLock.Lock()
|
||||
defer m.listenerLock.Unlock()
|
||||
l, ok := m.onDisconnectedListeners[serverAddress]
|
||||
if !ok {
|
||||
l = make(map[*func()]struct{})
|
||||
}
|
||||
l[&onClosedListener] = struct{}{}
|
||||
m.onDisconnectedListeners[serverAddress] = l
|
||||
m.listenerLock.Unlock()
|
||||
}
|
||||
|
||||
func (m *Manager) notifyOnDisconnectListeners(serverAddress string) {
|
||||
m.listenerLock.Lock()
|
||||
defer m.listenerLock.Unlock()
|
||||
|
||||
l, ok := m.onDisconnectedListeners[serverAddress]
|
||||
if !ok {
|
||||
return
|
||||
@@ -270,6 +279,4 @@ func (m *Manager) notifyOnDisconnectListeners(serverAddress string) {
|
||||
go (*f)()
|
||||
}
|
||||
delete(m.onDisconnectedListeners, serverAddress)
|
||||
m.listenerLock.Unlock()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user