-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
165 lines (135 loc) · 3.95 KB
/
main.go
File metadata and controls
165 lines (135 loc) · 3.95 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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 WriteWhitelist struct {
Pubkeys []string `json:"pubkeys"`
}
func loadWriteWhitelist() (*WriteWhitelist, error) {
// Try opening "whitelist.json" first
file, err := os.Open("whitelist.json")
if err != nil {
if os.IsNotExist(err) {
// Fallback to "write_whitelist.json" if "whitelist.json" does not exist
file, err = os.Open("write_whitelist.json")
if err != nil {
return nil, fmt.Errorf("could not open file: %w", err)
}
} else {
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 writeWhitelist WriteWhitelist
if err := json.Unmarshal(bytes, &writeWhitelist); err != nil {
return nil, fmt.Errorf("could not parse JSON: %w", err)
}
return &writeWhitelist, nil
}
type ReadWhitelist struct {
Pubkeys []string `json:"pubkeys"`
}
func loadReadWhitelist(filename string) (*ReadWhitelist, 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 readWhitelist ReadWhitelist
if err := json.Unmarshal(bytes, &readWhitelist); err != nil {
return nil, fmt.Errorf("could not parse JSON: %w", err)
}
return &readWhitelist, 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"
writeWhitelist, err := loadWriteWhitelist()
if err != nil {
fmt.Println("Error loading write whitelist:", err)
return
}
fmt.Println("Write whitelisted pubkeys:")
for _, pubkey := range writeWhitelist.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"
}
// Allow if writeWhitelist is empty
if len(writeWhitelist.Pubkeys) == 0 {
return false, ""
}
for _, pubkey := range writeWhitelist.Pubkeys {
if pubkey == event.PubKey {
return false, ""
}
}
return true, "pubkey not whitelisted"
})
relay.OnConnect = append(relay.OnConnect, func(ctx context.Context) {
khatru.RequestAuth(ctx)
})
readWhitelist, err := loadReadWhitelist("read_whitelist.json")
if err != nil {
fmt.Println("Error loading read whitelist:", err)
return
}
fmt.Println("Read whitelisted pubkeys:")
for _, pubkey := range readWhitelist.Pubkeys {
fmt.Println(pubkey)
}
relay.StoreEvent = append(relay.StoreEvent, db.SaveEvent)
relay.QueryEvents = append(relay.QueryEvents, db.QueryEvents)
relay.RejectFilter = append(relay.RejectFilter, func(ctx context.Context, filter nostr.Filter) (reject bool, msg string) {
authenticatedUser := khatru.GetAuthed(ctx)
if authenticatedUser == "" {
return true, "auth-required: this query requires you to be authenticated"
}
// Allow if readWhitelist is empty
if len(readWhitelist.Pubkeys) == 0 {
return false, ""
}
for _, pubkey := range readWhitelist.Pubkeys {
if pubkey == authenticatedUser {
return false, ""
}
}
return true, "restricted: you're not authorized to read"
})
relay.CountEvents = append(relay.CountEvents, db.CountEvents)
relay.DeleteEvent = append(relay.DeleteEvent, db.DeleteEvent)
fmt.Println("running on :3334")
http.ListenAndServe(":3334", relay)
}