mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-29 17:16:31 +00:00
temp ban count working, lockup when exceeding max temp bans
This commit is contained in:
parent
7d288cec1e
commit
2bf7b3d2f8
@ -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,41 +13,27 @@ 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
|
||||||
|
|
||||||
if !cfg.Enabled {
|
if !cfg.Enabled {
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for permanent blacklist by pubkey or npub
|
log.Printf("Checking blacklist for pubkey: %s", pubkey)
|
||||||
if isPubKeyPermanentlyBlacklisted(pubkey) {
|
|
||||||
return true, fmt.Sprintf("pubkey %s is permanently blacklisted", pubkey)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for temporary ban
|
// Check for permanent blacklist by pubkey or npub
|
||||||
if isPubKeyTemporarilyBlacklisted(pubkey) {
|
if isPubKeyPermanentlyBlacklisted(pubkey) {
|
||||||
return true, fmt.Sprintf("pubkey %s is temporarily blacklisted", pubkey)
|
log.Printf("Pubkey %s is permanently blacklisted", pubkey)
|
||||||
}
|
return true, fmt.Sprintf("pubkey %s is permanently blacklisted", pubkey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for temporary ban
|
||||||
|
if isPubKeyTemporarilyBlacklisted(pubkey) {
|
||||||
|
log.Printf("Pubkey %s is temporarily blacklisted", pubkey)
|
||||||
|
return true, fmt.Sprintf("pubkey %s is temporarily blacklisted", pubkey)
|
||||||
|
}
|
||||||
|
|
||||||
// Check for permanent ban based on wordlist
|
// Check for permanent ban based on wordlist
|
||||||
for _, word := range cfg.PermanentBanWords {
|
for _, word := range cfg.PermanentBanWords {
|
||||||
@ -76,51 +63,82 @@ func CheckBlacklist(pubkey, eventContent string) (bool, string) {
|
|||||||
|
|
||||||
// Checks if a pubkey is temporarily blacklisted
|
// Checks if a pubkey is temporarily blacklisted
|
||||||
func isPubKeyTemporarilyBlacklisted(pubkey string) bool {
|
func isPubKeyTemporarilyBlacklisted(pubkey string) bool {
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
entry, exists := tempBannedPubkeys[pubkey]
|
||||||
|
if !exists {
|
||||||
|
log.Printf("Pubkey %s not found in temporary blacklist", pubkey)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
if now.After(entry.unbanTime) {
|
||||||
|
log.Printf("Temporary ban for pubkey %s has expired. Count: %d", pubkey, entry.count)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Pubkey %s is currently temporarily blacklisted. Count: %d, Unban time: %s", pubkey, entry.count, entry.unbanTime)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearTemporaryBans() {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
tempBannedPubkeys = make(map[string]*tempBanEntry)
|
||||||
|
}
|
||||||
|
|
||||||
entry, exists := tempBannedPubkeys[pubkey]
|
var (
|
||||||
if !exists {
|
tempBannedPubkeys = make(map[string]*tempBanEntry)
|
||||||
return false
|
mu sync.Mutex
|
||||||
}
|
)
|
||||||
|
|
||||||
// If the ban has expired, remove it from the temporary ban list
|
type tempBanEntry struct {
|
||||||
if time.Now().After(entry.unbanTime) {
|
count int
|
||||||
delete(tempBannedPubkeys, pubkey)
|
unbanTime time.Time
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|
||||||
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 {
|
|
||||||
delete(tempBannedPubkeys, pubkey)
|
|
||||||
return AddToPermanentBlacklist(pubkey)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
if entry.count > cfg.MaxTempBans {
|
||||||
|
log.Printf("Attempting to move pubkey %s to permanent blacklist", pubkey)
|
||||||
|
delete(tempBannedPubkeys, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if a pubkey is permanently blacklisted (only using config.yml)
|
// Checks if a pubkey is permanently blacklisted (only using config.yml)
|
||||||
|
Loading…
Reference in New Issue
Block a user