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

6
flake.lock generated
View file

@ -20,11 +20,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1749285348,
"narHash": "sha256-frdhQvPbmDYaScPFiCnfdh3B/Vh81Uuoo0w5TkWmmjU=",
"lastModified": 1749794982,
"narHash": "sha256-Kh9K4taXbVuaLC0IL+9HcfvxsSUx8dPB5s5weJcc9pc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "3e3afe5174c561dee0df6f2c2b2236990146329f",
"rev": "ee930f9755f58096ac6e8ca94a1887e0534e2d81",
"type": "github"
},
"original": {

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)
}
}