http: favicon support.

Signed-off-by: Yonle <yonle@lecturify.net>
This commit is contained in:
Yonle 2024-01-09 06:58:27 +07:00
parent d0b8a6b3da
commit 8863a73c65
3 changed files with 10 additions and 1 deletions

View File

@ -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;

View File

@ -102,6 +102,9 @@ module.exports = {
"version": require("./package.json").version
},
// Path to favicon file.
favicon: "",
// Cache relays
// Used for caching received events from <relays> to reduce bandwidth waste.
// Keeping this empty will disable caching function.

View File

@ -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?");
}