From 387096b2a111331fa8e22cab7a35c01c43019aa5 Mon Sep 17 00:00:00 2001 From: WofWca Date: Mon, 18 Nov 2024 20:46:27 +0400 Subject: [PATCH 1/2] refactor: rewrite `IsLocal` with `ip.IsPrivate()` The referenced MR has been implemented. The extra checks have been added in 8467c01e9e88523fcdef22fed8efadbd07484966. With this rewrite the checks are exactly the same as of Go 1.23.3. --- common/util/util.go | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/common/util/util.go b/common/util/util.go index 55b2656..57170cf 100644 --- a/common/util/util.go +++ b/common/util/util.go @@ -56,20 +56,21 @@ func DeserializeSessionDescription(msg string) (*webrtc.SessionDescription, erro }, nil } -// Stolen from https://github.com/golang/go/pull/30278 func IsLocal(ip net.IP) bool { - if ip4 := ip.To4(); ip4 != nil { - // Local IPv4 addresses are defined in https://tools.ietf.org/html/rfc1918 - return ip4[0] == 10 || - (ip4[0] == 172 && ip4[1]&0xf0 == 16) || - (ip4[0] == 192 && ip4[1] == 168) || - // Carrier-Grade NAT as per https://tools.ietf.org/htm/rfc6598 - (ip4[0] == 100 && ip4[1]&0xc0 == 64) || - // Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927 - (ip4[0] == 169 && ip4[1] == 254) + if ip.IsPrivate() { + return true } - // Local IPv6 addresses are defined in https://tools.ietf.org/html/rfc4193 - return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc + if ip4 := ip.To4(); ip4 != nil { + // Carrier-Grade NAT as per https://tools.ietf.org/htm/rfc6598 + if ip4[0] == 100 && ip4[1]&0xc0 == 64 { + return true + } + // Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927 + if ip4[0] == 169 && ip4[1] == 254 { + return true + } + } + return false } // Removes local LAN address ICE candidates From f65f1d850f6ae7a4115605f2892a2f69cdfb5bc5 Mon Sep 17 00:00:00 2001 From: WofWca Date: Thu, 21 Nov 2024 17:31:56 +0400 Subject: [PATCH 2/2] improvement: use IsLinkLocalUnicast in IsLocal Looking at the code, this commit appears to change behavior, because `IsLocal` will now return `true` for IPv6 link-local unicast addresses. --- common/util/util.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/common/util/util.go b/common/util/util.go index 57170cf..a180692 100644 --- a/common/util/util.go +++ b/common/util/util.go @@ -60,15 +60,15 @@ func IsLocal(ip net.IP) bool { if ip.IsPrivate() { return true } + // Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927 + if ip.IsLinkLocalUnicast() { + return true + } if ip4 := ip.To4(); ip4 != nil { // Carrier-Grade NAT as per https://tools.ietf.org/htm/rfc6598 if ip4[0] == 100 && ip4[1]&0xc0 == 64 { return true } - // Dynamic Configuration as per https://tools.ietf.org/htm/rfc3927 - if ip4[0] == 169 && ip4[1] == 254 { - return true - } } return false }