Skip to content

Commit

Permalink
Update txeh.go
Browse files Browse the repository at this point in the history
fix ipv4 and ipv6 using localhost at same time
  • Loading branch information
docooler authored Aug 17, 2021
1 parent 09e2953 commit cd12e07
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions txeh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package txeh
import (
"fmt"
"io/ioutil"
"regexp"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -211,6 +212,10 @@ func (h *Hosts) AddHost(addressRaw string, hostRaw string) {

// if the hostname is at a different address, go and remove it from the address
for hidx, hst := range h.hostFileLines[hflIdx].Hostnames {
//for localhost we can match more than one host
if isLocalhost(address) {
break
}
if hst == host {
h.Lock()
h.hostFileLines[hflIdx].Hostnames = removeStringElement(h.hostFileLines[hflIdx].Hostnames, hidx)
Expand Down Expand Up @@ -372,3 +377,9 @@ func lineFormatter(hfl HostFileLine) string {
}
return fmt.Sprintf("%-16s %s", hfl.Address, strings.Join(hfl.Hostnames, " "))
}
// IPLocalhost is a regex pattern for IPv4 or IPv6 loopback range.
const ipLocalhost = `((127\.([0-9]{1,3}\.){2}[0-9]{1,3})|(::1)$)`
var localhostIPRegexp = regexp.MustCompile(ipLocalhost)
func isLocalhost(address string) bool {
return localhostIPRegexp.MatchString(address)
}

0 comments on commit cd12e07

Please sign in to comment.