Skip to content

Commit

Permalink
rename, move
Browse files Browse the repository at this point in the history
  • Loading branch information
szolin committed Mar 20, 2020
1 parent 9e5ed49 commit a22b621
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 34 deletions.
11 changes: 6 additions & 5 deletions dnsfilter/dnsfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"sync"

"github.com/AdguardTeam/AdGuardHome/util"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/cache"
"github.com/AdguardTeam/golibs/log"
Expand Down Expand Up @@ -54,7 +55,7 @@ type Config struct {
BlockedServices []string `yaml:"blocked_services"`

// IP-hostname pairs taken from system configuration (e.g. /etc/hosts) files
AutoHosts *AutoHosts `yaml:"-"`
AutoHosts *util.AutoHosts `yaml:"-"`

// Called when the configuration is changed by HTTP request
ConfigModified func() `yaml:"-"`
Expand Down Expand Up @@ -143,8 +144,8 @@ const (
// ReasonRewrite - rewrite rule was applied
ReasonRewrite

// ReasonRewriteAuto - automatic DNS record
ReasonRewriteAuto
// RewriteEtcHosts - rewrite by /etc/hosts rule
RewriteEtcHosts
)

var reasonNames = []string{
Expand All @@ -160,7 +161,7 @@ var reasonNames = []string{
"FilteredBlockedService",

"Rewrite",
"RewriteAuto",
"RewriteEtcHosts",
}

func (r Reason) String() string {
Expand Down Expand Up @@ -313,7 +314,7 @@ func (d *Dnsfilter) CheckHost(host string, qtype uint16, setts *RequestFiltering
if d.Config.AutoHosts != nil {
ips := d.Config.AutoHosts.Process(host)
if ips != nil {
result.Reason = ReasonRewriteAuto
result.Reason = RewriteEtcHosts
result.IPList = ips
return result, nil
}
Expand Down
19 changes: 0 additions & 19 deletions dnsfilter/dnsfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dnsfilter

import (
"fmt"
"io/ioutil"
"net"
"os"
"path"
Expand Down Expand Up @@ -630,24 +629,6 @@ func prepareTestDir() string {
return dir
}

func TestAutoHosts(t *testing.T) {
ah := AutoHosts{}
ah.table = make(map[string][]net.IP)

dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }()

f, _ := ioutil.TempFile(dir, "")
defer os.Remove(f.Name())
defer f.Close()

f.WriteString(" 127.0.0.1 host localhost ")

ah.load(ah.table, f.Name())
ips := ah.Process("localhost")
assert.True(t, ips[0].Equal(net.ParseIP("127.0.0.1")))
}

// BENCHMARKS

func BenchmarkSafeBrowsing(b *testing.B) {
Expand Down
4 changes: 2 additions & 2 deletions dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func processFilteringAfterResponse(ctx *dnsContext) int {
d.Res.Answer = answer
}

case dnsfilter.ReasonRewriteAuto:
case dnsfilter.RewriteEtcHosts:
case dnsfilter.NotFilteredWhiteList:
// nothing

Expand Down Expand Up @@ -856,7 +856,7 @@ func (s *Server) filterDNSRequest(ctx *dnsContext) (*dnsfilter.Result, error) {
// log.Tracef("Host %s is filtered, reason - '%s', matched rule: '%s'", host, res.Reason, res.Rule)
d.Res = s.genDNSFilterMessage(d, &res)

} else if (res.Reason == dnsfilter.ReasonRewrite || res.Reason == dnsfilter.ReasonRewriteAuto) &&
} else if (res.Reason == dnsfilter.ReasonRewrite || res.Reason == dnsfilter.RewriteEtcHosts) &&
len(res.IPList) != 0 {
resp := s.makeResponse(req)

Expand Down
5 changes: 3 additions & 2 deletions home/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/AdguardTeam/AdGuardHome/dhcpd"
"github.com/AdguardTeam/AdGuardHome/dnsfilter"
"github.com/AdguardTeam/AdGuardHome/dnsforward"
"github.com/AdguardTeam/AdGuardHome/util"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/utils"
Expand Down Expand Up @@ -77,14 +78,14 @@ type clientsContainer struct {
// dhcpServer is used for looking up clients IP addresses by MAC addresses
dhcpServer *dhcpd.Server

autoHosts *dnsfilter.AutoHosts // get entries from system hosts-files
autoHosts *util.AutoHosts // get entries from system hosts-files

testing bool // if TRUE, this object is used for internal tests
}

// Init initializes clients container
// Note: this function must be called only once
func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server, autoHosts *dnsfilter.AutoHosts) {
func (clients *clientsContainer) Init(objects []clientObject, dhcpServer *dhcpd.Server, autoHosts *util.AutoHosts) {
if clients.list != nil {
log.Fatal("clients.list != nil")
}
Expand Down
2 changes: 1 addition & 1 deletion home/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type homeContext struct {
filters Filtering // DNS filtering module
web *Web // Web (HTTP, HTTPS) module
tls *TLSMod // TLS module
autoHosts dnsfilter.AutoHosts // IP-hostname pairs taken from system configuration (e.g. /etc/hosts) files
autoHosts util.AutoHosts // IP-hostname pairs taken from system configuration (e.g. /etc/hosts) files

// Runtime properties
// --
Expand Down
9 changes: 4 additions & 5 deletions dnsfilter/auto_hosts.go → util/auto_hosts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dnsfilter
package util

import (
"bufio"
Expand All @@ -10,7 +10,6 @@ import (
"strings"
"sync"

"github.com/AdguardTeam/AdGuardHome/util"
"github.com/AdguardTeam/golibs/log"
"github.com/fsnotify/fsnotify"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ func (a *AutoHosts) Init() {
a.hostsFn = os.ExpandEnv("$SystemRoot\\system32\\drivers\\etc\\hosts")
}

if util.IsOpenWrt() {
if IsOpenWrt() {
a.hostsDirs = append(a.hostsDirs, "/tmp/hosts") // OpenWRT: "/tmp/hosts/dhcp.cfg01411c"
}

Expand Down Expand Up @@ -112,13 +111,13 @@ func (a *AutoHosts) load(table map[string][]net.IP, fn string) {
}
line = strings.TrimSpace(line)

ip := util.SplitNext(&line, ' ')
ip := SplitNext(&line, ' ')
ipAddr := net.ParseIP(ip)
if ipAddr == nil {
continue
}
for {
host := util.SplitNext(&line, ' ')
host := SplitNext(&line, ' ')
if len(host) == 0 {
break
}
Expand Down
28 changes: 28 additions & 0 deletions util/auto_hosts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package util

import (
"io/ioutil"
"net"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAutoHosts(t *testing.T) {
ah := AutoHosts{}
ah.table = make(map[string][]net.IP)

dir := prepareTestDir()
defer func() { _ = os.RemoveAll(dir) }()

f, _ := ioutil.TempFile(dir, "")
defer os.Remove(f.Name())
defer f.Close()

f.WriteString(" 127.0.0.1 host localhost ")

ah.load(ah.table, f.Name())
ips := ah.Process("localhost")
assert.True(t, ips[0].Equal(net.ParseIP("127.0.0.1")))
}

0 comments on commit a22b621

Please sign in to comment.