server: handle bracketed IPv6 addresses correctly
1 file changed, 16 insertions(+), 5 deletions(-)
jump to
M internal/server/tcp.go → internal/server/tcp.go
@@ -8,18 +8,29 @@ "go.alanpearce.eu/x/listenfd" ) func (s *Server) serveTCP() error { + addr := joinHostPort(s.options.ListenAddress, s.options.Port) s.log.Debug( "fallback listener", "addr", - net.JoinHostPort(s.options.ListenAddress, strconv.Itoa(s.options.Port)), + addr, ) - l, err := listenfd.GetListener(0, - net.JoinHostPort(s.options.ListenAddress, strconv.Itoa(s.options.Port)), - s.log.Named("tcp.listenfd"), - ) + l, err := listenfd.GetListener(0, addr, s.log.Named("tcp.listenfd")) if err != nil { return err } return s.server.Serve(l) } + +func joinHostPort(host string, port int) string { + if len(host) < 1 { + return "" + } + + portString := strconv.Itoa(port) + if host[0] == '[' && host[len(host)-1] == ']' { + return host + ":" + portString + } + + return net.JoinHostPort(host, portString) +}