Skip to content

Commit

Permalink
chore: init Wireguard connection of a peer on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini committed Apr 15, 2021
1 parent d3454eb commit 4ba9c95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
30 changes: 14 additions & 16 deletions engine/agent.go
Expand Up @@ -36,6 +36,18 @@ type PeerAgent struct {
func NewPeerAgent(localKey string, remoteKey string, stunTurnURLS []*ice.URL, wgAddr string, signal *signal.Client,
wgIface string) (*PeerAgent, error) {

// connect to local Wireguard instance
wgConn, err := net.Dial("udp", wgAddr)
if err != nil {
log.Fatalf("failed dialing to local Wireguard port %s", err)
return nil, err
}
// add local proxy connection as a Wireguard peer
err = iface.UpdatePeer(wgIface, remoteKey, "0.0.0.0/0", 15*time.Second, wgConn.LocalAddr().String())
if err != nil {
log.Errorf("error while configuring Wireguard peer [%s] %s", remoteKey, err.Error())
}

// init ICE Agent
iceAgent, err := ice.NewAgent(&ice.AgentConfig{
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4},
Expand All @@ -51,7 +63,7 @@ func NewPeerAgent(localKey string, remoteKey string, stunTurnURLS []*ice.URL, wg
iceAgent: iceAgent,
wgAddr: wgAddr,
conn: nil,
wgConn: nil,
wgConn: wgConn,
signal: signal,
wgIface: wgIface,
}
Expand Down Expand Up @@ -120,22 +132,8 @@ func (pa *PeerAgent) proxyToLocalWireguard() {
// - proxy all incoming data from the remote peer to local Wireguard
func (pa *PeerAgent) OpenConnection(initiator bool) error {

// connect to local Wireguard instance
wgConn, err := net.Dial("udp", pa.wgAddr)
if err != nil {
log.Fatalf("failed dialing to local Wireguard port %s", err)
return err
}
pa.wgConn = wgConn

// add local proxy connection as a Wireguard peer
err = iface.UpdatePeer(pa.wgIface, pa.RemoteKey, "0.0.0.0/0", 15*time.Second, wgConn.LocalAddr().String())
if err != nil {
log.Errorf("error while configuring Wireguard peer [%s] %s", pa.RemoteKey, err.Error())
}

// start gathering candidates
err = pa.iceAgent.GatherCandidates()
err := pa.iceAgent.GatherCandidates()
if err != nil {
return err
}
Expand Down
7 changes: 3 additions & 4 deletions engine/engine.go
Expand Up @@ -113,8 +113,7 @@ func (e *Engine) receiveSignal(localKey string) {
Pwd: cred.Pwd,
}, sProto.Message_ANSWER)

//notify the remote peer of our credentials
err = peerAgent.signal.Send(answer)
err = e.signal.Send(answer)
if err != nil {
return err
}
Expand Down Expand Up @@ -154,9 +153,9 @@ func (e *Engine) handle(msg *sProto.Message, peerAgent *PeerAgent, initiator boo

go func() {

err := peerAgent.OpenConnection(initiator)
err = peerAgent.OpenConnection(initiator)
if err != nil {
log.Errorf("error opening connection ot remote peer %s", msg.Key)
log.Errorf("error opening connection to remote peer %s %s", msg.Key, err)
}
}()

Expand Down

0 comments on commit 4ba9c95

Please sign in to comment.