fix: only track ip without port

This commit is contained in:
crapStone 2025-06-17 23:07:51 +02:00
parent b22d3665a9
commit 612a5554d5
No known key found for this signature in database
GPG key ID: 22D4BF0CF7CC29C8
2 changed files with 13 additions and 5 deletions

View file

@ -1,6 +1,7 @@
package handler
import (
"net"
"net/http"
"strings"
"sync"
@ -32,11 +33,18 @@ func Handler(
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(req.RemoteAddr, uint(0))
value, _ := mostActiveIpMap.LoadOrStore(ip, uint(0))
count := value.(uint)
success = mostActiveIpMap.CompareAndSwap(req.RemoteAddr, count, count+1)
success = mostActiveIpMap.CompareAndSwap(ip, count, count+1)
}
}