mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-11 18:22:57 +03:00
edfa8c147f
Squashed commit of the following: commit 8d83eebba851e8e09bb08b1c94a247cb049a1b75 Merge:c6574a33c
b6ed76965
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Aug 5 16:59:50 2024 +0300 Merge branch 'master' into AGDNS-2280-upd-golibs commitc6574a33c6
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Jul 31 19:56:58 2024 +0300 all: upd proxy, golibs
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
)
|
|
|
|
// DialContext is an [aghnet.DialContextFunc] that uses s to resolve hostnames.
|
|
// addr should be a valid host:port address, where host could be a domain name
|
|
// or an IP address.
|
|
func (s *Server) DialContext(ctx context.Context, network, addr string) (conn net.Conn, err error) {
|
|
log.Debug("dnsforward: dialing %q for network %q", addr, network)
|
|
|
|
host, portStr, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dialer := &net.Dialer{
|
|
// TODO(a.garipov): Consider making configurable.
|
|
Timeout: time.Minute * 5,
|
|
}
|
|
|
|
if netutil.IsValidIPString(host) {
|
|
return dialer.DialContext(ctx, network, addr)
|
|
}
|
|
|
|
port, err := strconv.Atoi(portStr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid port %s: %w", portStr, err)
|
|
}
|
|
|
|
ips, err := s.Resolve(ctx, network, host)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolving %q: %w", host, err)
|
|
} else if len(ips) == 0 {
|
|
return nil, fmt.Errorf("no addresses for host %q", host)
|
|
}
|
|
|
|
log.Debug("dnsforward: resolved %q: %v", host, ips)
|
|
|
|
var dialErrs []error
|
|
for _, ip := range ips {
|
|
addrPort := netip.AddrPortFrom(ip, uint16(port))
|
|
conn, err = dialer.DialContext(ctx, network, addrPort.String())
|
|
if err != nil {
|
|
dialErrs = append(dialErrs, err)
|
|
|
|
continue
|
|
}
|
|
|
|
return conn, nil
|
|
}
|
|
|
|
return nil, errors.Join(dialErrs...)
|
|
}
|