diff --git a/internal/core/api.go b/internal/core/api.go index a7ed9406d2c..1398f455af1 100644 --- a/internal/core/api.go +++ b/internal/core/api.go @@ -117,9 +117,9 @@ type api struct { webRTCServer apiWebRTCServer parent apiParent - ln net.Listener - mutex sync.Mutex - s *http.Server + ln net.Listener + httpServer *http.Server + mutex sync.Mutex } func newAPI( @@ -153,7 +153,6 @@ func newAPI( } router := gin.New() - router.SetTrustedProxies(nil) mwLog := httpLoggerMiddleware(a) @@ -199,12 +198,12 @@ func newAPI( group.POST("/v1/webrtcconns/kick/:id", a.onWebRTCConnsKick) } - a.s = &http.Server{ + a.httpServer = &http.Server{ Handler: router, ErrorLog: log.New(&nilWriter{}, "", 0), } - go a.s.Serve(ln) + go a.httpServer.Serve(ln) a.log(logger.Info, "listener opened on "+address) @@ -213,7 +212,7 @@ func newAPI( func (a *api) close() { a.log(logger.Info, "listener is closing") - a.s.Shutdown(context.Background()) + a.httpServer.Shutdown(context.Background()) a.ln.Close() // in case Shutdown() is called before Serve() } diff --git a/internal/core/hls_server.go b/internal/core/hls_server.go index da7be643ed6..9dd4e51a4f1 100644 --- a/internal/core/hls_server.go +++ b/internal/core/hls_server.go @@ -63,19 +63,18 @@ type hlsServer struct { partDuration conf.StringDuration segmentMaxSize conf.StringSize allowOrigin string - trustedProxies conf.IPsOrCIDRs directory string readBufferCount int pathManager *pathManager metrics *metrics parent hlsServerParent - ctx context.Context - ctxCancel func() - wg sync.WaitGroup - ln net.Listener - tlsConfig *tls.Config - muxers map[string]*hlsMuxer + ctx context.Context + ctxCancel func() + wg sync.WaitGroup + ln net.Listener + httpServer *http.Server + muxers map[string]*hlsMuxer // in chPathSourceReady chan *path @@ -135,7 +134,6 @@ func newHLSServer( partDuration: partDuration, segmentMaxSize: segmentMaxSize, allowOrigin: allowOrigin, - trustedProxies: trustedProxies, directory: directory, readBufferCount: readBufferCount, pathManager: pathManager, @@ -144,7 +142,6 @@ func newHLSServer( ctx: ctx, ctxCancel: ctxCancel, ln: ln, - tlsConfig: tlsConfig, muxers: make(map[string]*hlsMuxer), chPathSourceReady: make(chan *path), chPathSourceNotReady: make(chan *path), @@ -153,6 +150,17 @@ func newHLSServer( chAPIMuxerList: make(chan hlsServerAPIMuxersListReq), } + router := gin.New() + httpSetTrustedProxies(router, trustedProxies) + + router.NoRoute(httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest) + + s.httpServer = &http.Server{ + Handler: router, + TLSConfig: tlsConfig, + ErrorLog: log.New(&nilWriter{}, "", 0), + } + s.log(logger.Info, "listener opened on "+address) s.pathManager.hlsServerSet(s) @@ -181,26 +189,10 @@ func (s *hlsServer) close() { func (s *hlsServer) run() { defer s.wg.Done() - router := gin.New() - - tmp := make([]string, len(s.trustedProxies)) - for i, entry := range s.trustedProxies { - tmp[i] = entry.String() - } - router.SetTrustedProxies(tmp) - - router.NoRoute(httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest) - - hs := &http.Server{ - Handler: router, - TLSConfig: s.tlsConfig, - ErrorLog: log.New(&nilWriter{}, "", 0), - } - - if s.tlsConfig != nil { - go hs.ServeTLS(s.ln, "", "") + if s.httpServer.TLSConfig != nil { + go s.httpServer.ServeTLS(s.ln, "", "") } else { - go hs.Serve(s.ln) + go s.httpServer.Serve(s.ln) } outer: @@ -258,7 +250,7 @@ outer: s.ctxCancel() - hs.Shutdown(context.Background()) + s.httpServer.Shutdown(context.Background()) s.ln.Close() // in case Shutdown() is called before Serve() s.pathManager.hlsServerSet(nil) diff --git a/internal/core/http_set_trusted_proxies.go b/internal/core/http_set_trusted_proxies.go new file mode 100644 index 00000000000..2bac9d4fd3b --- /dev/null +++ b/internal/core/http_set_trusted_proxies.go @@ -0,0 +1,15 @@ +package core + +import ( + "github.com/gin-gonic/gin" + + "github.com/aler9/mediamtx/internal/conf" +) + +func httpSetTrustedProxies(router *gin.Engine, trustedProxies conf.IPsOrCIDRs) { + tmp := make([]string, len(trustedProxies)) + for i, entry := range trustedProxies { + tmp[i] = entry.String() + } + router.SetTrustedProxies(tmp) +} diff --git a/internal/core/metrics.go b/internal/core/metrics.go index 3a2e474c9a4..ef4dfed37da 100644 --- a/internal/core/metrics.go +++ b/internal/core/metrics.go @@ -26,7 +26,7 @@ type metrics struct { parent metricsParent ln net.Listener - server *http.Server + httpServer *http.Server mutex sync.Mutex pathManager apiPathManager rtspServer apiRTSPServer @@ -52,23 +52,26 @@ func newMetrics( router := gin.New() router.SetTrustedProxies(nil) - router.GET("/metrics", m.onMetrics) - m.server = &http.Server{ + mwLog := httpLoggerMiddleware(m) + router.NoRoute(mwLog) + router.GET("/metrics", mwLog, m.onMetrics) + + m.httpServer = &http.Server{ Handler: router, ErrorLog: log.New(&nilWriter{}, "", 0), } m.log(logger.Info, "listener opened on "+address) - go m.server.Serve(m.ln) + go m.httpServer.Serve(m.ln) return m, nil } func (m *metrics) close() { m.log(logger.Info, "listener is closing") - m.server.Shutdown(context.Background()) + m.httpServer.Shutdown(context.Background()) m.ln.Close() // in case Shutdown() is called before Serve() } diff --git a/internal/core/pprof.go b/internal/core/pprof.go index bae04d98a42..a58cf8953e1 100644 --- a/internal/core/pprof.go +++ b/internal/core/pprof.go @@ -19,8 +19,8 @@ type pprofParent interface { type pprof struct { parent pprofParent - ln net.Listener - server *http.Server + ln net.Listener + httpServer *http.Server } func newPPROF( @@ -37,21 +37,21 @@ func newPPROF( ln: ln, } - pp.server = &http.Server{ + pp.httpServer = &http.Server{ Handler: http.DefaultServeMux, ErrorLog: log.New(&nilWriter{}, "", 0), } pp.log(logger.Info, "listener opened on "+address) - go pp.server.Serve(pp.ln) + go pp.httpServer.Serve(pp.ln) return pp, nil } func (pp *pprof) close() { pp.log(logger.Info, "listener is closing") - pp.server.Shutdown(context.Background()) + pp.httpServer.Shutdown(context.Background()) pp.ln.Close() // in case Shutdown() is called before Serve() } diff --git a/internal/core/webrtc_server.go b/internal/core/webrtc_server.go index 227aa9823e9..2997b558344 100644 --- a/internal/core/webrtc_server.go +++ b/internal/core/webrtc_server.go @@ -80,9 +80,10 @@ type webRTCServer struct { ctx context.Context ctxCancel func() ln net.Listener + requestPool *httpRequestPool + httpServer *http.Server udpMuxLn net.PacketConn tcpMuxLn net.Listener - tlsConfig *tls.Config conns map[*webRTCConn]struct{} iceHostNAT1To1IPs []string iceUDPMux ice.UDPMux @@ -170,7 +171,6 @@ func newWebRTCServer( ln: ln, udpMuxLn: udpMuxLn, tcpMuxLn: tcpMuxLn, - tlsConfig: tlsConfig, iceUDPMux: iceUDPMux, iceTCPMux: iceTCPMux, iceHostNAT1To1IPs: iceHostNAT1To1IPs, @@ -182,6 +182,19 @@ func newWebRTCServer( done: make(chan struct{}), } + s.requestPool = newHTTPRequestPool() + + router := gin.New() + httpSetTrustedProxies(router, trustedProxies) + + router.NoRoute(s.requestPool.mw, httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest) + + s.httpServer = &http.Server{ + Handler: router, + TLSConfig: tlsConfig, + ErrorLog: log.New(&nilWriter{}, "", 0), + } + str := "listener opened on " + address + " (HTTP)" if udpMuxLn != nil { str += ", " + iceUDPMuxAddress + " (ICE/UDP)" @@ -214,29 +227,10 @@ func (s *webRTCServer) close() { func (s *webRTCServer) run() { defer close(s.done) - rp := newHTTPRequestPool() - defer rp.close() - - router := gin.New() - - tmp := make([]string, len(s.trustedProxies)) - for i, entry := range s.trustedProxies { - tmp[i] = entry.String() - } - router.SetTrustedProxies(tmp) - - router.NoRoute(rp.mw, httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest) - - hs := &http.Server{ - Handler: router, - TLSConfig: s.tlsConfig, - ErrorLog: log.New(&nilWriter{}, "", 0), - } - - if s.tlsConfig != nil { - go hs.ServeTLS(s.ln, "", "") + if s.httpServer.TLSConfig != nil { + go s.httpServer.ServeTLS(s.ln, "", "") } else { - go hs.Serve(s.ln) + go s.httpServer.Serve(s.ln) } var wg sync.WaitGroup @@ -307,8 +301,9 @@ outer: s.ctxCancel() - hs.Shutdown(context.Background()) + s.httpServer.Shutdown(context.Background()) s.ln.Close() // in case Shutdown() is called before Serve() + s.requestPool.close() wg.Wait()