From c4dc841175f40b2ff7ebb13b17247ebca4ec0d5b Mon Sep 17 00:00:00 2001 From: Rif'at Ahdi Ramadhani <10791791+atrifat@users.noreply.github.com> Date: Tue, 31 Oct 2023 10:52:45 +0800 Subject: [PATCH] Add support forward req headers (#14) fix #13 * feat: add support for forwarding req headers * chore: update .env.sample w/ forward headers --- .env.sample | 2 ++ filter.ts | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.env.sample b/.env.sample index 9ad502e..64cdd5b 100644 --- a/.env.sample +++ b/.env.sample @@ -8,5 +8,7 @@ BLOCKED_PUBKEYS= WHITELISTED_PUBKEYS= # Set true to filter proxy events FILTER_PROXY_EVENTS= +# Set true to enable forwarding of request headers to upstream server +ENABLE_FORWARD_REQ_HEADERS=false # Use classification result from monitoring bot as filter data NOSTR_MONITORING_BOT_PUBLIC_KEY= diff --git a/filter.ts b/filter.ts index 64672fe..779b67d 100644 --- a/filter.ts +++ b/filter.ts @@ -127,6 +127,8 @@ const whitelistedPubkeys: string[] = : []; // Filter proxy events const filterProxyEvents = process.env.FILTER_PROXY_EVENTS === "true"; +// Forward request headers to upstream +const enableForwardReqHeaders = process.env.ENABLE_FORWARD_REQ_HEADERS === "true"; // クライアントIPアドレスのCIDRフィルタ const cidrRanges: string[] = [ @@ -487,8 +489,12 @@ async function listen(): Promise { // ソケットごとにユニークなIDを付与 const socketId = uuidv4(); + // Check whether we want to forward original request headers to the upstream server + // This will be useful if the upstream server need original request headers to do operations like rate-limiting, etc. + let wsClientOptions = enableForwardReqHeaders ? { headers: req.headers } : undefined; + // 上流となるリレーサーバーと接続 - let upstreamSocket = new WebSocket(upstreamWsUrl); + let upstreamSocket = new WebSocket(upstreamWsUrl, wsClientOptions); connectUpstream(upstreamSocket, downstreamSocket); // クライアントとの接続が確立したら、アイドルタイムアウトを設定