+ auto-hosts: respond to PTR requests

Close #1562

Squashed commit of the following:

commit d5c6bb0e5f0c8c1618bd0df764ae86a5e62a850b
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Mon Apr 13 14:10:10 2020 +0300

    + auto-hosts: respond to PTR requests
This commit is contained in:
Simon Zolin
2020-04-16 18:56:47 +03:00
parent 215e3eeaf6
commit 1a1c09135d
5 changed files with 210 additions and 49 deletions

View File

@@ -4,9 +4,11 @@ import (
"io/ioutil"
"net"
"os"
"strings"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
@@ -28,19 +30,21 @@ func TestAutoHostsResolution(t *testing.T) {
defer f.Close()
_, _ = f.WriteString(" 127.0.0.1 host localhost \n")
_, _ = f.WriteString(" ::1 localhost \n")
ah.Init(f.Name())
// Update from the hosts file
ah.updateHosts()
// Existing host
ips := ah.Process("localhost")
ips := ah.Process("localhost", dns.TypeA)
assert.NotNil(t, ips)
assert.Equal(t, 1, len(ips))
assert.Equal(t, net.ParseIP("127.0.0.1"), ips[0])
// Unknown host
ips = ah.Process("newhost")
ips = ah.Process("newhost", dns.TypeA)
assert.Nil(t, ips)
// Test hosts file
@@ -49,6 +53,14 @@ func TestAutoHostsResolution(t *testing.T) {
assert.NotNil(t, ips)
assert.Equal(t, 1, len(ips))
assert.Equal(t, "127.0.0.1", ips[0].String())
// Test PTR
a, _ := dns.ReverseAddr("127.0.0.1")
a = strings.TrimSuffix(a, ".")
assert.True(t, ah.ProcessReverse(a, dns.TypePTR) == "host")
a, _ = dns.ReverseAddr("::1")
a = strings.TrimSuffix(a, ".")
assert.True(t, ah.ProcessReverse(a, dns.TypePTR) == "localhost")
}
func TestAutoHostsFSNotify(t *testing.T) {
@@ -67,7 +79,7 @@ func TestAutoHostsFSNotify(t *testing.T) {
ah.updateHosts()
// Unknown host
ips := ah.Process("newhost")
ips := ah.Process("newhost", dns.TypeA)
assert.Nil(t, ips)
// Stat monitoring for changes
@@ -82,8 +94,18 @@ func TestAutoHostsFSNotify(t *testing.T) {
time.Sleep(50 * time.Millisecond)
// Check if we are notified about changes
ips = ah.Process("newhost")
ips = ah.Process("newhost", dns.TypeA)
assert.NotNil(t, ips)
assert.Equal(t, 1, len(ips))
assert.Equal(t, "127.0.0.2", ips[0].String())
}
func TestIP(t *testing.T) {
assert.True(t, dnsUnreverseAddr("1.0.0.127.in-addr.arpa").Equal(net.ParseIP("127.0.0.1").To4()))
assert.True(t, dnsUnreverseAddr("4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa").Equal(net.ParseIP("::abcd:1234")))
assert.True(t, dnsUnreverseAddr("1.0.0.127.in-addr.arpa.") == nil)
assert.True(t, dnsUnreverseAddr(".0.0.127.in-addr.arpa") == nil)
assert.True(t, dnsUnreverseAddr(".3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") == nil)
assert.True(t, dnsUnreverseAddr("4.3.2.1.d.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0..ip6.arpa") == nil)
}