mirror of
https://github.com/go-kratos/kratos.git
synced 2025-02-05 13:15:11 +02:00
fix loopback addr
This commit is contained in:
parent
e1d6377542
commit
f69cf59537
@ -5,18 +5,10 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func isPrivateIP(addr string) bool {
|
||||
func isValidIP(addr string) bool {
|
||||
ip := net.ParseIP(addr)
|
||||
if ip4 := ip.To4(); ip4 != nil {
|
||||
// Following RFC 4193, Section 3. Local IPv6 Unicast Addresses which says:
|
||||
// The Internet Assigned Numbers Authority (IANA) has reserved the
|
||||
// following three blocks of the IPv4 address space for private internets:
|
||||
// 10.0.0.0 - 10.255.255.255 (10/8 prefix)
|
||||
// 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
|
||||
// 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
|
||||
return ip4[0] == 10 ||
|
||||
(ip4[0] == 172 && ip4[1]&0xf0 == 16) ||
|
||||
(ip4[0] == 192 && ip4[1] == 168)
|
||||
return !ip4.IsLoopback()
|
||||
}
|
||||
// Following RFC 4193, Section 3. Private Address Space which says:
|
||||
// The Internet Assigned Numbers Authority (IANA) has reserved the
|
||||
@ -66,7 +58,7 @@ func Extract(hostPort string, lis net.Listener) (string, error) {
|
||||
default:
|
||||
continue
|
||||
}
|
||||
if isPrivateIP(ip.String()) {
|
||||
if isValidIP(ip.String()) {
|
||||
return net.JoinHostPort(ip.String(), port), nil
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ func TestPrivateIP(t *testing.T) {
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.addr, func(t *testing.T) {
|
||||
res := isPrivateIP(test.addr)
|
||||
res := isValidIP(test.addr)
|
||||
if res != test.expect {
|
||||
t.Fatalf("expected %t got %t", test.expect, res)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user