From 8863a73c655a8f1bf3ce9257cb8b67d5b268b07f Mon Sep 17 00:00:00 2001 From: Yonle Date: Tue, 9 Jan 2024 06:58:27 +0700 Subject: [PATCH] http: favicon support. Signed-off-by: Yonle --- bouncer.js | 2 +- config.js.example | 3 +++ http.js | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bouncer.js b/bouncer.js index e107f5d..8643355 100644 --- a/bouncer.js +++ b/bouncer.js @@ -57,7 +57,7 @@ module.exports = (ws, req) => { JSON.stringify(["NOTICE", "error: bad JSON."]) ) } - +console.log(data) switch (data[0]) { case "EVENT": if (!authorized) return; diff --git a/config.js.example b/config.js.example index da245fb..644ebff 100644 --- a/config.js.example +++ b/config.js.example @@ -102,6 +102,9 @@ module.exports = { "version": require("./package.json").version }, + // Path to favicon file. + favicon: "", + // Cache relays // Used for caching received events from to reduce bandwidth waste. // Keeping this empty will disable caching function. diff --git a/http.js b/http.js index c3430c2..202b233 100644 --- a/http.js +++ b/http.js @@ -2,6 +2,7 @@ const { version } = require("./package.json"); const WebSocket = require("ws"); const config = require("./config"); const http = require("http"); +const fs = require("fs"); const bouncer = require(`./bouncer.js`); // For log @@ -13,6 +14,8 @@ const server = http.createServer({ noDelay: true }) const wss = new WebSocket.WebSocketServer({ noServer: true }); const lastConn = new Map(); +const favicon = fs.existsSync(config.favicon) ? fs.readFileSync(config.favicon) : null; + server.on('request', (req, res) => { log(`${req.headers["x-forwarded-for"]?.split(",")[0] || req.socket.address()?.address} - ${req.method} ${req.url} [${req.headers["user-agent"] || ""}]`) @@ -35,6 +38,9 @@ server.on('request', (req, res) => { if (config?.authorized_keys?.length) res.write("\nNOTE: This relay has configured for personal use only. Only authorized users could use this bostr relay.\n"); res.write(`\nConnect to this bouncer with nostr client: ws://${req.headers.host}${req.url} or wss://${req.headers.host}${req.url}\n\n---\n`); res.end(`Powered by Bostr (${version}) - Open source Nostr bouncer\nhttps://github.com/Yonle/bostr`); + } else if (req.url.startsWith("/favicon") && favicon) { + res.writeHead(200, { "Content-Type": "image/" + config.favicon?.split(".").pop() }); + res.end(favicon); } else { res.writeHead(404).end("What are you looking for?"); }