refactor: rewrite IsLocal with ip.IsPrivate()

The referenced MR has been implemented.
The extra checks have been added in 8467c01e9e.

With this rewrite the checks are exactly the same as of Go 1.23.3.
This commit is contained in:
WofWca 2024-11-18 20:46:27 +04:00
parent 239357509f
commit 387096b2a1

View file

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