GithubHelp home page GithubHelp logo

imksoo / nostr-filter Goto Github PK

View Code? Open in Web Editor NEW
20.0 2.0 4.0 93 KB

`nostr-filter` is a Nostr relay server that filters messages based on regular expressions.

License: MIT License

TypeScript 97.99% Dockerfile 2.01%
nostr nostr-relay

nostr-filter's People

Contributors

atrifat avatar imksoo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

nostr-filter's Issues

Add production node env support

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.

Bug logic in whitelisted pubkey check

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.

Better error handling for invalid JSON request

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.

Clarification for IP code block position

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.

Add whitelisted pubkeys support to write events

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.