Added host names in request log
This commit is contained in:
parent
f552a803db
commit
45f85d9a1e
1
main.go
1
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)
|
||||
|
47
middleware/IPToHost.go
Normal file
47
middleware/IPToHost.go
Normal file
@ -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)
|
||||
}
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user