node: fix listening on IPv6 address (#27628) (#27635)

This commit is contained in:
Curith 2023-07-02 19:21:16 +08:00 committed by GitHub
parent 8bbb16b70e
commit 6ca3ef9a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 6 deletions

View File

@ -27,6 +27,7 @@ import (
"fmt"
"io"
"math/big"
"net"
"os"
"os/signal"
"path/filepath"
@ -743,7 +744,7 @@ func signer(c *cli.Context) error {
port := c.Int(rpcPortFlag.Name)
// start http server
httpEndpoint := fmt.Sprintf("%s:%d", c.String(utils.HTTPListenAddrFlag.Name), port)
httpEndpoint := net.JoinHostPort(c.String(utils.HTTPListenAddrFlag.Name), fmt.Sprintf("%d", port))
httpServer, addr, err := node.StartHTTPEndpoint(httpEndpoint, rpc.DefaultHTTPTimeouts, handler)
if err != nil {
utils.Fatalf("Could not start RPC api: %v", err)

View File

@ -26,6 +26,7 @@ import (
"fmt"
"math"
"math/big"
"net"
"net/http"
"os"
"path/filepath"
@ -2014,7 +2015,7 @@ func SetupMetrics(ctx *cli.Context) {
}
if ctx.IsSet(MetricsHTTPFlag.Name) {
address := fmt.Sprintf("%s:%d", ctx.String(MetricsHTTPFlag.Name), ctx.Int(MetricsPortFlag.Name))
address := net.JoinHostPort(ctx.String(MetricsHTTPFlag.Name), fmt.Sprintf("%d", ctx.Int(MetricsPortFlag.Name)))
log.Info("Enabling stand-alone metrics HTTP endpoint", "address", address)
exp.Setup(address)
} else if ctx.IsSet(MetricsPortFlag.Name) {

View File

@ -19,6 +19,7 @@ package debug
import (
"fmt"
"io"
"net"
"net/http"
_ "net/http/pprof"
"os"
@ -308,7 +309,7 @@ func Setup(ctx *cli.Context) error {
port := ctx.Int(pprofPortFlag.Name)
address := fmt.Sprintf("%s:%d", listenHost, port)
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"))

View File

@ -19,6 +19,7 @@ package node
import (
"crypto/ecdsa"
"fmt"
"net"
"os"
"path/filepath"
"runtime"
@ -263,7 +264,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.
@ -278,7 +279,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.

View File

@ -112,7 +112,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
}