* dhcp,clients: DHCP server module is passed to Clients module during initialization.

This commit is contained in:
Simon Zolin 2019-11-22 14:21:08 +03:00
parent 127a68a39f
commit 149fcc0f2d
5 changed files with 38 additions and 8 deletions

View File

@ -88,6 +88,13 @@ func (s *Server) CheckConfig(config ServerConfig) error {
return tmpServer.setConfig(config)
}
// Create - create object
func Create(config ServerConfig) *Server {
s := Server{}
s.conf = config
return &s
}
// Init checks the configuration and initializes the server
func (s *Server) Init(config ServerConfig) error {
err := s.setConfig(config)
@ -98,6 +105,11 @@ func (s *Server) Init(config ServerConfig) error {
return nil
}
// WriteDiskConfig - write configuration
func (s *Server) WriteDiskConfig(c *ServerConfig) {
*c = s.conf
}
func (s *Server) setConfig(config ServerConfig) error {
s.conf = config
s.conf.DBFilePath = filepath.Join(config.WorkDir, dbFilename)

View File

@ -12,6 +12,7 @@ import (
"sync"
"time"
"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/utils"
)
@ -59,17 +60,20 @@ type clientsContainer struct {
idIndex map[string]*Client // IP -> client
ipHost map[string]*ClientHost // IP -> Hostname
lock sync.Mutex
dhcpServer *dhcpd.Server
}
// Init initializes clients container
// Note: this function must be called only once
func (clients *clientsContainer) Init(objects []clientObject) {
func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server) {
if clients.list != nil {
log.Fatal("clients.list != nil")
}
clients.list = make(map[string]*Client)
clients.idIndex = make(map[string]*Client)
clients.ipHost = make(map[string]*ClientHost)
clients.dhcpServer = dhcpServer
clients.addFromConfig(objects)
go clients.periodicUpdate()
@ -190,7 +194,10 @@ func (clients *clientsContainer) Find(ip string) (Client, bool) {
}
}
macFound := config.dhcpServer.FindMACbyIP(ipAddr)
if clients.dhcpServer == nil {
return Client{}, false
}
macFound := clients.dhcpServer.FindMACbyIP(ipAddr)
if macFound == nil {
return Client{}, false
}
@ -533,13 +540,16 @@ func (clients *clientsContainer) addFromSystemARP() {
// add clients from DHCP that have non-empty Hostname property
func (clients *clientsContainer) addFromDHCP() {
leases := config.dhcpServer.Leases()
if clients.dhcpServer == nil {
return
}
leases := clients.dhcpServer.Leases()
n := 0
for _, l := range leases {
if len(l.Hostname) == 0 {
continue
}
ok, _ := config.clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
ok, _ := clients.AddHost(l.IP.String(), l.Hostname, ClientSourceDHCP)
if ok {
n++
}

View File

@ -12,7 +12,7 @@ func TestClients(t *testing.T) {
var b bool
clients := clientsContainer{}
clients.Init(nil)
clients.Init(nil, nil)
// add
c = Client{
@ -149,7 +149,7 @@ func TestClients(t *testing.T) {
func TestClientsWhois(t *testing.T) {
var c Client
clients := clientsContainer{}
clients.Init(nil)
clients.Init(nil, nil)
whois := [][]string{{"orgname", "orgname-val"}, {"country", "country-val"}}
// set whois info on new client

View File

@ -67,7 +67,7 @@ type configuration struct {
dnsctx dnsContext
dnsFilter *dnsfilter.Dnsfilter
dnsServer *dnsforward.Server
dhcpServer dhcpd.Server
dhcpServer *dhcpd.Server
httpServer *http.Server
httpsServer HTTPSServer
@ -325,6 +325,12 @@ func (c *configuration) write() error {
config.DNS.DnsfilterConf = c
}
if config.dhcpServer != nil {
c := dhcpd.ServerConfig{}
config.dhcpServer.WriteDiskConfig(&c)
config.DHCP = c
}
configFile := config.getConfigFilename()
log.Debug("Writing YAML file: %s", configFile)
yamlText, err := yaml.Marshal(&config)

View File

@ -20,6 +20,7 @@ import (
"syscall"
"time"
"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/golibs/log"
"github.com/NYTimes/gziphandler"
"github.com/gobuffalo/packr"
@ -118,7 +119,8 @@ func run(args options) {
}
}
config.clients.Init(config.Clients)
config.dhcpServer = dhcpd.Create(config.DHCP)
config.clients.Init(config.Clients, config.dhcpServer)
config.Clients = nil
if (runtime.GOOS == "linux" || runtime.GOOS == "darwin") &&