temp ban count working, lockup when exceeding max temp bans

This commit is contained in:
0ceanSlim 2024-08-16 09:12:15 -04:00
parent 7d288cec1e
commit 2bf7b3d2f8

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"grain/config" "grain/config"
cfg "grain/config/types" cfg "grain/config/types"
"log"
"os" "os"
"strings" "strings"
"sync" "sync"
@ -12,24 +13,6 @@ import (
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
// Structure to manage temporary bans with timestamps
type tempBanEntry struct {
count int // Number of temporary bans
unbanTime time.Time // Time when the pubkey should be unbanned
}
var (
tempBannedPubkeys = make(map[string]*tempBanEntry)
mu sync.Mutex
)
func ClearTemporaryBans() {
mu.Lock()
defer mu.Unlock()
tempBannedPubkeys = make(map[string]*tempBanEntry)
}
// CheckBlacklist checks if a pubkey is in the blacklist based on event content // CheckBlacklist checks if a pubkey is in the blacklist based on event content
func CheckBlacklist(pubkey, eventContent string) (bool, string) { func CheckBlacklist(pubkey, eventContent string) (bool, string) {
cfg := config.GetConfig().Blacklist cfg := config.GetConfig().Blacklist
@ -38,13 +21,17 @@ func CheckBlacklist(pubkey, eventContent string) (bool, string) {
return false, "" return false, ""
} }
log.Printf("Checking blacklist for pubkey: %s", pubkey)
// Check for permanent blacklist by pubkey or npub // Check for permanent blacklist by pubkey or npub
if isPubKeyPermanentlyBlacklisted(pubkey) { if isPubKeyPermanentlyBlacklisted(pubkey) {
log.Printf("Pubkey %s is permanently blacklisted", pubkey)
return true, fmt.Sprintf("pubkey %s is permanently blacklisted", pubkey) return true, fmt.Sprintf("pubkey %s is permanently blacklisted", pubkey)
} }
// Check for temporary ban // Check for temporary ban
if isPubKeyTemporarilyBlacklisted(pubkey) { if isPubKeyTemporarilyBlacklisted(pubkey) {
log.Printf("Pubkey %s is temporarily blacklisted", pubkey)
return true, fmt.Sprintf("pubkey %s is temporarily blacklisted", pubkey) return true, fmt.Sprintf("pubkey %s is temporarily blacklisted", pubkey)
} }
@ -81,18 +68,36 @@ func isPubKeyTemporarilyBlacklisted(pubkey string) bool {
entry, exists := tempBannedPubkeys[pubkey] entry, exists := tempBannedPubkeys[pubkey]
if !exists { if !exists {
log.Printf("Pubkey %s not found in temporary blacklist", pubkey)
return false return false
} }
// If the ban has expired, remove it from the temporary ban list now := time.Now()
if time.Now().After(entry.unbanTime) { if now.After(entry.unbanTime) {
delete(tempBannedPubkeys, pubkey) log.Printf("Temporary ban for pubkey %s has expired. Count: %d", pubkey, entry.count)
return false return false
} }
log.Printf("Pubkey %s is currently temporarily blacklisted. Count: %d, Unban time: %s", pubkey, entry.count, entry.unbanTime)
return true return true
} }
func ClearTemporaryBans() {
mu.Lock()
defer mu.Unlock()
tempBannedPubkeys = make(map[string]*tempBanEntry)
}
var (
tempBannedPubkeys = make(map[string]*tempBanEntry)
mu sync.Mutex
)
type tempBanEntry struct {
count int
unbanTime time.Time
}
// Adds a pubkey to the temporary blacklist // Adds a pubkey to the temporary blacklist
func AddToTemporaryBlacklist(pubkey string) error { func AddToTemporaryBlacklist(pubkey string) error {
mu.Lock() mu.Lock()
@ -100,24 +105,37 @@ func AddToTemporaryBlacklist(pubkey string) error {
cfg := config.GetConfig().Blacklist cfg := config.GetConfig().Blacklist
// Check if the pubkey is already temporarily banned
entry, exists := tempBannedPubkeys[pubkey] entry, exists := tempBannedPubkeys[pubkey]
if !exists { if !exists {
log.Printf("Creating new temporary ban entry for pubkey %s", pubkey)
entry = &tempBanEntry{ entry = &tempBanEntry{
count: 1, count: 0,
unbanTime: time.Now().Add(time.Duration(cfg.TempBanDuration) * time.Second), unbanTime: time.Now(),
} }
tempBannedPubkeys[pubkey] = entry tempBannedPubkeys[pubkey] = entry
} else {
log.Printf("Updating existing temporary ban entry for pubkey %s. Current count: %d", pubkey, entry.count)
// If the ban has expired, we don't reset the count, just update the unban time
if time.Now().After(entry.unbanTime) {
log.Printf("Previous ban for pubkey %s has expired. Keeping count at %d", pubkey, entry.count)
}
} }
// Increment the temporary ban count and set the unban time // Increment the count
entry.count++ entry.count++
entry.unbanTime = time.Now().Add(time.Duration(cfg.TempBanDuration) * time.Second) entry.unbanTime = time.Now().Add(time.Duration(cfg.TempBanDuration) * time.Second)
// If the count exceeds max_temp_bans, move to permanent blacklist log.Printf("Pubkey %s temporary ban count updated to: %d, MaxTempBans: %d, New unban time: %s", pubkey, entry.count, cfg.MaxTempBans, entry.unbanTime)
if entry.count >= cfg.MaxTempBans {
if entry.count > cfg.MaxTempBans {
log.Printf("Attempting to move pubkey %s to permanent blacklist", pubkey)
delete(tempBannedPubkeys, pubkey) delete(tempBannedPubkeys, pubkey)
return AddToPermanentBlacklist(pubkey) err := AddToPermanentBlacklist(pubkey)
if err != nil {
log.Printf("Error adding pubkey %s to permanent blacklist: %v", pubkey, err)
return err
}
log.Printf("Successfully added pubkey %s to permanent blacklist", pubkey)
} }
return nil return nil