From 45f85d9a1e65422d791ac5094c1763a6b2db8ca2 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 23 Jun 2017 15:38:46 +0200 Subject: [PATCH] Added host names in request log --- main.go | 1 + middleware/IPToHost.go | 47 ++++++++++++++++++++++++++++++++++++++++++ middleware/Log.go | 16 ++++++++++---- 3 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 middleware/IPToHost.go diff --git a/main.go b/main.go index 4dfe6162..2df2a9e9 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,7 @@ func configure(app *aero.Application) *aero.Application { app.Use(middleware.Log()) app.Use(middleware.Session()) app.Use(middleware.UserInfo()) + app.Use(middleware.IPToHost()) // API api := api.New("/api/", arn.DB) diff --git a/middleware/IPToHost.go b/middleware/IPToHost.go new file mode 100644 index 00000000..f702bf3b --- /dev/null +++ b/middleware/IPToHost.go @@ -0,0 +1,47 @@ +package middleware + +import ( + "net" + "time" + + "github.com/aerogo/aero" + cache "github.com/patrickmn/go-cache" +) + +var ipToHosts = cache.New(60*time.Minute, 30*time.Minute) + +// IPToHost middleware tries to find domain names for the given IP. +func IPToHost() aero.Middleware { + return func(ctx *aero.Context, next func()) { + next() + + go findHostsForIP(ctx.RealIP()) + } +} + +// GetHostsForIP returns all host names for the given IP (if cached). +func GetHostsForIP(ip string) []string { + hosts, found := ipToHosts.Get(ip) + + if !found || hosts == nil { + return nil + } + + return hosts.([]string) +} + +// Finds all host names for the given IP +func findHostsForIP(ip string) { + hosts, err := net.LookupAddr(ip) + + if err != nil { + return + } + + if len(hosts) == 0 { + return + } + + // Cache host names + ipToHosts.Set(ip, hosts, cache.DefaultExpiration) +} diff --git a/middleware/Log.go b/middleware/Log.go index 59dad41d..84c53f4d 100644 --- a/middleware/Log.go +++ b/middleware/Log.go @@ -29,12 +29,20 @@ func Log() aero.Middleware { responseTimeString = strings.Repeat(" ", 8-len(responseTimeString)) + responseTimeString user := utils.GetUser(ctx) + ip := ctx.RealIP() + + hostName := "" + hostNames := GetHostsForIP(ip) + + if len(hostNames) != 0 { + hostName = hostNames[0] + } // Log every request if user != nil { - request.Info(user.Nick, ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI()) + request.Info(user.Nick, ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) } else { - request.Info("[guest]", ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI()) + request.Info("[guest]", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) } // Log all requests that failed @@ -43,13 +51,13 @@ func Log() aero.Middleware { // Ok. default: - err.Error(http.StatusText(ctx.StatusCode), ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI()) + err.Error(http.StatusText(ctx.StatusCode), ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) } // Notify us about long requests. // However ignore requests under /auth/ because those depend on 3rd party servers. if responseTime >= 200*time.Millisecond && !strings.HasPrefix(ctx.URI(), "/auth/") { - err.Error("Long response time", ctx.RealIP(), ctx.StatusCode, responseTimeString, ctx.URI()) + err.Error("Long response time", ip, hostName, responseTimeString, ctx.StatusCode, ctx.URI()) } } }