Fix relay mode evaulation

This commit is contained in:
Zoltán Papp 2024-06-18 11:10:17 +02:00
parent deb8203f06
commit 5b86a7f3f2
5 changed files with 17 additions and 15 deletions

View File

@ -212,7 +212,7 @@ func NewEngineWithProbes(
clientCtx: clientCtx, clientCtx: clientCtx,
clientCancel: clientCancel, clientCancel: clientCancel,
signal: signalClient, signal: signalClient,
signaler: NewSignaler(signalClient, config.WgPrivateKey), signaler: peer.NewSignaler(signalClient, config.WgPrivateKey),
mgmClient: mgmClient, mgmClient: mgmClient,
relayManager: relayManager, relayManager: relayManager,
peerConns: make(map[string]*peer.Conn), peerConns: make(map[string]*peer.Conn),

View File

@ -152,7 +152,7 @@ func (conn *Conn) Open() {
log.Warnf("error while updating the state of peer %s,err: %v", conn.config.Key, err) log.Warnf("error while updating the state of peer %s,err: %v", conn.config.Key, err)
} }
*/ */
relayIsSupportedLocally := conn.connectorRelay.RelayIsSupported() relayIsSupportedLocally := conn.connectorRelay.RelayIsSupportedLocally()
if relayIsSupportedLocally { if relayIsSupportedLocally {
go conn.connectorRelay.SetupRelayConnection() go conn.connectorRelay.SetupRelayConnection()
} }

View File

@ -97,16 +97,7 @@ func NewConnectorICE(ctx context.Context, log *log.Entry, config ConnConfig, con
// If the relay mode is supported then try to connect in p2p way only. // If the relay mode is supported then try to connect in p2p way only.
// It is trying to reconnection in a loop until the context is canceled. // It is trying to reconnection in a loop until the context is canceled.
// In case of success connection it will call the onICEConnReady callback. // In case of success connection it will call the onICEConnReady callback.
func (conn *ConnectorICE) SetupICEConnection(relayMode bool) { func (conn *ConnectorICE) SetupICEConnection(hasRelayOnLocally bool) {
var preferredCandidateTypes []ice.CandidateType
if relayMode {
conn.connPriority = connPriorityICEP2P
preferredCandidateTypes = candidateTypesP2P()
} else {
conn.connPriority = connPriorityICETurn
preferredCandidateTypes = candidateTypes()
}
for { for {
if !conn.waitForReconnectTry() { if !conn.waitForReconnectTry() {
return return
@ -120,6 +111,15 @@ func (conn *ConnectorICE) SetupICEConnection(relayMode bool) {
continue continue
} }
var preferredCandidateTypes []ice.CandidateType
if hasRelayOnLocally && remoteOfferAnswer.RelaySrvAddress != "" {
conn.connPriority = connPriorityICEP2P
preferredCandidateTypes = candidateTypesP2P()
} else {
conn.connPriority = connPriorityICETurn
preferredCandidateTypes = candidateTypes()
}
ctx, ctxCancel := context.WithCancel(conn.ctx) ctx, ctxCancel := context.WithCancel(conn.ctx)
agent, err := conn.reCreateAgent(ctxCancel, preferredCandidateTypes) agent, err := conn.reCreateAgent(ctxCancel, preferredCandidateTypes)
if err != nil { if err != nil {

View File

@ -78,6 +78,8 @@ func (conn *ConnectorRelay) SetupRelayConnection() {
rosenpassPubKey: remoteOfferAnswer.RosenpassPubKey, rosenpassPubKey: remoteOfferAnswer.RosenpassPubKey,
rosenpassAddr: remoteOfferAnswer.RosenpassAddr, rosenpassAddr: remoteOfferAnswer.RosenpassAddr,
}) })
// todo: waitForDisconnection()
} }
} }
@ -97,8 +99,8 @@ func (conn *ConnectorRelay) preferredRelayServer(myRelayAddress, remoteRelayAddr
return remoteRelayAddress return remoteRelayAddress
} }
func (conn *ConnectorRelay) RelayIsSupported() bool { func (conn *ConnectorRelay) RelayIsSupportedLocally() bool {
return conn.relayManager.IsSupported() return conn.relayManager.HasRelayAddress()
} }
// waitForReconnectTry waits for a random duration before trying to reconnect // waitForReconnectTry waits for a random duration before trying to reconnect

View File

@ -108,7 +108,7 @@ func (m *Manager) RelayAddress() (net.Addr, error) {
return m.relayClient.RelayRemoteAddress() return m.relayClient.RelayRemoteAddress()
} }
func (m *Manager) IsSupported() bool { func (m *Manager) HasRelayAddress() bool {
return m.srvAddress != "" return m.srvAddress != ""
} }