2020-03-20 05:05:43 -07:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2020-09-11 01:53:36 -07:00
|
|
|
"github.com/miekg/dns"
|
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
|
|
|
type onChangedT func()
|
|
|
|
|
|
|
|
// AutoHosts - automatic DNS records
|
|
|
|
type AutoHosts struct {
|
2020-11-06 07:34:40 -07:00
|
|
|
// lock protects table and tableReverse.
|
2020-12-22 05:39:50 -07:00
|
|
|
lock sync.RWMutex
|
2020-11-06 07:34:40 -07: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 08:56:47 -07: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
|
|
|
|
updateChan chan bool // signal for 'updateLoop' goroutine
|
2020-03-20 05:05:43 -07:00
|
|
|
|
|
|
|
onChanged onChangedT // notification to other modules
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOnChanged - set callback function that will be called when the data is changed
|
|
|
|
func (a *AutoHosts) SetOnChanged(onChanged onChangedT) {
|
|
|
|
a.onChanged = onChanged
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify other modules
|
|
|
|
func (a *AutoHosts) notify() {
|
|
|
|
if a.onChanged == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
a.onChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initialize
|
|
|
|
// hostsFn: Override default name for the hosts-file (optional)
|
|
|
|
func (a *AutoHosts) Init(hostsFn string) {
|
|
|
|
a.table = make(map[string][]net.IP)
|
|
|
|
a.updateChan = make(chan bool, 2)
|
|
|
|
|
|
|
|
a.hostsFn = "/etc/hosts"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
a.hostsFn = os.ExpandEnv("$SystemRoot\\system32\\drivers\\etc\\hosts")
|
|
|
|
}
|
|
|
|
if len(hostsFn) != 0 {
|
|
|
|
a.hostsFn = hostsFn
|
|
|
|
}
|
|
|
|
|
2020-11-06 02:15:08 -07:00
|
|
|
if IsOpenWRT() {
|
2020-03-20 05:05:43 -07:00
|
|
|
a.hostsDirs = append(a.hostsDirs, "/tmp/hosts") // OpenWRT: "/tmp/hosts/dhcp.cfg01411c"
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:53:36 -07:00
|
|
|
// Load hosts initially
|
|
|
|
a.updateHosts()
|
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
var err error
|
|
|
|
a.watcher, err = fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("AutoHosts: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start - start module
|
|
|
|
func (a *AutoHosts) Start() {
|
2020-04-15 04:36:47 -07:00
|
|
|
log.Debug("Start AutoHosts module")
|
|
|
|
|
|
|
|
go a.updateLoop()
|
2020-03-20 05:05:43 -07:00
|
|
|
a.updateChan <- true
|
|
|
|
|
2020-06-19 04:27:23 -07:00
|
|
|
if a.watcher != nil {
|
|
|
|
go a.watcherLoop()
|
2020-03-20 05:05:43 -07:00
|
|
|
|
2020-06-19 04:27:23 -07:00
|
|
|
err := a.watcher.Add(a.hostsFn)
|
2020-03-20 05:05:43 -07:00
|
|
|
if err != nil {
|
2020-06-19 04:27:23 -07:00
|
|
|
log.Error("Error while initializing watcher for a file %s: %s", a.hostsFn, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range a.hostsDirs {
|
|
|
|
err = a.watcher.Add(dir)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error while initializing watcher for a directory %s: %s", dir, err)
|
|
|
|
}
|
2020-03-20 05:05:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - close module
|
|
|
|
func (a *AutoHosts) Close() {
|
|
|
|
a.updateChan <- false
|
2020-04-15 04:36:47 -07:00
|
|
|
close(a.updateChan)
|
2020-06-19 04:27:23 -07:00
|
|
|
if a.watcher != nil {
|
|
|
|
_ = a.watcher.Close()
|
|
|
|
}
|
2020-03-20 05:05:43 -07:00
|
|
|
}
|
|
|
|
|
2020-11-06 02:15:08 -07:00
|
|
|
// Process returns the list of IP addresses for the hostname or nil if nothing
|
|
|
|
// found.
|
2020-09-11 01:53:36 -07:00
|
|
|
func (a *AutoHosts) Process(host string, qtype uint16) []net.IP {
|
|
|
|
if qtype == dns.TypePTR {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipsCopy []net.IP
|
2020-12-22 05:39:50 -07:00
|
|
|
a.lock.RLock()
|
2020-11-06 02:15:08 -07:00
|
|
|
|
|
|
|
if ips, ok := a.table[host]; ok {
|
2020-09-11 01:53:36 -07:00
|
|
|
ipsCopy = make([]net.IP, len(ips))
|
|
|
|
copy(ipsCopy, ips)
|
|
|
|
}
|
2020-11-06 02:15:08 -07:00
|
|
|
|
2020-12-22 05:39:50 -07:00
|
|
|
a.lock.RUnlock()
|
2020-09-11 01:53:36 -07:00
|
|
|
|
|
|
|
log.Debug("AutoHosts: answer: %s -> %v", host, ipsCopy)
|
|
|
|
return ipsCopy
|
|
|
|
}
|
|
|
|
|
2020-11-06 07:34:40 -07:00
|
|
|
// ProcessReverse processes a PTR request. It returns nil if nothing is found.
|
|
|
|
func (a *AutoHosts) ProcessReverse(addr string, qtype uint16) (hosts []string) {
|
2020-09-11 01:53:36 -07:00
|
|
|
if qtype != dns.TypePTR {
|
2020-11-06 07:34:40 -07:00
|
|
|
return nil
|
2020-09-11 01:53:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ipReal := DNSUnreverseAddr(addr)
|
|
|
|
if ipReal == nil {
|
2020-11-06 07:34:40 -07:00
|
|
|
return nil
|
2020-09-11 01:53:36 -07:00
|
|
|
}
|
2020-11-06 07:34:40 -07:00
|
|
|
|
2020-09-11 01:53:36 -07:00
|
|
|
ipStr := ipReal.String()
|
|
|
|
|
2020-12-22 05:39:50 -07:00
|
|
|
a.lock.RLock()
|
|
|
|
defer a.lock.RUnlock()
|
2020-09-11 01:53:36 -07:00
|
|
|
|
2020-11-06 07:34:40 -07:00
|
|
|
hosts = a.tableReverse[ipStr]
|
|
|
|
|
|
|
|
if len(hosts) == 0 {
|
|
|
|
return nil // not found
|
2020-09-11 01:53:36 -07:00
|
|
|
}
|
|
|
|
|
2020-11-06 07:34:40 -07:00
|
|
|
log.Debug("AutoHosts: reverse-lookup: %s -> %s", addr, hosts)
|
|
|
|
|
|
|
|
return hosts
|
2020-09-11 01:53:36 -07:00
|
|
|
}
|
|
|
|
|
2020-11-06 07:34:40 -07:00
|
|
|
// List returns an IP-to-hostnames table. It is safe for concurrent use.
|
|
|
|
func (a *AutoHosts) List() (ipToHosts map[string][]string) {
|
2020-12-22 05:39:50 -07:00
|
|
|
a.lock.RLock()
|
|
|
|
defer a.lock.RUnlock()
|
2020-11-06 07:34:40 -07:00
|
|
|
|
|
|
|
ipToHosts = make(map[string][]string, len(a.tableReverse))
|
2020-09-11 01:53:36 -07:00
|
|
|
for k, v := range a.tableReverse {
|
2020-11-06 07:34:40 -07:00
|
|
|
ipToHosts[k] = v
|
2020-09-11 01:53:36 -07:00
|
|
|
}
|
2020-11-06 07:34:40 -07:00
|
|
|
|
|
|
|
return ipToHosts
|
2020-09-11 01:53:36 -07:00
|
|
|
}
|
|
|
|
|
2020-04-16 08:56:47 -07:00
|
|
|
// update table
|
|
|
|
func (a *AutoHosts) updateTable(table map[string][]net.IP, host string, ipAddr net.IP) {
|
|
|
|
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 {
|
|
|
|
log.Debug("AutoHosts: added %s -> %s", ipAddr, host)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 07:34:40 -07:00
|
|
|
// updateTableRev updates the reverse address table.
|
|
|
|
func (a *AutoHosts) updateTableRev(tableRev map[string][]string, newHost string, ipAddr net.IP) {
|
2020-04-16 08:56:47 -07:00
|
|
|
ipStr := ipAddr.String()
|
2020-11-06 07:34:40 -07:00
|
|
|
hosts, ok := tableRev[ipStr]
|
2020-04-16 08:56:47 -07:00
|
|
|
if !ok {
|
2020-11-06 07:34:40 -07:00
|
|
|
tableRev[ipStr] = []string{newHost}
|
|
|
|
log.Debug("AutoHosts: added reverse-address %s -> %s", ipStr, newHost)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, host := range hosts {
|
|
|
|
if host == newHost {
|
|
|
|
return
|
|
|
|
}
|
2020-04-16 08:56:47 -07:00
|
|
|
}
|
2020-11-06 07:34:40 -07:00
|
|
|
|
|
|
|
tableRev[ipStr] = append(tableRev[ipStr], newHost)
|
|
|
|
log.Debug("AutoHosts: added reverse-address %s -> %s", ipStr, newHost)
|
2020-04-16 08:56:47 -07:00
|
|
|
}
|
|
|
|
|
2020-03-20 05:05:43 -07:00
|
|
|
// Read IP-hostname pairs from file
|
|
|
|
// Multiple hostnames per line (per one IP) is supported.
|
2020-11-06 07:34:40 -07:00
|
|
|
func (a *AutoHosts) load(table map[string][]net.IP, tableRev map[string][]string, fn string) {
|
2020-03-20 05:05:43 -07:00
|
|
|
f, err := os.Open(fn)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("AutoHosts: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
r := bufio.NewReader(f)
|
|
|
|
log.Debug("AutoHosts: loading hosts from file %s", fn)
|
|
|
|
|
|
|
|
finish := false
|
|
|
|
for !finish {
|
|
|
|
line, err := r.ReadString('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
finish = true
|
|
|
|
} else if err != nil {
|
|
|
|
log.Error("AutoHosts: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
line = strings.TrimSpace(line)
|
2020-05-18 08:46:17 -07:00
|
|
|
if len(line) == 0 || line[0] == '#' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-20 05:05:43 -07:00
|
|
|
|
2020-05-18 08:46:17 -07:00
|
|
|
ipAddr := net.ParseIP(fields[0])
|
2020-03-20 05:05:43 -07:00
|
|
|
if ipAddr == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-18 08:46:17 -07:00
|
|
|
for i := 1; i != len(fields); i++ {
|
|
|
|
host := fields[i]
|
2020-03-20 05:05:43 -07:00
|
|
|
if len(host) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-06-17 09:36:35 -07:00
|
|
|
sharp := strings.IndexByte(host, '#')
|
|
|
|
if sharp == 0 {
|
|
|
|
break // skip the rest of the line after #
|
|
|
|
} else if sharp > 0 {
|
|
|
|
host = host[:sharp]
|
|
|
|
}
|
|
|
|
|
2020-04-16 08:56:47 -07:00
|
|
|
a.updateTable(table, host, ipAddr)
|
|
|
|
a.updateTableRev(tableRev, host, ipAddr)
|
2020-06-17 09:36:35 -07:00
|
|
|
if sharp >= 0 {
|
|
|
|
break // skip the rest of the line after #
|
|
|
|
}
|
2020-03-20 05:05:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receive notifications from fsnotify package
|
|
|
|
func (a *AutoHosts) watcherLoop() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-a.watcher.Events:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repeat := true
|
|
|
|
for repeat {
|
|
|
|
select {
|
2020-11-06 02:15:08 -07:00
|
|
|
case <-a.watcher.Events:
|
|
|
|
// Skip this duplicating event
|
2020-03-20 05:05:43 -07:00
|
|
|
default:
|
|
|
|
repeat = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Op&fsnotify.Write == fsnotify.Write {
|
|
|
|
log.Debug("AutoHosts: modified: %s", event.Name)
|
|
|
|
select {
|
|
|
|
case a.updateChan <- true:
|
2020-04-15 04:36:47 -07:00
|
|
|
// sent a signal to 'updateLoop' goroutine
|
2020-03-20 05:05:43 -07:00
|
|
|
default:
|
|
|
|
// queue is full
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case err, ok := <-a.watcher.Errors:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Error("AutoHosts: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 02:15:08 -07:00
|
|
|
// updateLoop reads static hosts from system files.
|
2020-04-15 04:36:47 -07:00
|
|
|
func (a *AutoHosts) updateLoop() {
|
2020-11-06 02:15:08 -07:00
|
|
|
for ok := range a.updateChan {
|
|
|
|
if !ok {
|
|
|
|
log.Debug("Finished AutoHosts update loop")
|
|
|
|
return
|
2020-04-15 04:36:47 -07:00
|
|
|
}
|
2020-11-06 02:15:08 -07:00
|
|
|
|
|
|
|
a.updateHosts()
|
2020-04-15 04:36:47 -07:00
|
|
|
}
|
|
|
|
}
|
2020-03-20 05:05:43 -07:00
|
|
|
|
2020-04-15 04:36:47 -07:00
|
|
|
// updateHosts - loads system hosts
|
|
|
|
func (a *AutoHosts) updateHosts() {
|
|
|
|
table := make(map[string][]net.IP)
|
2020-11-06 07:34:40 -07:00
|
|
|
tableRev := make(map[string][]string)
|
2020-03-20 05:05:43 -07:00
|
|
|
|
2020-04-16 08:56:47 -07:00
|
|
|
a.load(table, tableRev, a.hostsFn)
|
2020-03-20 05:05:43 -07:00
|
|
|
|
2020-04-15 04:36:47 -07:00
|
|
|
for _, dir := range a.hostsDirs {
|
|
|
|
fis, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
log.Error("AutoHosts: Opening directory: %s: %s", dir, err)
|
2020-03-20 05:05:43 -07:00
|
|
|
}
|
2020-04-15 04:36:47 -07:00
|
|
|
continue
|
|
|
|
}
|
2020-03-20 05:05:43 -07:00
|
|
|
|
2020-04-15 04:36:47 -07:00
|
|
|
for _, fi := range fis {
|
2020-04-16 08:56:47 -07:00
|
|
|
a.load(table, tableRev, dir+"/"+fi.Name())
|
2020-03-20 05:05:43 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-15 04:36:47 -07:00
|
|
|
|
|
|
|
a.lock.Lock()
|
|
|
|
a.table = table
|
2020-04-16 08:56:47 -07:00
|
|
|
a.tableReverse = tableRev
|
2020-04-15 04:36:47 -07:00
|
|
|
a.lock.Unlock()
|
|
|
|
|
|
|
|
a.notify()
|
2020-03-20 05:05:43 -07:00
|
|
|
}
|