From 67f16ff24760169c3bd7402e986ed558f92453d9 Mon Sep 17 00:00:00 2001 From: Yonle Date: Wed, 28 Feb 2024 22:43:03 +0700 Subject: [PATCH] feat(bouncer): Tell upstream relays the client IP address if needed. Signed-off-by: Yonle --- bouncer.js | 9 +++++++-- config.js.example | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/bouncer.js b/bouncer.js index 96fe3d6..9a70fc6 100644 --- a/bouncer.js +++ b/bouncer.js @@ -6,7 +6,7 @@ const { validateEvent, nip19, matchFilters, mergeFilters, getFilterLimit } = req const auth = require("./auth.js"); const nip42 = require("./nip42.js"); -let { relays, approved_publishers, blocked_publishers, log_about_relays, authorized_keys, private_keys, reconnect_time, wait_eose, pause_on_limit, max_eose_score, broadcast_ratelimit, upstream_ratelimit_expiration, max_client_subs } = require(process.env.BOSTR_CONFIG_PATH || "./config"); +let { relays, approved_publishers, blocked_publishers, log_about_relays, authorized_keys, private_keys, reconnect_time, wait_eose, pause_on_limit, max_eose_score, broadcast_ratelimit, upstream_ratelimit_expiration, max_client_subs, forward_ip_address_to_upstream } = require(process.env.BOSTR_CONFIG_PATH || "./config"); log_about_relays = process.env.LOG_ABOUT_RELAYS || log_about_relays; authorized_keys = authorized_keys?.map(i => i.startsWith("npub") ? nip19.decode(i).data : i); @@ -211,9 +211,14 @@ function bc(msg, ws) { // WS - Sessions function newConn(addr, client, reconn_t = 0) { if (client.readyState !== 1) return; + let additionalReqHeaders = {}; + if (forward_ip_address_to_upstream) + additionalReqHeaders["x-forwarded-for"] = client.ip; + const relay = new WebSocket(addr, { headers: { - "User-Agent": `Bostr ${version}; The nostr relay bouncer; https://github.com/Yonle/bostr` + "User-Agent": `Bostr ${version}; The nostr relay bouncer; https://github.com/Yonle/bostr`, + ...additionalReqHeaders }, noDelay: true, allowSynchronousEvents: true diff --git a/config.js.example b/config.js.example index eac09b7..ec132fe 100644 --- a/config.js.example +++ b/config.js.example @@ -139,6 +139,9 @@ module.exports = { // Path to favicon file. favicon: "", + // Forward client IP address to upstream relays with x-forwarded-for + forward_ip_address_to_upstream: true, + // Nostr relays to bounce [Required] relays: [ "wss://example1.com",