feat: Enhance AddEntries and RemoveByHostname functions with subdomain and alias handling

This commit is contained in:
2026-03-04 17:23:24 -05:00
parent 3550193e63
commit dfa7e1697d
2 changed files with 109 additions and 2 deletions
+15 -2
View File
@@ -57,6 +57,19 @@ func ParseManagedBlock(lines []string) (prefix, managed, postfix []string, hasMa
return prefix, managed, postfix, true, nil
}
// lineMatchesHostname parses a hosts-file line ("IP<whitespace>hostname [aliases...]")
// and returns true if any hostname field (including aliases) matches,
// using case-insensitive comparison per RFC 4343.
func lineMatchesHostname(line, hostname string) bool {
fields := strings.Fields(line)
for _, f := range fields[1:] {
if strings.EqualFold(f, hostname) {
return true
}
}
return false
}
// AddEntries removes existing entries for the same hostnames as the new entries,
// then appends the new entries. Returns deduplicated managed content lines.
func AddEntries(existing []string, entries []DNSEntry) []string {
@@ -71,7 +84,7 @@ func AddEntries(existing []string, entries []DNSEntry) []string {
for _, line := range existing {
keep := true
for h := range hostnames {
if strings.Contains(line, h) {
if lineMatchesHostname(line, h) {
keep = false
break
}
@@ -101,7 +114,7 @@ func RemoveByHostname(existing []string, hostnames []string) []string {
for _, line := range existing {
shouldRemove := false
for _, host := range hostnames {
if strings.Contains(line, host) {
if lineMatchesHostname(line, host) {
shouldRemove = true
break
}