Skip to content

Commit

Permalink
fix android DNS issues while parsing wireguard config endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: thehxdev <[email protected]>
  • Loading branch information
thehxdev authored and markpash committed Jul 30, 2024
1 parent b727aba commit c805ea8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
13 changes: 11 additions & 2 deletions iputils/iputils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iputils

import (
"context"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit c805ea8

Please sign in to comment.