-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgen_router.go
More file actions
426 lines (395 loc) · 11.5 KB
/
gen_router.go
File metadata and controls
426 lines (395 loc) · 11.5 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
)
type streamConfig struct {
Name string
Dir string // "down" or "up"
Authors []string
URLs []string
Kinds string // raw JSON array or empty
PTag string // for #p filter (notifications)
}
// greedySelectAndAssignN selects relays greedily so that each author is assigned
// to up to 'replicas' distinct relays. It returns the selected relays and a mapping
// of relay -> assigned authors.
func greedySelectAndAssignN(relayAuthors map[string][]string, replicas int) ([]string, map[string][]string) {
// remaining need per author
need := make(map[string]int)
// track which authors each relay covers for quick iteration
for _, authors := range relayAuthors {
for _, a := range authors {
if need[a] == 0 {
need[a] = replicas
}
}
}
selected := []string{}
assigned := make(map[string][]string)
// Also prevent duplicate assignment of same author to same relay
assignedSet := make(map[string]map[string]struct{}) // relay -> set(author)
// helper to count gain
gainOf := func(relay string) int {
cnt := 0
for _, a := range relayAuthors[relay] {
if need[a] > 0 {
// avoid counting if already assigned to this relay
if set, ok := assignedSet[relay]; ok {
if _, has := set[a]; has {
continue
}
}
cnt++
}
}
return cnt
}
// loop until no author needs more or no gain
for {
// check completion
done := true
for _, v := range need {
if v > 0 {
done = false
break
}
}
if done {
break
}
bestRelay := ""
bestGain := 0
for relay := range relayAuthors {
g := gainOf(relay)
if g > bestGain {
bestGain = g
bestRelay = relay
}
}
if bestGain == 0 || bestRelay == "" {
break
}
// assign as many needing authors as possible to bestRelay
for _, a := range relayAuthors[bestRelay] {
if need[a] <= 0 {
continue
}
if assignedSet[bestRelay] == nil {
assignedSet[bestRelay] = make(map[string]struct{})
}
if _, has := assignedSet[bestRelay][a]; has {
continue
}
assignedSet[bestRelay][a] = struct{}{}
assigned[bestRelay] = append(assigned[bestRelay], a)
need[a]--
}
selected = append(selected, bestRelay)
}
// normalize and sort authors per relay
for r := range assigned {
assigned[r] = uniqueSorted(assigned[r])
}
for i := range selected {
selected[i] = normalizeURL(selected[i])
}
return selected, assigned
}
func genRouterCmd(args []string) {
fs := flag.NewFlagSet("gen-router", flag.ExitOnError)
dataDir := commonFlags(fs)
output := fs.String("output", "./strfry-router.config", "output router config path")
authorsPerStream := fs.Int("authors-per-stream", 50, "max authors per stream section")
streamPrefix := fs.String("stream-prefix", "follows", "prefix for down streams")
includeUnassigned := fs.Bool("include-unassigned", false, "add one stream querying all selected relays for any unassigned authors (rare)")
replicas := fs.Int("replicas", 1, "number of distinct relays to assign each author to (>=1)")
kindsJSON := fs.String("kinds-json", "", "JSON array for down streams kinds filter (e.g. [0,1,3])")
onlineOnly := fs.Bool("online-only", false, "use only online relays from NIP-66 monitoring (requires analyze --check-monitors)")
// Notification sync options
includeNotifs := fs.Bool("include-notifs", false, "add streams for user notifications (your posts and mentions)")
if err := fs.Parse(args); err != nil {
fmt.Fprintf(os.Stderr, "failed to parse flags: %v\n", err)
os.Exit(1)
}
dd := *dataDir
// Inputs
mapFile := filepath.Join(dd, "pubkey_relays_map.txt")
if *onlineOnly {
mapFile = filepath.Join(dd, "pubkey_relays_map_online.txt")
fmt.Println("Using online-only relay map from NIP-66 monitoring")
}
followsFile := filepath.Join(dd, "follows_list.txt")
userRelayListFile := filepath.Join(dd, "user_relay_list.txt")
userPubkeyFile := filepath.Join(dd, "user_pubkey.txt")
followsSet := loadSetMust(followsFile)
// Build relay->authors from pubkey_relays_map
relayAuthors := make(map[string][]string)
{
pairs := readLinesMust(mapFile)
for _, line := range pairs {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
pk := strings.ToLower(fields[0])
rurl := normalizeURL(strings.Join(fields[1:], " "))
if _, ok := followsSet[pk]; !ok {
continue
}
// Skip invalid relay URLs
if !isValidRelayURL(rurl) {
continue
}
relayAuthors[rurl] = append(relayAuthors[rurl], pk)
}
}
// dedupe and sort authors per relay
for r := range relayAuthors {
relayAuthors[r] = uniqueSorted(relayAuthors[r])
}
// Compute greedy optimal set from relayAuthors and assign authors to up to N replicas
if *replicas < 1 {
*replicas = 1
}
selected, assigned := greedySelectAndAssignN(relayAuthors, *replicas)
var streams []streamConfig
// Create per-relay down streams for selected relays with their assigned authors
for _, relay := range selected {
relay = normalizeURL(relay)
auths := assigned[relay]
if len(auths) == 0 {
continue
}
// Validate authors are 64-char hex and normalize to lowercase
filtered := make([]string, 0, len(auths))
for _, a := range auths {
a = strings.ToLower(strings.TrimSpace(a))
if isHex64(a) {
filtered = append(filtered, a)
}
}
if len(filtered) == 0 {
continue
}
chunks := chunk(filtered, *authorsPerStream)
for i, chunkAuthors := range chunks {
name := fmt.Sprintf("%s_%s_%d", *streamPrefix, safeName(relay), i+1)
streams = append(streams, streamConfig{Name: name, Dir: "down", Authors: chunkAuthors, URLs: []string{relay}, Kinds: *kindsJSON})
}
}
// Optionally include authors still needing replicas across all selected relays
if *includeUnassigned {
// Build a count of assigned replicas per author
counts := make(map[string]int)
for _, s := range streams {
for _, a := range s.Authors {
counts[a]++
}
}
var needMore []string
for a := range followsSet {
if counts[a] < *replicas {
needMore = append(needMore, a)
}
}
needMore = uniqueSorted(needMore)
if len(needMore) > 0 {
// Validate authors and normalize
filtered := make([]string, 0, len(needMore))
for _, a := range needMore {
a = strings.ToLower(strings.TrimSpace(a))
if isHex64(a) {
filtered = append(filtered, a)
}
}
if len(filtered) == 0 {
// nothing valid to add
} else {
chunks := chunk(filtered, *authorsPerStream)
for i, ch := range chunks {
name := fmt.Sprintf("%s_unassigned_%d", *streamPrefix, i+1)
// Query across selected relays for any missed authors
urls := make([]string, len(selected))
copy(urls, selected)
streams = append(streams, streamConfig{Name: name, Dir: "down", Authors: ch, URLs: urls, Kinds: *kindsJSON})
}
}
}
}
// Add notification streams if requested
if *includeNotifs {
// Load user's pubkey from file
userPubkeyLines := readLinesIfExists(userPubkeyFile)
if len(userPubkeyLines) == 0 {
fmt.Fprintf(os.Stderr, "error: no user pubkey found at %s\n", userPubkeyFile)
fmt.Fprintln(os.Stderr, "hint: run 'collect' command first with --pubkey to save your pubkey")
os.Exit(1)
}
pubkey := strings.ToLower(strings.TrimSpace(userPubkeyLines[0]))
if !isHex64(pubkey) {
fmt.Fprintf(os.Stderr, "error: invalid pubkey in %s: %s\n", userPubkeyFile, pubkey)
os.Exit(1)
}
// Load user's relay list from file and filter out invalid URLs
userRelaysRaw := readLinesIfExists(userRelayListFile)
var userRelays []string
for _, relay := range userRelaysRaw {
if isValidRelayURL(relay) {
userRelays = append(userRelays, relay)
}
}
if len(userRelays) == 0 {
fmt.Fprintf(os.Stderr, "warning: no user relay list found at %s, skipping notification streams\n", userRelayListFile)
fmt.Fprintln(os.Stderr, "hint: run 'collect' command first with --pubkey to fetch your relay list")
} else {
fmt.Printf("Adding notification streams for pubkey %s using %d relays\n", pubkey, len(userRelays))
// Add stream for notifications mentioning user (inbox)
for _, relay := range userRelays {
relay = normalizeURL(relay)
name := fmt.Sprintf("notifs_inbox_%s", safeName(relay))
streams = append(streams, streamConfig{
Name: name,
Dir: "down",
Authors: nil, // No authors filter for inbox
URLs: []string{relay},
Kinds: *kindsJSON,
PTag: pubkey, // Special field for #p filter
})
}
}
}
// Write taocpp::config
if err := writeRouterConfig(*output, streams); err != nil {
fmt.Fprintf(os.Stderr, "error writing router config: %v\n", err)
os.Exit(1)
}
fmt.Printf("Wrote %s (%d streams)\n", *output, len(streams))
}
func readLinesMust(path string) []string {
lines, err := readLines(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading %s: %v\n", path, err)
os.Exit(1)
}
for i := range lines {
lines[i] = normalizeURL(lines[i])
}
return lines
}
func readLinesIfExists(path string) []string {
lines, err := readLines(path)
if err != nil {
return nil
}
for i := range lines {
lines[i] = normalizeURL(lines[i])
}
return lines
}
func loadSetMust(path string) map[string]struct{} {
m := make(map[string]struct{})
lines, err := readLines(path)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading %s: %v\n", path, err)
os.Exit(1)
}
for _, l := range lines {
l = strings.ToLower(strings.TrimSpace(l))
if l != "" {
m[l] = struct{}{}
}
}
return m
}
func uniqueSorted(in []string) []string {
m := make(map[string]struct{})
for _, v := range in {
m[v] = struct{}{}
}
var out []string
for v := range m {
out = append(out, v)
}
sort.Strings(out)
return out
}
func chunk[T any](in []T, n int) [][]T {
if n <= 0 || len(in) == 0 {
return nil
}
var out [][]T
for i := 0; i < len(in); i += n {
j := i + n
if j > len(in) {
j = len(in)
}
out = append(out, in[i:j])
}
return out
}
func safeName(relay string) string {
name := strings.TrimPrefix(relay, "wss://")
name = strings.TrimPrefix(name, "ws://")
name = strings.ReplaceAll(name, ":", "_")
name = strings.ReplaceAll(name, "/", "_")
name = strings.ReplaceAll(name, ".", "_")
return name
}
func writeRouterConfig(path string, streams []streamConfig) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
w := bufio.NewWriter(f)
fmt.Fprintln(w, "connectionTimeout = 20")
fmt.Fprintln(w)
fmt.Fprintln(w, "streams {")
for _, s := range streams {
fmt.Fprintf(w, " %s {\n", s.Name)
fmt.Fprintf(w, " dir = \"%s\"\n", s.Dir)
if s.Dir == "down" && (len(s.Authors) > 0 || s.PTag != "") {
filter := make(map[string]any)
// Add authors filter if present
if len(s.Authors) > 0 {
filter["authors"] = s.Authors
}
// Add #p filter if present (for notifications)
if s.PTag != "" {
filter["#p"] = []string{s.PTag}
}
// Add kinds filter if specified
if s.Kinds != "" {
var kinds any
if err := json.Unmarshal([]byte(s.Kinds), &kinds); err == nil {
filter["kinds"] = kinds
}
}
b, _ := json.Marshal(filter)
fmt.Fprintf(w, " filter = %s\n", string(b))
} else if s.Dir == "up" && s.Kinds != "" {
// Optional kinds filter for uploads
fmt.Fprintf(w, " filter = { \"kinds\": %s }\n", s.Kinds)
}
fmt.Fprintln(w)
fmt.Fprintln(w, " urls = [")
for _, u := range s.URLs {
fmt.Fprintf(w, " \"%s\"\n", u)
}
fmt.Fprintln(w, " ]")
fmt.Fprintln(w, " }")
fmt.Fprintln(w)
}
fmt.Fprintln(w, "}")
return w.Flush()
}