mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 19:31:45 +03:00
5be0e84719
Squashed commit of the following: commit 081d10e6909def3a075707e75dbd0c5f63f91903 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Jul 20 14:17:01 2023 +0300 aghnet: fix docs commit 7433b72c0653cb33fe5ff810ae8a1346a6994f95 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Jul 20 14:03:16 2023 +0300 all: imp tests; upd golibs
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package dnsforward
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
)
|
|
|
|
// DialContext is an [aghnet.DialContextFunc] that uses s to resolve hostnames.
|
|
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, port, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dialer := &net.Dialer{
|
|
// TODO(a.garipov): Consider making configurable.
|
|
Timeout: time.Minute * 5,
|
|
}
|
|
|
|
if net.ParseIP(host) != nil {
|
|
return dialer.DialContext(ctx, network, addr)
|
|
}
|
|
|
|
addrs, err := s.Resolve(host)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("resolving %q: %w", host, err)
|
|
}
|
|
|
|
log.Debug("dnsforward: resolving %q: %v", host, addrs)
|
|
|
|
if len(addrs) == 0 {
|
|
return nil, fmt.Errorf("no addresses for host %q", host)
|
|
}
|
|
|
|
var dialErrs []error
|
|
for _, a := range addrs {
|
|
addr = net.JoinHostPort(a.String(), port)
|
|
conn, err = dialer.DialContext(ctx, network, addr)
|
|
if err != nil {
|
|
dialErrs = append(dialErrs, err)
|
|
|
|
continue
|
|
}
|
|
|
|
return conn, err
|
|
}
|
|
|
|
// TODO(a.garipov): Use errors.Join in Go 1.20.
|
|
return nil, errors.List(fmt.Sprintf("dialing %q", addr), dialErrs...)
|
|
}
|