feat: Enhance AddEntries and RemoveByHostname functions with subdomain and alias handling
This commit is contained in:
+15
-2
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user