Skip to content

Commit

Permalink
Feature: add PTR domain name resolution
Browse files Browse the repository at this point in the history
Signed-off-by: Fxzx micah <[email protected]>
  • Loading branch information
fxzxmicah committed Mar 18, 2024
1 parent fe50065 commit 7c71ee6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dns/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func LoadHosts() *trie.DomainTrie {
t := trie.New()
h := map[string][]net.IP{}
p := map[string][]string{}
n := map[string][]net.IP{}

sc := bufio.NewScanner(f)
for sc.Scan() {
Expand All @@ -36,22 +37,23 @@ func LoadHosts() *trie.DomainTrie {
continue
}

name := strings.Fields(line)
names := strings.Fields(line)
// ignore blank lines
if len(name) == 0 {
if len(names) == 0 {
continue
}

ip := net.ParseIP(name[0])
ip := net.ParseIP(names[0])
// ignore lines that do not start with IP
if ip == nil {
continue
}

ptr := transIpToPtr(ip)
for _, name := range name[1:] {
for _, name := range names[1:] {
h[name] = append(h[name], ip)
p[ptr] = append(p[ptr], name+".")
n[ptr] = append(n[ptr], h[name]...)
}
}

Expand All @@ -63,6 +65,10 @@ func LoadHosts() *trie.DomainTrie {
t.Insert(ptr, names)
}

for ptr, ips := range n {
t.Insert(ptr, ips)
}

return t
}

Expand Down
8 changes: 8 additions & 0 deletions test/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ dns:
rr, err = exchange("127.0.0.1:8553", "2606-4700-4700--1111.sslip.io", dns.TypeAAAA)
assert.NoError(t, err)
assert.Empty(t, rr)

rr, err := exchange("127.0.0.1:8553", "127.0.0.1", dns.TypePTR)
assert.NoError(t, err)
assert.Empty(t, rr)

rr, err = exchange("127.0.0.1:8553", "::1", dns.TypePTR)
assert.NoError(t, err)
assert.Empty(t, rr)
}

func TestClash_DNSHostAndFakeIP(t *testing.T) {
Expand Down

0 comments on commit 7c71ee6

Please sign in to comment.