From c805ea83225c2cef9155f60ca4d07d600074f9a9 Mon Sep 17 00:00:00 2001 From: thehxdev Date: Tue, 30 Jul 2024 11:06:44 +0330 Subject: [PATCH] fix android DNS issues while parsing wireguard config endpoint Signed-off-by: thehxdev --- app/app.go | 2 +- iputils/iputils.go | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/app.go b/app/app.go index a49f523be..dea951783 100644 --- a/app/app.go +++ b/app/app.go @@ -125,7 +125,7 @@ func runWireguard(ctx context.Context, l *slog.Logger, opts WarpOptions) error { peer.KeepAlive = 3 // Try resolving if the endpoint is a domain - addr, err := iputils.ParseResolveAddressPort(peer.Endpoint, false) + addr, err := iputils.ParseResolveAddressPort(peer.Endpoint, false, opts.DnsAddr.String()) if err == nil { peer.Endpoint = addr.String() } diff --git a/iputils/iputils.go b/iputils/iputils.go index 98b863e0a..bab501ef1 100644 --- a/iputils/iputils.go +++ b/iputils/iputils.go @@ -1,6 +1,7 @@ package iputils import ( + "context" "errors" "fmt" "math/big" @@ -57,7 +58,7 @@ func RandomIPFromPrefix(cidr netip.Prefix) (netip.Addr, error) { return randomAddress.Unmap(), nil } -func ParseResolveAddressPort(hostname string, includev6 bool) (netip.AddrPort, error) { +func ParseResolveAddressPort(hostname string, includev6 bool, dnsServer string) (netip.AddrPort, error) { // Attempt to split the hostname into a host and port host, port, err := net.SplitHostPort(hostname) if err != nil { @@ -80,8 +81,16 @@ func ParseResolveAddressPort(hostname string, includev6 bool) (netip.AddrPort, e return netip.AddrPortFrom(addr.Unmap(), uint16(portInt)), nil } + // Use Go's built-in DNS resolver + resolver := &net.Resolver{ + PreferGo: true, + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { + return net.Dial("udp", net.JoinHostPort(dnsServer, "53")) + }, + } + // If the host wasn't an IP, perform a lookup - ips, err := net.LookupIP(host) + ips, err := resolver.LookupIP(context.Background(), "ip", host) if err != nil { return netip.AddrPort{}, fmt.Errorf("hostname lookup failed: %w", err) }