From 3b04ba36c358b0b5803558091098b47d9f35402f Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Fri, 15 Jan 2021 18:42:53 +0100 Subject: [PATCH] add parameter listenIP to listen on a specific IP/interface (#166) --- internal/conf/conf.go | 1 + internal/serverplain/server.go | 9 ++++++--- internal/servertls/server.go | 9 ++++++--- internal/serverudpl/server.go | 9 ++++++--- main.go | 16 ++++++++++++---- rtsp-simple-server.yml | 2 ++ 6 files changed, 33 insertions(+), 13 deletions(-) diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 5d0e94f9..c57558e9 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -34,6 +34,7 @@ type Conf struct { ProtocolsParsed map[gortsplib.StreamProtocol]struct{} `yaml:"-" json:"-"` Encryption string `yaml:"encryption"` EncryptionParsed Encryption `yaml:"-" json:"-"` + ListenIP string `yaml:"listenIP"` RtspPort int `yaml:"rtspPort"` RtspsPort int `yaml:"rtspsPort"` RTPPort int `yaml:"rtpPort"` diff --git a/internal/serverplain/server.go b/internal/serverplain/server.go index b7d06d25..320aea58 100644 --- a/internal/serverplain/server.go +++ b/internal/serverplain/server.go @@ -26,7 +26,9 @@ type Server struct { } // New allocates a Server. -func New(port int, +func New( + listenIP string, + port int, readTimeout time.Duration, writeTimeout time.Duration, readBufferCount uint64, @@ -42,7 +44,8 @@ func New(port int, UDPRTCPListener: udpRTCPListener, } - srv, err := conf.Serve(":" + strconv.FormatInt(int64(port), 10)) + address := listenIP + ":" + strconv.FormatInt(int64(port), 10) + srv, err := conf.Serve(address) if err != nil { return nil, err } @@ -54,7 +57,7 @@ func New(port int, done: make(chan struct{}), } - parent.Log(logger.Info, "[TCP/RTSP listener] opened on :%d", port) + parent.Log(logger.Info, "[TCP/RTSP listener] opened on %s", address) go s.run() return s, nil diff --git a/internal/servertls/server.go b/internal/servertls/server.go index cbf55677..fde85a3b 100644 --- a/internal/servertls/server.go +++ b/internal/servertls/server.go @@ -27,7 +27,9 @@ type Server struct { } // New allocates a Server. -func New(port int, +func New( + listenIP string, + port int, readTimeout time.Duration, writeTimeout time.Duration, readBufferCount uint64, @@ -47,7 +49,8 @@ func New(port int, ReadBufferCount: readBufferCount, } - srv, err := conf.Serve(":" + strconv.FormatInt(int64(port), 10)) + address := listenIP + ":" + strconv.FormatInt(int64(port), 10) + srv, err := conf.Serve(address) if err != nil { return nil, err } @@ -59,7 +62,7 @@ func New(port int, done: make(chan struct{}), } - parent.Log(logger.Info, "[TCP/TLS/RTSPS listener] opened on :%d", port) + parent.Log(logger.Info, "[TCP/TLS/RTSPS listener] opened on %s", address) go s.run() return s, nil diff --git a/internal/serverudpl/server.go b/internal/serverudpl/server.go index 0a4d9b85..68a9266f 100644 --- a/internal/serverudpl/server.go +++ b/internal/serverudpl/server.go @@ -13,11 +13,14 @@ type Parent interface { } // New allocates a gortsplib.ServerUDPListener. -func New(port int, +func New( + listenIP string, + port int, streamType gortsplib.StreamType, parent Parent) (*gortsplib.ServerUDPListener, error) { - listener, err := gortsplib.NewServerUDPListener(":" + strconv.FormatInt(int64(port), 10)) + address := listenIP + ":" + strconv.FormatInt(int64(port), 10) + listener, err := gortsplib.NewServerUDPListener(address) if err != nil { return nil, err } @@ -28,7 +31,7 @@ func New(port int, } return "RTCP" }() - parent.Log(logger.Info, "[UDP/"+label+" listener] opened on :%d", port) + parent.Log(logger.Info, "[UDP/"+label+" listener] opened on %s", address) return listener, nil } diff --git a/main.go b/main.go index a1f4cfe6..a7b50dcf 100644 --- a/main.go +++ b/main.go @@ -183,6 +183,7 @@ func (p *program) createResources(initial bool) error { if _, ok := p.conf.ProtocolsParsed[gortsplib.StreamProtocolUDP]; ok { if p.serverUDPRTP == nil { p.serverUDPRTP, err = serverudpl.New( + p.conf.ListenIP, p.conf.RTPPort, gortsplib.StreamTypeRTP, p) @@ -193,6 +194,7 @@ func (p *program) createResources(initial bool) error { if p.serverUDPRTCP == nil { p.serverUDPRTCP, err = serverudpl.New( + p.conf.ListenIP, p.conf.RTCPPort, gortsplib.StreamTypeRTCP, p) @@ -205,6 +207,7 @@ func (p *program) createResources(initial bool) error { if p.serverPlain == nil { if p.conf.EncryptionParsed == conf.EncryptionNo || p.conf.EncryptionParsed == conf.EncryptionOptional { p.serverPlain, err = serverplain.New( + p.conf.ListenIP, p.conf.RtspPort, p.conf.ReadTimeout, p.conf.WriteTimeout, @@ -221,6 +224,7 @@ func (p *program) createResources(initial bool) error { if p.serverTLS == nil { if p.conf.EncryptionParsed == conf.EncryptionStrict || p.conf.EncryptionParsed == conf.EncryptionOptional { p.serverTLS, err = servertls.New( + p.conf.ListenIP, p.conf.RtspsPort, p.conf.ReadTimeout, p.conf.WriteTimeout, @@ -286,22 +290,25 @@ func (p *program) closeResources(newConf *conf.Conf) { closeServerUDPRTP := false if newConf == nil || !reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) || - newConf.WriteTimeout != p.conf.WriteTimeout || - newConf.RTPPort != p.conf.RTPPort { + newConf.ListenIP != p.conf.ListenIP || + newConf.RTPPort != p.conf.RTPPort || + newConf.WriteTimeout != p.conf.WriteTimeout { closeServerUDPRTP = true } closeServerUDPRTCP := false if newConf == nil || !reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) || - newConf.WriteTimeout != p.conf.WriteTimeout || - newConf.RTCPPort != p.conf.RTCPPort { + newConf.ListenIP != p.conf.ListenIP || + newConf.RTCPPort != p.conf.RTCPPort || + newConf.WriteTimeout != p.conf.WriteTimeout { closeServerUDPRTCP = true } closeServerPlain := false if newConf == nil || newConf.EncryptionParsed != p.conf.EncryptionParsed || + newConf.ListenIP != p.conf.ListenIP || newConf.RtspPort != p.conf.RtspPort || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || @@ -314,6 +321,7 @@ func (p *program) closeResources(newConf *conf.Conf) { closeServerTLS := false if newConf == nil || newConf.EncryptionParsed != p.conf.EncryptionParsed || + newConf.ListenIP != p.conf.ListenIP || newConf.RtspsPort != p.conf.RtspsPort || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || diff --git a/rtsp-simple-server.yml b/rtsp-simple-server.yml index 7d24b7b5..c692ec7c 100644 --- a/rtsp-simple-server.yml +++ b/rtsp-simple-server.yml @@ -15,6 +15,8 @@ protocols: [udp, tcp] # encrypt handshake and TCP streams with TLS (RTSPS). # available values are "no", "strict", "optional". encryption: no +# listen IP. If provided, all listeners will listen on this specific IP. +listenIP: # port of the TCP/RTSP listener. This is used only if encryption is "no" or "optional". rtspPort: 8554 # port of the TCP/TLS/RTSPS listener. This is used only if encryption is "strict" or "optional".