1
0
Fork 0
forked from External/ergo

factor out some shared code

This commit is contained in:
Shivaram Lingamneni 2021-09-18 21:28:16 -04:00
parent 657ce0f1a4
commit e0e4791f72
5 changed files with 66 additions and 52 deletions

View file

@ -193,3 +193,37 @@ func HandleXForwardedFor(remoteAddr string, xForwardedFor string, whitelist []ne
// or nil:
return
}
// LookupHostname does an (optionally reverse-confirmed) hostname lookup
// suitable for use as an IRC hostname. It falls back to a string
// representation of the IP address (again suitable for use as an IRC
// hostname).
func LookupHostname(ip net.IP, forwardConfirm bool) (hostname string, lookupSuccessful bool) {
ipString := ip.String()
var candidate string
names, err := net.LookupAddr(ipString)
if err == nil && 0 < len(names) {
candidate = strings.TrimSuffix(names[0], ".")
}
if IsHostname(candidate) {
if forwardConfirm {
addrs, err := net.LookupHost(candidate)
if err == nil {
for _, addr := range addrs {
if forwardIP := net.ParseIP(addr); ip.Equal(forwardIP) {
hostname = candidate // successful forward confirmation
break
}
}
}
} else {
hostname = candidate
}
}
if hostname != "" {
return hostname, true
} else {
return IPStringToHostname(ipString), false
}
}