[CHERRY-PICK] PR#4036 (#4037)

* fix: correct selections of server url, remove ip duplicates (#4036)
* fix `TestConnectionParams_GetLocalAddressesForPairingServer` flaky test
This commit is contained in:
Igor Sirotin 2023-09-20 10:37:51 +01:00 committed by GitHub
parent db9adb631f
commit 6977a94cdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 16 deletions

View file

@ -1 +1 @@
0.167.0 0.167.1

View file

@ -42,7 +42,7 @@ func addrToIPNet(addr net.Addr) *net.IPNet {
// ips is a 2-dimensional array, where each sub-array is a list of IP // ips is a 2-dimensional array, where each sub-array is a list of IP
// addresses for a single network interface. // addresses for a single network interface.
func filterAddressesForPairingServer(ips [][]net.IP) []net.IP { func filterAddressesForPairingServer(ips [][]net.IP) []net.IP {
var result []net.IP var result = map[string]net.IP{}
for _, niIps := range ips { for _, niIps := range ips {
var ipv4, ipv6 []net.IP var ipv4, ipv6 []net.IP
@ -63,13 +63,22 @@ func filterAddressesForPairingServer(ips [][]net.IP) []net.IP {
// Prefer IPv4 over IPv6 for shorter connection string // Prefer IPv4 over IPv6 for shorter connection string
if len(ipv4) == 0 { if len(ipv4) == 0 {
result = append(result, ipv6...) for _, ip := range ipv6 {
result[ip.String()] = ip
}
} else { } else {
result = append(result, ipv4...) for _, ip := range ipv4 {
result[ip.String()] = ip
}
} }
} }
return result var out []net.IP
for _, v := range result {
out = append(out, v)
}
return out
} }
// getAndroidLocalIP uses the net dial default ip as the standard Android IP address // getAndroidLocalIP uses the net dial default ip as the standard Android IP address

View file

@ -84,10 +84,21 @@ func (s *IPsTestingSuite) TestConnectionParams_GetLocalAddressesForPairingServer
ips := filterAddressesForPairingServer(allIps) ips := filterAddressesForPairingServer(allIps)
s.Require().Len(ips, 2) s.Require().Len(ips, 2)
s.Require().NotNil(ips[0].To4())
var ip1, ip2 net.IP
if ips[0].To4() != nil {
ip1 = ips[0]
ip2 = ips[1]
} else {
ip1 = ips[1]
ip2 = ips[0]
}
s.Require().NotNil(ip1.To4())
s.Require().NotNil(ni1[0].To4()) s.Require().NotNil(ni1[0].To4())
s.Require().Equal(ips[0].To4(), ni1[0].To4()) s.Require().Equal(ip1.To4(), ni1[0].To4())
s.Require().Equal(ips[1], ni3[0]) s.Require().Equal(ip2, ni3[0])
} }
func (s *IPsTestingSuite) TestConnectionParams_FindReachableAddresses() { func (s *IPsTestingSuite) TestConnectionParams_FindReachableAddresses() {

View file

@ -46,11 +46,8 @@ func findServerCert(c *ConnectionParams) (*url.URL, *x509.Certificate, error) {
var baseAddress *url.URL var baseAddress *url.URL
var serverCert *x509.Certificate var serverCert *x509.Certificate
var certErrs error var certErrs error
for i := range netIps { for _, ip := range netIps {
u, err := c.URL(i) u := c.BuildURL(ip)
if err != nil {
return nil, nil, err
}
serverCert, err = getServerCert(u) serverCert, err = getServerCert(u)
if err != nil { if err != nil {

View file

@ -236,11 +236,14 @@ func (cp *ConnectionParams) URL(IPIndex int) (*url.URL, error) {
return nil, err return nil, err
} }
u := &url.URL{ return cp.BuildURL(cp.netIPs[IPIndex]), nil
}
func (cp *ConnectionParams) BuildURL(ip net.IP) *url.URL {
return &url.URL{
Scheme: "https", Scheme: "https",
Host: fmt.Sprintf("%s:%d", cp.netIPs[IPIndex], cp.port), Host: fmt.Sprintf("%s:%d", ip, cp.port),
} }
return u, nil
} }
func ValidateConnectionString(cs string) error { func ValidateConnectionString(cs string) error {