all: imp code

This commit is contained in:
Stanislav Chzhen 2024-09-24 19:06:37 +03:00
parent 23896ae5a6
commit df7492e9de
3 changed files with 20 additions and 11 deletions

View File

@ -83,8 +83,8 @@ type HostsContainer interface {
// StorageConfig is the client storage configuration structure. // StorageConfig is the client storage configuration structure.
type StorageConfig struct { type StorageConfig struct {
// DHCP is used to update [SourceDHCP] runtime client information. It must // DHCP is used to match IPs against MACs of persistent clients and update
// not be nil. // [SourceDHCP] runtime client information. It must not be nil.
DHCP DHCP DHCP DHCP
// EtcHosts is used to update [SourceHostsFile] runtime client information. // EtcHosts is used to update [SourceHostsFile] runtime client information.
@ -100,6 +100,10 @@ type StorageConfig struct {
// ARPClientsUpdatePeriod defines how often [SourceARP] runtime client // ARPClientsUpdatePeriod defines how often [SourceARP] runtime client
// information is updated. // information is updated.
ARPClientsUpdatePeriod time.Duration ARPClientsUpdatePeriod time.Duration
// RuntimeSourceDHCP specifies whether to update [SourceDHCP] information
// of runtime clients.
RuntimeSourceDHCP bool
} }
// Storage contains information about persistent and runtime clients. // Storage contains information about persistent and runtime clients.
@ -131,6 +135,10 @@ type Storage struct {
// arpClientsUpdatePeriod defines how often [SourceARP] runtime client // arpClientsUpdatePeriod defines how often [SourceARP] runtime client
// information is updated. It must be greater than zero. // information is updated. It must be greater than zero.
arpClientsUpdatePeriod time.Duration arpClientsUpdatePeriod time.Duration
// runtimeSourceDHCP specifies whether to update [SourceDHCP] information
// of runtime clients.
runtimeSourceDHCP bool
} }
// NewStorage returns initialized client storage. conf must not be nil. // NewStorage returns initialized client storage. conf must not be nil.
@ -143,8 +151,9 @@ func NewStorage(conf *StorageConfig) (s *Storage, err error) {
dhcp: conf.DHCP, dhcp: conf.DHCP,
etcHosts: conf.EtcHosts, etcHosts: conf.EtcHosts,
arpDB: conf.ARPDB, arpDB: conf.ARPDB,
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
done: make(chan struct{}), done: make(chan struct{}),
arpClientsUpdatePeriod: conf.ARPClientsUpdatePeriod,
runtimeSourceDHCP: conf.RuntimeSourceDHCP,
} }
for i, p := range conf.InitialClients { for i, p := range conf.InitialClients {
@ -306,7 +315,7 @@ func (s *Storage) UpdateAddress(ip netip.Addr, host string, info *whois.Info) {
// UpdateDHCP updates [SourceDHCP] runtime client information. // UpdateDHCP updates [SourceDHCP] runtime client information.
func (s *Storage) UpdateDHCP() { func (s *Storage) UpdateDHCP() {
if s.dhcp == nil { if s.dhcp == nil || !s.runtimeSourceDHCP {
return return
} }
@ -545,6 +554,10 @@ func (s *Storage) ClientRuntime(ip netip.Addr) (rc *Runtime) {
return rc.clone() return rc.clone()
} }
if !s.runtimeSourceDHCP {
return nil
}
host := s.dhcp.HostByIP(ip) host := s.dhcp.HostByIP(ip)
if host == "" { if host == "" {
return nil return nil

View File

@ -364,7 +364,8 @@ func TestClientsDHCP(t *testing.T) {
} }
storage, err := client.NewStorage(&client.StorageConfig{ storage, err := client.NewStorage(&client.StorageConfig{
DHCP: d, DHCP: d,
RuntimeSourceDHCP: true,
}) })
require.NoError(t, err) require.NoError(t, err)

View File

@ -27,9 +27,6 @@ type clientsContainer struct {
// storage stores information about persistent clients. // storage stores information about persistent clients.
storage *client.Storage storage *client.Storage
// dhcp is the DHCP service implementation.
dhcp client.DHCP
// clientChecker checks if a client is blocked by the current access // clientChecker checks if a client is blocked by the current access
// settings. // settings.
clientChecker BlockedClientChecker clientChecker BlockedClientChecker
@ -75,9 +72,6 @@ func (clients *clientsContainer) Init(
return errors.Error("clients container already initialized") return errors.Error("clients container already initialized")
} }
// TODO(e.burkov): Use [dhcpsvc] implementation when it's ready.
clients.dhcp = dhcpServer
confClients := make([]*client.Persistent, 0, len(objects)) confClients := make([]*client.Persistent, 0, len(objects))
for i, o := range objects { for i, o := range objects {
var p *client.Persistent var p *client.Persistent
@ -106,6 +100,7 @@ func (clients *clientsContainer) Init(
EtcHosts: EtcHosts, EtcHosts: EtcHosts,
ARPDB: arpDB, ARPDB: arpDB,
ARPClientsUpdatePeriod: arpClientsUpdatePeriod, ARPClientsUpdatePeriod: arpClientsUpdatePeriod,
RuntimeSourceDHCP: config.Clients.Sources.DHCP,
}) })
if err != nil { if err != nil {
return fmt.Errorf("init client storage: %w", err) return fmt.Errorf("init client storage: %w", err)