mirror of
https://code.rocketnine.space/tslocum/twins.git
synced 2024-11-27 10:48:13 +01:00
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:
parent
24f3196a61
commit
066fe276eb
2 changed files with 8 additions and 4 deletions
|
@ -14,6 +14,10 @@ Address to listen for connections on in the format of `interface:port`.
|
||||||
|
|
||||||
`localhost:1965`
|
`localhost:1965`
|
||||||
|
|
||||||
|
### Listen on an IPv4 or IPv6 address
|
||||||
|
|
||||||
|
`192.0.2.1:1965` or `[2001:db8::1]:1965`
|
||||||
|
|
||||||
### Listen on all interfaces
|
### Listen on all interfaces
|
||||||
|
|
||||||
`:1965`
|
`:1965`
|
||||||
|
|
|
@ -127,13 +127,13 @@ func readconfig(configPath string) error {
|
||||||
newLine = "\r\n"
|
newLine = "\r\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
split := strings.Split(config.Listen, ":")
|
listenRe := regexp.MustCompile("(.*):([0-9]+)$")
|
||||||
if len(split) != 2 {
|
if !listenRe.MatchString(config.Listen) {
|
||||||
config.hostname = config.Listen
|
config.hostname = config.Listen
|
||||||
config.Listen += ":1965"
|
config.Listen += ":1965"
|
||||||
} else {
|
} else {
|
||||||
config.hostname = split[0]
|
config.hostname = listenRe.ReplaceAllString(config.Listen, "$1")
|
||||||
config.port, err = strconv.Atoi(split[1])
|
config.port, err = strconv.Atoi(listenRe.ReplaceAllString(config.Listen, "$2"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("invalid port specified: %s", err)
|
log.Fatalf("invalid port specified: %s", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue