chore: init Wireguard connection of a peer on creation

This commit is contained in:
braginini
2021-04-15 21:20:12 +02:00
parent d3454eb0f9
commit 4ba9c958b4
2 changed files with 17 additions and 20 deletions

View File

@@ -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},
@@ -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,
}
@@ -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
}

View File

@@ -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
}
@@ -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)
}
}()