Release 2.6.1: resolve AWG endpoint hostnames to IP before connect.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package awg
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -139,6 +140,81 @@ func TestDecodeKeyURLSafe(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveEndpointPrefersIPv4(t *testing.T) {
|
||||
got, err := resolveEndpointWith("peer.example:41421", func(host string) ([]net.IP, error) {
|
||||
if host != "peer.example" {
|
||||
t.Fatalf("host %q", host)
|
||||
}
|
||||
return []net.IP{
|
||||
net.ParseIP("2001:db8::1"),
|
||||
net.ParseIP("203.0.113.44"),
|
||||
}, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "203.0.113.44:41421" {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveEndpointAlreadyIP(t *testing.T) {
|
||||
got, err := resolveEndpointWith("203.0.113.10:51820", func(string) ([]net.IP, error) {
|
||||
t.Fatal("lookup should not run for IP")
|
||||
return nil, nil
|
||||
})
|
||||
if err != nil || got != "203.0.113.10:51820" {
|
||||
t.Fatalf("%q %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveEndpointDNSError(t *testing.T) {
|
||||
_, err := resolveEndpointWith("missing.invalid:41421", func(string) ([]net.IP, error) {
|
||||
return nil, &net.DNSError{Err: "no such host", Name: "missing.invalid", IsNotFound: true}
|
||||
})
|
||||
if err == nil || !stringsContains(err.Error(), "не удалось разрешить DNS") {
|
||||
t.Fatalf("expected Russian DNS error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToIPCResolvesEndpoint(t *testing.T) {
|
||||
old := lookupIP
|
||||
lookupIP = func(host string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("198.51.100.9")}, nil
|
||||
}
|
||||
defer func() { lookupIP = old }()
|
||||
|
||||
c := Conf{
|
||||
PrivateKey: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
|
||||
PublicKey: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEE=",
|
||||
Address: []string{"10.8.0.2/32"},
|
||||
Endpoint: "peer.example:41421",
|
||||
}
|
||||
ipc, err := c.ToIPC()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !stringsContains(ipc, "endpoint=198.51.100.9:41421") {
|
||||
t.Fatalf("ipc missing resolved endpoint: %s", ipc)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfResolveEndpointMutates(t *testing.T) {
|
||||
old := lookupIP
|
||||
lookupIP = func(string) ([]net.IP, error) {
|
||||
return []net.IP{net.ParseIP("198.51.100.1")}, nil
|
||||
}
|
||||
defer func() { lookupIP = old }()
|
||||
|
||||
c := Conf{Endpoint: "host.example:41421"}
|
||||
if err := c.ResolveEndpoint(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.Endpoint != "198.51.100.1:41421" {
|
||||
t.Fatalf("endpoint %q", c.Endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
func containsAll(s string, parts ...string) bool {
|
||||
for _, p := range parts {
|
||||
if !stringsContains(s, p) {
|
||||
|
||||
Reference in New Issue
Block a user