Merge in DNS/adguard-home from AGDNS-2750-find-client to master Squashed commit of the following: commit 98f1a8ca4622b6f502a5092273b9724203fe0bd8 Merge: 9270222d84ccc2a213Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 23 17:53:20 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 9270222d8e9e03038e9434b54496cbb6164463cd Merge: 6468ceec8c7c62ad3bAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:58 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6468ceec82d30084771a53ff6720a8c11c68bf2f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 21 19:40:52 2025 +0300 home: imp docs commit 3fd4735a0d6db4fdf2d46f3da9794a687fdcaa8b Merge: 1311a5869a8fdf1c55Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:43:36 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 1311a58695de00f20c9704378ee6e964a44d1c59 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 18 19:42:41 2025 +0300 home: imp code commit b1f2c4c883c9476c5135140abac31f8ae6609b4f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 16 16:47:59 2025 +0300 home: imp code commit d0a5abd66587c1ad602c2ccf6c8a45a3dfe39a5c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 15 14:58:31 2025 +0300 client: imp naming commit 5accdca325551237f003f1c416891b488fe5290b Merge: 6a00232f74d258972dAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:40:40 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 6a00232f76a0fe5ce781aa01637b6e04ace7250d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 14 19:30:32 2025 +0300 home: imp code commit 8633886457c6aab75f5676494b1f49d9811e9ab9 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Apr 11 15:29:25 2025 +0300 all: imp code commit d6f16879e7b054a5ffac59131d2a6eff1da659c0 Merge: 58236fdec6d282ae71Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:35:23 2025 +0300 Merge branch 'master' into AGDNS-2750-find-client commit 58236fdec5b64e83a44680ff8a89badc18ec81f1 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:23:01 2025 +0300 all: upd ci commit 3c4d946d7970987677d4ac984394e18987a29f9a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 21:16:03 2025 +0300 all: upd go commit cc1c97734506a9ffbe70fd3c676284e58a21ba46 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Apr 10 20:58:56 2025 +0300 all: imp code commit 8f061c933152481a4c80eef2af575efd4919d82b Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 9 16:49:11 2025 +0300 all: imp docs commit 8d19355f1c519211a56cec3f23d527922d4f2ee0 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Apr 7 21:35:06 2025 +0300 all: imp code commit f1e853f57e5d54d13bedcdab4f8e21e112f3a356 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Apr 2 14:57:40 2025 +0300 all: imp code commit 6a6ac7f899f29ddc90a583c80562233e646ba1d6 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:51:56 2025 +0300 client: imp tests commit 52040ee7393d0483c682f2f37d7b70f12f9cf621 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Apr 1 19:28:18 2025 +0300 all: imp code commit 1e09208dbd2d35c3f6b2ade169324e23d1a643a5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Mar 26 15:33:02 2025 +0300 all: imp code ... and 2 more commits
222 lines
5.3 KiB
Go
222 lines
5.3 KiB
Go
// Package client contains types and logic dealing with AdGuard Home's DNS
|
|
// clients.
|
|
//
|
|
// TODO(a.garipov): Expand.
|
|
package client
|
|
|
|
import (
|
|
"encoding"
|
|
"fmt"
|
|
"net/netip"
|
|
"slices"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
)
|
|
|
|
// ClientID is a unique identifier for a persistent client used in
|
|
// DNS-over-HTTPS, DNS-over-TLS, and DNS-over-QUIC queries.
|
|
//
|
|
// TODO(s.chzhen): Use everywhere.
|
|
type ClientID string
|
|
|
|
// ValidateClientID returns an error if id is not a valid ClientID.
|
|
//
|
|
// TODO(s.chzhen): Consider implementing [validate.Interface] for ClientID.
|
|
func ValidateClientID(id string) (err error) {
|
|
err = netutil.ValidateHostnameLabel(id)
|
|
if err != nil {
|
|
// Replace the domain name label wrapper with our own.
|
|
return fmt.Errorf("invalid clientid %q: %w", id, errors.Unwrap(err))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// isValidClientID returns false if id is not a valid ClientID.
|
|
func isValidClientID(id string) (ok bool) {
|
|
return netutil.IsValidHostnameLabel(id)
|
|
}
|
|
|
|
// Source represents the source from which the information about the client has
|
|
// been obtained.
|
|
type Source uint8
|
|
|
|
// Clients information sources. The order determines the priority.
|
|
const (
|
|
SourceWHOIS Source = iota + 1
|
|
SourceARP
|
|
SourceRDNS
|
|
SourceDHCP
|
|
SourceHostsFile
|
|
SourcePersistent
|
|
)
|
|
|
|
// type check
|
|
var _ fmt.Stringer = Source(0)
|
|
|
|
// String returns a human-readable name of cs.
|
|
func (cs Source) String() (s string) {
|
|
switch cs {
|
|
case SourceWHOIS:
|
|
return "WHOIS"
|
|
case SourceARP:
|
|
return "ARP"
|
|
case SourceRDNS:
|
|
return "rDNS"
|
|
case SourceDHCP:
|
|
return "DHCP"
|
|
case SourceHostsFile:
|
|
return "etc/hosts"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
// type check
|
|
var _ encoding.TextMarshaler = Source(0)
|
|
|
|
// MarshalText implements encoding.TextMarshaler for the Source.
|
|
func (cs Source) MarshalText() (text []byte, err error) {
|
|
return []byte(cs.String()), nil
|
|
}
|
|
|
|
// Runtime is a client information from different sources.
|
|
type Runtime struct {
|
|
// ip is an IP address of a client.
|
|
ip netip.Addr
|
|
|
|
// whois is the filtered WHOIS information of a client.
|
|
whois *whois.Info
|
|
|
|
// arp is the ARP information of a client. nil indicates that there is no
|
|
// information from the source. Empty non-nil slice indicates that the data
|
|
// from the source is present, but empty.
|
|
arp []string
|
|
|
|
// rdns is the RDNS information of a client. nil indicates that there is no
|
|
// information from the source. Empty non-nil slice indicates that the data
|
|
// from the source is present, but empty.
|
|
rdns []string
|
|
|
|
// dhcp is the DHCP information of a client. nil indicates that there is no
|
|
// information from the source. Empty non-nil slice indicates that the data
|
|
// from the source is present, but empty.
|
|
dhcp []string
|
|
|
|
// hostsFile is the information from the hosts file. nil indicates that
|
|
// there is no information from the source. Empty non-nil slice indicates
|
|
// that the data from the source is present, but empty.
|
|
hostsFile []string
|
|
}
|
|
|
|
// NewRuntime constructs a new runtime client. ip must be valid IP address.
|
|
//
|
|
// TODO(s.chzhen): Validate IP address.
|
|
func NewRuntime(ip netip.Addr) (r *Runtime) {
|
|
return &Runtime{
|
|
ip: ip,
|
|
}
|
|
}
|
|
|
|
// Info returns a client information from the highest-priority source.
|
|
func (r *Runtime) Info() (cs Source, host string) {
|
|
info := []string{}
|
|
|
|
switch {
|
|
case r.hostsFile != nil:
|
|
cs, info = SourceHostsFile, r.hostsFile
|
|
case r.dhcp != nil:
|
|
cs, info = SourceDHCP, r.dhcp
|
|
case r.rdns != nil:
|
|
cs, info = SourceRDNS, r.rdns
|
|
case r.arp != nil:
|
|
cs, info = SourceARP, r.arp
|
|
case r.whois != nil:
|
|
cs = SourceWHOIS
|
|
}
|
|
|
|
if len(info) == 0 {
|
|
return cs, ""
|
|
}
|
|
|
|
// TODO(s.chzhen): Return the full information.
|
|
return cs, info[0]
|
|
}
|
|
|
|
// setInfo sets a host as a client information from the cs.
|
|
func (r *Runtime) setInfo(cs Source, hosts []string) {
|
|
// TODO(s.chzhen): Use contract where hosts must contain non-empty host.
|
|
if len(hosts) == 1 && hosts[0] == "" {
|
|
hosts = []string{}
|
|
}
|
|
|
|
switch cs {
|
|
case SourceARP:
|
|
r.arp = hosts
|
|
case SourceRDNS:
|
|
r.rdns = hosts
|
|
case SourceDHCP:
|
|
r.dhcp = hosts
|
|
case SourceHostsFile:
|
|
r.hostsFile = hosts
|
|
}
|
|
}
|
|
|
|
// WHOIS returns a copy of WHOIS client information.
|
|
func (r *Runtime) WHOIS() (info *whois.Info) {
|
|
return r.whois.Clone()
|
|
}
|
|
|
|
// setWHOIS sets a WHOIS client information. info must be non-nil.
|
|
func (r *Runtime) setWHOIS(info *whois.Info) {
|
|
r.whois = info
|
|
}
|
|
|
|
// unset clears a cs information.
|
|
func (r *Runtime) unset(cs Source) {
|
|
switch cs {
|
|
case SourceWHOIS:
|
|
r.whois = nil
|
|
case SourceARP:
|
|
r.arp = nil
|
|
case SourceRDNS:
|
|
r.rdns = nil
|
|
case SourceDHCP:
|
|
r.dhcp = nil
|
|
case SourceHostsFile:
|
|
r.hostsFile = nil
|
|
}
|
|
}
|
|
|
|
// isEmpty returns true if there is no information from any source.
|
|
func (r *Runtime) isEmpty() (ok bool) {
|
|
return r.whois == nil &&
|
|
r.arp == nil &&
|
|
r.rdns == nil &&
|
|
r.dhcp == nil &&
|
|
r.hostsFile == nil
|
|
}
|
|
|
|
// Addr returns an IP address of the client.
|
|
func (r *Runtime) Addr() (ip netip.Addr) {
|
|
return r.ip
|
|
}
|
|
|
|
// clone returns a deep copy of the runtime client. If r is nil, c is nil.
|
|
func (r *Runtime) clone() (c *Runtime) {
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
|
|
return &Runtime{
|
|
ip: r.ip,
|
|
whois: r.whois.Clone(),
|
|
arp: slices.Clone(r.arp),
|
|
rdns: slices.Clone(r.rdns),
|
|
dhcp: slices.Clone(r.dhcp),
|
|
hostsFile: slices.Clone(r.hostsFile),
|
|
}
|
|
}
|