AdGuardHome/internal/client/client.go

178 lines
4.0 KiB
Go
Raw Normal View History

// Package client contains types and logic dealing with AdGuard Home's DNS
// clients.
//
// TODO(a.garipov): Expand.
package client
import (
"encoding"
"fmt"
"net/netip"
Pull request 2083: AG-27492-client-runtime Squashed commit of the following: commit e4c2abd37f5885a12da5c68179e3dba18534c3be Merge: 7411b40b2 dae304fc3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Dec 13 13:15:40 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 7411b40b234a438df4c84823fcb19efecf5eb64d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 17:51:51 2023 +0300 all: imp code commit d5edd0258779c6dfba56da1e4d79e0380b137385 Merge: 371f5b23c c908eec5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:45:33 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:44:38 2023 +0300 client: imp code commit 9aefb1443082a342270a7e5e77a07d5d1518e327 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 8 17:08:07 2023 +0300 all: imp code commit 3aa51d10aadfd124f36238a31f3d623706e3c870 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 5 19:20:41 2023 +0300 all: imp docs commit 71d7187e0c14e143865b055e9b01ff61a2e30083 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 1 17:17:06 2023 +0300 all: imp code commit e09f8a0bd90f295b5a580e24b2696cc78993713a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 30 14:46:53 2023 +0300 all: imp log msg commit ce0a9454df9a173694395b4dd8b05347a2e56fdc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Nov 28 16:21:01 2023 +0300 all: imp code commit e84f176aaba6953b22e13a5fa84079a832d30a24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Nov 22 17:40:18 2023 +0300 all: add client runtime
2023-12-13 10:39:52 +00:00
"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 (
Pull request 2083: AG-27492-client-runtime Squashed commit of the following: commit e4c2abd37f5885a12da5c68179e3dba18534c3be Merge: 7411b40b2 dae304fc3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Dec 13 13:15:40 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 7411b40b234a438df4c84823fcb19efecf5eb64d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 17:51:51 2023 +0300 all: imp code commit d5edd0258779c6dfba56da1e4d79e0380b137385 Merge: 371f5b23c c908eec5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:45:33 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:44:38 2023 +0300 client: imp code commit 9aefb1443082a342270a7e5e77a07d5d1518e327 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 8 17:08:07 2023 +0300 all: imp code commit 3aa51d10aadfd124f36238a31f3d623706e3c870 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 5 19:20:41 2023 +0300 all: imp docs commit 71d7187e0c14e143865b055e9b01ff61a2e30083 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 1 17:17:06 2023 +0300 all: imp code commit e09f8a0bd90f295b5a580e24b2696cc78993713a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 30 14:46:53 2023 +0300 all: imp log msg commit ce0a9454df9a173694395b4dd8b05347a2e56fdc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Nov 28 16:21:01 2023 +0300 all: imp code commit e84f176aaba6953b22e13a5fa84079a832d30a24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Nov 22 17:40:18 2023 +0300 all: add client runtime
2023-12-13 10:39:52 +00:00
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
}
Pull request 2083: AG-27492-client-runtime Squashed commit of the following: commit e4c2abd37f5885a12da5c68179e3dba18534c3be Merge: 7411b40b2 dae304fc3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Dec 13 13:15:40 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 7411b40b234a438df4c84823fcb19efecf5eb64d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 17:51:51 2023 +0300 all: imp code commit d5edd0258779c6dfba56da1e4d79e0380b137385 Merge: 371f5b23c c908eec5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:45:33 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:44:38 2023 +0300 client: imp code commit 9aefb1443082a342270a7e5e77a07d5d1518e327 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 8 17:08:07 2023 +0300 all: imp code commit 3aa51d10aadfd124f36238a31f3d623706e3c870 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 5 19:20:41 2023 +0300 all: imp docs commit 71d7187e0c14e143865b055e9b01ff61a2e30083 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 1 17:17:06 2023 +0300 all: imp code commit e09f8a0bd90f295b5a580e24b2696cc78993713a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 30 14:46:53 2023 +0300 all: imp log msg commit ce0a9454df9a173694395b4dd8b05347a2e56fdc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Nov 28 16:21:01 2023 +0300 all: imp code commit e84f176aaba6953b22e13a5fa84079a832d30a24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Nov 22 17:40:18 2023 +0300 all: add client runtime
2023-12-13 10:39:52 +00:00
// Runtime is a client information from different sources.
type Runtime struct {
// ip is an IP address of a client.
ip netip.Addr
Pull request 2083: AG-27492-client-runtime Squashed commit of the following: commit e4c2abd37f5885a12da5c68179e3dba18534c3be Merge: 7411b40b2 dae304fc3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Dec 13 13:15:40 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 7411b40b234a438df4c84823fcb19efecf5eb64d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 17:51:51 2023 +0300 all: imp code commit d5edd0258779c6dfba56da1e4d79e0380b137385 Merge: 371f5b23c c908eec5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:45:33 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:44:38 2023 +0300 client: imp code commit 9aefb1443082a342270a7e5e77a07d5d1518e327 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 8 17:08:07 2023 +0300 all: imp code commit 3aa51d10aadfd124f36238a31f3d623706e3c870 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 5 19:20:41 2023 +0300 all: imp docs commit 71d7187e0c14e143865b055e9b01ff61a2e30083 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 1 17:17:06 2023 +0300 all: imp code commit e09f8a0bd90f295b5a580e24b2696cc78993713a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 30 14:46:53 2023 +0300 all: imp log msg commit ce0a9454df9a173694395b4dd8b05347a2e56fdc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Nov 28 16:21:01 2023 +0300 all: imp code commit e84f176aaba6953b22e13a5fa84079a832d30a24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Nov 22 17:40:18 2023 +0300 all: add client runtime
2023-12-13 10:39:52 +00:00
// 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,
}
}
Pull request 2083: AG-27492-client-runtime Squashed commit of the following: commit e4c2abd37f5885a12da5c68179e3dba18534c3be Merge: 7411b40b2 dae304fc3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Dec 13 13:15:40 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 7411b40b234a438df4c84823fcb19efecf5eb64d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 17:51:51 2023 +0300 all: imp code commit d5edd0258779c6dfba56da1e4d79e0380b137385 Merge: 371f5b23c c908eec5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:45:33 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:44:38 2023 +0300 client: imp code commit 9aefb1443082a342270a7e5e77a07d5d1518e327 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 8 17:08:07 2023 +0300 all: imp code commit 3aa51d10aadfd124f36238a31f3d623706e3c870 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 5 19:20:41 2023 +0300 all: imp docs commit 71d7187e0c14e143865b055e9b01ff61a2e30083 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 1 17:17:06 2023 +0300 all: imp code commit e09f8a0bd90f295b5a580e24b2696cc78993713a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 30 14:46:53 2023 +0300 all: imp log msg commit ce0a9454df9a173694395b4dd8b05347a2e56fdc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Nov 28 16:21:01 2023 +0300 all: imp code commit e84f176aaba6953b22e13a5fa84079a832d30a24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Nov 22 17:40:18 2023 +0300 all: add client runtime
2023-12-13 10:39:52 +00:00
// 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) {
Pull request 2083: AG-27492-client-runtime Squashed commit of the following: commit e4c2abd37f5885a12da5c68179e3dba18534c3be Merge: 7411b40b2 dae304fc3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Dec 13 13:15:40 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 7411b40b234a438df4c84823fcb19efecf5eb64d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 17:51:51 2023 +0300 all: imp code commit d5edd0258779c6dfba56da1e4d79e0380b137385 Merge: 371f5b23c c908eec5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:45:33 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:44:38 2023 +0300 client: imp code commit 9aefb1443082a342270a7e5e77a07d5d1518e327 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 8 17:08:07 2023 +0300 all: imp code commit 3aa51d10aadfd124f36238a31f3d623706e3c870 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 5 19:20:41 2023 +0300 all: imp docs commit 71d7187e0c14e143865b055e9b01ff61a2e30083 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 1 17:17:06 2023 +0300 all: imp code commit e09f8a0bd90f295b5a580e24b2696cc78993713a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 30 14:46:53 2023 +0300 all: imp log msg commit ce0a9454df9a173694395b4dd8b05347a2e56fdc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Nov 28 16:21:01 2023 +0300 all: imp code commit e84f176aaba6953b22e13a5fa84079a832d30a24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Nov 22 17:40:18 2023 +0300 all: add client runtime
2023-12-13 10:39:52 +00:00
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) {
Pull request 2083: AG-27492-client-runtime Squashed commit of the following: commit e4c2abd37f5885a12da5c68179e3dba18534c3be Merge: 7411b40b2 dae304fc3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Dec 13 13:15:40 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 7411b40b234a438df4c84823fcb19efecf5eb64d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 17:51:51 2023 +0300 all: imp code commit d5edd0258779c6dfba56da1e4d79e0380b137385 Merge: 371f5b23c c908eec5d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:45:33 2023 +0300 Merge branch 'master' into AG-27492-client-runtime commit 371f5b23c9507fb6e8e8f8ec1f644ca8e67ea412 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 12 13:44:38 2023 +0300 client: imp code commit 9aefb1443082a342270a7e5e77a07d5d1518e327 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 8 17:08:07 2023 +0300 all: imp code commit 3aa51d10aadfd124f36238a31f3d623706e3c870 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Dec 5 19:20:41 2023 +0300 all: imp docs commit 71d7187e0c14e143865b055e9b01ff61a2e30083 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Dec 1 17:17:06 2023 +0300 all: imp code commit e09f8a0bd90f295b5a580e24b2696cc78993713a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Nov 30 14:46:53 2023 +0300 all: imp log msg commit ce0a9454df9a173694395b4dd8b05347a2e56fdc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Nov 28 16:21:01 2023 +0300 all: imp code commit e84f176aaba6953b22e13a5fa84079a832d30a24 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Nov 22 17:40:18 2023 +0300 all: add client runtime
2023-12-13 10:39:52 +00:00
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
}