-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathnostr_hub.py
More file actions
332 lines (272 loc) Β· 12 KB
/
nostr_hub.py
File metadata and controls
332 lines (272 loc) Β· 12 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
"""Nostr backfeed, via long-lived websocket connection(s) to relay(s)."""
from datetime import datetime, timedelta
import logging
import secrets
from threading import Event, Lock, Thread, Timer
import time
import cachetools
from google.cloud.ndb import context
from google.cloud.ndb.exceptions import ContextError
from granary.nostr import (
KIND_DELETE,
KIND_REACTION,
KIND_RELAYS,
uri_for,
uri_to_id,
verify,
)
from oauth_dropins.webutil import util
from oauth_dropins.webutil.appengine_config import ndb_client
from oauth_dropins.webutil.appengine_info import DEBUG
from oauth_dropins.webutil.util import HTTP_TIMEOUT, json_dumps, json_loads
from websockets.exceptions import ConnectionClosed
from websockets.sync.client import connect
from common import (
create_task,
NDB_CONTEXT_KWARGS,
report_error,
report_exception,
)
from models import Object, PROTOCOLS
from nostr import normalize_relay_uri, Nostr, NostrRelay
from protocol import DELETE_TASK_DELAY
from ui import UIProtocol
logger = logging.getLogger(__name__)
AUTHOR_FILTER_KINDS = list(Nostr.SUPPORTED_KINDS - {KIND_REACTION})
RECONNECT_DELAY = timedelta(seconds=10)
LOAD_USERS_FREQ = timedelta(seconds=10)
STORE_RELAY_SINCE_FREQ = timedelta(seconds=10)
# global: _load_pubkeys populates them, subscribe uses them
nostr_pubkeys = set()
bridged_pubkeys = set()
pubkeys_loaded_at = datetime(1900, 1, 1)
pubkeys_initialized = Event()
seen_ids = cachetools.TTLCache(maxsize=1_000_000,
ttl=timedelta(weeks=1).total_seconds())
seen_ids_lock = Lock()
# maps string relay websocket address URI to NostrRelay
subscribed_relays = {}
subscribed_relays_lock = Lock()
def ndb_context():
if ctx := context.get_context(raise_context_error=False):
return ctx.use()
return ndb_client.context(**NDB_CONTEXT_KWARGS)
def init(subscribe=True):
logger.info('Starting _load_users timer')
# run in a separate thread since it needs to make its own NDB
# context when it runs in the timer thread
Thread(target=_load_users, daemon=True, name='nostr_hub._load_users',
kwargs={'subscribe': subscribe}).start()
pubkeys_initialized.wait()
pubkeys_initialized.clear()
if subscribe:
with ndb_context():
add_relay(Nostr.DEFAULT_TARGET)
def _load_users(subscribe=True):
global pubkeys_loaded_at
if not DEBUG:
Timer(LOAD_USERS_FREQ.total_seconds(), _load_users).start()
with ndb_context():
try:
loaded_at = util.now().replace(tzinfo=None)
new_nostr = Nostr.query(Nostr.status == None,
Nostr.enabled_protocols != None,
Nostr.updated > pubkeys_loaded_at,
).fetch()
Nostr.load_multi(new_nostr)
for user in new_nostr:
nostr_pubkeys.add(uri_to_id(user.key.id()))
if subscribe and (target := Nostr.target_for(user.obj)):
add_relay(target)
new_bridged = []
for proto in PROTOCOLS.values():
if proto and proto not in (Nostr, UIProtocol):
# query for all users, then filter for nostr enabled
users = proto.query(proto.status == None,
proto.enabled_protocols == 'nostr',
proto.updated > pubkeys_loaded_at,
).fetch()
new_bridged.extend(users)
bridged_pubkeys.update(user.hex_pubkey() for user in new_bridged)
# set *after* we populate bridged_pubkeys and nostr_pubkeys so that if we
# crash earlier, we re-query from the earlier timestamp
pubkeys_loaded_at = loaded_at
pubkeys_initialized.set()
total = len(nostr_pubkeys) + len(bridged_pubkeys)
logger.info(f'Nostr pubkeys: {total}, Nostr {len(nostr_pubkeys)} (+{len(new_nostr)}), bridged {len(bridged_pubkeys)} (+{len(new_bridged)})')
except BaseException:
# eg google.cloud.ndb.exceptions.ContextError when we lose the ndb context
# https://console.cloud.google.com/errors/detail/CLO6nJnRtKXRyQE?project=bridgy-federated
report_exception()
def add_relay(uri):
"""Subscribes to a new relay if we're not already connected to it.
Args:
uri (str): URI, relay websocket adddress, starting with ``ws://`` or ``wss://``
"""
uri = normalize_relay_uri(uri)
if Nostr.is_blocklisted(uri):
logger.warning(f'Not subscribing to relay {uri}')
return
with subscribed_relays_lock:
if uri not in subscribed_relays:
relay = NostrRelay.get_or_insert(uri)
subscribed_relays[uri] = relay
Thread(target=subscriber, daemon=True, args=(relay,),
name=f'nostr_hub.subscriber {uri}').start()
def subscriber(relay):
"""Wrapper around :func:`_subscribe` that catches exceptions and reconnects.
Args:
relay (NostrRelay)
"""
logger.info(f'started thread to subscribe to relay {relay.key.id()}')
while True:
try:
with ndb_context():
subscribe(relay)
except (ConnectionClosed, ConnectionError, TimeoutError) as err:
logger.warning(err)
logger.info(f'disconnected! waiting {RECONNECT_DELAY}, then reconnecting')
except BaseException as err:
report_exception()
if DEBUG:
return
time.sleep(RECONNECT_DELAY.total_seconds())
def subscribe(relay, limit=None):
"""Subscribes to relay(s), backfeeds responses to our users' activities.
Args:
relay (NostrRelay)
limit (int): return after receiving this many messages. Only used in tests.
"""
if not DEBUG:
assert limit is None
uri = relay.key.id()
# connect's default max_size kwarg value (for websocket frames) is 1MB, which is
# probably fine, but we could consider increasing it if we come across bigger
# events. we also call connect in a few different places in granary.nostr, so
# we'd need to update those too, or centralize them.
#
# https://websockets.readthedocs.io/en/stable/reference/legacy/client.html#websockets.legacy.client.connect
# https://websockets.readthedocs.io/en/stable/reference/legacy/common.html#websockets.legacy.protocol.WebSocketCommonProtocol
with connect(uri, user_agent_header=util.user_agent,
open_timeout=util.HTTP_TIMEOUT, close_timeout=util.HTTP_TIMEOUT,
) as ws:
while True:
nostr_pubkeys_count = len(nostr_pubkeys)
bridged_pubkeys_count = len(bridged_pubkeys)
received = 0
subscription = secrets.token_urlsafe(16)
req = [
'REQ', subscription,
{
'#p': sorted(bridged_pubkeys),
'kinds': list(Nostr.SUPPORTED_KINDS),
},
{
'authors': sorted(nostr_pubkeys),
'kinds': AUTHOR_FILTER_KINDS,
},
]
# ideally this should prevent us seeing and handling many duplicate
# events. since is compared against events' created_at, though, which is
# client-provided, so malicious clients could publish events in the
# future, which would always come back as results. is there any way to
# handle/prevent that?
#
# some discussion in https://github.com/nostr-protocol/nips/issues/407 ,
# but I don't fully follow it, and it doesn't have a solution anyway.
if relay.since:
req[2]['since'] = req[3]['since'] = relay.since
req = json_dumps(req)
logger.debug(f'{uri} {ws.remote_address} <= {req}')
ws.send(req)
while True:
if (nostr_pubkeys_count != len(nostr_pubkeys)
or bridged_pubkeys_count != len(bridged_pubkeys)):
logger.info(f're-querying to pick up new user(s)')
ws.send(json_dumps(['CLOSE', subscription]))
break
try:
# use timeout to make sure we periodically loop and check whether
# we've loaded any new users, above, and need to re-query
msg = ws.recv(timeout=util.HTTP_TIMEOUT)
except TimeoutError:
continue
logger.debug(f'{ws.remote_address} => {msg}')
resp = json_loads(msg)
# https://nips.nostr.com/1
event = None
match resp[0]:
case 'EVENT':
event = resp[2]
if event.get('kind') in Nostr.SUPPORTED_KINDS:
with seen_ids_lock:
id = event.get('id')
if not (seen := id in seen_ids):
seen_ids[id] = True
if not seen:
handle(event)
case 'CLOSED':
# relay closed our query. reconnect!
relay.put()
break
case 'OK':
# TODO: this is a response to an EVENT we sent
pass
case 'EOSE':
# switching from stored results to live
pass
case 'NOTICE':
# already logged this
pass
# update stored relay timestamp if it's been a while
if event and (created_at := event.get('created_at')):
relay.since = int(created_at)
elapsed = util.now() - relay.updated
if elapsed > STORE_RELAY_SINCE_FREQ:
behind_s = util.now().timestamp() - relay.since
logger.info(f"updating {uri}'s since to {relay.since}, {behind_s} s behind")
relay.put()
# for unit tests
received += 1
if limit and received >= limit:
relay.put()
return
def handle(event):
"""Handles a Nostr event. Enqueues a receive task for it if necessary.
Args:
event (dict): Nostr event
"""
if not (isinstance(event, dict) and event.get('kind') is not None
and event.get('pubkey') and event.get('id') and event.get('sig')):
logger.info(f'ignoring bad event: {event}')
return
id = event['id']
pubkey = event['pubkey']
mentions = set(tag[1] for tag in event.get('tags', []) if tag[0] == 'p')
if not (pubkey in nostr_pubkeys # from a Nostr user who's bridged
or mentions & bridged_pubkeys): # mentions a user bridged into Nostr
return
if not verify(event):
logger.debug(f'bad id or sig for {id}')
return
logger.debug(f'Got Nostr event {id} from {pubkey}')
obj_id = 'nostr:' + id
authed_as = 'nostr:' + pubkey
# special case relay events, just store them
if event.get('kind') == KIND_RELAYS and pubkey in nostr_pubkeys:
obj = Object.get_or_create(obj_id, nostr=event, authed_as=authed_as,
source_protocol=Nostr.LABEL)
if user := Nostr.get_by_id(authed_as):
user.relays = obj.key
user.put()
return
delay = DELETE_TASK_DELAY if event.get('kind') == KIND_DELETE else None
try:
create_task(queue='receive', id=obj_id, source_protocol=Nostr.LABEL,
authed_as=authed_as, nostr=event, delay=delay)
# when running locally, comment out above and uncomment this
# logger.info(f'enqueuing receive task for {obj_id}')
except ContextError:
raise # handled in subscriber()
except BaseException:
report_exception(obj_id)