chore: simplify direct connection logic

This commit is contained in:
braginini 2021-09-09 16:43:52 +02:00
parent 13288374f1
commit 528a26ea3e

View File

@ -189,14 +189,7 @@ func (conn *Connection) Open(timeout time.Duration) error {
// in case the remote peer is in the local network or one of the peers has public static IP -> no need for a Wireguard proxy, direct communication is possible. // in case the remote peer is in the local network or one of the peers has public static IP -> no need for a Wireguard proxy, direct communication is possible.
if !useProxy(pair) { if !useProxy(pair) {
log.Debugf("it is possible to establish a direct connection (without proxy) to peer %s - my addr: %s, remote addr: %s", conn.Config.RemoteWgKey.String(), pair.Local, pair.Remote) log.Debugf("it is possible to establish a direct connection (without proxy) to peer %s - my addr: %s, remote addr: %s", conn.Config.RemoteWgKey.String(), pair.Local, pair.Remote)
var endpoint string err = conn.wgProxy.StartLocal(fmt.Sprintf("%s:%d", pair.Remote.Address(), iface.WgPort))
if isPublicIP(net.ParseIP(pair.Local.Address())) {
//skip endpoint because we are public - it will be discovered by Wireguard automatically
endpoint = ""
} else {
endpoint = fmt.Sprintf("%s:%d", pair.Remote.Address(), iface.WgPort)
}
err = conn.wgProxy.StartLocal(endpoint)
if err != nil { if err != nil {
return err return err
} }
@ -254,24 +247,22 @@ func useProxy(pair *ice.CandidatePair) bool {
myIp := net.ParseIP(pair.Local.Address()) myIp := net.ParseIP(pair.Local.Address())
remoteIsPublic := isPublicIP(remoteIP) remoteIsPublic := isPublicIP(remoteIP)
myIsPublic := isPublicIP(myIp) myIsPublic := isPublicIP(myIp)
if pair.Local.Type() == ice.CandidateTypeHost && pair.Remote.Type() == ice.CandidateTypeHost {
if remoteIsPublic || myIsPublic {
//one of the hosts has a public IP
return false
}
//one of the hosts has a public IP
if remoteIsPublic && pair.Remote.Type() == ice.CandidateTypeHost {
return false
}
if myIsPublic && pair.Local.Type() == ice.CandidateTypeHost {
return false
}
if pair.Local.Type() == ice.CandidateTypeHost && pair.Remote.Type() == ice.CandidateTypeHost {
if !remoteIsPublic && !myIsPublic { if !remoteIsPublic && !myIsPublic {
//both hosts are in the same private network //both hosts are in the same private network
return false return false
} }
} }
if (pair.Local.Type() == ice.CandidateTypeHost && myIsPublic) && pair.Remote.Type() == ice.CandidateTypePeerReflexive {
// same as the case when either host is public but adds additional case when remote is peer reflexive
// remote is peer reflexive and we are public -> no proxy needed
return false
}
return true return true
} }