mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-22 04:55:33 +03:00
fd25dcacbd
Squashed commit of the following:
commit d0b37e3de1552ea42d776461045a76ff0ae18128
Merge: 025c29bcd ee619b2db
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Apr 4 18:58:08 2024 +0300
Merge branch 'master' into AG-27492-client-runtime-index
commit 025c29bcd279b5448908df7c3a1a997a64095641
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Apr 1 17:20:15 2024 +0300
client: imp code
commit 548a15c000db3989b1398e68fa4c05a450e93ea0
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Thu Mar 28 13:43:17 2024 +0300
all: add tests
commit c9015e732f1e0475ec8cf95487c9ec56cd69a395
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Mar 25 16:33:30 2024 +0300
all: imp docs
commit 81e8b944928176733b2971b2b6400b55496a7843
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Mar 25 15:33:17 2024 +0300
all: imp code
commit 1428d60bf72d7a0ffd9dc854403391646f82c6cc
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date: Mon Mar 25 14:45:01 2024 +0300
all: client runtime index
177 lines
4 KiB
Go
177 lines
4 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"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/whois"
|
|
)
|
|
|
|
// 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) {
|
|
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 WHOIS client information.
|
|
func (r *Runtime) WHOIS() (info *whois.Info) {
|
|
return r.whois
|
|
}
|
|
|
|
// 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
|
|
}
|