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 { type IceCredentials struct {
uFrag string uFrag string
pwd string pwd string
isControlling bool //todo think of better solution??
} }
type Connection struct { type Connection struct {
@ -57,8 +56,6 @@ type Connection struct {
agent *ice.Agent agent *ice.Agent
wgConn net.Conn wgConn net.Conn
// isActive indicates whether connection is active or not.
isActive bool
connected *Cond connected *Cond
closeCond *Cond closeCond *Cond
@ -81,7 +78,6 @@ func NewConnection(config ConnConfig,
closeCond: NewCond(), closeCond: NewCond(),
connected: NewCond(), connected: NewCond(),
agent: nil, agent: nil,
isActive: false,
} }
} }
@ -128,7 +124,8 @@ func (conn *Connection) Open(timeout time.Duration) error {
return err 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 { if err != nil {
log.Errorf("failed establishing connection with the remote peer %s %s", conn.Config.RemoteWgKey.String(), err) log.Errorf("failed establishing connection with the remote peer %s %s", conn.Config.RemoteWgKey.String(), err)
return err return err
@ -144,8 +141,6 @@ func (conn *Connection) Open(timeout time.Duration) error {
go conn.proxyToLocalWireguard(*wgConn, remoteConn) go conn.proxyToLocalWireguard(*wgConn, remoteConn)
log.Infof("opened connection to peer %s", conn.Config.RemoteWgKey.String()) log.Infof("opened connection to peer %s", conn.Config.RemoteWgKey.String())
conn.isActive = true
case <-time.After(timeout): case <-time.After(timeout):
err := conn.Close() err := conn.Close()
if err != nil { 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) conn := NewConnection(*connConfig, signalCandidate, signalOffer, signalAnswer)
e.conns[remoteKey.String()] = conn e.conns[remoteKey.String()] = conn
// blocks until the connection is open (or timeout) // blocks until the connection is open (or timeout)
err := conn.Open(20 * time.Second) err := conn.Open(60 * time.Second)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -196,9 +196,8 @@ func (e *Engine) receiveSignal(localKey string) {
return err return err
} }
err = conn.OnOffer(IceCredentials{ err = conn.OnOffer(IceCredentials{
uFrag: remoteCred.UFrag, uFrag: remoteCred.UFrag,
pwd: remoteCred.Pwd, pwd: remoteCred.Pwd,
isControlling: false,
}) })
if err != nil { if err != nil {
@ -212,9 +211,8 @@ func (e *Engine) receiveSignal(localKey string) {
return err return err
} }
err = conn.OnAnswer(IceCredentials{ err = conn.OnAnswer(IceCredentials{
uFrag: remoteCred.UFrag, uFrag: remoteCred.UFrag,
pwd: remoteCred.Pwd, pwd: remoteCred.Pwd,
isControlling: true,
}) })
if err != nil { if err != nil {