From 898e23b871840f9c58f6d94c6b2f86bd713855b0 Mon Sep 17 00:00:00 2001 From: Chris Chrome Date: Tue, 3 Jun 2025 13:10:33 -0600 Subject: [PATCH] Change some stuff around --- index.js | 16 ++++++++++++++-- package-lock.json | 10 ++++++++++ package.json | 1 + 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index dd5b9b3..0638434 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const express = require('express'); const expressWs = require('express-ws'); const EventEmitter = require('events'); +const colors = require("colors"); const app = express(); app.use(express.urlencoded({ extended: true })); @@ -23,8 +24,15 @@ app.ws('/client', (ws, req) => { ws.close(1008, "cid already in use"); return; } + const remoteIp = req.headers['x-forwarded-for'] || req.connection.remoteAddress; + console.log(`Client connected with cid: ${cid} from ${remoteIp}`.green); cidsInUse.add(cid); hookEvent.on(cid, (data) => { + console.log(`Received data for cid: ${cid}`.blue); + if (!data || !data.data || !data.hook_id || !data.hook_token) { + console.error(`Invalid data received for cid: ${cid}`.red); + return; + } sendData = { creds: { id: data.hook_id, @@ -32,23 +40,27 @@ app.ws('/client', (ws, req) => { }, data: data.data } - console.log(sendData) + console.log(`Sending data to client with cid: ${cid}`.yellow); ws.send(JSON.stringify(sendData)); }) ws.on('close', () => { + console.log(`Client disconnected with cid: ${cid}`.red); cidsInUse.delete(cid); hookEvent.removeAllListeners("msg"); }); }); app.post('/:cid/:hook_id/:hook_token', (req, res) => { + const cid = req.params.cid; const hookId = req.params.hook_id; const hookToken = req.params.hook_token; if (!cid || !hookId || !hookToken) { + console.log(`Invalid request: cid, hook_id, and hook_token are required`.red); return res.status(400).json({ error: 'cid, hook_id, and hook_token are required' }); } if (!cidsInUse.has(cid)) { + console.log(`No client connected with cid: ${cid}`.red); return res.status(504).json({ error: 'no client connected' }); } hookEvent.emit(cid, {data: req.body, hook_id: hookId, hook_token: hookToken});; @@ -57,5 +69,5 @@ app.post('/:cid/:hook_id/:hook_token', (req, res) => { const PORT = process.env.SERVER_PORT || 3000; app.listen(PORT, () => { - console.log(`Server is running on http://localhost:${PORT}`); + console.log(`Server is listening on port ${PORT}`.green); }); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index e2f01c5..556cd61 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "colors": "^1.4.0", "express": "^5.1.0", "express-ws": "^5.0.2" } @@ -84,6 +85,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/colors": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", + "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==", + "license": "MIT", + "engines": { + "node": ">=0.1.90" + } + }, "node_modules/content-disposition": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-1.0.0.tgz", diff --git a/package.json b/package.json index f3f8767..c6b73c7 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "ISC", "type": "commonjs", "dependencies": { + "colors": "^1.4.0", "express": "^5.1.0", "express-ws": "^5.0.2" }