Skip to content

Commit

Permalink
all: correct naming
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Apr 12, 2021
1 parent cbe2b2e commit 7f1386f
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 104 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ and this project adheres to

### Added

- New flag --no-localhosts to disable client domain name lookups in the
- New flag `--no-etc-hosts` to disable client domain name lookups in the
operating system's /etc/hosts files ([#1947]).
- The ability to set up custom upstreams to resolve PTR queries for local
addresses and to disable the automatic resolving of clients' addresses
Expand Down
128 changes: 64 additions & 64 deletions internal/aghnet/etchostscontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,67 +42,67 @@ type EtcHostsContainer struct {
}

// SetOnChanged - set callback function that will be called when the data is changed
func (a *EtcHostsContainer) SetOnChanged(onChanged onChangedT) {
a.onChanged = onChanged
func (ehc *EtcHostsContainer) SetOnChanged(onChanged onChangedT) {
ehc.onChanged = onChanged
}

// Notify other modules
func (a *EtcHostsContainer) notify() {
if a.onChanged == nil {
func (ehc *EtcHostsContainer) notify() {
if ehc.onChanged == nil {
return
}
a.onChanged()
ehc.onChanged()
}

// Init - initialize
// hostsFn: Override default name for the hosts-file (optional)
func (a *EtcHostsContainer) Init(hostsFn string) {
a.table = make(map[string][]net.IP)
a.onlyWritesChan = make(chan fsnotify.Event, 2)
func (ehc *EtcHostsContainer) Init(hostsFn string) {
ehc.table = make(map[string][]net.IP)
ehc.onlyWritesChan = make(chan fsnotify.Event, 2)

a.hostsFn = "/etc/hosts"
ehc.hostsFn = "/etc/hosts"
if runtime.GOOS == "windows" {
a.hostsFn = os.ExpandEnv("$SystemRoot\\system32\\drivers\\etc\\hosts")
ehc.hostsFn = os.ExpandEnv("$SystemRoot\\system32\\drivers\\etc\\hosts")
}
if len(hostsFn) != 0 {
a.hostsFn = hostsFn
ehc.hostsFn = hostsFn
}

if aghos.IsOpenWrt() {
// OpenWrt: "/tmp/hosts/dhcp.cfg01411c".
a.hostsDirs = append(a.hostsDirs, "/tmp/hosts")
ehc.hostsDirs = append(ehc.hostsDirs, "/tmp/hosts")
}

// Load hosts initially
a.updateHosts()
ehc.updateHosts()

var err error
a.watcher, err = fsnotify.NewWatcher()
ehc.watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Error("etchostscontainer: %s", err)
}
}

// Start - start module
func (a *EtcHostsContainer) Start() {
if a == nil {
func (ehc *EtcHostsContainer) Start() {
if ehc == nil {
return
}

log.Debug("Start etchostscontainer module")

a.updateHosts()
ehc.updateHosts()

if a.watcher != nil {
go a.watcherLoop()
if ehc.watcher != nil {
go ehc.watcherLoop()

err := a.watcher.Add(a.hostsFn)
err := ehc.watcher.Add(ehc.hostsFn)
if err != nil {
log.Error("Error while initializing watcher for a file %s: %s", a.hostsFn, err)
log.Error("Error while initializing watcher for a file %s: %s", ehc.hostsFn, err)
}

for _, dir := range a.hostsDirs {
err = a.watcher.Add(dir)
for _, dir := range ehc.hostsDirs {
err = ehc.watcher.Add(dir)
if err != nil {
log.Error("Error while initializing watcher for a directory %s: %s", dir, err)
}
Expand All @@ -111,29 +111,29 @@ func (a *EtcHostsContainer) Start() {
}

// Close - close module
func (a *EtcHostsContainer) Close() {
if a == nil {
func (ehc *EtcHostsContainer) Close() {
if ehc == nil {
return
}

if a.watcher != nil {
_ = a.watcher.Close()
if ehc.watcher != nil {
_ = ehc.watcher.Close()
}
close(a.onlyWritesChan)
close(ehc.onlyWritesChan)
}

// Process returns the list of IP addresses for the hostname or nil if nothing
// found.
func (a *EtcHostsContainer) Process(host string, qtype uint16) []net.IP {
func (ehc *EtcHostsContainer) Process(host string, qtype uint16) []net.IP {
if qtype == dns.TypePTR {
return nil
}

var ipsCopy []net.IP
a.lock.RLock()
defer a.lock.RUnlock()
ehc.lock.RLock()
defer ehc.lock.RUnlock()

if ips, ok := a.table[host]; ok {
if ips, ok := ehc.table[host]; ok {
ipsCopy = make([]net.IP, len(ips))
copy(ipsCopy, ips)
}
Expand All @@ -143,7 +143,7 @@ func (a *EtcHostsContainer) Process(host string, qtype uint16) []net.IP {
}

// ProcessReverse processes a PTR request. It returns nil if nothing is found.
func (a *EtcHostsContainer) ProcessReverse(addr string, qtype uint16) (hosts []string) {
func (ehc *EtcHostsContainer) ProcessReverse(addr string, qtype uint16) (hosts []string) {
if qtype != dns.TypePTR {
return nil
}
Expand All @@ -155,10 +155,10 @@ func (a *EtcHostsContainer) ProcessReverse(addr string, qtype uint16) (hosts []s

ipStr := ipReal.String()

a.lock.RLock()
defer a.lock.RUnlock()
ehc.lock.RLock()
defer ehc.lock.RUnlock()

hosts = a.tableReverse[ipStr]
hosts = ehc.tableReverse[ipStr]

if len(hosts) == 0 {
return nil // not found
Expand All @@ -170,20 +170,20 @@ func (a *EtcHostsContainer) ProcessReverse(addr string, qtype uint16) (hosts []s
}

// List returns an IP-to-hostnames table. It is safe for concurrent use.
func (a *EtcHostsContainer) List() (ipToHosts map[string][]string) {
a.lock.RLock()
defer a.lock.RUnlock()
func (ehc *EtcHostsContainer) List() (ipToHosts map[string][]string) {
ehc.lock.RLock()
defer ehc.lock.RUnlock()

ipToHosts = make(map[string][]string, len(a.tableReverse))
for k, v := range a.tableReverse {
ipToHosts = make(map[string][]string, len(ehc.tableReverse))
for k, v := range ehc.tableReverse {
ipToHosts[k] = v
}

return ipToHosts
}

// update table
func (a *EtcHostsContainer) updateTable(table map[string][]net.IP, host string, ipAddr net.IP) {
func (ehc *EtcHostsContainer) updateTable(table map[string][]net.IP, host string, ipAddr net.IP) {
ips, ok := table[host]
if ok {
for _, ip := range ips {
Expand All @@ -207,7 +207,7 @@ func (a *EtcHostsContainer) updateTable(table map[string][]net.IP, host string,
}

// updateTableRev updates the reverse address table.
func (a *EtcHostsContainer) updateTableRev(tableRev map[string][]string, newHost string, ipAddr net.IP) {
func (ehc *EtcHostsContainer) updateTableRev(tableRev map[string][]string, newHost string, ipAddr net.IP) {
ipStr := ipAddr.String()
hosts, ok := tableRev[ipStr]
if !ok {
Expand All @@ -229,7 +229,7 @@ func (a *EtcHostsContainer) updateTableRev(tableRev map[string][]string, newHost

// Read IP-hostname pairs from file
// Multiple hostnames per line (per one IP) is supported.
func (a *EtcHostsContainer) load(table map[string][]net.IP, tableRev map[string][]string, fn string) {
func (ehc *EtcHostsContainer) load(table map[string][]net.IP, tableRev map[string][]string, fn string) {
f, err := os.Open(fn)
if err != nil {
log.Error("etchostscontainer: %s", err)
Expand Down Expand Up @@ -279,8 +279,8 @@ func (a *EtcHostsContainer) load(table map[string][]net.IP, tableRev map[string]
host = host[:sharp]
}

a.updateTable(table, host, ip)
a.updateTableRev(tableRev, host, ip)
ehc.updateTable(table, host, ip)
ehc.updateTableRev(tableRev, host, ip)
if sharp >= 0 {
// Skip the comments again.
break
Expand All @@ -290,20 +290,20 @@ func (a *EtcHostsContainer) load(table map[string][]net.IP, tableRev map[string]
}

// onlyWrites is a filter for (*fsnotify.Watcher).Events.
func (a *EtcHostsContainer) onlyWrites() {
for event := range a.watcher.Events {
func (ehc *EtcHostsContainer) onlyWrites() {
for event := range ehc.watcher.Events {
if event.Op&fsnotify.Write == fsnotify.Write {
a.onlyWritesChan <- event
ehc.onlyWritesChan <- event
}
}
}

// Receive notifications from fsnotify package
func (a *EtcHostsContainer) watcherLoop() {
go a.onlyWrites()
func (ehc *EtcHostsContainer) watcherLoop() {
go ehc.onlyWrites()
for {
select {
case event, ok := <-a.onlyWritesChan:
case event, ok := <-ehc.onlyWritesChan:
if !ok {
return
}
Expand All @@ -313,7 +313,7 @@ func (a *EtcHostsContainer) watcherLoop() {
repeat := true
for repeat {
select {
case _, ok = <-a.onlyWritesChan:
case _, ok = <-ehc.onlyWritesChan:
repeat = ok
default:
repeat = false
Expand All @@ -322,10 +322,10 @@ func (a *EtcHostsContainer) watcherLoop() {

if event.Op&fsnotify.Write == fsnotify.Write {
log.Debug("etchostscontainer: modified: %s", event.Name)
a.updateHosts()
ehc.updateHosts()
}

case err, ok := <-a.watcher.Errors:
case err, ok := <-ehc.watcher.Errors:
if !ok {
return
}
Expand All @@ -335,13 +335,13 @@ func (a *EtcHostsContainer) watcherLoop() {
}

// updateHosts - loads system hosts
func (a *EtcHostsContainer) updateHosts() {
func (ehc *EtcHostsContainer) updateHosts() {
table := make(map[string][]net.IP)
tableRev := make(map[string][]string)

a.load(table, tableRev, a.hostsFn)
ehc.load(table, tableRev, ehc.hostsFn)

for _, dir := range a.hostsDirs {
for _, dir := range ehc.hostsDirs {
fis, err := ioutil.ReadDir(dir)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
Expand All @@ -352,17 +352,17 @@ func (a *EtcHostsContainer) updateHosts() {
}

for _, fi := range fis {
a.load(table, tableRev, filepath.Join(dir, fi.Name()))
ehc.load(table, tableRev, filepath.Join(dir, fi.Name()))
}
}

func() {
a.lock.Lock()
defer a.lock.Unlock()
ehc.lock.Lock()
defer ehc.lock.Unlock()

a.table = table
a.tableReverse = tableRev
ehc.table = table
ehc.tableReverse = tableRev
}()

a.notify()
ehc.notify()
}
26 changes: 13 additions & 13 deletions internal/aghnet/etshostscontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,33 @@ func assertWriting(t *testing.T, f *os.File, strs ...string) {
}

func TestEtcHostsContainerResolution(t *testing.T) {
ah := &EtcHostsContainer{}
ehc := &EtcHostsContainer{}

f := prepareTestFile(t)

assertWriting(t, f,
" 127.0.0.1 host localhost # comment \n",
" ::1 localhost#comment \n",
)
ah.Init(f.Name())
ehc.Init(f.Name())

t.Run("existing_host", func(t *testing.T) {
ips := ah.Process("localhost", dns.TypeA)
ips := ehc.Process("localhost", dns.TypeA)
require.Len(t, ips, 1)
assert.Equal(t, net.IPv4(127, 0, 0, 1), ips[0])
})

t.Run("unknown_host", func(t *testing.T) {
ips := ah.Process("newhost", dns.TypeA)
ips := ehc.Process("newhost", dns.TypeA)
assert.Nil(t, ips)

// Comment.
ips = ah.Process("comment", dns.TypeA)
ips = ehc.Process("comment", dns.TypeA)
assert.Nil(t, ips)
})

t.Run("hosts_file", func(t *testing.T) {
names, ok := ah.List()["127.0.0.1"]
names, ok := ehc.List()["127.0.0.1"]
require.True(t, ok)
assert.Equal(t, []string{"host", "localhost"}, names)
})
Expand All @@ -90,29 +90,29 @@ func TestEtcHostsContainerResolution(t *testing.T) {
require.Nil(t, err)

a = strings.TrimSuffix(a, ".")
hosts := ah.ProcessReverse(a, dns.TypePTR)
hosts := ehc.ProcessReverse(a, dns.TypePTR)
require.Len(t, hosts, tc.wantLen)
assert.Equal(t, tc.wantHost, hosts[0])
}
})
}

func TestEtcHostsContainerFSNotify(t *testing.T) {
ah := &EtcHostsContainer{}
ehc := &EtcHostsContainer{}

f := prepareTestFile(t)

assertWriting(t, f, " 127.0.0.1 host localhost \n")
ah.Init(f.Name())
ehc.Init(f.Name())

t.Run("unknown_host", func(t *testing.T) {
ips := ah.Process("newhost", dns.TypeA)
ips := ehc.Process("newhost", dns.TypeA)
assert.Nil(t, ips)
})

// Start monitoring for changes.
ah.Start()
t.Cleanup(ah.Close)
ehc.Start()
t.Cleanup(ehc.Close)

assertWriting(t, f, "127.0.0.2 newhost\n")
require.Nil(t, f.Sync())
Expand All @@ -122,7 +122,7 @@ func TestEtcHostsContainerFSNotify(t *testing.T) {
time.Sleep(50 * time.Millisecond)

t.Run("notified", func(t *testing.T) {
ips := ah.Process("newhost", dns.TypeA)
ips := ehc.Process("newhost", dns.TypeA)
assert.NotNil(t, ips)
require.Len(t, ips, 1)
assert.True(t, net.IP{127, 0, 0, 2}.Equal(ips[0]))
Expand Down
Loading

0 comments on commit 7f1386f

Please sign in to comment.