-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.mjs
276 lines (228 loc) · 8.6 KB
/
index.mjs
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
import express from 'express'
import bodyparser from 'body-parser'
import { admin } from './firebase-config.js'
import { nip44 } from 'nostr-tools'
import { finalizeEvent, generateSecretKey, verifyEvent } from 'nostr-tools/pure'
import { RelayPool } from './relay-pool.js'
import { LRUCache } from 'lru-cache'
import {
registerInDatabaseTuples,
getAllKeys,
getAllRelays,
getTokensByPubKey,
deleteToken,
deleteRelay,
checkIfThereIsANewRelay
} from './database.mjs'
const app = express()
app.use(bodyparser.json())
const port = process.env.PORT || 3000
var relayPool;
const sentCache = new LRUCache(
{
max: 5000,
maxSize: 5000,
sizeCalculation: (value, key) => {
return 1
},
// how long to live in ms
ttl: 1000 * 60 * 5,
}
)
app.post('/register', (req, res) => {
register(req.body.token, req.body.events).then((processed) => {
res.status(200).send(processed)
});
})
app.listen(port, () => {
console.log("Listening to port" + port)
})
function isValidUrl(urlString) {
try {
return Boolean(new URL(urlString));
}
catch(e){
return false;
}
}
function isSupportedUrl(url) {
return url &&
!url.includes("brb.io") && // no broken relays
!url.includes("echo.websocket.org") && // test relay
!url.includes("127.0") && // no local relays
!url.includes("umbrel.local") && // no local relays
!url.includes("192.168.") && // no local relays
!url.includes(".onion") && // we are not running on Tor
!url.includes("https://") && // not a websocket
!url.includes("http://") && // not a websocket
!url.includes("www://") && // not a websocket
!url.includes("https//") && // not a websocket
!url.includes("http//") && // not a websocket
!url.includes("www//") && // not a websocket
!url.includes("npub1") && // does not allow custom uris
!url.includes("was://") && // common mispellings
!url.includes("ws://umbrel:") && // local domain
!url.includes("\t") && // tab is not allowed
!url.includes(" ") && // space is not allowed
isValidUrl(url)
}
// -- registering tokens with pubkeys.
async function register(token, events) {
let processed = []
//let newPubKeys = false
let newRelays = false
for (const event of events) {
let veryOk = verifyEvent(event)
let tokenTag = event.tags
.find(tag => tag[0] == "challenge" && tag.length > 1)
let relayTags = event.tags
.filter(tag => tag[0] == "relay" && tag.length > 1 && tag[1].length > 1 && isSupportedUrl(tag[1]))
.map(tag => tag[1])
.filter(function (value, index, array) {
// remove duplicates
return array.indexOf(value) === index;
})
if (tokenTag[1] && veryOk && relayTags.length > 0) {
newRelays = await checkIfThereIsANewRelay(relayTags)
await registerInDatabaseTuples(relayTags.map(relayUrl => [event.pubkey,relayUrl || null,tokenTag[1]]))
} else {
console.log("Invalid registration", veryOk, tokenTag, relayTags)
}
processed.push(
{
"pubkey": event.pubkey,
"added": tokenTag && veryOk && relayTags.length > 0
}
)
}
if (newRelays)
restartRelayPool()
return processed
}
// -- notifiying new events to pub keys.
async function notify(event, relay) {
let pubkeyTag = event.tags.find(tag => tag[0] == "p" && tag.length > 1)
if (pubkeyTag && pubkeyTag[1]) {
//console.log("New kind", event.kind, "event for", pubkeyTag[1], "from", relay.url)
let tokens = await getTokensByPubKey(pubkeyTag[1])
let tokensAsUrls = tokens.filter(isValidHttpUrl)
let firebaseTokens = tokens.filter(item => !tokensAsUrls.includes(item))
if (tokens.length > 0) {
const stringifiedWrappedEventToPush = JSON.stringify(createWrap(pubkeyTag[1], event))
if (tokensAsUrls.length > 0) {
tokensAsUrls.forEach(async function (tokenUrl) {
fetch(tokenUrl, {
method: 'POST',
body: stringifiedWrappedEventToPush,
signal: AbortSignal.timeout(5000) // NTFY waits for 30 seconds to send a timeout when the user sent too many reqs
}).then((response) => {
if (!response.ok) {
console.log("Error posting to NTFY", stringifiedWrappedEventToPush.length, "chars.", tokenUrl, response.status, response.statusText)
deleteToken(tokenUrl)
}
}).catch(err => {
console.log("Error posting to NTFY", stringifiedWrappedEventToPush.length, "chars.", tokenUrl, err)
//deleteToken(tokenUrl)
})
});
console.log("NTFY New kind", event.kind, "event for", pubkeyTag[1], "with", stringifiedWrappedEventToPush.length, "bytes")
}
if (firebaseTokens.length > 0) {
const message = {
data: {
encryptedEvent: stringifiedWrappedEventToPush
},
tokens: firebaseTokens
};
admin.messaging().sendEachForMulticast(message).then((response) => {
if (response.failureCount > 0) {
response.responses.forEach((resp, idx) => {
if (!resp.success) {
console.log('Failed: ', resp.error.code, resp.error.message, JSON.stringify(message).length, "chars");
if (resp.error.code === "messaging/registration-token-not-registered") {
console.log('Deleting Token ', tokens[idx]);
deleteToken(tokens[idx])
}
}
});
}
});
console.log("Firebase New kind", event.kind, "event for", pubkeyTag[1], "with", stringifiedWrappedEventToPush.length, "bytes")
}
}
}
}
function isValidHttpUrl(string) {
let givenURL;
try {
givenURL = new URL(string);
} catch (error) {
//console.log("error is",error)
return false;
}
return givenURL.protocol === "http:" || givenURL.protocol === "https:";
}
var isInRelayPollFunction = false
// -- relay connection
async function restartRelayPool() {
if (isInRelayPollFunction) return
isInRelayPollFunction = true
let relays = await getAllRelays()
if (relayPool) {
let hasNewRelay = relays.filter(x => !relayPool.has(x)).length > 0
if (!hasNewRelay) return
}
if (relayPool) {
relayPool.close()
}
relayPool = RelayPool( Array.from( relays ), {reconnect: true} )
relayPool.on('open', relay => {
relay.subscribe("subid",
{
kinds: [4, 9735, 1059],
limit: 1
}
)
});
relayPool.on('eose', relay => {
//console.log("EOSE")
});
relayPool.on('event', (relay, sub_id, ev) => {
try {
if (sentCache.has(ev.id)) return
sentCache.set(ev.id, ev.id)
notify(ev, relay)
} catch (e) {
console.log(relay, ev, e)
}
});
relayPool.on('error', (relay, e) => {
if (
!isSupportedUrl(relay.url)
|| e.message.includes("Invalid URL")
|| e.message.includes("ECONNREFUSED")
|| e.message.includes("Invalid WebSocket frame: FIN must be set")
|| e.message.includes("The URL's protocol must be one of")
) {
relayPool.remove(relay.url)
deleteRelay(relay.url)
}
//console.log("Error", relay.url, e.message)
})
console.log("Restarted pool with", relays.length, "relays")
isInRelayPollFunction = false
}
function createWrap(recipientPubkey, event, tags = []) {
const wrapperPrivkey = generateSecretKey()
const wrapTemplate = {
kind: 1059,
created_at: Math.floor(Date.now() / 1000),
tags: tags,
content: nip44.encrypt(
JSON.stringify(event),
nip44.getConversationKey(wrapperPrivkey, recipientPubkey)
)
}
return finalizeEvent(wrapTemplate, wrapperPrivkey)
}
restartRelayPool()