fix(netxlite): consolidate IPv4/IPv6 checking code (#726)
Originally 966e7f7cdd
See https://github.com/ooni/probe/issues/2096
This commit is contained in:
parent
e126e73de7
commit
192dfd49b4
|
@ -2,6 +2,7 @@ package netxlite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"net"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -52,26 +53,21 @@ func quirkReduceErrors(errorslist []error) error {
|
||||||
// before IPv6. Dialers SHOULD call this code.
|
// before IPv6. Dialers SHOULD call this code.
|
||||||
//
|
//
|
||||||
// It saddens me to have this quirk, but it is here to pair
|
// It saddens me to have this quirk, but it is here to pair
|
||||||
// with quirkReduceErrors, which assumes that <facepalm>.
|
// with quirkReduceErrors, which assumes that IPv4 addrs
|
||||||
|
// appear before IPv6 addrs <facepalm>.
|
||||||
|
//
|
||||||
|
// Note: this function will skip any input that is not not
|
||||||
|
// a valid IPv4 or IPv6 address.
|
||||||
//
|
//
|
||||||
// See TODO(https://github.com/ooni/probe/issues/1779).
|
// See TODO(https://github.com/ooni/probe/issues/1779).
|
||||||
func quirkSortIPAddrs(addrs []string) (out []string) {
|
func quirkSortIPAddrs(addrs []string) (out []string) {
|
||||||
isIPv6 := func(x string) bool {
|
|
||||||
// This check for identifying IPv6 is discussed
|
|
||||||
// at https://stackoverflow.com/questions/22751035
|
|
||||||
// and seems good-enough for our purposes.
|
|
||||||
return strings.Contains(x, ":")
|
|
||||||
}
|
|
||||||
isIPv4 := func(x string) bool {
|
|
||||||
return !isIPv6(x)
|
|
||||||
}
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
if isIPv4(addr) {
|
if net.ParseIP(addr) != nil && !isIPv6(addr) {
|
||||||
out = append(out, addr)
|
out = append(out, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
if isIPv6(addr) {
|
if net.ParseIP(addr) != nil && isIPv6(addr) {
|
||||||
out = append(out, addr)
|
out = append(out, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,9 @@ func TestQuirkSortIPAddrs(t *testing.T) {
|
||||||
addrs := []string{
|
addrs := []string{
|
||||||
"::1",
|
"::1",
|
||||||
"192.168.1.2",
|
"192.168.1.2",
|
||||||
|
"x.org", // ensure we skip non IP addrs
|
||||||
"2a00:1450:4002:404::2004",
|
"2a00:1450:4002:404::2004",
|
||||||
|
"example.com", // ensure we skip non IP addrs
|
||||||
"142.250.184.36",
|
"142.250.184.36",
|
||||||
"2604:8800:5000:82:466:38ff:fecb:d46e",
|
"2604:8800:5000:82:466:38ff:fecb:d46e",
|
||||||
"198.145.29.83",
|
"198.145.29.83",
|
||||||
|
@ -83,4 +85,14 @@ func TestQuirkSortIPAddrs(t *testing.T) {
|
||||||
t.Fatal("expected nil output")
|
t.Fatal("expected nil output")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("with non-IP addrs", func(t *testing.T) {
|
||||||
|
addrs := []string{
|
||||||
|
"example.com",
|
||||||
|
"x.org",
|
||||||
|
}
|
||||||
|
if quirkSortIPAddrs(addrs) != nil {
|
||||||
|
t.Fatal("expected nil output")
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user