Compare commits

...

2 Commits

10 changed files with 237 additions and 40 deletions

View File

@ -1,17 +1,6 @@
{{define "view"}} {{define "view"}}
<main class="flex flex-col items-center justify-center p-8"> <main class="flex flex-col items-center justify-center p-8">
<div class="mb-4">You are now viewing the {{.Title}}</div> <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 <button
hx-get="/import-events" hx-get="/import-events"
hx-swap="outerHTML" hx-swap="outerHTML"

View File

@ -1,6 +1,6 @@
{{define "layout"}} {{define "layout"}}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" data-theme="{{.Theme}}"> <html lang="en" data-theme="">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />

View 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"`
}

View File

@ -17,4 +17,5 @@ type ServerConfig struct {
PubkeyWhitelist PubkeyWhitelistConfig `yaml:"pubkey_whitelist"` PubkeyWhitelist PubkeyWhitelistConfig `yaml:"pubkey_whitelist"`
KindWhitelist KindWhitelistConfig `yaml:"kind_whitelist"` KindWhitelist KindWhitelistConfig `yaml:"kind_whitelist"`
DomainWhitelist DomainWhitelistConfig `yaml:"domain_whitelist"` DomainWhitelist DomainWhitelistConfig `yaml:"domain_whitelist"`
Blacklist BlacklistConfig `yaml:"blacklist"`
} }

2
go.mod
View File

@ -12,6 +12,7 @@ require (
require ( require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/kr/pretty v0.3.1 // indirect github.com/kr/pretty v0.3.1 // indirect
golang.org/x/sys v0.22.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
) )
@ -31,4 +32,5 @@ require (
golang.org/x/sync v0.7.0 // indirect golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.16.0 // indirect golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 golang.org/x/time v0.5.0
gopkg.in/fsnotify.v1 v1.4.7
) )

4
go.sum
View File

@ -21,6 +21,7 @@ github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK
github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
@ -82,6 +83,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@ -99,6 +102,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

88
main.go
View File

@ -11,6 +11,10 @@ import (
"grain/server/utils" "grain/server/utils"
"log" "log"
"net/http" "net/http"
"os"
"os/signal"
"sync"
"syscall"
"time" "time"
"golang.org/x/net/websocket" "golang.org/x/net/websocket"
@ -20,28 +24,51 @@ func main() {
utils.EnsureFileExists("config.yml", "app/static/examples/config.example.yml") utils.EnsureFileExists("config.yml", "app/static/examples/config.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")
cfg, err := config.LoadConfig("config.yml") restartChan := make(chan struct{})
if err != nil { go utils.WatchConfigFile("config.yml", restartChan)
log.Fatal("Error loading config: ", err)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
var wg sync.WaitGroup
for {
wg.Add(1)
cfg, err := config.LoadConfig("config.yml")
if err != nil {
log.Fatal("Error loading config: ", err)
}
client, err := db.InitDB(cfg)
if err != nil {
log.Fatal("Error initializing database: ", err)
}
config.SetupRateLimiter(cfg)
config.SetupSizeLimiter(cfg)
err = utils.LoadRelayMetadataJSON()
if err != nil {
log.Fatal("Failed to load relay metadata: ", err)
}
mux := setupRoutes()
server := startServer(cfg, mux, &wg)
select {
case <-restartChan:
log.Println("Restarting server...")
server.Close()
db.DisconnectDB(client)
wg.Wait() // Wait for the server to fully shut down before restarting
time.Sleep(3 * time.Second) // Add a delay before restarting
case <-signalChan:
log.Println("Shutting down server...")
server.Close()
db.DisconnectDB(client)
wg.Wait() // Wait for the server to fully shut down before exiting
return
}
} }
client, err := db.InitDB(cfg)
if err != nil {
log.Fatal("Error initializing database: ", err)
}
defer db.DisconnectDB(client)
config.SetupRateLimiter(cfg)
config.SetupSizeLimiter(cfg)
err = utils.LoadRelayMetadataJSON()
if err != nil {
log.Fatal("Failed to load relay metadata: ", err)
}
mux := setupRoutes()
startServer(cfg, mux)
} }
func setupRoutes() *http.ServeMux { func setupRoutes() *http.ServeMux {
@ -56,7 +83,7 @@ func setupRoutes() *http.ServeMux {
return mux return mux
} }
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) { func startServer(config *configTypes.ServerConfig, mux *http.ServeMux, wg *sync.WaitGroup) *http.Server {
server := &http.Server{ server := &http.Server{
Addr: config.Server.Port, Addr: config.Server.Port,
Handler: mux, Handler: mux,
@ -64,11 +91,16 @@ func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) {
WriteTimeout: time.Duration(config.Server.WriteTimeout) * time.Second, WriteTimeout: time.Duration(config.Server.WriteTimeout) * time.Second,
IdleTimeout: time.Duration(config.Server.IdleTimeout) * time.Second, IdleTimeout: time.Duration(config.Server.IdleTimeout) * time.Second,
} }
fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
err := server.ListenAndServe() go func() {
if err != nil { defer wg.Done() // Notify that the server is done shutting down
fmt.Println("Error starting server:", err) fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
} err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
fmt.Println("Error starting server:", err)
}
}()
return server
} }
var wsServer = &websocket.Server{ var wsServer = &websocket.Server{
@ -87,4 +119,4 @@ func ListenAndServe(w http.ResponseWriter, r *http.Request) {
} else { } else {
routes.IndexHandler(w, r) routes.IndexHandler(w, r)
} }
} }

View File

@ -84,6 +84,13 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn, eventS
return 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) category := determineCategory(evt.Kind)
if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed { if allowed, msg := rateLimiter.AllowEvent(evt.Kind, category); !allowed {

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

View File

@ -0,0 +1,52 @@
package utils
import (
"fmt"
"log"
"time"
"gopkg.in/fsnotify.v1"
)
func WatchConfigFile(filePath string, restartChan chan<- struct{}) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal("Error creating file watcher:", err)
}
defer watcher.Close()
err = watcher.Add(filePath)
if err != nil {
log.Fatal("Error adding file to watcher:", err)
}
var debounceTimer *time.Timer
debounceDuration := 1 * time.Second // Adjust this duration as needed
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
if debounceTimer != nil {
debounceTimer.Stop() // Cancel the previous timer
}
debounceTimer = time.AfterFunc(debounceDuration, func() {
fmt.Println("Config file modified, restarting server...")
select {
case restartChan <- struct{}{}:
default:
// Skip sending restart signal if there's already one in the channel
}
})
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("Error watching file:", err)
}
}
}