Browse Source

add read deadline to all incoming HTTP requests (#1689)

pull/1685/head
Alessandro Ros 2 years ago committed by GitHub
parent
commit
88953f36a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      internal/core/api.go
  2. 14
      internal/core/core.go
  3. 8
      internal/core/hls_server.go
  4. 8
      internal/core/metrics.go
  5. 8
      internal/core/pprof.go
  6. 8
      internal/core/webrtc_server.go

7
internal/core/api.go

@ -8,6 +8,7 @@ import (
"net/http" "net/http"
"reflect" "reflect"
"sync" "sync"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -124,6 +125,7 @@ type api struct {
func newAPI( func newAPI(
address string, address string,
readTimeout conf.StringDuration,
conf *conf.Conf, conf *conf.Conf,
pathManager apiPathManager, pathManager apiPathManager,
rtspServer apiRTSPServer, rtspServer apiRTSPServer,
@ -199,8 +201,9 @@ func newAPI(
} }
a.httpServer = &http.Server{ a.httpServer = &http.Server{
Handler: router, Handler: router,
ErrorLog: log.New(&nilWriter{}, "", 0), ReadHeaderTimeout: time.Duration(readTimeout),
ErrorLog: log.New(&nilWriter{}, "", 0),
} }
go a.httpServer.Serve(ln) go a.httpServer.Serve(ln)

14
internal/core/core.go

@ -217,6 +217,7 @@ func (p *Core) createResources(initial bool) error {
if p.metrics == nil { if p.metrics == nil {
p.metrics, err = newMetrics( p.metrics, err = newMetrics(
p.conf.MetricsAddress, p.conf.MetricsAddress,
p.conf.ReadTimeout,
p, p,
) )
if err != nil { if err != nil {
@ -229,6 +230,7 @@ func (p *Core) createResources(initial bool) error {
if p.pprof == nil { if p.pprof == nil {
p.pprof, err = newPPROF( p.pprof, err = newPPROF(
p.conf.PPROFAddress, p.conf.PPROFAddress,
p.conf.ReadTimeout,
p, p,
) )
if err != nil { if err != nil {
@ -402,6 +404,7 @@ func (p *Core) createResources(initial bool) error {
p.conf.HLSAllowOrigin, p.conf.HLSAllowOrigin,
p.conf.HLSTrustedProxies, p.conf.HLSTrustedProxies,
p.conf.HLSDirectory, p.conf.HLSDirectory,
p.conf.ReadTimeout,
p.conf.ReadBufferCount, p.conf.ReadBufferCount,
p.pathManager, p.pathManager,
p.metrics, p.metrics,
@ -425,6 +428,7 @@ func (p *Core) createResources(initial bool) error {
p.conf.WebRTCAllowOrigin, p.conf.WebRTCAllowOrigin,
p.conf.WebRTCTrustedProxies, p.conf.WebRTCTrustedProxies,
p.conf.WebRTCICEServers, p.conf.WebRTCICEServers,
p.conf.ReadTimeout,
p.conf.ReadBufferCount, p.conf.ReadBufferCount,
p.pathManager, p.pathManager,
p.metrics, p.metrics,
@ -443,6 +447,7 @@ func (p *Core) createResources(initial bool) error {
if p.api == nil { if p.api == nil {
p.api, err = newAPI( p.api, err = newAPI(
p.conf.APIAddress, p.conf.APIAddress,
p.conf.ReadTimeout,
p.conf, p.conf,
p.pathManager, p.pathManager,
p.rtspServer, p.rtspServer,
@ -476,11 +481,13 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
closeMetrics := newConf == nil || closeMetrics := newConf == nil ||
newConf.Metrics != p.conf.Metrics || newConf.Metrics != p.conf.Metrics ||
newConf.MetricsAddress != p.conf.MetricsAddress newConf.MetricsAddress != p.conf.MetricsAddress ||
newConf.ReadTimeout != p.conf.ReadTimeout
closePPROF := newConf == nil || closePPROF := newConf == nil ||
newConf.PPROF != p.conf.PPROF || newConf.PPROF != p.conf.PPROF ||
newConf.PPROFAddress != p.conf.PPROFAddress newConf.PPROFAddress != p.conf.PPROFAddress ||
newConf.ReadTimeout != p.conf.ReadTimeout
closePathManager := newConf == nil || closePathManager := newConf == nil ||
newConf.RTSPAddress != p.conf.RTSPAddress || newConf.RTSPAddress != p.conf.RTSPAddress ||
@ -579,6 +586,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin || newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin ||
!reflect.DeepEqual(newConf.HLSTrustedProxies, p.conf.HLSTrustedProxies) || !reflect.DeepEqual(newConf.HLSTrustedProxies, p.conf.HLSTrustedProxies) ||
newConf.HLSDirectory != p.conf.HLSDirectory || newConf.HLSDirectory != p.conf.HLSDirectory ||
newConf.ReadTimeout != p.conf.ReadTimeout ||
newConf.ReadBufferCount != p.conf.ReadBufferCount || newConf.ReadBufferCount != p.conf.ReadBufferCount ||
closePathManager || closePathManager ||
closeMetrics closeMetrics
@ -593,6 +601,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
newConf.WebRTCAllowOrigin != p.conf.WebRTCAllowOrigin || newConf.WebRTCAllowOrigin != p.conf.WebRTCAllowOrigin ||
!reflect.DeepEqual(newConf.WebRTCTrustedProxies, p.conf.WebRTCTrustedProxies) || !reflect.DeepEqual(newConf.WebRTCTrustedProxies, p.conf.WebRTCTrustedProxies) ||
!reflect.DeepEqual(newConf.WebRTCICEServers, p.conf.WebRTCICEServers) || !reflect.DeepEqual(newConf.WebRTCICEServers, p.conf.WebRTCICEServers) ||
newConf.ReadTimeout != p.conf.ReadTimeout ||
newConf.ReadBufferCount != p.conf.ReadBufferCount || newConf.ReadBufferCount != p.conf.ReadBufferCount ||
closeMetrics || closeMetrics ||
closePathManager || closePathManager ||
@ -603,6 +612,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
closeAPI := newConf == nil || closeAPI := newConf == nil ||
newConf.API != p.conf.API || newConf.API != p.conf.API ||
newConf.APIAddress != p.conf.APIAddress || newConf.APIAddress != p.conf.APIAddress ||
newConf.ReadTimeout != p.conf.ReadTimeout ||
closePathManager || closePathManager ||
closeRTSPServer || closeRTSPServer ||
closeRTSPSServer || closeRTSPSServer ||

8
internal/core/hls_server.go

@ -100,6 +100,7 @@ func newHLSServer(
allowOrigin string, allowOrigin string,
trustedProxies conf.IPsOrCIDRs, trustedProxies conf.IPsOrCIDRs,
directory string, directory string,
readTimeout conf.StringDuration,
readBufferCount int, readBufferCount int,
pathManager *pathManager, pathManager *pathManager,
metrics *metrics, metrics *metrics,
@ -156,9 +157,10 @@ func newHLSServer(
router.NoRoute(httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest) router.NoRoute(httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)
s.httpServer = &http.Server{ s.httpServer = &http.Server{
Handler: router, Handler: router,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
ErrorLog: log.New(&nilWriter{}, "", 0), ReadHeaderTimeout: time.Duration(readTimeout),
ErrorLog: log.New(&nilWriter{}, "", 0),
} }
s.log(logger.Info, "listener opened on "+address) s.log(logger.Info, "listener opened on "+address)

8
internal/core/metrics.go

@ -8,9 +8,11 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"sync" "sync"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/aler9/mediamtx/internal/conf"
"github.com/aler9/mediamtx/internal/logger" "github.com/aler9/mediamtx/internal/logger"
) )
@ -38,6 +40,7 @@ type metrics struct {
func newMetrics( func newMetrics(
address string, address string,
readTimeout conf.StringDuration,
parent metricsParent, parent metricsParent,
) (*metrics, error) { ) (*metrics, error) {
ln, err := net.Listen(restrictNetwork(restrictNetwork("tcp", address))) ln, err := net.Listen(restrictNetwork(restrictNetwork("tcp", address)))
@ -58,8 +61,9 @@ func newMetrics(
router.GET("/metrics", mwLog, m.onMetrics) router.GET("/metrics", mwLog, m.onMetrics)
m.httpServer = &http.Server{ m.httpServer = &http.Server{
Handler: router, Handler: router,
ErrorLog: log.New(&nilWriter{}, "", 0), ReadHeaderTimeout: time.Duration(readTimeout),
ErrorLog: log.New(&nilWriter{}, "", 0),
} }
m.log(logger.Info, "listener opened on "+address) m.log(logger.Info, "listener opened on "+address)

8
internal/core/pprof.go

@ -5,10 +5,12 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"time"
// start pprof // start pprof
_ "net/http/pprof" _ "net/http/pprof"
"github.com/aler9/mediamtx/internal/conf"
"github.com/aler9/mediamtx/internal/logger" "github.com/aler9/mediamtx/internal/logger"
) )
@ -25,6 +27,7 @@ type pprof struct {
func newPPROF( func newPPROF(
address string, address string,
readTimeout conf.StringDuration,
parent pprofParent, parent pprofParent,
) (*pprof, error) { ) (*pprof, error) {
ln, err := net.Listen(restrictNetwork("tcp", address)) ln, err := net.Listen(restrictNetwork("tcp", address))
@ -38,8 +41,9 @@ func newPPROF(
} }
pp.httpServer = &http.Server{ pp.httpServer = &http.Server{
Handler: http.DefaultServeMux, Handler: http.DefaultServeMux,
ErrorLog: log.New(&nilWriter{}, "", 0), ReadHeaderTimeout: time.Duration(readTimeout),
ErrorLog: log.New(&nilWriter{}, "", 0),
} }
pp.log(logger.Info, "listener opened on "+address) pp.log(logger.Info, "listener opened on "+address)

8
internal/core/webrtc_server.go

@ -109,6 +109,7 @@ func newWebRTCServer(
allowOrigin string, allowOrigin string,
trustedProxies conf.IPsOrCIDRs, trustedProxies conf.IPsOrCIDRs,
iceServers []string, iceServers []string,
readTimeout conf.StringDuration,
readBufferCount int, readBufferCount int,
pathManager *pathManager, pathManager *pathManager,
metrics *metrics, metrics *metrics,
@ -190,9 +191,10 @@ func newWebRTCServer(
router.NoRoute(s.requestPool.mw, httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest) router.NoRoute(s.requestPool.mw, httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)
s.httpServer = &http.Server{ s.httpServer = &http.Server{
Handler: router, Handler: router,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
ErrorLog: log.New(&nilWriter{}, "", 0), ReadHeaderTimeout: time.Duration(readTimeout),
ErrorLog: log.New(&nilWriter{}, "", 0),
} }
str := "listener opened on " + address + " (HTTP)" str := "listener opened on " + address + " (HTTP)"

Loading…
Cancel
Save