2021-04-12 15:31:45 +00:00
|
|
|
package aghnet
|
2020-03-20 12:05:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"net"
|
|
|
|
"os"
|
2021-03-15 11:19:04 +00:00
|
|
|
"path/filepath"
|
2020-03-20 12:05:43 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-04-08 17:42:04 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2021-05-24 14:28:11 +00:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2020-03-20 12:05:43 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2021-03-11 12:38:39 +00:00
|
|
|
"github.com/miekg/dns"
|
2020-03-20 12:05:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type onChangedT func()
|
|
|
|
|
2021-04-12 15:31:45 +00: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 14:34:40 +00:00
|
|
|
// lock protects table and tableReverse.
|
2020-12-22 12:39:50 +00:00
|
|
|
lock sync.RWMutex
|
2020-11-06 14:34:40 +00:00
|
|
|
// table is the host-to-IPs map.
|
|
|
|
table map[string][]net.IP
|
2021-06-29 12:53:28 +00:00
|
|
|
// tableReverse is the IP-to-hosts map. The type of the values in the
|
|
|
|
// map is []string.
|
|
|
|
tableReverse *IPMap
|
2020-04-16 15:56:47 +00:00
|
|
|
|
2020-12-28 16:07:29 +00: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 12:05:43 +00:00
|
|
|
|
|
|
|
onChanged onChangedT // notification to other modules
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOnChanged - set callback function that will be called when the data is changed
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) SetOnChanged(onChanged onChangedT) {
|
|
|
|
ehc.onChanged = onChanged
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify other modules
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) notify() {
|
|
|
|
if ehc.onChanged == nil {
|
2020-03-20 12:05:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.onChanged()
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initialize
|
|
|
|
// hostsFn: Override default name for the hosts-file (optional)
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) Init(hostsFn string) {
|
|
|
|
ehc.table = make(map[string][]net.IP)
|
|
|
|
ehc.onlyWritesChan = make(chan fsnotify.Event, 2)
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.hostsFn = "/etc/hosts"
|
2020-03-20 12:05:43 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.hostsFn = os.ExpandEnv("$SystemRoot\\system32\\drivers\\etc\\hosts")
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
if len(hostsFn) != 0 {
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.hostsFn = hostsFn
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 17:42:04 +00:00
|
|
|
if aghos.IsOpenWrt() {
|
2021-03-12 11:32:08 +00:00
|
|
|
// OpenWrt: "/tmp/hosts/dhcp.cfg01411c".
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.hostsDirs = append(ehc.hostsDirs, "/tmp/hosts")
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:53:36 +00:00
|
|
|
// Load hosts initially
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.updateHosts()
|
2020-09-11 08:53:36 +00:00
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
var err error
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.watcher, err = fsnotify.NewWatcher()
|
2020-03-20 12:05:43 +00:00
|
|
|
if err != nil {
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Error("etchosts: %s", err)
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start - start module
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) Start() {
|
|
|
|
if ehc == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-04-15 11:36:47 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
log.Debug("Start etchostscontainer module")
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.updateHosts()
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
if ehc.watcher != nil {
|
|
|
|
go ehc.watcherLoop()
|
|
|
|
|
|
|
|
err := ehc.watcher.Add(ehc.hostsFn)
|
2020-03-20 12:05:43 +00:00
|
|
|
if err != nil {
|
2021-04-12 15:31:45 +00:00
|
|
|
log.Error("Error while initializing watcher for a file %s: %s", ehc.hostsFn, err)
|
2020-06-19 11:27:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
for _, dir := range ehc.hostsDirs {
|
|
|
|
err = ehc.watcher.Add(dir)
|
2020-06-19 11:27:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error while initializing watcher for a directory %s: %s", dir, err)
|
|
|
|
}
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - close module
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) Close() {
|
|
|
|
if ehc == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ehc.watcher != nil {
|
|
|
|
_ = ehc.watcher.Close()
|
2020-06-19 11:27:23 +00:00
|
|
|
}
|
2021-05-21 16:20:42 +00:00
|
|
|
|
|
|
|
// Don't close onlyWritesChan here and let onlyWrites close it after
|
|
|
|
// watcher.Events is closed to prevent close races.
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 09:15:08 +00:00
|
|
|
// Process returns the list of IP addresses for the hostname or nil if nothing
|
|
|
|
// found.
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) Process(host string, qtype uint16) []net.IP {
|
2020-09-11 08:53:36 +00:00
|
|
|
if qtype == dns.TypePTR {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipsCopy []net.IP
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.lock.RLock()
|
|
|
|
defer ehc.lock.RUnlock()
|
2020-11-06 09:15:08 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
if ips, ok := ehc.table[host]; ok {
|
2020-09-11 08:53:36 +00:00
|
|
|
ipsCopy = make([]net.IP, len(ips))
|
|
|
|
copy(ipsCopy, ips)
|
|
|
|
}
|
2020-11-06 09:15:08 +00:00
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Debug("etchosts: answer: %s -> %v", host, ipsCopy)
|
2020-09-11 08:53:36 +00:00
|
|
|
return ipsCopy
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:34:40 +00:00
|
|
|
// ProcessReverse processes a PTR request. It returns nil if nothing is found.
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) ProcessReverse(addr string, qtype uint16) (hosts []string) {
|
2020-09-11 08:53:36 +00:00
|
|
|
if qtype != dns.TypePTR {
|
2020-11-06 14:34:40 +00:00
|
|
|
return nil
|
2020-09-11 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
ip := UnreverseAddr(addr)
|
|
|
|
if ip == nil {
|
2020-11-06 14:34:40 +00:00
|
|
|
return nil
|
2020-09-11 08:53:36 +00:00
|
|
|
}
|
2020-11-06 14:34:40 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.lock.RLock()
|
|
|
|
defer ehc.lock.RUnlock()
|
2020-09-11 08:53:36 +00:00
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
v, ok := ehc.tableReverse.Get(ip)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts, ok = v.([]string)
|
|
|
|
if !ok {
|
|
|
|
log.Error("etchosts: bad type %T in tableReverse for %s", v, ip)
|
2020-11-06 14:34:40 +00:00
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
return nil
|
|
|
|
} else if len(hosts) == 0 {
|
|
|
|
return nil
|
2020-09-11 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Debug("etchosts: reverse-lookup: %s -> %s", addr, hosts)
|
2020-11-06 14:34:40 +00:00
|
|
|
|
|
|
|
return hosts
|
2020-09-11 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
// List returns an IP-to-hostnames table. The type of the values in the map is
|
|
|
|
// []string. It is safe for concurrent use.
|
|
|
|
func (ehc *EtcHostsContainer) List() (ipToHosts *IPMap) {
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.lock.RLock()
|
|
|
|
defer ehc.lock.RUnlock()
|
2020-11-06 14:34:40 +00:00
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
return ehc.tableReverse.ShallowClone()
|
2020-09-11 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:56:47 +00:00
|
|
|
// update table
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) updateTable(table map[string][]net.IP, host string, ipAddr net.IP) {
|
2020-04-16 15:56:47 +00: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-06-29 12:53:28 +00:00
|
|
|
log.Debug("etchosts: added %s -> %s", ipAddr, host)
|
2020-04-16 15:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 14:34:40 +00:00
|
|
|
// updateTableRev updates the reverse address table.
|
2021-06-29 12:53:28 +00:00
|
|
|
func (ehc *EtcHostsContainer) updateTableRev(tableRev *IPMap, newHost string, ip net.IP) {
|
|
|
|
v, ok := tableRev.Get(ip)
|
2020-04-16 15:56:47 +00:00
|
|
|
if !ok {
|
2021-06-29 12:53:28 +00:00
|
|
|
tableRev.Set(ip, []string{newHost})
|
|
|
|
log.Debug("etchosts: added reverse-address %s -> %s", ip, newHost)
|
2020-11-06 14:34:40 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
hosts, _ := v.([]string)
|
2020-11-06 14:34:40 +00:00
|
|
|
for _, host := range hosts {
|
|
|
|
if host == newHost {
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 15:56:47 +00:00
|
|
|
}
|
2020-11-06 14:34:40 +00:00
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
hosts = append(hosts, newHost)
|
|
|
|
tableRev.Set(ip, hosts)
|
|
|
|
|
|
|
|
log.Debug("etchosts: added reverse-address %s -> %s", ip, newHost)
|
2020-04-16 15:56:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
// parseHostsLine parses hosts from the fields.
|
|
|
|
func parseHostsLine(fields []string) (hosts []string) {
|
|
|
|
for _, f := range fields {
|
|
|
|
hashIdx := strings.IndexByte(f, '#')
|
|
|
|
if hashIdx == 0 {
|
|
|
|
// The rest of the fields are a part of the comment.
|
|
|
|
// Skip immediately.
|
|
|
|
return
|
|
|
|
} else if hashIdx > 0 {
|
|
|
|
// Only a part of the field is a comment.
|
|
|
|
hosts = append(hosts, f[:hashIdx])
|
|
|
|
|
|
|
|
return hosts
|
|
|
|
}
|
|
|
|
|
|
|
|
hosts = append(hosts, f)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hosts
|
|
|
|
}
|
|
|
|
|
|
|
|
// load reads IP-hostname pairs from the hosts file. Multiple hostnames per
|
|
|
|
// line for one IP are supported.
|
|
|
|
func (ehc *EtcHostsContainer) load(
|
|
|
|
table map[string][]net.IP,
|
2021-06-29 12:53:28 +00:00
|
|
|
tableRev *IPMap,
|
2021-05-31 17:11:06 +00:00
|
|
|
fn string,
|
|
|
|
) {
|
2020-03-20 12:05:43 +00:00
|
|
|
f, err := os.Open(fn)
|
|
|
|
if err != nil {
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Error("etchosts: %s", err)
|
2021-05-31 17:11:06 +00:00
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
return
|
|
|
|
}
|
2021-05-31 17:11:06 +00:00
|
|
|
|
2021-05-24 14:28:11 +00:00
|
|
|
defer func() {
|
|
|
|
derr := f.Close()
|
|
|
|
if derr != nil {
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Error("etchosts: closing file: %s", err)
|
2021-05-24 14:28:11 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Debug("etchosts: loading hosts from file %s", fn)
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
s := bufio.NewScanner(f)
|
|
|
|
for s.Scan() {
|
|
|
|
line := strings.TrimSpace(s.Text())
|
2020-05-18 15:46:17 +00:00
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-03-11 12:38:39 +00:00
|
|
|
ip := net.ParseIP(fields[0])
|
|
|
|
if ip == nil {
|
2020-03-20 12:05:43 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-11 12:38:39 +00:00
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
hosts := parseHostsLine(fields[1:])
|
|
|
|
for _, host := range hosts {
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.updateTable(table, host, ip)
|
|
|
|
ehc.updateTableRev(tableRev, host, ip)
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-31 17:11:06 +00:00
|
|
|
|
|
|
|
err = s.Err()
|
|
|
|
if err != nil {
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Error("etchosts: %s", err)
|
2021-05-31 17:11:06 +00:00
|
|
|
}
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 16:07:29 +00:00
|
|
|
// onlyWrites is a filter for (*fsnotify.Watcher).Events.
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) onlyWrites() {
|
|
|
|
for event := range ehc.watcher.Events {
|
2020-12-28 16:07:29 +00:00
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.onlyWritesChan <- event
|
2020-12-28 16:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-21 16:20:42 +00:00
|
|
|
|
|
|
|
close(ehc.onlyWritesChan)
|
2020-12-28 16:07:29 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 12:05:43 +00:00
|
|
|
// Receive notifications from fsnotify package
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) watcherLoop() {
|
|
|
|
go ehc.onlyWrites()
|
2020-03-20 12:05:43 +00:00
|
|
|
for {
|
|
|
|
select {
|
2021-04-12 15:31:45 +00:00
|
|
|
case event, ok := <-ehc.onlyWritesChan:
|
2020-03-20 12:05:43 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-28 16:07:29 +00:00
|
|
|
// Assume that we sometimes have the same event occurred
|
|
|
|
// several times.
|
2020-03-20 12:05:43 +00:00
|
|
|
repeat := true
|
|
|
|
for repeat {
|
|
|
|
select {
|
2021-04-12 15:31:45 +00:00
|
|
|
case _, ok = <-ehc.onlyWritesChan:
|
2020-12-28 16:07:29 +00:00
|
|
|
repeat = ok
|
2020-03-20 12:05:43 +00:00
|
|
|
default:
|
|
|
|
repeat = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Debug("etchosts: modified: %s", event.Name)
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.updateHosts()
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
case err, ok := <-ehc.watcher.Errors:
|
2020-03-20 12:05:43 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Error("etchosts: %s", err)
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
// updateHosts - loads system hosts
|
2021-04-12 15:31:45 +00:00
|
|
|
func (ehc *EtcHostsContainer) updateHosts() {
|
2020-04-15 11:36:47 +00:00
|
|
|
table := make(map[string][]net.IP)
|
2021-06-29 12:53:28 +00:00
|
|
|
tableRev := NewIPMap(0)
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.load(table, tableRev, ehc.hostsFn)
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
for _, dir := range ehc.hostsDirs {
|
2021-05-21 11:55:42 +00:00
|
|
|
des, err := os.ReadDir(dir)
|
2020-04-15 11:36:47 +00:00
|
|
|
if err != nil {
|
2021-03-15 11:19:04 +00:00
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
2021-06-29 12:53:28 +00:00
|
|
|
log.Error("etchosts: Opening directory: %q: %s", dir, err)
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
2021-03-15 11:19:04 +00:00
|
|
|
|
2020-04-15 11:36:47 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-03-20 12:05:43 +00:00
|
|
|
|
2021-05-21 11:55:42 +00:00
|
|
|
for _, de := range des {
|
|
|
|
ehc.load(table, tableRev, filepath.Join(dir, de.Name()))
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 11:36:47 +00:00
|
|
|
|
2021-03-03 13:14:23 +00:00
|
|
|
func() {
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.lock.Lock()
|
|
|
|
defer ehc.lock.Unlock()
|
2021-03-03 13:14:23 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.table = table
|
|
|
|
ehc.tableReverse = tableRev
|
2021-03-03 13:14:23 +00:00
|
|
|
}()
|
2020-04-15 11:36:47 +00:00
|
|
|
|
2021-04-12 15:31:45 +00:00
|
|
|
ehc.notify()
|
2020-03-20 12:05:43 +00:00
|
|
|
}
|