mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-23 17:07:13 +00:00
Compare commits
No commits in common. "158f284be92a35ac00144ce736135a5cdb2c5c1d" and "9df03646dbfd18d38f48530bfdca45b272cc39e3" have entirely different histories.
158f284be9
...
9df03646db
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,7 +1,5 @@
|
|||||||
/tmp
|
/tmp
|
||||||
config.yml
|
config.yml
|
||||||
whitelist.yml
|
|
||||||
blacklist.yml
|
|
||||||
relay_metadata.json
|
relay_metadata.json
|
||||||
grain.exe
|
grain.exe
|
||||||
/build
|
/build
|
||||||
|
@ -1,16 +0,0 @@
|
|||||||
enabled: true
|
|
||||||
permanent_ban_words:
|
|
||||||
- nigger
|
|
||||||
temp_ban_words:
|
|
||||||
- crypto
|
|
||||||
- web3
|
|
||||||
- airdrop
|
|
||||||
max_temp_bans: 3
|
|
||||||
temp_ban_duration: 3600
|
|
||||||
permanent_blacklist_pubkeys:
|
|
||||||
- db0c9b8acd6101adb9b281c5321f98f6eebb33c5719d230ed1870997538a9765
|
|
||||||
permanent_blacklist_npubs:
|
|
||||||
- npub1x0r5gflnk2mn6h3c70nvnywpy2j46gzqwg6k7uw6fxswyz0md9qqnhshtn
|
|
||||||
mutelist_authors:
|
|
||||||
- 3fe0ab6cbdb7ee27148202249e3fb3b89423c6f6cda6ef43ea5057c3d93088e4
|
|
||||||
# mutelist Event MUST be stored in this relay for it to be retrieved.
|
|
@ -14,12 +14,6 @@ server:
|
|||||||
max_connections: 100
|
max_connections: 100
|
||||||
max_subscriptions_per_client: 10
|
max_subscriptions_per_client: 10
|
||||||
|
|
||||||
event_time_constraints:
|
|
||||||
min_created_at: 1577836800 # January 1, 2020, as Unix timestamp
|
|
||||||
# min_created_at_string: now-5m # Custom value to indicate 5 minutes in the past
|
|
||||||
# max_created_at: 0 # Set to 0 to use the default behavior of 'now'
|
|
||||||
max_created_at_string: now+5m # Use a string to set a date for max created at in the future or past from current time
|
|
||||||
|
|
||||||
resource_limits:
|
resource_limits:
|
||||||
cpu_cores: 2 # Limit the number of CPU cores the application can use
|
cpu_cores: 2 # Limit the number of CPU cores the application can use
|
||||||
memory_mb: 1024 # Cap the maximum amount of RAM in MB the application can use
|
memory_mb: 1024 # Cap the maximum amount of RAM in MB the application can use
|
||||||
@ -69,6 +63,33 @@ rate_limit:
|
|||||||
limit: 25
|
limit: 25
|
||||||
burst: 50
|
burst: 50
|
||||||
|
|
||||||
|
pubkey_whitelist:
|
||||||
|
enabled: false
|
||||||
|
pubkeys: [] # List of allowed public keys
|
||||||
|
npubs: [] # List of allowed npubs (Nostr public keys in bech32 format)
|
||||||
|
|
||||||
|
kind_whitelist:
|
||||||
|
enabled: false
|
||||||
|
kinds: [] # List of allowed event kinds
|
||||||
|
|
||||||
|
domain_whitelist:
|
||||||
|
enabled: false
|
||||||
|
domains: [] # List of allowed domains
|
||||||
|
|
||||||
|
blacklist: #Removing a pubkey from the Blacklist requires a hard restart; Blacklist overides the Whitelist
|
||||||
|
enabled: true
|
||||||
|
permanent_ban_words: [] # Words that trigger a permanent ban
|
||||||
|
temp_ban_words: # Words that trigger a temporary ban
|
||||||
|
- crypto
|
||||||
|
- web3
|
||||||
|
- airdrop
|
||||||
|
max_temp_bans: 3 # Number of temporary bans before a permanent ban
|
||||||
|
temp_ban_duration: 3600 # Temporary ban duration in seconds
|
||||||
|
permanent_blacklist_pubkeys: # List of permanently banned public keys
|
||||||
|
- db0c9b8acd6101adb9b281c5321f98f6eebb33c5719d230ed1870997538a9765
|
||||||
|
permanent_blacklist_npubs: # List of permanently banned npubs
|
||||||
|
- npub1x0r5gflnk2mn6h3c70nvnywpy2j46gzqwg6k7uw6fxswyz0md9qqnhshtn
|
||||||
|
|
||||||
event_purge:
|
event_purge:
|
||||||
enabled: true # Toggle to enable/disable event purging
|
enabled: true # Toggle to enable/disable event purging
|
||||||
keep_duration_days: 2 # Number of days to keep events
|
keep_duration_days: 2 # Number of days to keep events
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
pubkey_whitelist:
|
|
||||||
enabled: false
|
|
||||||
pubkeys:
|
|
||||||
- pubkey1
|
|
||||||
- pubkey2
|
|
||||||
npubs:
|
|
||||||
- npub18ls2km9aklhzw9yzqgjfu0anhz2z83hkeknw7sl22ptu8kfs3rjq54am44
|
|
||||||
- npub2
|
|
||||||
|
|
||||||
kind_whitelist:
|
|
||||||
enabled: false
|
|
||||||
kinds:
|
|
||||||
- "1"
|
|
||||||
- "2"
|
|
||||||
|
|
||||||
domain_whitelist:
|
|
||||||
enabled: false
|
|
||||||
domains:
|
|
||||||
- "example.com"
|
|
||||||
- "anotherdomain.com"
|
|
@ -1,43 +1,41 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
types "grain/config/types"
|
types "grain/config/types"
|
||||||
"grain/server/utils"
|
"grain/server/utils"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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) {
|
||||||
blacklistConfig := GetBlacklistConfig()
|
blacklistConfig := GetConfig().Blacklist
|
||||||
if blacklistConfig == nil || !blacklistConfig.Enabled {
|
|
||||||
|
if !blacklistConfig.Enabled {
|
||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Checking blacklist for pubkey: %s", pubkey)
|
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, blacklistConfig) {
|
if isPubKeyPermanentlyBlacklisted(pubkey, blacklistConfig) {
|
||||||
log.Printf("Pubkey %s is permanently blacklisted", 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)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for permanent ban based on wordlist.
|
// Check for permanent ban based on wordlist
|
||||||
for _, word := range blacklistConfig.PermanentBanWords {
|
for _, word := range blacklistConfig.PermanentBanWords {
|
||||||
if strings.Contains(eventContent, word) {
|
if strings.Contains(eventContent, word) {
|
||||||
err := AddToPermanentBlacklist(pubkey)
|
err := AddToPermanentBlacklist(pubkey)
|
||||||
@ -48,10 +46,10 @@ func CheckBlacklist(pubkey, eventContent string) (bool, string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for temporary ban based on wordlist.
|
// Check for temporary ban based on wordlist
|
||||||
for _, word := range blacklistConfig.TempBanWords {
|
for _, word := range blacklistConfig.TempBanWords {
|
||||||
if strings.Contains(eventContent, word) {
|
if strings.Contains(eventContent, word) {
|
||||||
err := AddToTemporaryBlacklist(pubkey, *blacklistConfig)
|
err := AddToTemporaryBlacklist(pubkey, blacklistConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, fmt.Sprintf("pubkey %s is temporarily banned and failed to save: %v", pubkey, err)
|
return true, fmt.Sprintf("pubkey %s is temporarily banned and failed to save: %v", pubkey, err)
|
||||||
}
|
}
|
||||||
@ -62,7 +60,6 @@ func CheckBlacklist(pubkey, eventContent string) (bool, string) {
|
|||||||
return false, ""
|
return false, ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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()
|
mu.Lock()
|
||||||
@ -145,19 +142,20 @@ func AddToTemporaryBlacklist(pubkey string, blacklistConfig types.BlacklistConfi
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPubKeyPermanentlyBlacklisted(pubKey string, blacklistConfig *types.BlacklistConfig) bool {
|
// Checks if a pubkey is permanently blacklisted (only using config.yml)
|
||||||
if blacklistConfig == nil || !blacklistConfig.Enabled {
|
func isPubKeyPermanentlyBlacklisted(pubKey string, blacklistConfig types.BlacklistConfig) bool {
|
||||||
|
if !blacklistConfig.Enabled {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check pubkeys.
|
// Check pubkeys
|
||||||
for _, blacklistedKey := range blacklistConfig.PermanentBlacklistPubkeys {
|
for _, blacklistedKey := range blacklistConfig.PermanentBlacklistPubkeys {
|
||||||
if pubKey == blacklistedKey {
|
if pubKey == blacklistedKey {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check npubs.
|
// Check npubs
|
||||||
for _, npub := range blacklistConfig.PermanentBlacklistNpubs {
|
for _, npub := range blacklistConfig.PermanentBlacklistNpubs {
|
||||||
decodedPubKey, err := utils.DecodeNpub(npub)
|
decodedPubKey, err := utils.DecodeNpub(npub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -173,188 +171,34 @@ func isPubKeyPermanentlyBlacklisted(pubKey string, blacklistConfig *types.Blackl
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AddToPermanentBlacklist(pubkey string) error {
|
func AddToPermanentBlacklist(pubkey string) error {
|
||||||
blacklistConfig := GetBlacklistConfig()
|
// Remove the mutex lock from here
|
||||||
if blacklistConfig == nil {
|
blacklistConfig := GetConfig().Blacklist
|
||||||
return fmt.Errorf("blacklist configuration is not loaded")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if already blacklisted.
|
// Check if already blacklisted
|
||||||
if isPubKeyPermanentlyBlacklisted(pubkey, blacklistConfig) {
|
if isPubKeyPermanentlyBlacklisted(pubkey, blacklistConfig) {
|
||||||
return fmt.Errorf("pubkey %s is already in the permanent blacklist", pubkey)
|
return fmt.Errorf("pubkey %s is already in the permanent blacklist", pubkey)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add pubkey to the permanent blacklist.
|
// Add pubkey to the blacklist
|
||||||
blacklistConfig.PermanentBlacklistPubkeys = append(blacklistConfig.PermanentBlacklistPubkeys, pubkey)
|
blacklistConfig.PermanentBlacklistPubkeys = append(blacklistConfig.PermanentBlacklistPubkeys, pubkey)
|
||||||
|
|
||||||
// Persist changes to blacklist.yml.
|
// Persist changes to config.yml
|
||||||
return saveBlacklistConfig(*blacklistConfig)
|
return saveBlacklistConfig(blacklistConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveBlacklistConfig(blacklistConfig types.BlacklistConfig) error {
|
func saveBlacklistConfig(blacklistConfig types.BlacklistConfig) error {
|
||||||
data, err := yaml.Marshal(blacklistConfig)
|
configData := GetConfig()
|
||||||
|
configData.Blacklist = blacklistConfig
|
||||||
|
|
||||||
|
data, err := yaml.Marshal(configData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal blacklist config: %v", err)
|
return fmt.Errorf("failed to marshal config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile("blacklist.yml", data, 0644)
|
err = os.WriteFile("config.yml", data, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write config to file: %v", err)
|
return fmt.Errorf("failed to write config to file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchPubkeysFromLocalMuteList sends a REQ to the local relay for mute list events.
|
|
||||||
func FetchPubkeysFromLocalMuteList(localRelayURL string, muteListAuthors []string) ([]string, error) {
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
var mu sync.Mutex
|
|
||||||
var allPubkeys []string
|
|
||||||
results := make(chan []string, 1)
|
|
||||||
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
|
|
||||||
conn, _, err := websocket.DefaultDialer.Dial(localRelayURL, nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to connect to local relay %s: %v", localRelayURL, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
subscriptionID := "mutelist-fetch"
|
|
||||||
|
|
||||||
// Create the REQ message to fetch the mute list events by IDs.
|
|
||||||
req := []interface{}{"REQ", subscriptionID, map[string]interface{}{
|
|
||||||
"authors": muteListAuthors,
|
|
||||||
"kinds": []int{10000}, // Mute list events kind.
|
|
||||||
}}
|
|
||||||
|
|
||||||
reqJSON, err := json.Marshal(req)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to marshal request: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = conn.WriteMessage(websocket.TextMessage, reqJSON)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to send request to local relay %s: %v", localRelayURL, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Listen for messages from the local relay.
|
|
||||||
for {
|
|
||||||
_, message, err := conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Error reading message from local relay %s: %v", localRelayURL, err)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log the raw message for debugging.
|
|
||||||
log.Printf("Received raw message: %s", message)
|
|
||||||
|
|
||||||
var response []interface{}
|
|
||||||
err = json.Unmarshal(message, &response)
|
|
||||||
if err != nil || len(response) < 2 {
|
|
||||||
log.Printf("Invalid message format or failed to unmarshal: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for "EVENT" type messages.
|
|
||||||
eventType, ok := response[0].(string)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("Unexpected event type: %v", response[0])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if eventType == "EOSE" {
|
|
||||||
// End of subscription events; send a "CLOSE" message to the relay.
|
|
||||||
closeReq := []interface{}{"CLOSE", subscriptionID}
|
|
||||||
closeReqJSON, err := json.Marshal(closeReq)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to marshal close request: %v", err)
|
|
||||||
} else {
|
|
||||||
if err = conn.WriteMessage(websocket.TextMessage, closeReqJSON); err != nil {
|
|
||||||
log.Printf("Failed to send close request to relay %s: %v", localRelayURL, err)
|
|
||||||
} else {
|
|
||||||
log.Println("Sent CLOSE request to end subscription.")
|
|
||||||
|
|
||||||
// Wait for a potential response or timeout
|
|
||||||
conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
|
|
||||||
_, _, err = conn.ReadMessage()
|
|
||||||
if err != nil {
|
|
||||||
if err == io.EOF {
|
|
||||||
log.Println("Connection closed by the server after CLOSE request (EOF)")
|
|
||||||
} else if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
|
|
||||||
log.Println("WebSocket closed normally after CLOSE request")
|
|
||||||
} else {
|
|
||||||
log.Printf("Unexpected error after CLOSE request: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we break the loop after handling EOSE
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if eventType == "EVENT" {
|
|
||||||
// Safely cast the event data from the third element.
|
|
||||||
if len(response) < 3 {
|
|
||||||
log.Printf("Unexpected event format with insufficient data: %v", response)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
eventData, ok := response[2].(map[string]interface{})
|
|
||||||
if !ok {
|
|
||||||
log.Printf("Expected event data to be a map, got: %T", response[2])
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log event data for debugging.
|
|
||||||
log.Printf("Event data received: %+v", eventData)
|
|
||||||
|
|
||||||
pubkeys := extractPubkeysFromMuteListEvent(eventData)
|
|
||||||
results <- pubkeys
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
wg.Wait()
|
|
||||||
close(results)
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Collect results from the relay.
|
|
||||||
for pubkeys := range results {
|
|
||||||
mu.Lock()
|
|
||||||
allPubkeys = append(allPubkeys, pubkeys...)
|
|
||||||
mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
return allPubkeys, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractPubkeysFromMuteListEvent extracts pubkeys from a mute list event.
|
|
||||||
func extractPubkeysFromMuteListEvent(eventData map[string]interface{}) []string {
|
|
||||||
var pubkeys []string
|
|
||||||
|
|
||||||
tags, ok := eventData["tags"].([]interface{})
|
|
||||||
if !ok {
|
|
||||||
log.Println("Tags field is missing or not an array")
|
|
||||||
return pubkeys
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tag := range tags {
|
|
||||||
tagArray, ok := tag.([]interface{})
|
|
||||||
if ok && len(tagArray) > 1 && tagArray[0] == "p" {
|
|
||||||
pubkey, ok := tagArray[1].(string)
|
|
||||||
if ok {
|
|
||||||
pubkeys = append(pubkeys, pubkey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Extracted pubkeys: %v", pubkeys)
|
|
||||||
return pubkeys
|
|
||||||
}
|
|
||||||
|
@ -6,19 +6,21 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Check if a pubkey or npub is whitelisted
|
// Helper function to check if a pubkey or npub is whitelisted
|
||||||
func IsPubKeyWhitelisted(pubKey string) bool {
|
func IsPubKeyWhitelisted(pubKey string) bool {
|
||||||
cfg := GetWhitelistConfig()
|
cfg := GetConfig()
|
||||||
if !cfg.PubkeyWhitelist.Enabled {
|
if !cfg.PubkeyWhitelist.Enabled {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check pubkeys
|
||||||
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
|
for _, whitelistedKey := range cfg.PubkeyWhitelist.Pubkeys {
|
||||||
if pubKey == whitelistedKey {
|
if pubKey == whitelistedKey {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check npubs
|
||||||
for _, npub := range cfg.PubkeyWhitelist.Npubs {
|
for _, npub := range cfg.PubkeyWhitelist.Npubs {
|
||||||
decodedPubKey, err := utils.DecodeNpub(npub)
|
decodedPubKey, err := utils.DecodeNpub(npub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -33,13 +35,13 @@ func IsPubKeyWhitelisted(pubKey string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if a kind is whitelisted
|
|
||||||
func IsKindWhitelisted(kind int) bool {
|
func IsKindWhitelisted(kind int) bool {
|
||||||
cfg := GetWhitelistConfig()
|
cfg := GetConfig()
|
||||||
if !cfg.KindWhitelist.Enabled {
|
if !cfg.KindWhitelist.Enabled {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check event kinds
|
||||||
for _, whitelistedKindStr := range cfg.KindWhitelist.Kinds {
|
for _, whitelistedKindStr := range cfg.KindWhitelist.Kinds {
|
||||||
whitelistedKind, err := strconv.Atoi(whitelistedKindStr)
|
whitelistedKind, err := strconv.Atoi(whitelistedKindStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -6,21 +6,14 @@ import (
|
|||||||
|
|
||||||
configTypes "grain/config/types"
|
configTypes "grain/config/types"
|
||||||
|
|
||||||
"grain/server/utils"
|
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
cfg *configTypes.ServerConfig
|
cfg *configTypes.ServerConfig
|
||||||
whitelistCfg *configTypes.WhitelistConfig
|
|
||||||
blacklistCfg *configTypes.BlacklistConfig
|
|
||||||
once sync.Once
|
once sync.Once
|
||||||
whitelistOnce sync.Once
|
|
||||||
blacklistOnce sync.Once
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// LoadConfig loads the server configuration from config.yml
|
|
||||||
func LoadConfig(filename string) (*configTypes.ServerConfig, error) {
|
func LoadConfig(filename string) (*configTypes.ServerConfig, error) {
|
||||||
data, err := os.ReadFile(filename)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -33,9 +26,6 @@ func LoadConfig(filename string) (*configTypes.ServerConfig, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adjust event time constraints after loading
|
|
||||||
utils.AdjustEventTimeConstraints(&config)
|
|
||||||
|
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
cfg = &config
|
cfg = &config
|
||||||
})
|
})
|
||||||
@ -43,54 +33,6 @@ func LoadConfig(filename string) (*configTypes.ServerConfig, error) {
|
|||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadWhitelistConfig loads the whitelist configuration from whitelist.yml
|
|
||||||
func LoadWhitelistConfig(filename string) (*configTypes.WhitelistConfig, error) {
|
|
||||||
data, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var config configTypes.WhitelistConfig
|
|
||||||
err = yaml.Unmarshal(data, &config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
whitelistOnce.Do(func() {
|
|
||||||
whitelistCfg = &config
|
|
||||||
})
|
|
||||||
|
|
||||||
return whitelistCfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetConfig() *configTypes.ServerConfig {
|
func GetConfig() *configTypes.ServerConfig {
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetWhitelistConfig() *configTypes.WhitelistConfig {
|
|
||||||
return whitelistCfg
|
|
||||||
}
|
|
||||||
|
|
||||||
// LoadBlacklistConfig loads the blacklist configuration from blacklist.yml
|
|
||||||
func LoadBlacklistConfig(filename string) (*configTypes.BlacklistConfig, error) {
|
|
||||||
data, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var config configTypes.BlacklistConfig
|
|
||||||
err = yaml.Unmarshal(data, &config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
blacklistOnce.Do(func() {
|
|
||||||
blacklistCfg = &config
|
|
||||||
})
|
|
||||||
|
|
||||||
return blacklistCfg, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetBlacklistConfig() *configTypes.BlacklistConfig {
|
|
||||||
return blacklistCfg
|
|
||||||
}
|
|
BIN
config/tmp/main.exe
Normal file
BIN
config/tmp/main.exe
Normal file
Binary file not shown.
@ -8,5 +8,4 @@ type BlacklistConfig struct {
|
|||||||
TempBanDuration int `yaml:"temp_ban_duration"`
|
TempBanDuration int `yaml:"temp_ban_duration"`
|
||||||
PermanentBlacklistPubkeys []string `yaml:"permanent_blacklist_pubkeys"`
|
PermanentBlacklistPubkeys []string `yaml:"permanent_blacklist_pubkeys"`
|
||||||
PermanentBlacklistNpubs []string `yaml:"permanent_blacklist_npubs"`
|
PermanentBlacklistNpubs []string `yaml:"permanent_blacklist_npubs"`
|
||||||
MuteListAuthors []string `yaml:"mutelist_authors"`
|
|
||||||
}
|
}
|
@ -1,12 +1,5 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
type EventTimeConstraints struct {
|
|
||||||
MinCreatedAt int64 `yaml:"min_created_at"` // Minimum allowed timestamp
|
|
||||||
MinCreatedAtString string `yaml:"min_created_at_string"` // Original string value for parsing (e.g., "now-5m")
|
|
||||||
MaxCreatedAt int64 `yaml:"max_created_at"` // Maximum allowed timestamp
|
|
||||||
MaxCreatedAtString string `yaml:"max_created_at_string"` // Original string value for parsing (e.g., "now+5m")
|
|
||||||
}
|
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
MongoDB struct {
|
MongoDB struct {
|
||||||
URI string `yaml:"uri"`
|
URI string `yaml:"uri"`
|
||||||
@ -14,16 +7,18 @@ type ServerConfig struct {
|
|||||||
} `yaml:"mongodb"`
|
} `yaml:"mongodb"`
|
||||||
Server struct {
|
Server struct {
|
||||||
Port string `yaml:"port"`
|
Port string `yaml:"port"`
|
||||||
ReadTimeout int `yaml:"read_timeout"`
|
ReadTimeout int `yaml:"read_timeout"` // Timeout in seconds
|
||||||
WriteTimeout int `yaml:"write_timeout"`
|
WriteTimeout int `yaml:"write_timeout"` // Timeout in seconds
|
||||||
IdleTimeout int `yaml:"idle_timeout"`
|
IdleTimeout int `yaml:"idle_timeout"` // Timeout in seconds
|
||||||
MaxConnections int `yaml:"max_connections"`
|
MaxConnections int `yaml:"max_connections"` // Maximum number of concurrent connections
|
||||||
MaxSubscriptionsPerClient int `yaml:"max_subscriptions_per_client"`
|
MaxSubscriptionsPerClient int `yaml:"max_subscriptions_per_client"` // Maximum number of subscriptions per client
|
||||||
} `yaml:"server"`
|
} `yaml:"server"`
|
||||||
RateLimit RateLimitConfig `yaml:"rate_limit"`
|
RateLimit RateLimitConfig `yaml:"rate_limit"`
|
||||||
|
PubkeyWhitelist PubkeyWhitelistConfig `yaml:"pubkey_whitelist"`
|
||||||
|
KindWhitelist KindWhitelistConfig `yaml:"kind_whitelist"`
|
||||||
|
DomainWhitelist DomainWhitelistConfig `yaml:"domain_whitelist"`
|
||||||
Blacklist BlacklistConfig `yaml:"blacklist"`
|
Blacklist BlacklistConfig `yaml:"blacklist"`
|
||||||
ResourceLimits ResourceLimits `yaml:"resource_limits"`
|
ResourceLimits ResourceLimits `yaml:"resource_limits"`
|
||||||
Auth AuthConfig `yaml:"auth"`
|
Auth AuthConfig `yaml:"auth"`
|
||||||
EventPurge EventPurgeConfig `yaml:"event_purge"`
|
EventPurge EventPurgeConfig `yaml:"event_purge"`
|
||||||
EventTimeConstraints EventTimeConstraints `yaml:"event_time_constraints"` // Added this field
|
|
||||||
}
|
}
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
type WhitelistConfig struct {
|
|
||||||
PubkeyWhitelist struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
Pubkeys []string `yaml:"pubkeys"`
|
|
||||||
Npubs []string `yaml:"npubs"`
|
|
||||||
} `yaml:"pubkey_whitelist"`
|
|
||||||
|
|
||||||
KindWhitelist struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
Kinds []string `yaml:"kinds"`
|
|
||||||
} `yaml:"kind_whitelist"`
|
|
||||||
|
|
||||||
DomainWhitelist struct {
|
|
||||||
Enabled bool `yaml:"enabled"`
|
|
||||||
Domains []string `yaml:"domains"`
|
|
||||||
} `yaml:"domain_whitelist"`
|
|
||||||
}
|
|
6
config/types/whitelistDomainConfig.go
Normal file
6
config/types/whitelistDomainConfig.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type DomainWhitelistConfig struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
Domains []string `yaml:"domains"`
|
||||||
|
}
|
6
config/types/whitelistKindConfig.go
Normal file
6
config/types/whitelistKindConfig.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type KindWhitelistConfig struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
Kinds []string `yaml:"kinds"`
|
||||||
|
}
|
7
config/types/whitelistPubkeyConfig.go
Normal file
7
config/types/whitelistPubkeyConfig.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type PubkeyWhitelistConfig struct {
|
||||||
|
Enabled bool `yaml:"enabled"`
|
||||||
|
Pubkeys []string `yaml:"pubkeys"`
|
||||||
|
Npubs []string `yaml:"npubs"`
|
||||||
|
}
|
1
go.mod
1
go.mod
@ -4,7 +4,6 @@ go 1.22.2
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/btcsuite/btcd/btcec/v2 v2.3.4
|
github.com/btcsuite/btcd/btcec/v2 v2.3.4
|
||||||
github.com/gorilla/websocket v1.5.3
|
|
||||||
go.mongodb.org/mongo-driver v1.16.0
|
go.mongodb.org/mongo-driver v1.16.0
|
||||||
golang.org/x/net v0.27.0
|
golang.org/x/net v0.27.0
|
||||||
gopkg.in/yaml.v2 v2.4.0
|
gopkg.in/yaml.v2 v2.4.0
|
||||||
|
2
go.sum
2
go.sum
@ -28,8 +28,6 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
|||||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
|
||||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
|
||||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||||
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||||
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
|
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
|
||||||
|
39
main.go
39
main.go
@ -22,42 +22,28 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
utils.EnsureFileExists("config.yml", "app/static/examples/config.example.yml")
|
utils.EnsureFileExists("config.yml", "app/static/examples/config.example.yml")
|
||||||
utils.EnsureFileExists("whitelist.yml", "app/static/examples/whitelist.example.yml")
|
|
||||||
utils.EnsureFileExists("blacklist.yml", "app/static/examples/blacklist.example.yml")
|
|
||||||
utils.EnsureFileExists("relay_metadata.json", "app/static/examples/relay_metadata.example.json")
|
utils.EnsureFileExists("relay_metadata.json", "app/static/examples/relay_metadata.example.json")
|
||||||
|
|
||||||
restartChan := make(chan struct{})
|
restartChan := make(chan struct{})
|
||||||
go config.WatchConfigFile("config.yml", restartChan)
|
go config.WatchConfigFile("config.yml", restartChan) // Critical goroutine
|
||||||
go config.WatchConfigFile("whitelist.yml", restartChan)
|
|
||||||
go config.WatchConfigFile("blacklist.yml", restartChan)
|
|
||||||
go config.WatchConfigFile("relay_metadata.json", restartChan)
|
|
||||||
|
|
||||||
signalChan := make(chan os.Signal, 1)
|
signalChan := make(chan os.Signal, 1)
|
||||||
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for {
|
for {
|
||||||
wg.Add(1)
|
wg.Add(1) // Add to WaitGroup for the server goroutine
|
||||||
|
|
||||||
cfg, err := config.LoadConfig("config.yml")
|
cfg, err := config.LoadConfig("config.yml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error loading config: ", err)
|
log.Fatal("Error loading config: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = config.LoadWhitelistConfig("whitelist.yml")
|
// Start event purging in the background
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Error loading whitelist config: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = config.LoadBlacklistConfig("blacklist.yml")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("Error loading blacklist config: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start event purging in the background.
|
|
||||||
go mongo.ScheduleEventPurging(cfg)
|
go mongo.ScheduleEventPurging(cfg)
|
||||||
|
|
||||||
config.SetResourceLimit(&cfg.ResourceLimits)
|
config.SetResourceLimit(&cfg.ResourceLimits) // Apply limits once before starting the server
|
||||||
|
|
||||||
client, err := mongo.InitDB(cfg)
|
client, err := mongo.InitDB(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Error initializing database: ", err)
|
log.Fatal("Error initializing database: ", err)
|
||||||
@ -65,6 +51,7 @@ func main() {
|
|||||||
|
|
||||||
config.SetRateLimit(cfg)
|
config.SetRateLimit(cfg)
|
||||||
config.SetSizeLimit(cfg)
|
config.SetSizeLimit(cfg)
|
||||||
|
|
||||||
config.ClearTemporaryBans()
|
config.ClearTemporaryBans()
|
||||||
|
|
||||||
err = utils.LoadRelayMetadataJSON()
|
err = utils.LoadRelayMetadataJSON()
|
||||||
@ -73,20 +60,22 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mux := setupRoutes()
|
mux := setupRoutes()
|
||||||
|
|
||||||
|
// Start the server
|
||||||
server := startServer(cfg, mux, &wg)
|
server := startServer(cfg, mux, &wg)
|
||||||
|
|
||||||
// Monitor for server restart or shutdown signals.
|
|
||||||
select {
|
select {
|
||||||
case <-restartChan:
|
case <-restartChan:
|
||||||
log.Println("Restarting server...")
|
log.Println("Restarting server...")
|
||||||
server.Close()
|
server.Close() // Stop the current server instance
|
||||||
wg.Wait()
|
wg.Wait() // Wait for the server goroutine to finish
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
|
|
||||||
case <-signalChan:
|
case <-signalChan:
|
||||||
log.Println("Shutting down server...")
|
log.Println("Shutting down server...")
|
||||||
server.Close()
|
server.Close() // Stop the server
|
||||||
mongo.DisconnectDB(client)
|
mongo.DisconnectDB(client) // Disconnect from MongoDB
|
||||||
wg.Wait()
|
wg.Wait() // Wait for all goroutines to finish
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package mongo
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"grain/config"
|
|
||||||
types "grain/config/types"
|
types "grain/config/types"
|
||||||
"grain/server/utils"
|
"grain/server/utils"
|
||||||
"log"
|
"log"
|
||||||
@ -11,7 +10,6 @@ import (
|
|||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// PurgeOldEvents removes old events based on the configuration and a list of whitelisted pubkeys.
|
|
||||||
func PurgeOldEvents(cfg *types.EventPurgeConfig, whitelist []string) {
|
func PurgeOldEvents(cfg *types.EventPurgeConfig, whitelist []string) {
|
||||||
if !cfg.Enabled {
|
if !cfg.Enabled {
|
||||||
return
|
return
|
||||||
@ -23,12 +21,10 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig, whitelist []string) {
|
|||||||
// Calculate the cutoff time
|
// Calculate the cutoff time
|
||||||
cutoff := time.Now().AddDate(0, 0, -cfg.KeepDurationDays).Unix()
|
cutoff := time.Now().AddDate(0, 0, -cfg.KeepDurationDays).Unix()
|
||||||
|
|
||||||
// Create the filter for purging old events
|
|
||||||
filter := bson.M{
|
filter := bson.M{
|
||||||
"created_at": bson.M{"$lt": cutoff}, // Filter older events
|
"created_at": bson.M{"$lt": cutoff}, // Filter older events
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exclude whitelisted pubkeys if specified in the config
|
|
||||||
if cfg.ExcludeWhitelisted && len(whitelist) > 0 {
|
if cfg.ExcludeWhitelisted && len(whitelist) > 0 {
|
||||||
filter["pubkey"] = bson.M{"$nin": whitelist} // Exclude whitelisted pubkeys
|
filter["pubkey"] = bson.M{"$nin": whitelist} // Exclude whitelisted pubkeys
|
||||||
}
|
}
|
||||||
@ -56,6 +52,7 @@ func PurgeOldEvents(cfg *types.EventPurgeConfig, whitelist []string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Example of a periodic purging task
|
||||||
// ScheduleEventPurging runs the event purging at a configurable interval.
|
// ScheduleEventPurging runs the event purging at a configurable interval.
|
||||||
func ScheduleEventPurging(cfg *types.ServerConfig) {
|
func ScheduleEventPurging(cfg *types.ServerConfig) {
|
||||||
// Use the purge interval from the configuration
|
// Use the purge interval from the configuration
|
||||||
@ -64,33 +61,22 @@ func ScheduleEventPurging(cfg *types.ServerConfig) {
|
|||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
// Fetch the whitelisted pubkeys without passing cfg directly
|
whitelist := getWhitelistedPubKeys(cfg)
|
||||||
whitelist := getWhitelistedPubKeys()
|
|
||||||
PurgeOldEvents(&cfg.EventPurge, whitelist)
|
PurgeOldEvents(&cfg.EventPurge, whitelist)
|
||||||
log.Printf("Purged old events, keeping whitelisted pubkeys: %v", whitelist)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch whitelisted pubkeys from both the whitelist config and any additional domains.
|
// Fetch whitelisted pubkeys from both the config and any additional domains.
|
||||||
func getWhitelistedPubKeys() []string {
|
func getWhitelistedPubKeys(cfg *types.ServerConfig) []string {
|
||||||
// Get the whitelist configuration
|
whitelistedPubkeys := cfg.PubkeyWhitelist.Pubkeys
|
||||||
whitelistCfg := config.GetWhitelistConfig()
|
|
||||||
if whitelistCfg == nil {
|
|
||||||
log.Println("whitelistCfg is nil, returning an empty list of whitelisted pubkeys.")
|
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start with the statically defined pubkeys
|
|
||||||
whitelistedPubkeys := whitelistCfg.PubkeyWhitelist.Pubkeys
|
|
||||||
|
|
||||||
// Fetch pubkeys from domains if domain whitelist is enabled
|
// Fetch pubkeys from domains if domain whitelist is enabled
|
||||||
if whitelistCfg.DomainWhitelist.Enabled {
|
if cfg.DomainWhitelist.Enabled {
|
||||||
domains := whitelistCfg.DomainWhitelist.Domains
|
domains := cfg.DomainWhitelist.Domains
|
||||||
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
|
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error fetching pubkeys from domains: %v", err)
|
log.Printf("Error fetching pubkeys from domains: %v", err)
|
||||||
// Return the existing statically whitelisted pubkeys in case of an error
|
return whitelistedPubkeys // Return existing whitelisted pubkeys in case of error
|
||||||
return whitelistedPubkeys
|
|
||||||
}
|
}
|
||||||
// Append fetched pubkeys from domains to the whitelisted pubkeys
|
// Append fetched pubkeys from domains to the whitelisted pubkeys
|
||||||
whitelistedPubkeys = append(whitelistedPubkeys, pubkeys...)
|
whitelistedPubkeys = append(whitelistedPubkeys, pubkeys...)
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"grain/config"
|
"grain/config"
|
||||||
"grain/server/db/mongo"
|
"grain/server/db/mongo"
|
||||||
"time"
|
|
||||||
|
|
||||||
"grain/server/handlers/response"
|
"grain/server/handlers/response"
|
||||||
"grain/server/utils"
|
"grain/server/utils"
|
||||||
@ -17,6 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
||||||
|
|
||||||
if len(message) != 2 {
|
if len(message) != 2 {
|
||||||
fmt.Println("Invalid EVENT message format")
|
fmt.Println("Invalid EVENT message format")
|
||||||
response.SendNotice(ws, "", "Invalid EVENT message format")
|
response.SendNotice(ws, "", "Invalid EVENT message format")
|
||||||
@ -44,19 +44,13 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate event timestamps
|
|
||||||
if !validateEventTimestamp(evt) {
|
|
||||||
response.SendOK(ws, evt.ID, false, "invalid: event created_at timestamp is out of allowed range")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Signature check moved here
|
// Signature check moved here
|
||||||
if !utils.CheckSignature(evt) {
|
if !utils.CheckSignature(evt) {
|
||||||
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
|
response.SendOK(ws, evt.ID, false, "invalid: signature verification failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
eventSize := len(eventBytes)
|
eventSize := len(eventBytes) // Calculate event size
|
||||||
|
|
||||||
if !handleBlacklistAndWhitelist(ws, evt) {
|
if !handleBlacklistAndWhitelist(ws, evt) {
|
||||||
return
|
return
|
||||||
@ -66,113 +60,38 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the event in MongoDB or other storage
|
// This is where I'll handle storage for multiple database types in the future
|
||||||
mongo.StoreMongoEvent(context.TODO(), evt, ws)
|
mongo.StoreMongoEvent(context.TODO(), evt, ws)
|
||||||
|
|
||||||
fmt.Println("Event processed:", evt.ID)
|
fmt.Println("Event processed:", evt.ID)
|
||||||
}
|
|
||||||
|
|
||||||
// Validate event timestamps against the configured min and max values
|
|
||||||
func validateEventTimestamp(evt nostr.Event) bool {
|
|
||||||
cfg := config.GetConfig()
|
|
||||||
if cfg == nil {
|
|
||||||
fmt.Println("Server configuration is not loaded")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use current time for max and a fixed date for min if not specified
|
|
||||||
now := time.Now().Unix()
|
|
||||||
minCreatedAt := cfg.EventTimeConstraints.MinCreatedAt
|
|
||||||
if minCreatedAt == 0 {
|
|
||||||
// Use January 1, 2020, as the default minimum timestamp
|
|
||||||
minCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
|
||||||
}
|
|
||||||
|
|
||||||
maxCreatedAt := cfg.EventTimeConstraints.MaxCreatedAt
|
|
||||||
if maxCreatedAt == 0 {
|
|
||||||
// Default to the current time if not set
|
|
||||||
maxCreatedAt = now
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the event's created_at timestamp falls within the allowed range
|
|
||||||
if evt.CreatedAt < minCreatedAt || evt.CreatedAt > maxCreatedAt {
|
|
||||||
fmt.Printf("Event %s created_at timestamp %d is out of range [%d, %d]\n", evt.ID, evt.CreatedAt, minCreatedAt, maxCreatedAt)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
func handleBlacklistAndWhitelist(ws *websocket.Conn, evt nostr.Event) bool {
|
||||||
// Get the current whitelist configuration
|
if config.GetConfig().DomainWhitelist.Enabled {
|
||||||
whitelistCfg := config.GetWhitelistConfig()
|
domains := config.GetConfig().DomainWhitelist.Domains
|
||||||
if whitelistCfg == nil {
|
|
||||||
fmt.Println("Whitelist configuration is not loaded.")
|
|
||||||
response.SendNotice(ws, "", "Internal server error: whitelist configuration is missing")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// If domain whitelisting is enabled, dynamically fetch pubkeys from domains
|
|
||||||
if whitelistCfg.DomainWhitelist.Enabled {
|
|
||||||
domains := whitelistCfg.DomainWhitelist.Domains
|
|
||||||
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
|
pubkeys, err := utils.FetchPubkeysFromDomains(domains)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error fetching pubkeys from domains:", err)
|
fmt.Println("Error fetching pubkeys from domains:", err)
|
||||||
response.SendNotice(ws, "", "Error fetching pubkeys from domains")
|
response.SendNotice(ws, "", "Error fetching pubkeys from domains")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
// Update the whitelisted pubkeys dynamically
|
for _, pubkey := range pubkeys {
|
||||||
whitelistCfg.PubkeyWhitelist.Pubkeys = append(whitelistCfg.PubkeyWhitelist.Pubkeys, pubkeys...)
|
config.GetConfig().PubkeyWhitelist.Pubkeys = append(config.GetConfig().PubkeyWhitelist.Pubkeys, pubkey)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the event's pubkey or content is blacklisted
|
|
||||||
if blacklisted, msg := config.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
|
if blacklisted, msg := config.CheckBlacklist(evt.PubKey, evt.Content); blacklisted {
|
||||||
response.SendOK(ws, evt.ID, false, msg)
|
response.SendOK(ws, evt.ID, false, msg)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check mutelist blacklist
|
if config.GetConfig().KindWhitelist.Enabled && !config.IsKindWhitelisted(evt.Kind) {
|
||||||
cfg := config.GetConfig()
|
|
||||||
if cfg == nil {
|
|
||||||
fmt.Println("Server configuration is not loaded")
|
|
||||||
response.SendNotice(ws, "", "Internal server error: server configuration is missing")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
blacklistCfg := config.GetBlacklistConfig()
|
|
||||||
if blacklistCfg == nil {
|
|
||||||
fmt.Println("Blacklist configuration is not loaded")
|
|
||||||
response.SendNotice(ws, "", "Internal server error: blacklist configuration is missing")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only proceed if there are mutelist event IDs specified
|
|
||||||
if len(blacklistCfg.MuteListAuthors) > 0 {
|
|
||||||
localRelayURL := fmt.Sprintf("ws://localhost%s", cfg.Server.Port)
|
|
||||||
mutelistedPubkeys, err := config.FetchPubkeysFromLocalMuteList(localRelayURL, blacklistCfg.MuteListAuthors)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error fetching pubkeys from mutelist:", err)
|
|
||||||
response.SendNotice(ws, "", "Error fetching pubkeys from mutelist")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, mutelistedPubkey := range mutelistedPubkeys {
|
|
||||||
if evt.PubKey == mutelistedPubkey {
|
|
||||||
response.SendOK(ws, evt.ID, false, "not allowed: pubkey is in mutelist")
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt.Println("No mutelist event IDs specified in the blacklist configuration")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the event's kind is whitelisted
|
|
||||||
if whitelistCfg.KindWhitelist.Enabled && !config.IsKindWhitelisted(evt.Kind) {
|
|
||||||
response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted")
|
response.SendOK(ws, evt.ID, false, "not allowed: event kind is not whitelisted")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the event's pubkey is whitelisted
|
if config.GetConfig().PubkeyWhitelist.Enabled && !config.IsPubKeyWhitelisted(evt.PubKey) {
|
||||||
if whitelistCfg.PubkeyWhitelist.Enabled && !config.IsPubKeyWhitelisted(evt.PubKey) {
|
|
||||||
response.SendOK(ws, evt.ID, false, "not allowed: pubkey or npub is not whitelisted")
|
response.SendOK(ws, evt.ID, false, "not allowed: pubkey or npub is not whitelisted")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -1,43 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
config "grain/config/types"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Adjusts the event time constraints based on the configuration
|
|
||||||
func AdjustEventTimeConstraints(cfg *config.ServerConfig) {
|
|
||||||
now := time.Now()
|
|
||||||
|
|
||||||
// Adjust min_created_at based on string value or default to January 1, 2020
|
|
||||||
if strings.HasPrefix(cfg.EventTimeConstraints.MinCreatedAtString, "now") {
|
|
||||||
offset := strings.TrimPrefix(cfg.EventTimeConstraints.MinCreatedAtString, "now")
|
|
||||||
duration, err := time.ParseDuration(offset)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Invalid time offset for min_created_at: %s\n", offset)
|
|
||||||
cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
|
||||||
} else {
|
|
||||||
cfg.EventTimeConstraints.MinCreatedAt = now.Add(duration).Unix()
|
|
||||||
}
|
|
||||||
} else if cfg.EventTimeConstraints.MinCreatedAt == 0 {
|
|
||||||
// Default to January 1, 2020, if not set
|
|
||||||
cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Adjust max_created_at based on string value or default to current time
|
|
||||||
if strings.HasPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now") {
|
|
||||||
offset := strings.TrimPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now")
|
|
||||||
duration, err := time.ParseDuration(offset)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Invalid time offset for max_created_at: %s\n", offset)
|
|
||||||
cfg.EventTimeConstraints.MaxCreatedAt = now.Unix() // Default to now if parsing fails
|
|
||||||
} else {
|
|
||||||
cfg.EventTimeConstraints.MaxCreatedAt = now.Add(duration).Unix()
|
|
||||||
}
|
|
||||||
} else if cfg.EventTimeConstraints.MaxCreatedAt == 0 {
|
|
||||||
// Default to the current time if it's set to zero and no "now" keyword is used
|
|
||||||
cfg.EventTimeConstraints.MaxCreatedAt = now.Unix()
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user