From 95b7b4d6f2469651e373dc708b52fac588367cc9 Mon Sep 17 00:00:00 2001 From: Thomas <27960254+thomiceli@users.noreply.github.com> Date: Mon, 29 Jun 2026 13:19:51 +0700 Subject: [PATCH] Support IPv6 literals (#749) --- internal/db/db.go | 3 ++- internal/ipc/ipc.go | 2 +- internal/ssh/run.go | 5 +++-- internal/web/handlers/metrics/server.go | 3 ++- internal/web/server/server.go | 2 +- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/internal/db/db.go b/internal/db/db.go index f35dbcc..63c5223 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -3,6 +3,7 @@ package db import ( "errors" "fmt" + "net" "net/url" "path/filepath" "slices" @@ -262,7 +263,7 @@ func setupMySQL(dbInfo databaseInfo) error { if dbInfo.Socket != "" { protocol = fmt.Sprintf("unix(%s)", dbInfo.Socket) } else { - protocol = fmt.Sprintf("tcp(%s:%s)", dbInfo.Host, dbInfo.Port) + protocol = fmt.Sprintf("tcp(%s)", net.JoinHostPort(dbInfo.Host, dbInfo.Port)) } dsn := fmt.Sprintf("%s:%s@%s/%s?charset=utf8mb4&parseTime=True&loc=Local", dbInfo.User, dbInfo.Password, protocol, dbInfo.Database) diff --git a/internal/ipc/ipc.go b/internal/ipc/ipc.go index ebf3391..a4c04dc 100644 --- a/internal/ipc/ipc.go +++ b/internal/ipc/ipc.go @@ -63,7 +63,7 @@ func client() (*http.Client, string) { if host == "0.0.0.0" || host == "::" || host == "" { host = "127.0.0.1" } - return &http.Client{}, "http://" + host + ":" + port + return &http.Client{}, "http://" + net.JoinHostPort(host, port) } func post(path string, reqBody, respBody any) error { diff --git a/internal/ssh/run.go b/internal/ssh/run.go index e49fbc2..c776d1f 100644 --- a/internal/ssh/run.go +++ b/internal/ssh/run.go @@ -49,8 +49,9 @@ func Start() { } func listen(serverConfig *ssh.ServerConfig) { - log.Info().Msg("Starting SSH server on ssh://" + config.C.SshHost + ":" + config.C.SshPort) - listener, err := net.Listen("tcp", config.C.SshHost+":"+config.C.SshPort) + addr := net.JoinHostPort(config.C.SshHost, config.C.SshPort) + log.Info().Msg("Starting SSH server on ssh://" + addr) + listener, err := net.Listen("tcp", addr) if err != nil { log.Fatal().Err(err).Msg("SSH: Failed to start SSH server") } diff --git a/internal/web/handlers/metrics/server.go b/internal/web/handlers/metrics/server.go index ebd46a5..30872e4 100644 --- a/internal/web/handlers/metrics/server.go +++ b/internal/web/handlers/metrics/server.go @@ -1,6 +1,7 @@ package metrics import ( + "net" "net/http" "github.com/labstack/echo-contrib/echoprometheus" @@ -31,7 +32,7 @@ func NewServer() *Server { } func (s *Server) Start() { - addr := config.C.MetricsHost + ":" + config.C.MetricsPort + addr := net.JoinHostPort(config.C.MetricsHost, config.C.MetricsPort) log.Info().Msg("Starting metrics server on http://" + addr) if err := s.echo.Start(addr); err != nil && err != http.ErrServerClosed { log.Error().Err(err).Msg("Failed to start metrics server") diff --git a/internal/web/server/server.go b/internal/web/server/server.go index 4fdd597..4835b9e 100644 --- a/internal/web/server/server.go +++ b/internal/web/server/server.go @@ -62,7 +62,7 @@ func (s *Server) Start() { } func (s *Server) startHTTP() { - addr := config.C.HttpHost + ":" + config.C.HttpPort + addr := net.JoinHostPort(config.C.HttpHost, config.C.HttpPort) log.Info().Msg("Starting HTTP server on http://" + addr) if err := s.echo.Start(addr); err != nil && err != http.ErrServerClosed {