client

package
v0.108.0-b.55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 15, 2024 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package client contains types and logic dealing with AdGuard Home's DNS clients.

TODO(a.garipov): Expand.

Index

Constants

View Source
const ErrClosed errors.Error = "use of closed address processor"

ErrClosed is returned from [AddressProcessor.Close] if it's closed more than once.

Variables

This section is empty.

Functions

func ValidateClientID added in v0.107.46

func ValidateClientID(id string) (err error)

ValidateClientID returns an error if id is not a valid ClientID.

TODO(s.chzhen): It's an exact copy of the [dnsforward.ValidateClientID] to avoid the import cycle. Remove it.

Types

type AddressProcessor

type AddressProcessor interface {
	Process(ip netip.Addr)
	Close() (err error)
}

AddressProcessor is the interface for types that can process clients.

type AddressUpdater

type AddressUpdater interface {
	// UpdateAddress updates information about an IP address, setting host (if
	// not empty) and WHOIS information (if not nil).
	UpdateAddress(ip netip.Addr, host string, info *whois.Info)
}

AddressUpdater is the interface for storages of DNS clients that can update information about them.

TODO(a.garipov): Consider using the actual client storage once it is moved into this package.

type DefaultAddrProc

type DefaultAddrProc struct {
	// contains filtered or unexported fields
}

DefaultAddrProc processes incoming client addresses with rDNS and WHOIS, if configured, and updates that information in a client storage.

func NewDefaultAddrProc

func NewDefaultAddrProc(c *DefaultAddrProcConfig) (p *DefaultAddrProc)

NewDefaultAddrProc returns a new running client address processor. c must not be nil.

func (*DefaultAddrProc) Close

func (p *DefaultAddrProc) Close() (err error)

Close implements the AddressProcessor interface for *DefaultAddrProc.

func (*DefaultAddrProc) Process

func (p *DefaultAddrProc) Process(ip netip.Addr)

Process implements the AddressProcessor interface for *DefaultAddrProc.

type DefaultAddrProcConfig

type DefaultAddrProcConfig struct {
	// DialContext is used to create TCP connections to WHOIS servers.
	// DialContext must not be nil if [DefaultAddrProcConfig.UseWHOIS] is true.
	DialContext aghnet.DialContextFunc

	// Exchanger is used to perform rDNS queries.  Exchanger must not be nil if
	// [DefaultAddrProcConfig.UseRDNS] is true.
	Exchanger rdns.Exchanger

	// PrivateSubnets are used to determine if an incoming IP address is
	// private.  It must not be nil.
	PrivateSubnets netutil.SubnetSet

	// AddressUpdater is used to update the information about a client's IP
	// address.  It must not be nil.
	AddressUpdater AddressUpdater

	// InitialAddresses are the addresses that are queued for processing
	// immediately by [NewDefaultAddrProc].
	InitialAddresses []netip.Addr

	// CatchPanics, if true, makes the address processor catch and log panics.
	//
	// TODO(a.garipov): Consider better ways to do this or apply this method to
	// other parts of the codebase.
	CatchPanics bool

	// UseRDNS, if true, enables resolving of client IP addresses using reverse
	// DNS.
	UseRDNS bool

	// UsePrivateRDNS, if true, enables resolving of private client IP addresses
	// using reverse DNS.  See [DefaultAddrProcConfig.PrivateSubnets].
	UsePrivateRDNS bool

	// UseWHOIS, if true, enables resolving of client IP addresses using WHOIS.
	UseWHOIS bool
}

DefaultAddrProcConfig is the configuration structure for address processors.

type EmptyAddrProc

type EmptyAddrProc struct{}

EmptyAddrProc is an AddressProcessor that does nothing.

func (EmptyAddrProc) Close

func (EmptyAddrProc) Close() (_ error)

Close implements the AddressProcessor interface for EmptyAddrProc.

func (EmptyAddrProc) Process

func (EmptyAddrProc) Process(_ netip.Addr)

Process implements the AddressProcessor interface for EmptyAddrProc.

type Index added in v0.107.46

type Index struct {
	// contains filtered or unexported fields
}

Index stores all information about persistent clients.

func NewIndex added in v0.107.46

func NewIndex() (ci *Index)

NewIndex initializes the new instance of client index.

func (*Index) Add added in v0.107.46

func (ci *Index) Add(c *Persistent)

Add stores information about a persistent client in the index. c must be non-nil and contain UID.

func (*Index) Clashes added in v0.107.46

func (ci *Index) Clashes(c *Persistent) (err error)

Clashes returns an error if the index contains a different persistent client with at least a single identifier contained by c. c must be non-nil.

func (*Index) ClashesUID added in v0.107.49

func (ci *Index) ClashesUID(c *Persistent) (err error)

ClashesUID returns existing persistent client with the same UID as c. Note that this is only possible when configuration contains duplicate fields.

func (*Index) CloseUpstreams added in v0.107.49

func (ci *Index) CloseUpstreams() (err error)

CloseUpstreams closes upstream configurations of persistent clients.

func (*Index) Delete added in v0.107.46

func (ci *Index) Delete(c *Persistent)

Delete removes information about persistent client from the index. c must be non-nil.

func (*Index) Find added in v0.107.46

func (ci *Index) Find(id string) (c *Persistent, ok bool)

Find finds persistent client by string representation of the client ID, IP address, or MAC.

func (*Index) FindByIPWithoutZone added in v0.107.49

func (ci *Index) FindByIPWithoutZone(ip netip.Addr) (c *Persistent)

FindByIPWithoutZone finds a persistent client by IP address without zone. It strips the IPv6 zone index from the stored IP addresses before comparing, because querylog entries don't have it. See TODO on [querylog.logEntry.IP].

Note that multiple clients can have the same IP address with different zones. Therefore, the result of this method is indeterminate.

func (*Index) FindByMAC added in v0.107.49

func (ci *Index) FindByMAC(mac net.HardwareAddr) (c *Persistent, found bool)

FindByMAC finds persistent client by MAC.

func (*Index) FindByName added in v0.107.49

func (ci *Index) FindByName(name string) (c *Persistent, found bool)

FindByName finds persistent client by name.

func (*Index) Range added in v0.107.49

func (ci *Index) Range(f func(c *Persistent) (cont bool))

Range calls f for each persistent client, unless cont is false. The order is undefined.

func (*Index) RangeByName added in v0.107.49

func (ci *Index) RangeByName(f func(c *Persistent) (cont bool))

RangeByName is like Index.Range but sorts the persistent clients by name before iterating ensuring a predictable order.

func (*Index) Size added in v0.107.49

func (ci *Index) Size() (n int)

Size returns the number of persistent clients.

type Persistent added in v0.107.46

type Persistent struct {
	// UpstreamConfig is the custom upstream configuration for this client.  If
	// it's nil, it has not been initialized yet.  If it's non-nil and empty,
	// there are no valid upstreams.  If it's non-nil and non-empty, these
	// upstream must be used.
	UpstreamConfig *proxy.CustomUpstreamConfig

	SafeSearch filtering.SafeSearch

	// BlockedServices is the configuration of blocked services of a client.
	BlockedServices *filtering.BlockedServices

	Name string

	Tags      []string
	Upstreams []string

	IPs []netip.Addr
	// TODO(s.chzhen):  Use netutil.Prefix.
	Subnets   []netip.Prefix
	MACs      []net.HardwareAddr
	ClientIDs []string

	// UID is the unique identifier of the persistent client.
	UID UID

	UpstreamsCacheSize    uint32
	UpstreamsCacheEnabled bool

	UseOwnSettings        bool
	FilteringEnabled      bool
	SafeBrowsingEnabled   bool
	ParentalEnabled       bool
	UseOwnBlockedServices bool
	IgnoreQueryLog        bool
	IgnoreStatistics      bool

	// TODO(d.kolyshev): Make SafeSearchConf a pointer.
	SafeSearchConf filtering.SafeSearchConfig
}

Persistent contains information about persistent clients.

func (*Persistent) CloseUpstreams added in v0.107.46

func (c *Persistent) CloseUpstreams() (err error)

CloseUpstreams closes the client-specific upstream config of c if any.

func (*Persistent) EqualIDs added in v0.107.46

func (c *Persistent) EqualIDs(prev *Persistent) (equal bool)

EqualIDs returns true if the ids of the current and previous clients are the same.

func (*Persistent) IDs added in v0.107.46

func (c *Persistent) IDs() (ids []string)

IDs returns a list of client IDs containing at least one element.

func (*Persistent) IDsLen added in v0.107.46

func (c *Persistent) IDsLen() (n int)

IDsLen returns a length of client ids.

func (*Persistent) SetIDs added in v0.107.46

func (c *Persistent) SetIDs(ids []string) (err error)

SetIDs parses a list of strings into typed fields and returns an error if there is one.

func (*Persistent) SetSafeSearch added in v0.107.46

func (c *Persistent) SetSafeSearch(
	conf filtering.SafeSearchConfig,
	cacheSize uint,
	cacheTTL time.Duration,
) (err error)

SetSafeSearch initializes and sets the safe search filter for this client.

func (*Persistent) SetTags added in v0.107.46

func (c *Persistent) SetTags(tags []string, known *container.MapSet[string])

SetTags sets the tags if they are known, otherwise logs an unknown tag.

func (*Persistent) ShallowClone added in v0.107.46

func (c *Persistent) ShallowClone() (clone *Persistent)

ShallowClone returns a deep copy of the client, except upstreamConfig, safeSearchConf, SafeSearch fields, because it's difficult to copy them.

type Runtime added in v0.107.44

type Runtime struct {
	// contains filtered or unexported fields
}

Runtime is a client information from different sources.

func NewRuntime added in v0.107.49

func NewRuntime(ip netip.Addr) (r *Runtime)

NewRuntime constructs a new runtime client. ip must be valid IP address.

TODO(s.chzhen): Validate IP address.

func (*Runtime) Addr added in v0.107.49

func (r *Runtime) Addr() (ip netip.Addr)

Addr returns an IP address of the client.

func (*Runtime) Info added in v0.107.44

func (r *Runtime) Info() (cs Source, host string)

Info returns a client information from the highest-priority source.

func (*Runtime) SetInfo added in v0.107.44

func (r *Runtime) SetInfo(cs Source, hosts []string)

SetInfo sets a host as a client information from the cs.

func (*Runtime) SetWHOIS added in v0.107.44

func (r *Runtime) SetWHOIS(info *whois.Info)

SetWHOIS sets a WHOIS client information. info must be non-nil.

func (*Runtime) WHOIS added in v0.107.44

func (r *Runtime) WHOIS() (info *whois.Info)

WHOIS returns a WHOIS client information.

type RuntimeIndex added in v0.107.49

type RuntimeIndex struct {
	// contains filtered or unexported fields
}

RuntimeIndex stores information about runtime clients.

func NewRuntimeIndex added in v0.107.49

func NewRuntimeIndex() (ri *RuntimeIndex)

NewRuntimeIndex returns initialized runtime index.

func (*RuntimeIndex) Add added in v0.107.49

func (ri *RuntimeIndex) Add(rc *Runtime)

Add saves the runtime client in the index. IP address of a client must be unique. See [Runtime.Client]. rc must not be nil.

func (*RuntimeIndex) Client added in v0.107.49

func (ri *RuntimeIndex) Client(ip netip.Addr) (rc *Runtime)

Client returns the saved runtime client by ip. If no such client exists, returns nil.

func (*RuntimeIndex) Delete added in v0.107.49

func (ri *RuntimeIndex) Delete(ip netip.Addr)

Delete removes the runtime client by ip.

func (*RuntimeIndex) DeleteBySource added in v0.107.49

func (ri *RuntimeIndex) DeleteBySource(src Source) (n int)

DeleteBySource removes all runtime clients that have information only from the specified source and returns the number of removed clients.

func (*RuntimeIndex) Range added in v0.107.49

func (ri *RuntimeIndex) Range(f func(rc *Runtime) (cont bool))

Range calls f for each runtime client in an undefined order.

func (*RuntimeIndex) Size added in v0.107.49

func (ri *RuntimeIndex) Size() (n int)

Size returns the number of the runtime clients.

type Source added in v0.107.37

type Source uint8

Source represents the source from which the information about the client has been obtained.

const (
	SourceWHOIS Source = iota + 1
	SourceARP
	SourceRDNS
	SourceDHCP
	SourceHostsFile
	SourcePersistent
)

Clients information sources. The order determines the priority.

func (Source) MarshalText added in v0.107.37

func (cs Source) MarshalText() (text []byte, err error)

MarshalText implements encoding.TextMarshaler for the Source.

func (Source) String added in v0.107.37

func (cs Source) String() (s string)

String returns a human-readable name of cs.

type UID added in v0.107.46

type UID uuid.UUID

UID is the type for the unique IDs of persistent clients.

func MustNewUID added in v0.107.46

func MustNewUID() (uid UID)

MustNewUID is a wrapper around NewUID that panics if there is an error.

func NewUID added in v0.107.46

func NewUID() (uid UID, err error)

NewUID returns a new persistent client UID. Any error returned is an error from the cryptographic randomness reader.

func (UID) MarshalText added in v0.107.46

func (uid UID) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler for UID.

func (*UID) UnmarshalText added in v0.107.46

func (uid *UID) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface for UID.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL