From 05fd3a025c6ac5e9062e578a2771936c9d1e2545 Mon Sep 17 00:00:00 2001 From: Leander Schulten Date: Mon, 8 Jul 2024 19:35:56 +0200 Subject: [PATCH] Generate config: Properly handle ipv6 endpoints --- util/util.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/util/util.go b/util/util.go index ec700ff..117e79d 100644 --- a/util/util.go +++ b/util/util.go @@ -62,13 +62,29 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G desiredHost := setting.EndpointAddress desiredPort := server.Interface.ListenPort - if strings.Contains(desiredHost, ":") { - split := strings.Split(desiredHost, ":") - desiredHost = split[0] - if n, err := strconv.Atoi(split[1]); err == nil { - desiredPort = n + is_ipv4 := strings.Contains(desiredHost, ".") + if is_ipv4 { + if strings.Contains(desiredHost, ":") { + split := strings.Split(desiredHost, ":") + desiredHost = split[0] + if n, err := strconv.Atoi(split[1]); err == nil { + desiredPort = n + } else { + log.Error("Endpoint appears to be incorrectly formatted: ", err) + } + } + } else { + if strings.Contains(desiredHost, "]") { + // IPv6 with port + split := strings.Split(desiredHost, "]") + desiredHost = split[0] + "]" + if n, err := strconv.Atoi(split[1][1:]); err == nil { + desiredPort = n + } else { + log.Error("Endpoint appears to be incorrectly formatted: ", err) + } } else { - log.Error("Endpoint appears to be incorrectly formatted: ", err) + desiredHost = "[" + desiredHost + "]" } } peerEndpoint := fmt.Sprintf("Endpoint = %s:%d\n", desiredHost, desiredPort)