refactor: minor code cleanup

This commit is contained in:
braginini 2021-04-21 18:27:45 +02:00
parent 2a8198c409
commit dd04cba8a7
2 changed files with 9 additions and 16 deletions

View File

@ -34,9 +34,8 @@ type ConnConfig struct {
}
type IceCredentials struct {
uFrag string
pwd string
isControlling bool //todo think of better solution??
uFrag string
pwd string
}
type Connection struct {
@ -57,8 +56,6 @@ type Connection struct {
agent *ice.Agent
wgConn net.Conn
// isActive indicates whether connection is active or not.
isActive bool
connected *Cond
closeCond *Cond
@ -81,7 +78,6 @@ func NewConnection(config ConnConfig,
closeCond: NewCond(),
connected: NewCond(),
agent: nil,
isActive: false,
}
}
@ -128,7 +124,8 @@ func (conn *Connection) Open(timeout time.Duration) error {
return err
}
remoteConn, err := conn.openConnectionToRemote(remoteAuth.isControlling, remoteAuth)
isControlling := conn.Config.WgKey.PublicKey().String() > conn.Config.RemoteWgKey.String()
remoteConn, err := conn.openConnectionToRemote(isControlling, remoteAuth)
if err != nil {
log.Errorf("failed establishing connection with the remote peer %s %s", conn.Config.RemoteWgKey.String(), err)
return err
@ -144,8 +141,6 @@ func (conn *Connection) Open(timeout time.Duration) error {
go conn.proxyToLocalWireguard(*wgConn, remoteConn)
log.Infof("opened connection to peer %s", conn.Config.RemoteWgKey.String())
conn.isActive = true
case <-time.After(timeout):
err := conn.Close()
if err != nil {

View File

@ -132,7 +132,7 @@ func (e *Engine) openPeerConnection(wgPort int, myKey wgtypes.Key, peer Peer) (*
conn := NewConnection(*connConfig, signalCandidate, signalOffer, signalAnswer)
e.conns[remoteKey.String()] = conn
// blocks until the connection is open (or timeout)
err := conn.Open(20 * time.Second)
err := conn.Open(60 * time.Second)
if err != nil {
return nil, err
}
@ -196,9 +196,8 @@ func (e *Engine) receiveSignal(localKey string) {
return err
}
err = conn.OnOffer(IceCredentials{
uFrag: remoteCred.UFrag,
pwd: remoteCred.Pwd,
isControlling: false,
uFrag: remoteCred.UFrag,
pwd: remoteCred.Pwd,
})
if err != nil {
@ -212,9 +211,8 @@ func (e *Engine) receiveSignal(localKey string) {
return err
}
err = conn.OnAnswer(IceCredentials{
uFrag: remoteCred.UFrag,
pwd: remoteCred.Pwd,
isControlling: true,
uFrag: remoteCred.UFrag,
pwd: remoteCred.Pwd,
})
if err != nil {