mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-10-29 17:16:31 +00:00
perma blacklist functionality, temp banning not working
This commit is contained in:
parent
22729612ff
commit
39436b564e
@ -1,17 +1,6 @@
|
||||
{{define "view"}}
|
||||
<main class="flex flex-col items-center justify-center p-8">
|
||||
<div class="mb-4">You are now viewing the {{.Title}}</div>
|
||||
<!--<h2>Top Ten Most Recent Events</h2>
|
||||
<ul>
|
||||
{{range .Events}}
|
||||
<li>
|
||||
<strong>ID:</strong> {{.ID}}<br />
|
||||
<strong>Content:</strong> {{.Content}}<br />
|
||||
<strong>Created At:</strong> {{.CreatedAt}}<br />
|
||||
<strong>PubKey:</strong> {{.PubKey}}
|
||||
</li>
|
||||
{{end}}
|
||||
</ul>-->
|
||||
<button
|
||||
hx-get="/import-events"
|
||||
hx-swap="outerHTML"
|
||||
|
@ -1,6 +1,6 @@
|
||||
{{define "layout"}}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" data-theme="{{.Theme}}">
|
||||
<html lang="en" data-theme="">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
11
config/types/blacklistConfig.go
Normal file
11
config/types/blacklistConfig.go
Normal file
@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
type BlacklistConfig struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
PermanentBanWords []string `yaml:"permanent_ban_words"`
|
||||
TempBanWords []string `yaml:"temp_ban_words"`
|
||||
MaxTempBans int `yaml:"max_temp_bans"`
|
||||
TempBanDuration int `yaml:"temp_ban_duration"`
|
||||
PermanentBlacklistPubkeys []string `yaml:"permanent_blacklist_pubkeys"`
|
||||
PermanentBlacklistNpubs []string `yaml:"permanent_blacklist_npubs"`
|
||||
}
|
@ -17,4 +17,5 @@ type ServerConfig struct {
|
||||
PubkeyWhitelist PubkeyWhitelistConfig `yaml:"pubkey_whitelist"`
|
||||
KindWhitelist KindWhitelistConfig `yaml:"kind_whitelist"`
|
||||
DomainWhitelist DomainWhitelistConfig `yaml:"domain_whitelist"`
|
||||
Blacklist BlacklistConfig `yaml:"blacklist"`
|
||||
}
|
||||
|
@ -84,6 +84,13 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventS
|
||||
return
|
||||
}
|
||||
|
||||
// Check against manual blacklist
|
||||
blacklisted, msg := utils.CheckBlacklist(evt.PubKey, evt.Content)
|
||||
if blacklisted {
|
||||
response.SendOK(ws, evt.ID, false, msg)
|
||||
return
|
||||
}
|
||||
|
||||
category := determineCategory(evt.Kind)
|
||||
|
||||
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {
|
||||
|
99
server/utils/checkBlacklist.go
Normal file
99
server/utils/checkBlacklist.go
Normal file
@ -0,0 +1,99 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"grain/config"
|
||||
cfg "grain/config/types"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// CheckBlacklist checks if a pubkey is in the blacklist based on event content
|
||||
func CheckBlacklist(pubkey, eventContent string) (bool, string) {
|
||||
cfg := config.GetConfig().Blacklist
|
||||
|
||||
if !cfg.Enabled {
|
||||
return false, ""
|
||||
}
|
||||
|
||||
// Check for permanent blacklist by pubkey or npub
|
||||
if isPubKeyPermanentlyBlacklisted(pubkey) {
|
||||
return true, fmt.Sprintf("pubkey %s is permanently blacklisted", pubkey)
|
||||
}
|
||||
|
||||
// Check for permanent ban based on wordlist
|
||||
for _, word := range cfg.PermanentBanWords {
|
||||
if strings.Contains(eventContent, word) {
|
||||
// Permanently ban the pubkey
|
||||
err := AddToPermanentBlacklist(pubkey)
|
||||
if err != nil {
|
||||
return true, fmt.Sprintf("pubkey %s is permanently banned and failed to save: %v", pubkey, err)
|
||||
}
|
||||
return true, fmt.Sprintf("pubkey %s is permanently banned for containing forbidden words", pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
return false, ""
|
||||
}
|
||||
|
||||
func isPubKeyPermanentlyBlacklisted(pubKey string) bool {
|
||||
cfg := config.GetConfig().Blacklist
|
||||
if !cfg.Enabled {
|
||||
return false
|
||||
}
|
||||
|
||||
// Check pubkeys
|
||||
for _, blacklistedKey := range cfg.PermanentBlacklistPubkeys {
|
||||
if pubKey == blacklistedKey {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Check npubs
|
||||
for _, npub := range cfg.PermanentBlacklistNpubs {
|
||||
decodedPubKey, err := DecodeNpub(npub)
|
||||
if err != nil {
|
||||
fmt.Println("Error decoding npub:", err)
|
||||
continue
|
||||
}
|
||||
if pubKey == decodedPubKey {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func AddToPermanentBlacklist(pubkey string) error {
|
||||
cfg := config.GetConfig().Blacklist
|
||||
|
||||
// Check if already blacklisted
|
||||
if isPubKeyPermanentlyBlacklisted(pubkey) {
|
||||
return fmt.Errorf("pubkey %s is already in the permanent blacklist", pubkey)
|
||||
}
|
||||
|
||||
// Add pubkey to the blacklist
|
||||
cfg.PermanentBlacklistPubkeys = append(cfg.PermanentBlacklistPubkeys, pubkey)
|
||||
|
||||
// Persist changes to config.yml
|
||||
return saveBlacklistConfig(cfg)
|
||||
}
|
||||
|
||||
func saveBlacklistConfig(blacklistConfig cfg.BlacklistConfig) error {
|
||||
cfg := config.GetConfig()
|
||||
cfg.Blacklist = blacklistConfig
|
||||
|
||||
data, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal config: %v", err)
|
||||
}
|
||||
|
||||
err = os.WriteFile("config.yml", data, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write config to file: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user