2021-04-12 18:31:45 +03:00
|
|
|
package aghnet
|
2020-03-20 15:05:43 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-03-15 14:19:04 +03:00
|
|
|
"errors"
|
2020-03-20 15:05:43 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
2021-03-15 14:19:04 +03:00
|
|
|
"path/filepath"
|
2020-03-20 15:05:43 +03:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-04-08 20:42:04 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2020-03-20 15:05:43 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2021-03-11 15:38:39 +03:00
|
|
|
"github.com/miekg/dns"
|
2020-03-20 15:05:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type onChangedT func()
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
// EtcHostsContainer - automatic DNS records
|
|
|
|
//
|
|
|
|
// TODO(e.burkov): Move the logic under interface. Refactor. Probably remove
|
|
|
|
// the resolving logic.
|
|
|
|
type EtcHostsContainer struct {
|
2020-11-06 17:34:40 +03:00
|
|
|
// lock protects table and tableReverse.
|
2020-12-22 15:39:50 +03:00
|
|
|
lock sync.RWMutex
|
2020-11-06 17:34:40 +03:00
|
|
|
// table is the host-to-IPs map.
|
|
|
|
table map[string][]net.IP
|
|
|
|
// tableReverse is the IP-to-hosts map.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): Make better use of newtypes. Perhaps a custom map.
|
|
|
|
tableReverse map[string][]string
|
2020-04-16 18:56:47 +03:00
|
|
|
|
2020-12-28 19:07:29 +03:00
|
|
|
hostsFn string // path to the main hosts-file
|
|
|
|
hostsDirs []string // paths to OS-specific directories with hosts-files
|
|
|
|
watcher *fsnotify.Watcher // file and directory watcher object
|
|
|
|
|
|
|
|
// onlyWritesChan used to contain only writing events from watcher.
|
|
|
|
onlyWritesChan chan fsnotify.Event
|
2020-03-20 15:05:43 +03:00
|
|
|
|
|
|
|
onChanged onChangedT // notification to other modules
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOnChanged - set callback function that will be called when the data is changed
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) SetOnChanged(onChanged onChangedT) {
|
|
|
|
ehc.onChanged = onChanged
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify other modules
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) notify() {
|
|
|
|
if ehc.onChanged == nil {
|
2020-03-20 15:05:43 +03:00
|
|
|
return
|
|
|
|
}
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.onChanged()
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initialize
|
|
|
|
// hostsFn: Override default name for the hosts-file (optional)
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) Init(hostsFn string) {
|
|
|
|
ehc.table = make(map[string][]net.IP)
|
|
|
|
ehc.onlyWritesChan = make(chan fsnotify.Event, 2)
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.hostsFn = "/etc/hosts"
|
2020-03-20 15:05:43 +03:00
|
|
|
if runtime.GOOS == "windows" {
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.hostsFn = os.ExpandEnv("$SystemRoot\\system32\\drivers\\etc\\hosts")
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
if len(hostsFn) != 0 {
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.hostsFn = hostsFn
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-08 20:42:04 +03:00
|
|
|
if aghos.IsOpenWrt() {
|
2021-03-12 14:32:08 +03:00
|
|
|
// OpenWrt: "/tmp/hosts/dhcp.cfg01411c".
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.hostsDirs = append(ehc.hostsDirs, "/tmp/hosts")
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
|
2020-09-11 11:53:36 +03:00
|
|
|
// Load hosts initially
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.updateHosts()
|
2020-09-11 11:53:36 +03:00
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
var err error
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.watcher, err = fsnotify.NewWatcher()
|
2020-03-20 15:05:43 +03:00
|
|
|
if err != nil {
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Error("etchostscontainer: %s", err)
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start - start module
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) Start() {
|
|
|
|
if ehc == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("Start etchostscontainer module")
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.updateHosts()
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
if ehc.watcher != nil {
|
|
|
|
go ehc.watcherLoop()
|
|
|
|
|
|
|
|
err := ehc.watcher.Add(ehc.hostsFn)
|
2020-03-20 15:05:43 +03:00
|
|
|
if err != nil {
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Error("Error while initializing watcher for a file %s: %s", ehc.hostsFn, err)
|
2020-06-19 14:27:23 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
for _, dir := range ehc.hostsDirs {
|
|
|
|
err = ehc.watcher.Add(dir)
|
2020-06-19 14:27:23 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error while initializing watcher for a directory %s: %s", dir, err)
|
|
|
|
}
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - close module
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) Close() {
|
|
|
|
if ehc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ehc.watcher != nil {
|
|
|
|
_ = ehc.watcher.Close()
|
2020-06-19 14:27:23 +03:00
|
|
|
}
|
2021-04-12 18:31:45 +03:00
|
|
|
close(ehc.onlyWritesChan)
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 12:15:08 +03:00
|
|
|
// Process returns the list of IP addresses for the hostname or nil if nothing
|
|
|
|
// found.
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) Process(host string, qtype uint16) []net.IP {
|
2020-09-11 11:53:36 +03:00
|
|
|
if qtype == dns.TypePTR {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipsCopy []net.IP
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.lock.RLock()
|
|
|
|
defer ehc.lock.RUnlock()
|
2020-11-06 12:15:08 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
if ips, ok := ehc.table[host]; ok {
|
2020-09-11 11:53:36 +03:00
|
|
|
ipsCopy = make([]net.IP, len(ips))
|
|
|
|
copy(ipsCopy, ips)
|
|
|
|
}
|
2020-11-06 12:15:08 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("etchostscontainer: answer: %s -> %v", host, ipsCopy)
|
2020-09-11 11:53:36 +03:00
|
|
|
return ipsCopy
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:34:40 +03:00
|
|
|
// ProcessReverse processes a PTR request. It returns nil if nothing is found.
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) ProcessReverse(addr string, qtype uint16) (hosts []string) {
|
2020-09-11 11:53:36 +03:00
|
|
|
if qtype != dns.TypePTR {
|
2020-11-06 17:34:40 +03:00
|
|
|
return nil
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ipReal := UnreverseAddr(addr)
|
2020-09-11 11:53:36 +03:00
|
|
|
if ipReal == nil {
|
2020-11-06 17:34:40 +03:00
|
|
|
return nil
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
2020-11-06 17:34:40 +03:00
|
|
|
|
2020-09-11 11:53:36 +03:00
|
|
|
ipStr := ipReal.String()
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.lock.RLock()
|
|
|
|
defer ehc.lock.RUnlock()
|
2020-09-11 11:53:36 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
hosts = ehc.tableReverse[ipStr]
|
2020-11-06 17:34:40 +03:00
|
|
|
|
|
|
|
if len(hosts) == 0 {
|
|
|
|
return nil // not found
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("etchostscontainer: reverse-lookup: %s -> %s", addr, hosts)
|
2020-11-06 17:34:40 +03:00
|
|
|
|
|
|
|
return hosts
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 17:34:40 +03:00
|
|
|
// List returns an IP-to-hostnames table. It is safe for concurrent use.
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) List() (ipToHosts map[string][]string) {
|
|
|
|
ehc.lock.RLock()
|
|
|
|
defer ehc.lock.RUnlock()
|
2020-11-06 17:34:40 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ipToHosts = make(map[string][]string, len(ehc.tableReverse))
|
|
|
|
for k, v := range ehc.tableReverse {
|
2020-11-06 17:34:40 +03:00
|
|
|
ipToHosts[k] = v
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
2020-11-06 17:34:40 +03:00
|
|
|
|
|
|
|
return ipToHosts
|
2020-09-11 11:53:36 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 18:56:47 +03:00
|
|
|
// update table
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) updateTable(table map[string][]net.IP, host string, ipAddr net.IP) {
|
2020-04-16 18:56:47 +03:00
|
|
|
ips, ok := table[host]
|
|
|
|
if ok {
|
|
|
|
for _, ip := range ips {
|
|
|
|
if ip.Equal(ipAddr) {
|
|
|
|
// IP already exists: don't add duplicates
|
|
|
|
ok = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
ips = append(ips, ipAddr)
|
|
|
|
table[host] = ips
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
table[host] = []net.IP{ipAddr}
|
|
|
|
ok = true
|
|
|
|
}
|
|
|
|
if ok {
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("etchostscontainer: added %s -> %s", ipAddr, host)
|
2020-04-16 18:56:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:34:40 +03:00
|
|
|
// updateTableRev updates the reverse address table.
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) updateTableRev(tableRev map[string][]string, newHost string, ipAddr net.IP) {
|
2020-04-16 18:56:47 +03:00
|
|
|
ipStr := ipAddr.String()
|
2020-11-06 17:34:40 +03:00
|
|
|
hosts, ok := tableRev[ipStr]
|
2020-04-16 18:56:47 +03:00
|
|
|
if !ok {
|
2020-11-06 17:34:40 +03:00
|
|
|
tableRev[ipStr] = []string{newHost}
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("etchostscontainer: added reverse-address %s -> %s", ipStr, newHost)
|
2020-11-06 17:34:40 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, host := range hosts {
|
|
|
|
if host == newHost {
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 18:56:47 +03:00
|
|
|
}
|
2020-11-06 17:34:40 +03:00
|
|
|
|
|
|
|
tableRev[ipStr] = append(tableRev[ipStr], newHost)
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("etchostscontainer: added reverse-address %s -> %s", ipStr, newHost)
|
2020-04-16 18:56:47 +03:00
|
|
|
}
|
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
// Read IP-hostname pairs from file
|
|
|
|
// Multiple hostnames per line (per one IP) is supported.
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) load(table map[string][]net.IP, tableRev map[string][]string, fn string) {
|
2020-03-20 15:05:43 +03:00
|
|
|
f, err := os.Open(fn)
|
|
|
|
if err != nil {
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Error("etchostscontainer: %s", err)
|
2020-03-20 15:05:43 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
r := bufio.NewReader(f)
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("etchostscontainer: loading hosts from file %s", fn)
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-03-11 15:38:39 +03:00
|
|
|
for done := false; !done; {
|
|
|
|
var line string
|
|
|
|
line, err = r.ReadString('\n')
|
2020-03-20 15:05:43 +03:00
|
|
|
if err == io.EOF {
|
2021-03-11 15:38:39 +03:00
|
|
|
done = true
|
2020-03-20 15:05:43 +03:00
|
|
|
} else if err != nil {
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Error("etchostscontainer: %s", err)
|
2021-03-11 15:38:39 +03:00
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
return
|
|
|
|
}
|
2021-03-11 15:38:39 +03:00
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
line = strings.TrimSpace(line)
|
2020-05-18 18:46:17 +03:00
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-03-11 15:38:39 +03:00
|
|
|
ip := net.ParseIP(fields[0])
|
|
|
|
if ip == nil {
|
2020-03-20 15:05:43 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-03-11 15:38:39 +03:00
|
|
|
|
2020-05-18 18:46:17 +03:00
|
|
|
for i := 1; i != len(fields); i++ {
|
|
|
|
host := fields[i]
|
2020-03-20 15:05:43 +03:00
|
|
|
if len(host) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2021-03-11 15:38:39 +03:00
|
|
|
|
2020-06-17 19:36:35 +03:00
|
|
|
sharp := strings.IndexByte(host, '#')
|
|
|
|
if sharp == 0 {
|
2021-03-11 15:38:39 +03:00
|
|
|
// Skip the comments.
|
|
|
|
break
|
2020-06-17 19:36:35 +03:00
|
|
|
} else if sharp > 0 {
|
|
|
|
host = host[:sharp]
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.updateTable(table, host, ip)
|
|
|
|
ehc.updateTableRev(tableRev, host, ip)
|
2020-06-17 19:36:35 +03:00
|
|
|
if sharp >= 0 {
|
2021-03-11 15:38:39 +03:00
|
|
|
// Skip the comments again.
|
|
|
|
break
|
2020-06-17 19:36:35 +03:00
|
|
|
}
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:07:29 +03:00
|
|
|
// onlyWrites is a filter for (*fsnotify.Watcher).Events.
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) onlyWrites() {
|
|
|
|
for event := range ehc.watcher.Events {
|
2020-12-28 19:07:29 +03:00
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.onlyWritesChan <- event
|
2020-12-28 19:07:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 15:05:43 +03:00
|
|
|
// Receive notifications from fsnotify package
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) watcherLoop() {
|
|
|
|
go ehc.onlyWrites()
|
2020-03-20 15:05:43 +03:00
|
|
|
for {
|
|
|
|
select {
|
2021-04-12 18:31:45 +03:00
|
|
|
case event, ok := <-ehc.onlyWritesChan:
|
2020-03-20 15:05:43 +03:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-28 19:07:29 +03:00
|
|
|
// Assume that we sometimes have the same event occurred
|
|
|
|
// several times.
|
2020-03-20 15:05:43 +03:00
|
|
|
repeat := true
|
|
|
|
for repeat {
|
|
|
|
select {
|
2021-04-12 18:31:45 +03:00
|
|
|
case _, ok = <-ehc.onlyWritesChan:
|
2020-12-28 19:07:29 +03:00
|
|
|
repeat = ok
|
2020-03-20 15:05:43 +03:00
|
|
|
default:
|
|
|
|
repeat = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Debug("etchostscontainer: modified: %s", event.Name)
|
|
|
|
ehc.updateHosts()
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
case err, ok := <-ehc.watcher.Errors:
|
2020-03-20 15:05:43 +03:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Error("etchostscontainer: %s", err)
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:36:47 +03:00
|
|
|
// updateHosts - loads system hosts
|
2021-04-12 18:31:45 +03:00
|
|
|
func (ehc *EtcHostsContainer) updateHosts() {
|
2020-04-15 14:36:47 +03:00
|
|
|
table := make(map[string][]net.IP)
|
2020-11-06 17:34:40 +03:00
|
|
|
tableRev := make(map[string][]string)
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.load(table, tableRev, ehc.hostsFn)
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
for _, dir := range ehc.hostsDirs {
|
2020-04-15 14:36:47 +03:00
|
|
|
fis, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
2021-03-15 14:19:04 +03:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2021-04-12 18:31:45 +03:00
|
|
|
log.Error("etchostscontainer: Opening directory: %q: %s", dir, err)
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
2021-03-15 14:19:04 +03:00
|
|
|
|
2020-04-15 14:36:47 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-03-20 15:05:43 +03:00
|
|
|
|
2020-04-15 14:36:47 +03:00
|
|
|
for _, fi := range fis {
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.load(table, tableRev, filepath.Join(dir, fi.Name()))
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-03-03 16:14:23 +03:00
|
|
|
func() {
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.lock.Lock()
|
|
|
|
defer ehc.lock.Unlock()
|
2021-03-03 16:14:23 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.table = table
|
|
|
|
ehc.tableReverse = tableRev
|
2021-03-03 16:14:23 +03:00
|
|
|
}()
|
2020-04-15 14:36:47 +03:00
|
|
|
|
2021-04-12 18:31:45 +03:00
|
|
|
ehc.notify()
|
2020-03-20 15:05:43 +03:00
|
|
|
}
|