mirror of
https://github.com/Yonle/bostr.git
synced 2025-03-18 05:42:03 +01:00
Holy shit http.js is left alone :((((
Signed-off-by: Yonle <yonle@lecturify.net>
This commit is contained in:
parent
94af76f8dc
commit
6d5e690269
42
http.js
Normal file
42
http.js
Normal file
@ -0,0 +1,42 @@
|
||||
const WebSocket = require("ws");
|
||||
const config = require("./config");
|
||||
const http = require("http");
|
||||
|
||||
const handleBouncer = require(`./bouncer/bouncer${config?.mode || 1}.js`);
|
||||
|
||||
// For log
|
||||
const curD = _ => (new Date()).toLocaleString("ia");
|
||||
const log = _ => console.log(process.pid, curD(), "-", _);
|
||||
|
||||
// Server
|
||||
const server = http.createServer()
|
||||
const wss = new WebSocket.WebSocketServer({ noServer: true });
|
||||
|
||||
server.on('request', (req, res) => {
|
||||
log(`${req.headers["x-forwarded-for"]?.split(",")[0] || req.socket.address()?.address} - ${req.method} ${req.url}`)
|
||||
|
||||
if (req.headers.accept?.includes("nostr"))
|
||||
return res.writeHead(200, {
|
||||
"Content-Type": "application/json"
|
||||
}).end(JSON.stringify(config.server_meta));
|
||||
|
||||
if (req.url === "/") {
|
||||
res.write("Hello. This nostr bouncer (bostr) is bouncing the following relays:\n\n");
|
||||
config.relays.forEach(_ => {
|
||||
res.write("- " + _ + "\n");
|
||||
});
|
||||
res.write(`\nConnect to this bouncer with nostr client: ws://${req.headers.host} or wss://${req.headers.host}\n\n---\n`);
|
||||
res.end("Powered by Bostr - Open source nostr Bouncer\nhttps://github.com/Yonle/bostr");
|
||||
} else {
|
||||
res.writeHead(404).end("What are you looking for?");
|
||||
}
|
||||
});
|
||||
|
||||
server.on('upgrade', (req, sock, head) => {
|
||||
log(`${req.headers["x-forwarded-for"]?.split(",")[0] || req.socket.address()?.address} - WS CONN`);
|
||||
wss.handleUpgrade(req, sock, head, _ => handleBouncer(_, req));
|
||||
});
|
||||
|
||||
const listened = server.listen(process.env.PORT || config.port, config.address || "0.0.0.0", _ => {
|
||||
log("Bostr is now listening on " + "ws://" + (config.address || "0.0.0.0") + ":" + config.port);
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user