do not listen on IPv6 when host is 0.0.0.0 (#1665) (#1678)

This commit is contained in:
Alessandro Ros 2023-04-10 22:48:33 +02:00 committed by GitHub
parent 67741d16dd
commit 34757620b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 28 additions and 10 deletions

View File

@ -134,7 +134,7 @@ func newAPI(
webRTCServer apiWebRTCServer, webRTCServer apiWebRTCServer,
parent apiParent, parent apiParent,
) (*api, error) { ) (*api, error) {
ln, err := net.Listen("tcp", address) ln, err := net.Listen(restrictNetwork("tcp", address))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -106,7 +106,7 @@ func newHLSServer(
metrics *metrics, metrics *metrics,
parent hlsServerParent, parent hlsServerParent,
) (*hlsServer, error) { ) (*hlsServer, error) {
ln, err := net.Listen("tcp", address) ln, err := net.Listen(restrictNetwork("tcp", address))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -40,7 +40,7 @@ func newMetrics(
address string, address string,
parent metricsParent, parent metricsParent,
) (*metrics, error) { ) (*metrics, error) {
ln, err := net.Listen("tcp", address) ln, err := net.Listen(restrictNetwork(restrictNetwork("tcp", address)))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -27,7 +27,7 @@ func newPPROF(
address string, address string,
parent pprofParent, parent pprofParent,
) (*pprof, error) { ) (*pprof, error) {
ln, err := net.Listen("tcp", address) ln, err := net.Listen(restrictNetwork("tcp", address))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -0,0 +1,17 @@
package core
import (
"net"
)
// do not listen on IPv6 when address is 0.0.0.0.
func restrictNetwork(network string, address string) (string, string) {
host, _, err := net.SplitHostPort(address)
if err == nil {
if host == "0.0.0.0" {
return network + "4", address
}
}
return network, address
}

View File

@ -93,7 +93,7 @@ func newRTMPServer(
) (*rtmpServer, error) { ) (*rtmpServer, error) {
ln, err := func() (net.Listener, error) { ln, err := func() (net.Listener, error) {
if !isTLS { if !isTLS {
return net.Listen("tcp", address) return net.Listen(restrictNetwork("tcp", address))
} }
cert, err := tls.LoadX509KeyPair(serverCert, serverKey) cert, err := tls.LoadX509KeyPair(serverCert, serverKey)
@ -101,7 +101,8 @@ func newRTMPServer(
return nil, err return nil, err
} }
return tls.Listen("tcp", address, &tls.Config{Certificates: []tls.Certificate{cert}}) network, address := restrictNetwork("tcp", address)
return tls.Listen(network, address, &tls.Config{Certificates: []tls.Certificate{cert}})
}() }()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -98,7 +98,7 @@ func (s *udpSource) run(ctx context.Context, cnf *conf.PathConf, reloadConf chan
hostPort := cnf.Source[len("udp://"):] hostPort := cnf.Source[len("udp://"):]
pc, err := net.ListenPacket("udp", hostPort) pc, err := net.ListenPacket(restrictNetwork("udp", hostPort))
if err != nil { if err != nil {
return err return err
} }

View File

@ -116,7 +116,7 @@ func newWebRTCServer(
iceUDPMuxAddress string, iceUDPMuxAddress string,
iceTCPMuxAddress string, iceTCPMuxAddress string,
) (*webRTCServer, error) { ) (*webRTCServer, error) {
ln, err := net.Listen("tcp", address) ln, err := net.Listen(restrictNetwork("tcp", address))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -137,7 +137,7 @@ func newWebRTCServer(
var iceUDPMux ice.UDPMux var iceUDPMux ice.UDPMux
var udpMuxLn net.PacketConn var udpMuxLn net.PacketConn
if iceUDPMuxAddress != "" { if iceUDPMuxAddress != "" {
udpMuxLn, err = net.ListenPacket("udp", iceUDPMuxAddress) udpMuxLn, err = net.ListenPacket(restrictNetwork("udp", iceUDPMuxAddress))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -147,7 +147,7 @@ func newWebRTCServer(
var iceTCPMux ice.TCPMux var iceTCPMux ice.TCPMux
var tcpMuxLn net.Listener var tcpMuxLn net.Listener
if iceTCPMuxAddress != "" { if iceTCPMuxAddress != "" {
tcpMuxLn, err = net.Listen("tcp", iceTCPMuxAddress) tcpMuxLn, err = net.Listen(restrictNetwork("tcp", iceTCPMuxAddress))
if err != nil { if err != nil {
return nil, err return nil, err
} }