From db5f9313733660b82ad00d3395f32a9bbde8cc87 Mon Sep 17 00:00:00 2001 From: braginini Date: Sat, 8 Apr 2023 17:22:53 +0200 Subject: [PATCH 1/4] use direct mode only when in the same private /16 network --- client/internal/peer/conn.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/client/internal/peer/conn.go b/client/internal/peer/conn.go index de56bf319..eb7ff9db4 100644 --- a/client/internal/peer/conn.go +++ b/client/internal/peer/conn.go @@ -340,13 +340,33 @@ func shouldUseProxy(pair *ice.CandidatePair, userspaceBind bool) bool { return false } - if isHostCandidateWithPrivateIP(pair.Local) && isHostCandidateWithPrivateIP(pair.Remote) { + if isHostCandidateWithPrivateIP(pair.Local) && isHostCandidateWithPrivateIP(pair.Remote) && isSameNetworkPrefix(pair) { return false } return true } +func isSameNetworkPrefix(pair *ice.CandidatePair) bool { + + localIPStr, _, err := net.SplitHostPort(pair.Local.Address()) + if err != nil { + return false + } + remoteIPStr, _, err := net.SplitHostPort(pair.Remote.Address()) + if err != nil { + return false + } + localIP := net.ParseIP(localIPStr) + remoteIP := net.ParseIP(remoteIPStr) + if localIP == nil || remoteIP == nil { + return false + } + // only consider /16 networks + mask := net.IPMask{255, 255, 0, 0} + return localIP.Mask(mask).Equal(remoteIP.Mask(mask)) +} + func isRelayCandidate(candidate ice.Candidate) bool { return candidate.Type() == ice.CandidateTypeRelay } From 189321f09d62f85e5363c8491f705f4bf53bff51 Mon Sep 17 00:00:00 2001 From: braginini Date: Sat, 8 Apr 2023 17:37:48 +0200 Subject: [PATCH 2/4] Only consider /16 network when using direct mode --- client/internal/peer/conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/internal/peer/conn.go b/client/internal/peer/conn.go index eb7ff9db4..81f7457f1 100644 --- a/client/internal/peer/conn.go +++ b/client/internal/peer/conn.go @@ -340,7 +340,7 @@ func shouldUseProxy(pair *ice.CandidatePair, userspaceBind bool) bool { return false } - if isHostCandidateWithPrivateIP(pair.Local) && isHostCandidateWithPrivateIP(pair.Remote) && isSameNetworkPrefix(pair) { + if isHostCandidateWithPrivateIP(pair.Local) && isHostCandidateWithPrivateIP(pair.Remote) && !isSameNetworkPrefix(pair) { return false } From 1c071e4981865d583f88e15f67e687ba1948d975 Mon Sep 17 00:00:00 2001 From: braginini Date: Sat, 8 Apr 2023 17:38:13 +0200 Subject: [PATCH 3/4] Revert "Only consider /16 network when using direct mode" This reverts commit 189321f09d62f85e5363c8491f705f4bf53bff51. --- client/internal/peer/conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/internal/peer/conn.go b/client/internal/peer/conn.go index 81f7457f1..eb7ff9db4 100644 --- a/client/internal/peer/conn.go +++ b/client/internal/peer/conn.go @@ -340,7 +340,7 @@ func shouldUseProxy(pair *ice.CandidatePair, userspaceBind bool) bool { return false } - if isHostCandidateWithPrivateIP(pair.Local) && isHostCandidateWithPrivateIP(pair.Remote) && !isSameNetworkPrefix(pair) { + if isHostCandidateWithPrivateIP(pair.Local) && isHostCandidateWithPrivateIP(pair.Remote) && isSameNetworkPrefix(pair) { return false } From a3849b978b4a65d0a1b18416727a1746ed15698b Mon Sep 17 00:00:00 2001 From: braginini Date: Sat, 8 Apr 2023 17:39:38 +0200 Subject: [PATCH 4/4] Fix shoulduseproxy tests --- client/internal/peer/conn_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/internal/peer/conn_test.go b/client/internal/peer/conn_test.go index 107852c51..957ec8719 100644 --- a/client/internal/peer/conn_test.go +++ b/client/internal/peer/conn_test.go @@ -203,12 +203,13 @@ func TestConn_ShouldUseProxy(t *testing.T) { } privateHostCandidate := &mockICECandidate{ AddressFunc: func() string { - return "10.0.0.1" + return "10.0.0.1:44576" }, TypeFunc: func() ice.CandidateType { return ice.CandidateTypeHost }, } + srflxCandidate := &mockICECandidate{ AddressFunc: func() string { return "1.1.1.1"