Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update txeh.go #16

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}