imksoo / nostr-filter Goto Github PK
View Code? Open in Web Editor NEW`nostr-filter` is a Nostr relay server that filters messages based on regular expressions.
License: MIT License
`nostr-filter` is a Nostr relay server that filters messages based on regular expressions.
License: MIT License
imksoo-san, i have seen some cases whenever nostr-filter log a lot messages (console.log) the performance of nostr-filter will be slower. In my case, i found that by limiting/suppressing in "production" environment can help solve this issue.
I will send the PR later. Thank you.
Kirino-san, there is bug logic in whitelisted pubkey check feature that i have proposed before in #2
The following test will show that despite the pubkey (pseudo pubkey) is true but the old implementation will give false result. It happens because the second and the third loop check change the shouldRelay=false
const event = ["", { pubkey: "alice" }];
const whitelistedPubkeys = ["alice", "bob", "charlie"];
let shouldRelay = true;
let because = "";
for (const allowed of whitelistedPubkeys) {
if (event[1].pubkey !== allowed) {
shouldRelay = false;
because = "Only whitelisted pubkey can write events";
// break;
} else {
shouldRelay = true;
because = "";
}
}
console.log("old implementation", "shouldRelay: ", shouldRelay);
I have fixed that in new implementation as follows
const event = ["", { pubkey: "alice" }];
const whitelistedPubkeys = ["alice", "bob", "charlie"];
let shouldRelay = true;
let because = "";
if (shouldRelay && whitelistedPubkeys.length > 0) {
const isWhitelistPubkey = whitelistedPubkeys.includes(event[1].pubkey);
if (!isWhitelistPubkey) {
shouldRelay = false;
because = "Only whitelisted pubkey can write events";
}
}
console.log("new implementation", "shouldRelay: ", shouldRelay);
I will send the PR shortly.
Thank you.
There is an edge case whenever nostr-filter proxy relay accept invalid JSON request. It will crash the program immediately since there is no error handler in JSON.parse() statement.
Example:
echo '["REQ","subid",{"limit:1}]' | nostcat ws://localhost:8085
or any invalid json message
echo '["EVENT",{"id"::1}]' | nostcat ws://localhost:8085
This issue will become bigger issue whenever bad actor repeat this request and make nostr-filter relay always crash (Denial of Service).
I will send PR later for this issue imksoo-san. Please review it.
imksoo-san, i want to ask and confirm about IP detection code block.
Currently, filter.ts has implementation like this:
let upstreamSocket = new WebSocket(upstreamWsUrl);
connectUpstream(upstreamSocket, downstreamSocket);
// クライアントとの接続が確立したら、アイドルタイムアウトを設定
setIdleTimeout(downstreamSocket);
// 接続が確立されるたびにカウントを増やす
connectionCount++;
const ip =
(typeof req.headers["cloudfront-viewer-address"] === "string"
? req.headers["cloudfront-viewer-address"]
.split(":")
.slice(0, -1)
.join(":")
: undefined) ||
(typeof req.headers["x-real-ip"] === "string"
? req.headers["x-real-ip"]
: undefined) ||
(typeof req.headers["x-forwarded-for"] === "string"
? req.headers["x-forwarded-for"].split(",")[0].trim()
: undefined) ||
(typeof req.socket.remoteAddress === "string"
? req.socket.remoteAddress
: "unknown-ip-addr");
// 接続元のクライアントPortを取得
const port =
(typeof req.headers["cloudfront-viewer-address"] === "string"
? parseInt(
req.headers["cloudfront-viewer-address"].split(":").slice(-1)[0],
)
: undefined) ||
(typeof req.headers["x-real-port"] === "string"
? parseInt(req.headers["x-real-port"])
: 0);
Is it fine if those const ip, and const port are moved into upper position before let upstreamSocket declaration? Based on my observation, it should be fine if we move them into upper position than let upstreamSocket declaration? Right?
const ip =
(typeof req.headers["cloudfront-viewer-address"] === "string"
? req.headers["cloudfront-viewer-address"]
.split(":")
.slice(0, -1)
.join(":")
: undefined) ||
(typeof req.headers["x-real-ip"] === "string"
? req.headers["x-real-ip"]
: undefined) ||
(typeof req.headers["x-forwarded-for"] === "string"
? req.headers["x-forwarded-for"].split(",")[0].trim()
: undefined) ||
(typeof req.socket.remoteAddress === "string"
? req.socket.remoteAddress
: "unknown-ip-addr");
// 接続元のクライアントPortを取得
const port =
(typeof req.headers["cloudfront-viewer-address"] === "string"
? parseInt(
req.headers["cloudfront-viewer-address"].split(":").slice(-1)[0],
)
: undefined) ||
(typeof req.headers["x-real-port"] === "string"
? parseInt(req.headers["x-real-port"])
: 0);
let upstreamSocket = new WebSocket(upstreamWsUrl);
connectUpstream(upstreamSocket, downstreamSocket);
// クライアントとの接続が確立したら、アイドルタイムアウトを設定
setIdleTimeout(downstreamSocket);
// 接続が確立されるたびにカウントを増やす
connectionCount++;
Currently, i need to make those ip and port position in upper position before upstreamSocket. I want to make sure if it is really ok to do that.
Thank you, imksoo-san.
Sometimes, relay operator want to limit their user only to selected whitelisted pubkey. Any other events outside of whitelisted pubkeys should be rejected. This will be helpful especially for relay implementation which doesn't have built-in whitelist features. nostr-filter can act as proxy in the front of those relay.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.