From 37bd0198d9fd237fe64318f6c7c172a9d62d8f42 Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Wed, 25 Jun 2025 18:40:13 +0800 Subject: [PATCH] node: allow listening on IPv6 address #27635 (#1156) --- internal/debug/flags.go | 7 ++++++- node/config.go | 5 +++-- node/rpcstack.go | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/debug/flags.go b/internal/debug/flags.go index cf5b49e475..cfa591a3db 100644 --- a/internal/debug/flags.go +++ b/internal/debug/flags.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "log/slog" + "net" "net/http" _ "net/http/pprof" "os" @@ -312,7 +313,11 @@ func Setup(ctx *cli.Context) error { // pprof server if ctx.Bool(pprofFlag.Name) { - address := fmt.Sprintf("%s:%d", ctx.String(pprofAddrFlag.Name), ctx.Int(pprofPortFlag.Name)) + listenHost := ctx.String(pprofAddrFlag.Name) + + port := ctx.Int(pprofPortFlag.Name) + + address := net.JoinHostPort(listenHost, fmt.Sprintf("%d", port)) // This context value ("metrics-addr") represents the utils.MetricsHTTPFlag.Name. // It cannot be imported because it will cause a cyclical dependency. StartPProf(address, !ctx.IsSet("metrics-addr") && !ctx.IsSet("metrics.addr")) diff --git a/node/config.go b/node/config.go index c6aaa18e3c..e3d30a4af6 100644 --- a/node/config.go +++ b/node/config.go @@ -19,6 +19,7 @@ package node import ( "crypto/ecdsa" "fmt" + "net" "os" "path/filepath" "runtime" @@ -246,7 +247,7 @@ func (c *Config) HTTPEndpoint() string { if c.HTTPHost == "" { return "" } - return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort) + return net.JoinHostPort(c.HTTPHost, fmt.Sprintf("%d", c.HTTPPort)) } // DefaultHTTPEndpoint returns the HTTP endpoint used by default. @@ -265,7 +266,7 @@ func (c *Config) WSEndpoint() string { if c.WSHost == "" { return "" } - return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort) + return net.JoinHostPort(c.WSHost, fmt.Sprintf("%d", c.WSPort)) } // DefaultWSEndpoint returns the websocket endpoint used by default. diff --git a/node/rpcstack.go b/node/rpcstack.go index 06645f1f35..e079b14acb 100644 --- a/node/rpcstack.go +++ b/node/rpcstack.go @@ -111,7 +111,7 @@ func (h *httpServer) setListenAddr(host string, port int) error { } h.host, h.port = host, port - h.endpoint = fmt.Sprintf("%s:%d", host, port) + h.endpoint = net.JoinHostPort(host, fmt.Sprintf("%d", port)) return nil }