feat: add timeout while initiating conenction

This commit is contained in:
braginini
2021-04-19 22:56:01 +02:00
parent e71c623e33
commit ac871a5306
2 changed files with 16 additions and 10 deletions

View File

@@ -53,7 +53,7 @@ type Connection struct {
// remoteAuthChannel is a channel used to wait for remote credentials to proceed with the connection // remoteAuthChannel is a channel used to wait for remote credentials to proceed with the connection
remoteAuthChannel chan IceCredentials remoteAuthChannel chan IceCredentials
closeChannel chan bool closeChannel chan struct{}
closedChannel chan struct{} closedChannel chan struct{}
// agent is an actual ice.Agent that is used to negotiate and maintain a connection to a remote peer // agent is an actual ice.Agent that is used to negotiate and maintain a connection to a remote peer
@@ -78,7 +78,7 @@ func NewConnection(config ConnConfig,
signalOffer: signalOffer, signalOffer: signalOffer,
signalAnswer: signalAnswer, signalAnswer: signalAnswer,
remoteAuthChannel: make(chan IceCredentials, 1), remoteAuthChannel: make(chan IceCredentials, 1),
closeChannel: make(chan bool, 2), closeChannel: make(chan struct{}),
closedChannel: make(chan struct{}), closedChannel: make(chan struct{}),
agent: nil, agent: nil,
isActive: false, isActive: false,
@@ -108,8 +108,7 @@ func (conn *Connection) Close() error {
log.Debugf("closing connection to peer %s", conn.Config.RemoteWgKey.String()) log.Debugf("closing connection to peer %s", conn.Config.RemoteWgKey.String())
conn.closeChannel <- true close(conn.closeChannel)
conn.closeChannel <- true
log.Debugf("closed connection to peer %s", conn.Config.RemoteWgKey.String()) log.Debugf("closed connection to peer %s", conn.Config.RemoteWgKey.String())
@@ -364,8 +363,10 @@ func (conn *Connection) proxyToRemotePeer(wgConn net.Conn, remoteConn *ice.Conn)
if err != nil { if err != nil {
log.Warnln("Error writing to remote peer: ", err.Error()) log.Warnln("Error writing to remote peer: ", err.Error())
} }
case <-conn.closeChannel: case _, ok := <-conn.closeChannel:
log.Infof("stopped proxying to remote peer %s", conn.Config.RemoteWgKey.String()) if !ok {
log.Infof("stopped proxying to remote peer %s", conn.Config.RemoteWgKey.String())
}
return return
} }
} }
@@ -388,8 +389,10 @@ func (conn *Connection) proxyToLocalWireguard(wgConn net.Conn, remoteConn *ice.C
if err != nil { if err != nil {
log.Errorf("failed writing to local Wireguard instance %s", err) log.Errorf("failed writing to local Wireguard instance %s", err)
} }
case <-conn.closeChannel: case _, ok := <-conn.closeChannel:
log.Infof("stopped proxying from remote peer %s", conn.Config.RemoteWgKey.String()) if !ok {
log.Infof("stopped proxying from remote peer %s", conn.Config.RemoteWgKey.String())
}
return return
} }
} }

View File

@@ -77,14 +77,16 @@ func (e *Engine) Start(privateKey string, peers []Peer) error {
go func() { go func() {
operation := func() error { operation := func() error {
_, closed, err := e.openConnection(*wgPort, myKey, peer) _, closed, err := e.openPeerConnection(*wgPort, myKey, peer)
if err != nil { if err != nil {
e.conns[peer.WgPubKey] = nil
return err return err
} }
select { select {
case _, ok := <-closed: case _, ok := <-closed:
if !ok { if !ok {
e.conns[peer.WgPubKey] = nil
return fmt.Errorf("connection to peer %s has been closed", peer.WgPubKey) return fmt.Errorf("connection to peer %s has been closed", peer.WgPubKey)
} }
return nil return nil
@@ -103,7 +105,8 @@ func (e *Engine) Start(privateKey string, peers []Peer) error {
return nil return nil
} }
func (e *Engine) openConnection(wgPort int, myKey wgtypes.Key, peer Peer) (*Connection, chan struct{}, error) { func (e *Engine) openPeerConnection(wgPort int, myKey wgtypes.Key, peer Peer) (*Connection, chan struct{}, error) {
remoteKey, _ := wgtypes.ParseKey(peer.WgPubKey) remoteKey, _ := wgtypes.ParseKey(peer.WgPubKey)
connConfig := &ConnConfig{ connConfig := &ConnConfig{
WgListenAddr: fmt.Sprintf("127.0.0.1:%d", wgPort), WgListenAddr: fmt.Sprintf("127.0.0.1:%d", wgPort),