From 066fe276ebad3f19f05a75f3b4d15179c767f69a Mon Sep 17 00:00:00 2001 From: Ivan Vilata-i-Balaguer Date: Wed, 3 May 2023 14:22:19 +0200 Subject: [PATCH] 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. --- CONFIGURATION.md | 4 ++++ config.go | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index de5d64d..5802950 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -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` diff --git a/config.go b/config.go index 20203d4..42b2044 100644 --- a/config.go +++ b/config.go @@ -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) }