Repository navigation
Expand file tree
/
Copy pathmanagement.go
More file actions
367 lines (331 loc) · 12.3 KB
/
Copy pathmanagement.go
File metadata and controls
367 lines (331 loc) · 12.3 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"cmp"
"encoding/json"
"errors"
"fmt"
"log"
"log/slog"
"maps"
"os"
"slices"
"sync"
"sync/atomic"
"github.com/spf13/afero"
)
// The four relays haven serves. These are keyed the same way as the dbs map in
// init.go, so a NIP-86 request addressed to one of them reaches its database
// without a second lookup table.
const (
relayPrivate = "private"
relayChat = "chat"
relayInbox = "inbox"
relayOutbox = "outbox"
)
// managementStateVersion goes into the state file so a future haven can tell
// what it is reading.
const managementStateVersion = 1
// managementState is an immutable snapshot of everything the NIP-86 relay
// management API can change. Every mutation clones it, so the readers on the
// hot paths — one per event, one per connection — only ever do an atomic load,
// the same trick banlist.go uses for the kind 10084 cache.
//
// The maps are keyed by the thing being moderated and hold the reason, rather
// than being arrays of {pubkey, reason} objects: that is one representation
// instead of a list plus a derived lookup set that has to be kept in sync,
// encoding/json sorts map keys so the file stays diffable, and duplicates
// become impossible.
type managementState struct {
Version int `json:"version"`
BannedPubKeys map[string]string `json:"banned_pubkeys"`
AllowedPubKeys map[string]string `json:"allowed_pubkeys"`
BlockedIPs map[string]string `json:"blocked_ips"`
BlockedBlobs map[string]string `json:"blocked_blobs"`
Relays map[string]*relayState `json:"relays"`
}
// relayState is the per-relay slice of the API. A NIP-86 request is addressed
// to a URL and haven serves four relays on four paths, so a kind rule or an
// event ban belongs to the relay it was sent to, not to haven as a whole.
type relayState struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
AllowedKinds []int `json:"allowed_kinds,omitempty"`
DisallowedKinds []int `json:"disallowed_kinds,omitempty"`
BannedEvents map[string]string `json:"banned_events,omitempty"`
AllowedEvents map[string]string `json:"allowed_events,omitempty"`
}
func newManagementState() *managementState {
return &managementState{
Version: managementStateVersion,
BannedPubKeys: map[string]string{},
AllowedPubKeys: map[string]string{},
BlockedIPs: map[string]string{},
BlockedBlobs: map[string]string{},
Relays: map[string]*relayState{},
}
}
// normalise fills in whatever a hand-written or older state file left out, so
// no reader has to nil check. It deliberately does not validate entries: the
// only thing we could do with a bad one is drop it, and a dropped entry is
// erased for good the next time the file is written.
func (st *managementState) normalise() {
st.Version = managementStateVersion
if st.BannedPubKeys == nil {
st.BannedPubKeys = map[string]string{}
}
if st.AllowedPubKeys == nil {
st.AllowedPubKeys = map[string]string{}
}
if st.BlockedIPs == nil {
st.BlockedIPs = map[string]string{}
}
if st.BlockedBlobs == nil {
st.BlockedBlobs = map[string]string{}
}
if st.Relays == nil {
st.Relays = map[string]*relayState{}
}
for _, rs := range st.Relays {
if rs.BannedEvents == nil {
rs.BannedEvents = map[string]string{}
}
if rs.AllowedEvents == nil {
rs.AllowedEvents = map[string]string{}
}
}
}
// clone deep copies the state. Every nested map and slice has to be copied too:
// sharing a *relayState would let a mutation leak into the snapshot readers are
// already holding.
func (st *managementState) clone() *managementState {
next := &managementState{
Version: managementStateVersion,
BannedPubKeys: maps.Clone(st.BannedPubKeys),
AllowedPubKeys: maps.Clone(st.AllowedPubKeys),
BlockedIPs: maps.Clone(st.BlockedIPs),
BlockedBlobs: maps.Clone(st.BlockedBlobs),
Relays: make(map[string]*relayState, len(st.Relays)),
}
for name, rs := range st.Relays {
next.Relays[name] = &relayState{
Name: rs.Name,
Description: rs.Description,
Icon: rs.Icon,
AllowedKinds: slices.Clone(rs.AllowedKinds),
DisallowedKinds: slices.Clone(rs.DisallowedKinds),
BannedEvents: maps.Clone(rs.BannedEvents),
AllowedEvents: maps.Clone(rs.AllowedEvents),
}
}
return next
}
// relay returns the state for one relay, creating it if this is the first time
// anything has been set on it. Only call it on a clone, from inside update.
func (st *managementState) relay(name string) *relayState {
rs, ok := st.Relays[name]
if !ok {
rs = &relayState{
BannedEvents: map[string]string{},
AllowedEvents: map[string]string{},
}
st.Relays[name] = rs
}
return rs
}
// relayOrEmpty returns the state for one relay, or an empty one when nothing
// has been set. For readers, which must not allocate into the shared snapshot.
func (st *managementState) relayOrEmpty(name string) *relayState {
if rs, ok := st.Relays[name]; ok {
return rs
}
return &relayState{}
}
// managementStore holds the state the NIP-86 API works on. Readers load the
// pointer and never take the lock; the lock only serialises writers, which are
// owner initiated and rare.
type managementStore struct {
path string
mu sync.Mutex
state atomic.Pointer[managementState]
frozen bool // guarded by mu; set when the file on disk could not be read
}
var management = &managementStore{}
// get returns the current snapshot. It is never nil once loadManagementStore
// has run, which main does before anything else touches it.
func (s *managementStore) get() *managementState {
if st := s.state.Load(); st != nil {
return st
}
return newManagementState()
}
// errManagementFrozen is what every mutating method returns when the state file
// could not be read at startup. Writing would overwrite whatever is in there,
// which is exactly what somebody with a broken file does not want.
func (s *managementStore) errManagementFrozen() error {
return fmt.Errorf("the management state file %s could not be read at startup; fix or remove it and restart haven", s.path)
}
// update applies mutate to a copy of the current state and saves it before it
// becomes visible, so a "true" answer from the API means the change survived a
// restart. If saving fails nothing changes at all.
func (s *managementStore) update(mutate func(*managementState) error) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.frozen {
return s.errManagementFrozen()
}
next := s.get().clone()
if err := mutate(next); err != nil {
return err
}
if err := s.persist(next); err != nil {
slog.Error("🚫 error saving the management state", "path", s.path, "error", err)
return fmt.Errorf("could not save the management state: %w", err)
}
s.state.Store(next)
return nil
}
// persist writes through a temp file and a rename, so a crash halfway through
// leaves the previous state intact instead of a truncated file. The temp file
// has to live next to the target: a rename is only atomic within a filesystem.
//
// afero has no way to fsync the containing directory, so a power loss in the
// instant after the rename could still lose the write. Nothing here is worth
// dropping the afero indirection for.
func (s *managementStore) persist(state *managementState) error {
tmp := s.path + ".tmp"
f, err := fs.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return err
}
cleanup := func(err error) error {
_ = f.Close()
_ = fs.Remove(tmp)
return err
}
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(state); err != nil {
return cleanup(err)
}
if err := f.Sync(); err != nil {
return cleanup(err)
}
if err := f.Close(); err != nil {
_ = fs.Remove(tmp)
return err
}
return fs.Rename(tmp, s.path)
}
// loadManagementStore reads the state the NIP-86 API saved on a previous run.
// A missing file is the normal first boot. A file that cannot be read or parsed
// freezes the API instead of starting empty: whatever is in there was either
// hand edited or written by a newer haven, and the next write would destroy it.
// The relay keeps serving either way — moderation state going bad is not a
// reason to refuse to start.
func loadManagementStore() {
management.path = config.ManagementStateFile
management.state.Store(newManagementState())
data, err := afero.ReadFile(fs, management.path)
switch {
case errors.Is(err, os.ErrNotExist):
slog.Debug("ℹ️ no management state file yet", "path", management.path)
return
case err != nil:
management.frozen = true
slog.Error("🚫 could not read the management state file, the relay management API is read-only until it is fixed",
"path", management.path, "error", err)
return
}
var state managementState
if err := json.Unmarshal(data, &state); err != nil {
management.frozen = true
slog.Error("🚫 could not parse the management state file, the relay management API is read-only until it is fixed",
"path", management.path, "error", err)
return
}
state.normalise()
management.state.Store(&state)
log.Println("🛡️ Management state loaded:",
len(state.BannedPubKeys), "banned pubkeys,",
len(state.AllowedPubKeys), "allowed pubkeys,",
len(state.BlockedIPs), "blocked IPs,",
len(state.BlockedBlobs), "blocked blobs")
}
//
// union readers
//
// The NIP-86 API is not the only source of bans or of whitelisting: the owner
// also publishes a kind 10084 list and maintains npub files. Neither of those
// can be written by the relay, so the API keeps its own store and everything
// that enforces a rule reads the union through one of these.
//
// isBanned reports whether a pubkey is banned, by either route: the kind 10084
// list the owner publishes, or the NIP-86 API. The list is read-only to the
// API — haven has no key to sign a replacement with.
func isBanned(pubKey string) bool {
if bannedPubKeys.has(pubKey) {
return true
}
_, ok := management.get().BannedPubKeys[pubKey]
return ok
}
// isWhitelisted reports whether a pubkey has owner-level access, from either
// the npubs file or the NIP-86 allow list.
func isWhitelisted(pubKey string) bool {
if _, ok := config.WhitelistedPubKeys[pubKey]; ok {
return true
}
_, ok := management.get().AllowedPubKeys[pubKey]
return ok
}
// isBlockedIP reports whether an address has been blocked over the API.
func isBlockedIP(ip string) bool {
if ip == "" {
return false
}
_, ok := management.get().BlockedIPs[ip]
return ok
}
// isBlockedBlob reports whether a blob hash is on the owner's blocklist. Unlike
// a banned pubkey this has only one source — there is no published list of
// blocked hashes to merge in — but it belongs with the other readers because it
// sits on the same kind of hot path they do: every upload and every download of
// a blob asks it.
func isBlockedBlob(sha256 string) bool {
if sha256 == "" {
return false
}
_, ok := management.get().BlockedBlobs[sha256]
return ok
}
// isBannedEvent reports whether an event was banned from one relay.
func isBannedEvent(relay, id string) bool {
_, ok := management.get().relayOrEmpty(relay).BannedEvents[id]
return ok
}
// effectiveRelayInfo applies the overrides set over the management API to the
// name, description and icon a relay was configured with. An override that was
// never set, or was cleared, leaves the configured value alone.
func effectiveRelayInfo(relay, name, description, icon string) (string, string, string) {
rs := management.get().relayOrEmpty(relay)
return cmp.Or(rs.Name, name), cmp.Or(rs.Description, description), cmp.Or(rs.Icon, icon)
}
// whitelistedPubKeys returns the union as a slice, for the nostr filters the
// import paths build.
func whitelistedPubKeys() []string {
return slices.Collect(maps.Keys(whitelistedPubKeySet()))
}
// whitelistedPubKeySet returns the union as a map. It is always a fresh map:
// the web of trust refresher reads whatever it is given on every refresh, so
// handing it config.WhitelistedPubKeys and then writing to that map would be a
// data race.
func whitelistedPubKeySet() map[string]struct{} {
allowed := management.get().AllowedPubKeys
set := make(map[string]struct{}, len(config.WhitelistedPubKeys)+len(allowed))
maps.Copy(set, config.WhitelistedPubKeys)
for pubKey := range allowed {
set[pubKey] = struct{}{}
}
return set
}