home: imp client handling

This commit is contained in:
Ainar Garipov 2021-12-13 15:18:21 +03:00
parent 0e1015a4ee
commit 39da2f9eaa
3 changed files with 50 additions and 49 deletions

View File

@ -106,7 +106,7 @@ type clientsContainer struct {
// dhcpServer: optional // dhcpServer: optional
// Note: this function must be called only once // Note: this function must be called only once
func (clients *clientsContainer) Init( func (clients *clientsContainer) Init(
objects []clientObject, objects []*clientObject,
dhcpServer *dhcpd.Server, dhcpServer *dhcpd.Server,
etcHosts *aghnet.HostsContainer, etcHosts *aghnet.HostsContainer,
) { ) {
@ -175,59 +175,64 @@ type clientObject struct {
UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"` UseGlobalBlockedServices bool `yaml:"use_global_blocked_services"`
} }
func (clients *clientsContainer) tagKnown(tag string) (ok bool) { // addFromConfig initializes the clients containter with objects from the
return clients.allTags.Has(tag) // configuration file.
} func (clients *clientsContainer) addFromConfig(objects []*clientObject) {
for _, o := range objects {
func (clients *clientsContainer) addFromConfig(objects []clientObject) {
for _, cy := range objects {
cli := &Client{ cli := &Client{
Name: cy.Name, Name: o.Name,
IDs: cy.IDs,
UseOwnSettings: !cy.UseGlobalSettings,
FilteringEnabled: cy.FilteringEnabled,
ParentalEnabled: cy.ParentalEnabled,
SafeSearchEnabled: cy.SafeSearchEnabled,
SafeBrowsingEnabled: cy.SafeBrowsingEnabled,
UseOwnBlockedServices: !cy.UseGlobalBlockedServices, IDs: o.IDs,
Upstreams: o.Upstreams,
Upstreams: cy.Upstreams, UseOwnSettings: !o.UseGlobalSettings,
FilteringEnabled: o.FilteringEnabled,
ParentalEnabled: o.ParentalEnabled,
SafeSearchEnabled: o.SafeSearchEnabled,
SafeBrowsingEnabled: o.SafeBrowsingEnabled,
UseOwnBlockedServices: !o.UseGlobalBlockedServices,
} }
for _, s := range cy.BlockedServices { for _, s := range o.BlockedServices {
if !filtering.BlockedSvcKnown(s) { if filtering.BlockedSvcKnown(s) {
log.Debug("clients: skipping unknown blocked-service %q", s) cli.BlockedServices = append(cli.BlockedServices, s)
continue } else {
log.Info("clients: skipping unknown blocked service %q", s)
} }
cli.BlockedServices = append(cli.BlockedServices, s)
} }
for _, t := range cy.Tags { for _, t := range o.Tags {
if !clients.tagKnown(t) { if clients.allTags.Has(t) {
log.Debug("clients: skipping unknown tag %q", t) cli.Tags = append(cli.Tags, t)
continue } else {
log.Info("clients: skipping unknown tag %q", t)
} }
cli.Tags = append(cli.Tags, t)
} }
sort.Strings(cli.Tags) sort.Strings(cli.Tags)
_, err := clients.Add(cli) _, err := clients.Add(cli)
if err != nil { if err != nil {
log.Tracef("clientAdd: %s", err) log.Error("clients: adding clients %s: %s", cli.Name, err)
} }
} }
} }
// WriteDiskConfig - write configuration // forConfig returns all currently known persistent clients as objects for the
func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) { // configuration file.
func (clients *clientsContainer) forConfig() (objs []*clientObject) {
clients.lock.Lock() clients.lock.Lock()
defer clients.lock.Unlock() defer clients.lock.Unlock()
var clientObjects []clientObject
for _, cli := range clients.list { for _, cli := range clients.list {
cliObj := clientObject{ o := &clientObject{
Name: cli.Name, Name: cli.Name,
Tags: stringutil.CloneSlice(cli.Tags),
IDs: stringutil.CloneSlice(cli.IDs),
BlockedServices: stringutil.CloneSlice(cli.BlockedServices),
Upstreams: stringutil.CloneSlice(cli.Upstreams),
UseGlobalSettings: !cli.UseOwnSettings, UseGlobalSettings: !cli.UseOwnSettings,
FilteringEnabled: cli.FilteringEnabled, FilteringEnabled: cli.FilteringEnabled,
ParentalEnabled: cli.ParentalEnabled, ParentalEnabled: cli.ParentalEnabled,
@ -236,22 +241,16 @@ func (clients *clientsContainer) WriteDiskConfig(objects *[]clientObject) {
UseGlobalBlockedServices: !cli.UseOwnBlockedServices, UseGlobalBlockedServices: !cli.UseOwnBlockedServices,
} }
cliObj.Tags = stringutil.CloneSlice(cli.Tags) objs = append(objs, o)
cliObj.IDs = stringutil.CloneSlice(cli.IDs)
cliObj.BlockedServices = stringutil.CloneSlice(cli.BlockedServices)
cliObj.Upstreams = stringutil.CloneSlice(cli.Upstreams)
clientObjects = append(clientObjects, cliObj)
} }
// Maps aren't guaranteed to iterate in the same order each time, so the // Maps aren't guaranteed to iterate in the same order each time, so the
// above loop can generate different orderings when writing to the config // above loop can generate different orderings when writing to the config
// file: this produces lots of diffs in config files, so sort objects by // file: this produces lots of diffs in config files, so sort objects by
// name before writing. // name before writing.
sort.Slice(clientObjects, func(i, j int) bool { sort.Slice(objs, func(i, j int) bool { return objs[i].Name < objs[j].Name })
return clientObjects[i].Name < clientObjects[j].Name
}) return objs
*objects = append(*objects, clientObjects...)
} }
func (clients *clientsContainer) periodicUpdate() { func (clients *clientsContainer) periodicUpdate() {
@ -537,7 +536,7 @@ func (clients *clientsContainer) check(c *Client) (err error) {
} }
for _, t := range c.Tags { for _, t := range c.Tags {
if !clients.tagKnown(t) { if !clients.allTags.Has(t) {
return fmt.Errorf("invalid tag: %q", t) return fmt.Errorf("invalid tag: %q", t)
} }
} }

View File

@ -84,8 +84,11 @@ type configuration struct {
DHCP dhcpd.ServerConfig `yaml:"dhcp"` DHCP dhcpd.ServerConfig `yaml:"dhcp"`
// Note: this array is filled only before file read/write and then it's cleared // Note: this array is filled only before file read/write and then it's
Clients []clientObject `yaml:"clients"` // cleared.
//
// TODO(a.garipov): Do something with this.
Clients []*clientObject `yaml:"clients"`
logSettings `yaml:",inline"` logSettings `yaml:",inline"`
@ -316,8 +319,6 @@ func (c *configuration) write() error {
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
Context.clients.WriteDiskConfig(&config.Clients)
if Context.auth != nil { if Context.auth != nil {
config.Users = Context.auth.GetUsers() config.Users = Context.auth.GetUsers()
} }
@ -365,10 +366,11 @@ func (c *configuration) write() error {
config.DHCP = c config.DHCP = c
} }
config.Clients = Context.clients.forConfig()
configFile := config.getConfigFilename() configFile := config.getConfigFilename()
log.Debug("Writing YAML file: %s", configFile) log.Debug("Writing YAML file: %s", configFile)
yamlText, err := yaml.Marshal(&config) yamlText, err := yaml.Marshal(&config)
config.Clients = nil
if err != nil { if err != nil {
log.Error("Couldn't generate YAML file: %s", err) log.Error("Couldn't generate YAML file: %s", err)

View File

@ -294,8 +294,8 @@ func setupConfig(args options) (err error) {
return err return err
} }
} }
Context.clients.Init(config.Clients, Context.dhcpServer, Context.etcHosts) Context.clients.Init(config.Clients, Context.dhcpServer, Context.etcHosts)
config.Clients = nil
// override bind host/port from the console // override bind host/port from the console
if args.bindHost != nil { if args.bindHost != nil {