Fix listening on literal IPv6 address

Those contain colons, so splitting by colons would break the address in the
middle.  Instead, use a regular expression to split host and port at the last
colon, if followed by port digits and the end of the string.

Also, document the syntax for listening on literal IPv4 and IPv6 addresses.
This commit is contained in:
Ivan Vilata-i-Balaguer 2023-05-03 14:22:19 +02:00
parent 24f3196a61
commit 066fe276eb
2 changed files with 8 additions and 4 deletions

View file

@ -14,6 +14,10 @@ Address to listen for connections on in the format of `interface:port`.
`localhost:1965`
### Listen on an IPv4 or IPv6 address
`192.0.2.1:1965` or `[2001:db8::1]:1965`
### Listen on all interfaces
`:1965`

View file

@ -127,13 +127,13 @@ func readconfig(configPath string) error {
newLine = "\r\n"
}
split := strings.Split(config.Listen, ":")
if len(split) != 2 {
listenRe := regexp.MustCompile("(.*):([0-9]+)$")
if !listenRe.MatchString(config.Listen) {
config.hostname = config.Listen
config.Listen += ":1965"
} else {
config.hostname = split[0]
config.port, err = strconv.Atoi(split[1])
config.hostname = listenRe.ReplaceAllString(config.Listen, "$1")
config.port, err = strconv.Atoi(listenRe.ReplaceAllString(config.Listen, "$2"))
if err != nil {
log.Fatalf("invalid port specified: %s", err)
}