-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (75 loc) · 2.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"github.com/fiatjaf/eventstore/lmdb"
"github.com/fiatjaf/khatru"
"github.com/joho/godotenv"
"github.com/nbd-wtf/go-nostr"
)
type Whitelist struct {
Pubkeys []string `json:"pubkeys"`
}
func loadWhitelist(filename string) (*Whitelist, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("could not open file: %w", err)
}
defer file.Close()
bytes, err := io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("could not read file: %w", err)
}
var whitelist Whitelist
if err := json.Unmarshal(bytes, &whitelist); err != nil {
return nil, fmt.Errorf("could not parse JSON: %w", err)
}
return &whitelist, nil
}
func main() {
godotenv.Load(".env")
relay := khatru.NewRelay()
db := lmdb.LMDBBackend{
Path: "db/",
}
if err := db.Init(); err != nil {
panic(err)
}
relay.Info.Name = os.Getenv("RELAY_NAME")
relay.Info.PubKey = os.Getenv("RELAY_PUBKEY")
relay.Info.Icon = os.Getenv("RELAY_ICON")
relay.Info.Contact = os.Getenv("RELAY_CONTACT")
relay.Info.Description = os.Getenv("RELAY_DESCRIPTION")
relay.Info.Software = "https://github.com/bitvora/sw2"
relay.Info.Version = "0.1.0"
whitelist, err := loadWhitelist("whitelist.json")
if err != nil {
fmt.Println("Error loading config:", err)
return
}
fmt.Println("Whitelisted pubkeys:")
for _, pubkey := range whitelist.Pubkeys {
fmt.Println(pubkey)
}
relay.RejectEvent = append(relay.RejectEvent, func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
if event.PubKey == "" {
return true, "no pubkey"
}
for _, pubkey := range whitelist.Pubkeys {
if pubkey == event.PubKey {
return false, ""
}
}
return true, "pubkey not whitelisted"
})
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.CountEvents = append(relay.CountEvents, db.CountEvents)
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
fmt.Println("running on :3334")
http.ListenAndServe(":3334", relay)
}