Skip to content

Commit

Permalink
fix(server): Fix link when external IP is IPv6 (#335)
Browse files Browse the repository at this point in the history
The external IP module can return an IPv6 address, in which case the
method to append a port is to wrap it in square brackets before
appending the ":%d" port string.

Unfortunately Go's IP module does not have a great way to determine
whether an address is an IPv6 address, so fall back to counting the
number of colons in the result as suggested in
https://stackoverflow.com/a/48519490.
  • Loading branch information
neverpanic authored Sep 1, 2024
1 parent a98e754 commit 0a0bd60
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ func New(cfg *config.Config) (*Server, error) {
if err != nil {
panic(err)
}
hostname = fmt.Sprintf("%s:%d", extIP.String(), port)
extIPString := extIP.String()
fmtstring := "%s:%d"
if strings.Count(extIPString, ":") >= 2 {
// IPv6 address, wrap it in [] to add a port
fmtstring = "[%s]:%d"
}
hostname = fmt.Sprintf(fmtstring, extIPString, port)
}
// Use a fully-qualified domain name if set
if cfg.FQDN != "" {
Expand Down

0 comments on commit 0a0bd60

Please sign in to comment.