mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-23 17:07:13 +00:00
Compare commits
No commits in common. "39436b564e4a3a8962d60eaad57885eabe84afe1" and "d81d78e25fc368ca3d2be413f564390cb90e1c29" have entirely different histories.
39436b564e
...
d81d78e25f
@ -1,6 +1,17 @@
|
||||
{{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="">
|
||||
<html lang="en" data-theme="{{.Theme}}">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
|
@ -1,11 +0,0 @@
|
||||
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,5 +17,4 @@ type ServerConfig struct {
|
||||
PubkeyWhitelist PubkeyWhitelistConfig `yaml:"pubkey_whitelist"`
|
||||
KindWhitelist KindWhitelistConfig `yaml:"kind_whitelist"`
|
||||
DomainWhitelist DomainWhitelistConfig `yaml:"domain_whitelist"`
|
||||
Blacklist BlacklistConfig `yaml:"blacklist"`
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -12,7 +12,6 @@ require (
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // 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
|
||||
)
|
||||
|
||||
@ -32,5 +31,4 @@ require (
|
||||
golang.org/x/sync v0.7.0 // indirect
|
||||
golang.org/x/text v0.16.0 // indirect
|
||||
golang.org/x/time v0.5.0
|
||||
gopkg.in/fsnotify.v1 v1.4.7
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -21,7 +21,6 @@ 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/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/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/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
@ -83,8 +82,6 @@ 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-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.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-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@ -102,7 +99,6 @@ 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 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/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||
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/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
86
main.go
86
main.go
@ -11,10 +11,6 @@ import (
|
||||
"grain/server/utils"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/websocket"
|
||||
@ -24,51 +20,28 @@ func main() {
|
||||
utils.EnsureFileExists("config.yml", "app/static/examples/config.example.yml")
|
||||
utils.EnsureFileExists("relay_metadata.json", "app/static/examples/relay_metadata.example.json")
|
||||
|
||||
restartChan := make(chan struct{})
|
||||
go utils.WatchConfigFile("config.yml", restartChan)
|
||||
|
||||
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
|
||||
}
|
||||
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)
|
||||
}
|
||||
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 {
|
||||
@ -83,7 +56,7 @@ func setupRoutes() *http.ServeMux {
|
||||
return mux
|
||||
}
|
||||
|
||||
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux, wg *sync.WaitGroup) *http.Server {
|
||||
func startServer(config *configTypes.ServerConfig, mux *http.ServeMux) {
|
||||
server := &http.Server{
|
||||
Addr: config.Server.Port,
|
||||
Handler: mux,
|
||||
@ -91,16 +64,11 @@ func startServer(config *configTypes.ServerConfig, mux *http.ServeMux, wg *sync.
|
||||
WriteTimeout: time.Duration(config.Server.WriteTimeout) * time.Second,
|
||||
IdleTimeout: time.Duration(config.Server.IdleTimeout) * time.Second,
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // Notify that the server is done shutting down
|
||||
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
|
||||
fmt.Printf("Server is running on http://localhost%s\n", config.Server.Port)
|
||||
err := server.ListenAndServe()
|
||||
if err != nil {
|
||||
fmt.Println("Error starting server:", err)
|
||||
}
|
||||
}
|
||||
|
||||
var wsServer = &websocket.Server{
|
||||
|
@ -84,13 +84,6 @@ 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 {
|
||||
|
@ -1,99 +0,0 @@
|
||||
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
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user