pull/973/head
longXboy 4 years ago
parent f69cf59537
commit 156146036b
  1. 9
      internal/host/host.go
  2. 26
      internal/host/host_test.go

@ -7,14 +7,7 @@ import (
func isValidIP(addr string) bool { func isValidIP(addr string) bool {
ip := net.ParseIP(addr) ip := net.ParseIP(addr)
if ip4 := ip.To4(); ip4 != nil { return ip.IsGlobalUnicast() && !ip.IsInterfaceLocalMulticast()
return !ip4.IsLoopback()
}
// Following RFC 4193, Section 3. Private Address Space which says:
// The Internet Assigned Numbers Authority (IANA) has reserved the
// following block of the IPv6 address space for local internets:
// FC00:: - FDFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF (FC00::/7 prefix)
return len(ip) == net.IPv6len && ip[0]&0xfe == 0xfc
} }
// Port return a real port. // Port return a real port.

@ -5,36 +5,40 @@ import (
"testing" "testing"
) )
func TestPrivateIP(t *testing.T) { func TestValidIP(t *testing.T) {
tests := []struct { tests := []struct {
addr string addr string
expect bool expect bool
}{ }{
{"127.0.0.1", false},
{"255.255.255.255", false},
{"0.0.0.0", false},
{"localhost", false},
{"10.1.0.1", true}, {"10.1.0.1", true},
{"172.16.0.1", true}, {"172.16.0.1", true},
{"192.168.1.1", true}, {"192.168.1.1", true},
{"8.8.8.8", false}, {"8.8.8.8", true},
{"1.1.1.1", false}, {"1.1.1.1", true},
{"9.255.255.255", false}, {"9.255.255.255", true},
{"10.0.0.0", true}, {"10.0.0.0", true},
{"10.255.255.255", true}, {"10.255.255.255", true},
{"11.0.0.0", false}, {"11.0.0.0", true},
{"172.15.255.255", false}, {"172.15.255.255", true},
{"172.16.0.0", true}, {"172.16.0.0", true},
{"172.16.255.255", true}, {"172.16.255.255", true},
{"172.23.18.255", true}, {"172.23.18.255", true},
{"172.31.255.255", true}, {"172.31.255.255", true},
{"172.31.0.0", true}, {"172.31.0.0", true},
{"172.32.0.0", false}, {"172.32.0.0", true},
{"192.167.255.255", false}, {"192.167.255.255", true},
{"192.168.0.0", true}, {"192.168.0.0", true},
{"192.168.255.255", true}, {"192.168.255.255", true},
{"192.169.0.0", false}, {"192.169.0.0", true},
{"fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false}, {"fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true},
{"fc00::", true}, {"fc00::", true},
{"fcff:1200:0:44::", true}, {"fcff:1200:0:44::", true},
{"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true}, {"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", true},
{"fe00::", false}, {"fe00::", true},
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.addr, func(t *testing.T) { t.Run(test.addr, func(t *testing.T) {

Loading…
Cancel
Save