2021-08-09 16:03:37 +03:00
|
|
|
package aghnet
|
|
|
|
|
|
|
|
import (
|
2023-04-12 14:48:42 +03:00
|
|
|
"net/netip"
|
|
|
|
"strings"
|
2021-08-09 16:03:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// GenerateHostname generates the hostname from ip. In case of using IPv4 the
|
|
|
|
// result should be like:
|
|
|
|
//
|
2022-09-07 18:03:18 +03:00
|
|
|
// 192-168-10-1
|
2021-08-09 16:03:37 +03:00
|
|
|
//
|
|
|
|
// In case of using IPv6, the result is like:
|
|
|
|
//
|
2022-09-07 18:03:18 +03:00
|
|
|
// ff80-f076-0000-0000-0000-0000-0000-0010
|
2021-08-09 16:03:37 +03:00
|
|
|
//
|
2022-04-19 15:01:49 +03:00
|
|
|
// ip must be either an IPv4 or an IPv6.
|
2023-04-12 14:48:42 +03:00
|
|
|
func GenerateHostname(ip netip.Addr) (hostname string) {
|
|
|
|
if !ip.IsValid() {
|
|
|
|
// TODO(s.chzhen): Get rid of it.
|
|
|
|
panic("aghnet generate hostname: invalid ip")
|
|
|
|
}
|
|
|
|
|
|
|
|
ip = ip.Unmap()
|
|
|
|
hostname = ip.StringExpanded()
|
|
|
|
|
|
|
|
if ip.Is4() {
|
2023-07-12 15:13:31 +03:00
|
|
|
return strings.ReplaceAll(hostname, ".", "-")
|
2021-08-09 16:03:37 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 15:13:31 +03:00
|
|
|
return strings.ReplaceAll(hostname, ":", "-")
|
2021-08-09 16:03:37 +03:00
|
|
|
}
|