Add hosts file as source for hostname resolver

This commit is contained in:
Cuong Manh Le
2023-08-22 02:08:44 +00:00
committed by Cuong Manh Le
parent e355fd70ab
commit 82e44b01af
6 changed files with 116 additions and 0 deletions
+19
View File
@@ -73,6 +73,7 @@ type Table struct {
arp *arpDiscover
ptr *ptrDiscover
mdns *mdns
hf *hostsFile
cfg *ctrld.Config
quitCh chan struct{}
selfIP string
@@ -134,6 +135,17 @@ func (t *Table) init() {
t.refreshers = append(t.refreshers, t.merlin)
}
}
if t.discoverHosts() {
t.hf = &hostsFile{}
ctrld.ProxyLogger.Load().Debug().Msg("start hosts file discovery")
if err := t.hf.init(); err != nil {
ctrld.ProxyLogger.Load().Error().Err(err).Msg("could not init hosts file discover")
} else {
t.hostnameResolvers = append(t.hostnameResolvers, t.hf)
t.refreshers = append(t.refreshers, t.hf)
}
go t.hf.watchChanges()
}
if t.discoverDHCP() {
t.dhcp = &dhcp{selfIP: t.selfIP}
ctrld.ProxyLogger.Load().Debug().Msg("start dhcp discovery")
@@ -328,6 +340,13 @@ func (t *Table) discoverPTR() bool {
return *t.cfg.Service.DiscoverPtr
}
func (t *Table) discoverHosts() bool {
if t.cfg.Service.DiscoverHosts == nil {
return true
}
return *t.cfg.Service.DiscoverHosts
}
// normalizeIP normalizes the ip parsed from dnsmasq/dhcpd lease file.
func normalizeIP(in string) string {
// dnsmasq may put ip with interface index in lease file, strip it here.
+86
View File
@@ -0,0 +1,86 @@
package clientinfo
import (
"os"
"github.com/fsnotify/fsnotify"
"github.com/txn2/txeh"
"github.com/Control-D-Inc/ctrld"
)
// hostsFile provides client discovery functionality using system hosts file.
type hostsFile struct {
h *txeh.Hosts
watcher *fsnotify.Watcher
}
// init performs initialization works, which is necessary before hostsFile can be fully operated.
func (hf *hostsFile) init() error {
h, err := txeh.NewHostsDefault()
if err != nil {
return err
}
hf.h = h
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
hf.watcher = watcher
if err := hf.watcher.Add(hf.h.ReadFilePath); err != nil {
return err
}
return nil
}
// refresh reloads hosts file entries.
func (hf *hostsFile) refresh() error {
return hf.h.Reload()
}
// watchChanges watches and updates hosts file data if any changes happens.
func (hf *hostsFile) watchChanges() {
if hf.watcher == nil {
return
}
for {
select {
case event, ok := <-hf.watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) || event.Has(fsnotify.Rename) || event.Has(fsnotify.Chmod) || event.Has(fsnotify.Remove) {
if err := hf.refresh(); err != nil && !os.IsNotExist(err) {
ctrld.ProxyLogger.Load().Err(err).Msg("hosts file changed but failed to update client info")
}
}
case err, ok := <-hf.watcher.Errors:
if !ok {
return
}
ctrld.ProxyLogger.Load().Err(err).Msg("could not watch client info file")
}
}
}
// LookupHostnameByIP returns hostname for given IP from current hosts file entries.
func (hf *hostsFile) LookupHostnameByIP(ip string) string {
hf.h.Lock()
defer hf.h.Unlock()
if names := hf.h.ListHostsByIP(ip); len(names) > 0 {
return names[0]
}
return ""
}
// LookupHostnameByMac returns hostname for given Mac from current hosts file entries.
func (hf *hostsFile) LookupHostnameByMac(mac string) string {
return ""
}
// String returns human-readable format of hostsFile.
func (hf *hostsFile) String() string {
return "hosts"
}