Skip to content

Commit

Permalink
Chore: use simpler logic to process hosts file
Browse files Browse the repository at this point in the history
Signed-off-by: Fxzx micah <[email protected]>
  • Loading branch information
fxzxmicah committed Mar 17, 2024
1 parent 9e02bc8 commit ca99546
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions dns/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dns

import (
"bufio"
"bytes"
"net"
"os"
"path"
Expand Down Expand Up @@ -37,26 +36,22 @@ func LoadHosts() *trie.DomainTrie {
continue
}

var ip net.IP
var ptr string
name := strings.Fields(line)
// ignore blank lines
if len(name) == 0 {
continue
}

buf := bytes.NewBuffer([]byte(line))
namesc := bufio.NewScanner(buf)
namesc.Split(bufio.ScanWords)
for namesc.Scan() {
name := namesc.Text()
ip := net.ParseIP(name[0])
// ignore lines that do not start with IP
if ip == nil {
continue
}

if ip == nil {
ip = net.ParseIP(name)
if ip == nil {
break
} else {
ptr = transIpToPtr(ip)
}
} else {
h[name] = append(h[name], ip)
p[ptr] = append(p[ptr], name+".")
}
ptr := transIpToPtr(ip)
for _, name := range name[1:] {
h[name] = append(h[name], ip)
p[ptr] = append(p[ptr], name+".")
}
}

Expand Down

0 comments on commit ca99546

Please sign in to comment.