mirror of
https://github.com/Yonle/bostr.git
synced 2025-06-29 02:01:40 +02:00
bouncer: improve NIP-42 compatibilities
Signed-off-by: Yonle <yonle@lecturify.net>
This commit is contained in:
9
auth.js
9
auth.js
@ -3,12 +3,13 @@ const { validateEvent, verifyEvent } = require("nostr-tools");
|
|||||||
const { authorized_keys, private_keys } = require(process.env.BOSTR_CONFIG_PATH || "./config");
|
const { authorized_keys, private_keys } = require(process.env.BOSTR_CONFIG_PATH || "./config");
|
||||||
|
|
||||||
module.exports = (authKey, data, ws, req) => {
|
module.exports = (authKey, data, ws, req) => {
|
||||||
|
if (!(authorized_keys?.length || Object.keys(private_keys).length)) return; // do nothing
|
||||||
if (!validateEvent(data) || !verifyEvent(data)) {
|
if (!validateEvent(data) || !verifyEvent(data)) {
|
||||||
ws.send(JSON.stringify(["NOTICE", "error: invalid challenge response."]));
|
ws.send(JSON.stringify(["NOTICE", "error: invalid challenge response."]));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((authorized_keys?.length || Object.keys(private_keys).length) && !authorized_keys?.includes(data.pubkey) && !(private_keys && private_keys[data.pubkey])) {
|
if (!authorized_keys?.includes(data.pubkey) && !private_keys[data.pubkey]) {
|
||||||
ws.send(JSON.stringify(["OK", data.id, false, "unauthorized."]));
|
ws.send(JSON.stringify(["OK", data.id, false, "unauthorized."]));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -18,14 +19,14 @@ module.exports = (authKey, data, ws, req) => {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const tags = new Map(data.tags);
|
const tags = Object.fromEntries(data.tags);
|
||||||
|
|
||||||
if (!tags.get("relay").includes(req.headers.host)) {
|
if (!tags.relay?.includes(req.headers.host)) {
|
||||||
ws.send(JSON.stringify(["OK", data.id, false, "unmatched relay url."]));
|
ws.send(JSON.stringify(["OK", data.id, false, "unmatched relay url."]));
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (tags.get("challenge") !== authKey) {
|
if (tags.challenge !== authKey) {
|
||||||
ws.send(JSON.stringify(["OK", data.id, false, "unmatched challenge string."]));
|
ws.send(JSON.stringify(["OK", data.id, false, "unmatched challenge string."]));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
13
bouncer.js
13
bouncer.js
@ -202,10 +202,18 @@ function handleConnection(ws, req, onClose) {
|
|||||||
ws.send(JSON.stringify(["CLOSED", origID, ""]));
|
ws.send(JSON.stringify(["CLOSED", origID, ""]));
|
||||||
break;
|
break;
|
||||||
case "AUTH":
|
case "AUTH":
|
||||||
if (authorized) return;
|
|
||||||
if (auth(authKey, data[1], ws, req)) {
|
if (auth(authKey, data[1], ws, req)) {
|
||||||
|
authKey = Date.now() + Math.random().toString(36);
|
||||||
ws.pubkey = data[1].pubkey;
|
ws.pubkey = data[1].pubkey;
|
||||||
console.log(process.pid, "---", ws.ip, "successfully authorized as", ws.pubkey, private_keys[ws.pubkey] ? "(admin)" : "(user)");
|
console.log(process.pid, "---", ws.ip, "successfully authorized as", ws.pubkey, private_keys[ws.pubkey] ? "(admin)" : "(user)");
|
||||||
|
if (private_keys[ws.pubkey]) {
|
||||||
|
for (const relay of userRelays.get(ws.id)) {
|
||||||
|
for (const challenge of relay.pendingNIP42) {
|
||||||
|
nip42(relay, client.pubkey, private_keys[ws.pubkey], challenge);
|
||||||
|
relay.pendingNIP42.delete(challenge);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (authorized) return;
|
if (authorized) return;
|
||||||
authorized = true;
|
authorized = true;
|
||||||
lastEvent = Date.now();
|
lastEvent = Date.now();
|
||||||
@ -323,6 +331,7 @@ function newConn(addr, id, reconn_t = 0) {
|
|||||||
relay.isCache = relay_type(addr) === "cache_relay";
|
relay.isCache = relay_type(addr) === "cache_relay";
|
||||||
relay.isLoadBalancer = relay_type(addr) === "loadbalancer";
|
relay.isLoadBalancer = relay_type(addr) === "loadbalancer";
|
||||||
relay.ratelimit = 0;
|
relay.ratelimit = 0;
|
||||||
|
relay.pendingNIP42 = new Set();
|
||||||
relay.on('open', _ => {
|
relay.on('open', _ => {
|
||||||
if (!csess.has(id)) return relay.terminate();
|
if (!csess.has(id)) return relay.terminate();
|
||||||
const client = csess.get(id);
|
const client = csess.get(id);
|
||||||
@ -414,7 +423,7 @@ function newConn(addr, id, reconn_t = 0) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "AUTH":
|
case "AUTH":
|
||||||
if (!private_keys || typeof(data[1]) !== "string" || !client.pubkey) return;
|
if (!private_keys || typeof(data[1]) !== "string" || !client.pubkey) return relay.pendingNIP42.add(data[1]);
|
||||||
nip42(relay, client.pubkey, private_keys[client.pubkey], data[1]);
|
nip42(relay, client.pubkey, private_keys[client.pubkey], data[1]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user