Browse Source

add remote port to HTTP requests in logs (#1663)

pull/1672/head
Alessandro Ros 3 years ago committed by GitHub
parent
commit
3ba133bc60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      internal/core/api.go
  2. 3
      internal/core/hls_server.go
  3. 8
      internal/core/http_logger.go
  4. 10
      internal/core/http_serverheader.go
  5. 3
      internal/core/webrtc_server.go

6
internal/core/api.go

@ -153,10 +153,12 @@ func newAPI(
} }
router := gin.New() router := gin.New()
router.SetTrustedProxies(nil) router.SetTrustedProxies(nil)
mwLog := httpLoggerMiddleware(a) mwLog := httpLoggerMiddleware(a)
router.NoRoute(mwLog) router.NoRoute(mwLog, httpServerHeaderMiddleware)
group := router.Group("/", mwLog) group := router.Group("/", mwLog, httpServerHeaderMiddleware)
group.GET("/v1/config/get", a.onConfigGet) group.GET("/v1/config/get", a.onConfigGet)
group.POST("/v1/config/set", a.onConfigSet) group.POST("/v1/config/set", a.onConfigSet)

3
internal/core/hls_server.go

@ -182,7 +182,6 @@ func (s *hlsServer) run() {
defer s.wg.Done() defer s.wg.Done()
router := gin.New() router := gin.New()
router.NoRoute(httpLoggerMiddleware(s), s.onRequest)
tmp := make([]string, len(s.trustedProxies)) tmp := make([]string, len(s.trustedProxies))
for i, entry := range s.trustedProxies { for i, entry := range s.trustedProxies {
@ -190,6 +189,8 @@ func (s *hlsServer) run() {
} }
router.SetTrustedProxies(tmp) router.SetTrustedProxies(tmp)
router.NoRoute(httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)
hs := &http.Server{ hs := &http.Server{
Handler: router, Handler: router,
TLSConfig: s.tlsConfig, TLSConfig: s.tlsConfig,

8
internal/core/http_logger.go

@ -43,18 +43,16 @@ type httpLoggerParent interface {
func httpLoggerMiddleware(p httpLoggerParent) func(*gin.Context) { func httpLoggerMiddleware(p httpLoggerParent) func(*gin.Context) {
return func(ctx *gin.Context) { return func(ctx *gin.Context) {
p.log(logger.Debug, "[conn %v] %s %s", ctx.ClientIP(), ctx.Request.Method, ctx.Request.URL.Path) p.log(logger.Debug, "[conn %v] %s %s", ctx.Request.RemoteAddr, ctx.Request.Method, ctx.Request.URL.Path)
byts, _ := httputil.DumpRequest(ctx.Request, true) byts, _ := httputil.DumpRequest(ctx.Request, true)
p.log(logger.Debug, "[conn %v] [c->s] %s", ctx.ClientIP(), string(byts)) p.log(logger.Debug, "[conn %v] [c->s] %s", ctx.Request.RemoteAddr, string(byts))
logw := &httpLoggerWriter{ResponseWriter: ctx.Writer} logw := &httpLoggerWriter{ResponseWriter: ctx.Writer}
ctx.Writer = logw ctx.Writer = logw
ctx.Writer.Header().Set("Server", "mediamtx")
ctx.Next() ctx.Next()
p.log(logger.Debug, "[conn %v] [s->c] %s", ctx.ClientIP(), logw.dump()) p.log(logger.Debug, "[conn %v] [s->c] %s", ctx.Request.RemoteAddr, logw.dump())
} }
} }

10
internal/core/http_serverheader.go

@ -0,0 +1,10 @@
package core
import (
"github.com/gin-gonic/gin"
)
func httpServerHeaderMiddleware(ctx *gin.Context) {
ctx.Writer.Header().Set("Server", "mediamtx")
ctx.Next()
}

3
internal/core/webrtc_server.go

@ -218,7 +218,6 @@ func (s *webRTCServer) run() {
defer rp.close() defer rp.close()
router := gin.New() router := gin.New()
router.NoRoute(rp.mw, httpLoggerMiddleware(s), s.onRequest)
tmp := make([]string, len(s.trustedProxies)) tmp := make([]string, len(s.trustedProxies))
for i, entry := range s.trustedProxies { for i, entry := range s.trustedProxies {
@ -226,6 +225,8 @@ func (s *webRTCServer) run() {
} }
router.SetTrustedProxies(tmp) router.SetTrustedProxies(tmp)
router.NoRoute(rp.mw, httpLoggerMiddleware(s), httpServerHeaderMiddleware, s.onRequest)
hs := &http.Server{ hs := &http.Server{
Handler: router, Handler: router,
TLSConfig: s.tlsConfig, TLSConfig: s.tlsConfig,

Loading…
Cancel
Save