mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-06-29 11:42:40 +02:00
feat: add option to log the n most active IPs each hour (#496)
Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/496 Co-authored-by: crapStone <me@crapstone.dev> Co-committed-by: crapStone <me@crapstone.dev>
This commit is contained in:
parent
d27c594c28
commit
5477ba2c46
8 changed files with 97 additions and 5 deletions
|
@ -1,8 +1,10 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
|
@ -24,10 +26,28 @@ func Handler(
|
|||
cfg config.ServerConfig,
|
||||
giteaClient *gitea.Client,
|
||||
canonicalDomainCache, redirectsCache cache.ICache,
|
||||
mostActiveIpMap *sync.Map,
|
||||
) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
ctx := context.New(w, req)
|
||||
log := log.With().Str("ReqId", ctx.ReqId).Strs("Handler", []string{req.Host, req.RequestURI}).Logger()
|
||||
|
||||
if cfg.LogMostActiveIps {
|
||||
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("Failed to get IP address. Using complete remoteAddr string")
|
||||
|
||||
ip = req.RemoteAddr
|
||||
}
|
||||
|
||||
success := false
|
||||
for !success {
|
||||
value, _ := mostActiveIpMap.LoadOrStore(ip, uint(0))
|
||||
count := value.(uint)
|
||||
success = mostActiveIpMap.CompareAndSwap(ip, count, count+1)
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug().Msg("\n----------------------------------------------------------")
|
||||
|
||||
ctx.RespWriter.Header().Set("Server", "pages-server")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue