aboutsummaryrefslogtreecommitdiff
path: root/ssh/knownhosts/knownhosts.go
diff options
context:
space:
mode:
Diffstat (limited to 'ssh/knownhosts/knownhosts.go')
-rw-r--r--ssh/knownhosts/knownhosts.go22
1 files changed, 14 insertions, 8 deletions
diff --git a/ssh/knownhosts/knownhosts.go b/ssh/knownhosts/knownhosts.go
index c022e41..1ebd7e6 100644
--- a/ssh/knownhosts/knownhosts.go
+++ b/ssh/knownhosts/knownhosts.go
@@ -421,20 +421,26 @@ func New(files ...string) (ssh.HostKeyCallback, error) {
return certChecker.CheckHostKey, nil
}
-// Normalize normalizes an address into the form used in known_hosts
+// Normalize normalizes an address into the form used in known_hosts. Supports
+// IPv4, hostnames, bracketed IPv6. Any other non-standard formats are returned
+// with minimal transformation.
func Normalize(address string) string {
+ const defaultSSHPort = "22"
+
host, port, err := net.SplitHostPort(address)
if err != nil {
host = address
- port = "22"
+ port = defaultSSHPort
+ }
+
+ if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
+ host = host[1 : len(host)-1]
}
- entry := host
- if port != "22" {
- entry = "[" + entry + "]:" + port
- } else if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
- entry = "[" + entry + "]"
+
+ if port == defaultSSHPort {
+ return host
}
- return entry
+ return "[" + host + "]:" + port
}
// Line returns a line to add append to the known_hosts files.