-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelay.go
More file actions
193 lines (161 loc) · 4.51 KB
/
Copy pathrelay.go
File metadata and controls
193 lines (161 loc) · 4.51 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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"time"
"github.com/bits-and-blooms/bloom/v3"
"github.com/fiatjaf/relayer/v2"
"github.com/jmoiron/sqlx"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip11"
"golang.org/x/time/rate"
)
func newRelay(cfg Config, subscriptionsDB *sqlx.DB) (*Relay, error) {
if len(cfg.AllowedKinds) == 0 {
cfg.AllowedKinds = defaultAllowedKinds
}
r := Relay{
cfg: cfg,
storage: newStorage(cfg),
updates: make(chan nostr.Event),
subscriptionsDB: subscriptionsDB,
}
opts := []relayer.Option{
relayer.WithPerConnectionLimiter(rate.Every(time.Millisecond*100), 10),
}
server, err := relayer.NewServer(r, opts...)
if err != nil {
return nil, fmt.Errorf("relayer new server: %w", err)
}
r.server = server
return &r, nil
}
type Relay struct {
cfg Config
server *relayer.Server
storage *storage
updates chan nostr.Event
subscriptionsDB *sqlx.DB
}
func (r *Relay) GetNIP11InformationDocument() nip11.RelayInformationDocument {
return nip11.RelayInformationDocument{
Name: r.Name(),
Description: r.cfg.Nip11Description,
PubKey: r.cfg.Nip11Pubkey,
Contact: r.cfg.Nip11Contact,
SupportedNIPs: []int{9, 11, 12, 15, 16, 20, 45, 78, 94},
Software: "https://github.com/Stemstr",
Version: r.cfg.Nip11Version,
}
}
func (r Relay) Name() string {
return "Stemstr relay"
}
func (r Relay) Storage(ctx context.Context) relayer.Storage {
return r.storage
}
func (r Relay) OnInitialized(*relayer.Server) {}
func (r Relay) Init() error {
return nil
}
func (r Relay) AcceptEvent(ctx context.Context, evt *nostr.Event) bool {
// Reject any kinds not explicitly allowed
allowed := false
for _, kind := range r.cfg.AllowedKinds {
if evt.Kind == kind {
allowed = true
break
}
}
if !allowed {
return false
}
// Reject events that are too large
jsonb, _ := json.Marshal(evt)
if len(jsonb) > 10000 {
log.Printf("rejected event, too large")
return false
}
// Require subscription for some events
if kindRequiresSubscription(evt.Kind) {
if ok, err := isSubscribed(r.subscriptionsDB, evt.PubKey); !ok {
if err != nil {
log.Printf("isSubscribed: %v\n", err)
}
log.Printf("rejected event, no sub. pubkey: %v", evt.PubKey)
return false
}
}
// Kind 1's must be from Stemstr client else reference a known event.
if evt.Kind == 1 {
if !fromStemstrClient(evt) && !referencesExistingEvent(r.storage.seenEvents, evt) {
log.Printf("rejected event, not from stemstr.app or referencing known event: %s", string(jsonb))
return false
}
}
// Reactions and reposts must reference a known event.
if evt.Kind == nostr.KindReaction || evt.Kind == 6 || evt.Kind == 16 {
if !referencesExistingEvent(r.storage.seenEvents, evt) {
fmt.Printf("rejected event, does not reference known event: %v\n", string(jsonb))
return false
}
}
// 1808s are only allowed from Stemstr client.
if evt.Kind == 1808 && !fromStemstrClient(evt) {
log.Printf("rejected event, not from stemstr.app: %s", string(jsonb))
return false
}
fmt.Printf("relay: received event: %v\n", string(jsonb))
return true
}
func (relay Relay) InjectEvents() chan nostr.Event {
return relay.updates
}
func (r Relay) Start() error {
log.Printf("listening on 0.0.0.0:%v\n", r.cfg.Port)
return r.server.Start("0.0.0.0", r.cfg.Port)
}
var defaultAllowedKinds = []int{
nostr.KindSetMetadata,
nostr.KindTextNote,
nostr.KindContactList,
nostr.KindReaction,
1808, // Stemstr Music Track
6, // NIP-18: Repost
16, // NIP-18: Generic Repost
9735, // NIP-57: Zap Receipt
30078, // NIP-78: Application-specific Data
1063, // NIP-94: File Metadata
}
func fromStemstrClient(event *nostr.Event) bool {
clientTag := event.Tags.GetFirst([]string{"client"})
if clientTag == nil {
return false
}
return strings.EqualFold(clientTag.Value(), "stemstr.app")
}
// referencesExistingEvent returns true if the given event has an e tag
// to an event in the provided bloom filter.
func referencesExistingEvent(f *bloom.BloomFilter, event *nostr.Event) bool {
// Has no e tags, cannot reference existing event
eTags := event.Tags.GetAll([]string{"e"})
if eTags == nil || len(eTags) == 0 {
return false
}
for _, tag := range eTags {
referencedEventID := tag.Value()
if referencedEventID == "" {
// e tag has no value, probably a malformed event.
// skip it and check the others
continue
}
if f.Test([]byte(referencedEventID)) {
// This new event DOES reference an existing event
return true
}
}
return false
}