2024-06-26 14:30:02 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/AdguardTeam/golibs/container"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
)
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
// Config is the client storage configuration structure.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Expand.
|
|
|
|
type Config struct {
|
|
|
|
// AllowedTags is a list of all allowed client tags.
|
|
|
|
AllowedTags []string
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:30:02 +03:00
|
|
|
// Storage contains information about persistent and runtime clients.
|
|
|
|
type Storage struct {
|
|
|
|
// allowedTags is a set of all allowed tags.
|
|
|
|
allowedTags *container.MapSet[string]
|
|
|
|
|
|
|
|
// mu protects indexes of persistent and runtime clients.
|
|
|
|
mu *sync.Mutex
|
|
|
|
|
|
|
|
// index contains information about persistent clients.
|
2024-07-01 17:34:47 +03:00
|
|
|
index *index
|
2024-06-26 14:30:02 +03:00
|
|
|
|
|
|
|
// runtimeIndex contains information about runtime clients.
|
2024-07-01 17:34:47 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use it.
|
2024-06-26 14:30:02 +03:00
|
|
|
runtimeIndex *RuntimeIndex
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
// NewStorage returns initialized client storage. conf must not be nil.
|
|
|
|
func NewStorage(conf *Config) (s *Storage) {
|
|
|
|
allowedTags := container.NewMapSet(conf.AllowedTags...)
|
|
|
|
|
2024-06-26 14:30:02 +03:00
|
|
|
return &Storage{
|
|
|
|
allowedTags: allowedTags,
|
|
|
|
mu: &sync.Mutex{},
|
2024-07-01 17:34:47 +03:00
|
|
|
index: newIndex(),
|
2024-06-26 14:30:02 +03:00
|
|
|
runtimeIndex: NewRuntimeIndex(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add stores persistent client information or returns an error.
|
|
|
|
func (s *Storage) Add(p *Persistent) (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "adding client: %w") }()
|
|
|
|
|
|
|
|
err = p.validate(s.allowedTags)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
err = s.index.clashesUID(p)
|
2024-06-26 14:30:02 +03:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
err = s.index.clashes(p)
|
2024-06-26 14:30:02 +03:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
s.index.add(p)
|
2024-06-26 14:30:02 +03:00
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
log.Debug("client storage: added %q: IDs: %q [%d]", p.Name, p.IDs(), s.index.size())
|
2024-06-26 14:30:02 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
// FindByName finds persistent client by name. And returns its shallow copy.
|
|
|
|
func (s *Storage) FindByName(name string) (p *Persistent, ok bool) {
|
2024-06-26 14:30:02 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
p, ok = s.index.findByName(name)
|
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
2024-06-26 14:30:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find finds persistent client by string representation of the client ID, IP
|
2024-07-01 17:34:47 +03:00
|
|
|
// address, or MAC. And returns its shallow copy.
|
2024-06-26 14:30:02 +03:00
|
|
|
func (s *Storage) Find(id string) (p *Persistent, ok bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
p, ok = s.index.find(id)
|
2024-06-26 14:30:02 +03:00
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindLoose is like [Storage.Find] but it also tries to find 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 (s *Storage) FindLoose(ip netip.Addr, id string) (p *Persistent, ok bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
p, ok = s.index.find(id)
|
2024-06-26 14:30:02 +03:00
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
p = s.index.findByIPWithoutZone(ip)
|
2024-06-26 14:30:02 +03:00
|
|
|
if p != nil {
|
|
|
|
return p.ShallowClone(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
// FindByMAC finds persistent client by MAC and returns its shallow copy.
|
|
|
|
func (s *Storage) FindByMAC(mac net.HardwareAddr) (p *Persistent, ok bool) {
|
2024-06-26 14:30:02 +03:00
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
p, ok = s.index.findByMAC(mac)
|
|
|
|
if ok {
|
|
|
|
return p.ShallowClone(), ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false
|
2024-06-26 14:30:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveByName removes persistent client information. ok is false if no such
|
|
|
|
// client exists by that name.
|
|
|
|
func (s *Storage) RemoveByName(name string) (ok bool) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
p, ok := s.index.findByName(name)
|
2024-06-26 14:30:02 +03:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.CloseUpstreams(); err != nil {
|
|
|
|
log.Error("client storage: removing client %q: %s", p.Name, err)
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
s.index.remove(p)
|
2024-06-26 14:30:02 +03:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update finds the stored persistent client by its name and updates its
|
|
|
|
// information from p.
|
|
|
|
func (s *Storage) Update(name string, p *Persistent) (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "updating client: %w") }()
|
|
|
|
|
|
|
|
err = p.validate(s.allowedTags)
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
stored, ok := s.index.findByName(name)
|
2024-06-26 14:30:02 +03:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("client %q is not found", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client p has a newly generated UID, so replace it with the stored one.
|
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Remove when frontend starts handling UIDs.
|
|
|
|
p.UID = stored.UID
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
err = s.index.clashes(p)
|
2024-06-26 14:30:02 +03:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since there is already an annotation deferred.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
s.index.remove(stored)
|
|
|
|
s.index.add(p)
|
2024-06-26 14:30:02 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeByName calls f for each persistent client sorted by name, unless cont is
|
|
|
|
// false.
|
|
|
|
func (s *Storage) RangeByName(f func(c *Persistent) (cont bool)) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
s.index.rangeByName(f)
|
2024-06-26 14:30:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size returns the number of persistent clients.
|
|
|
|
func (s *Storage) Size() (n int) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
return s.index.size()
|
2024-06-26 14:30:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseUpstreams closes upstream configurations of persistent clients.
|
|
|
|
func (s *Storage) CloseUpstreams() (err error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
2024-07-01 17:34:47 +03:00
|
|
|
return s.index.closeUpstreams()
|
2024-06-26 14:30:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientRuntime returns a copy of the saved runtime client by ip. If no such
|
|
|
|
// client exists, returns nil.
|
2024-07-01 17:34:47 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use it.
|
2024-06-26 14:30:02 +03:00
|
|
|
func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
return s.runtimeIndex.Client(ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRuntime saves the runtime client information in the storage. IP address
|
|
|
|
// of a client must be unique. rc must not be nil.
|
2024-07-01 17:34:47 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use it.
|
2024-06-26 14:30:02 +03:00
|
|
|
func (s *Storage) AddRuntime(rc *Runtime) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.runtimeIndex.Add(rc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SizeRuntime returns the number of the runtime clients.
|
2024-07-01 17:34:47 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use it.
|
2024-06-26 14:30:02 +03:00
|
|
|
func (s *Storage) SizeRuntime() (n int) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
return s.runtimeIndex.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RangeRuntime calls f for each runtime client in an undefined order.
|
2024-07-01 17:34:47 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use it.
|
2024-06-26 14:30:02 +03:00
|
|
|
func (s *Storage) RangeRuntime(f func(rc *Runtime) (cont bool)) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.runtimeIndex.Range(f)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRuntime removes the runtime client by ip.
|
2024-07-01 17:34:47 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use it.
|
2024-06-26 14:30:02 +03:00
|
|
|
func (s *Storage) DeleteRuntime(ip netip.Addr) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
s.runtimeIndex.Delete(ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBySource removes all runtime clients that have information only from
|
|
|
|
// the specified source and returns the number of removed clients.
|
2024-07-01 17:34:47 +03:00
|
|
|
//
|
|
|
|
// TODO(s.chzhen): Use it.
|
2024-06-26 14:30:02 +03:00
|
|
|
func (s *Storage) DeleteBySource(src Source) (n int) {
|
|
|
|
s.mu.Lock()
|
|
|
|
defer s.mu.Unlock()
|
|
|
|
|
|
|
|
return s.runtimeIndex.DeleteBySource(src)
|
|
|
|
}
|