From 9010c6a1d688f79951ecaf35d7c2fec991cf3e36 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Fri, 29 Nov 2019 17:32:40 +0200 Subject: [PATCH] lib/netutil: add `-enableTCP6` command-line flag for enabling listening for IPv6 additionally to IPv4 TCP ports --- lib/netutil/tcplistener.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/netutil/tcplistener.go b/lib/netutil/tcplistener.go index f63128b68..e1666348e 100644 --- a/lib/netutil/tcplistener.go +++ b/lib/netutil/tcplistener.go @@ -1,6 +1,7 @@ package netutil import ( + "flag" "fmt" "net" "time" @@ -8,12 +9,19 @@ import ( "github.com/VictoriaMetrics/metrics" ) +var enableTCP6 = flag.Bool("enableTCP6", false, "Whether to enable listening for IPv6 TCP ports. By default only IPv4 TCP ports are listened") + // NewTCPListener returns new TCP listener for the given addr. // // name is used for exported metrics. Each listener in the program must have // distinct name. func NewTCPListener(name, addr string) (*TCPListener, error) { - ln, err := net.Listen("tcp4", addr) + network := "tcp4" + if *enableTCP6 { + // Enable listening on both tcp4 and tcp6 + network = "tcp" + } + ln, err := net.Listen(network, addr) if err != nil { return nil, err }