Change some stuff around

This commit is contained in:
Christopher Cookman 2025-06-03 13:10:33 -06:00
parent 654b195e8a
commit 898e23b871
3 changed files with 25 additions and 2 deletions

View file

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

10
package-lock.json generated
View file

@ -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",

View file

@ -11,6 +11,7 @@
"license": "ISC",
"type": "commonjs",
"dependencies": {
"colors": "^1.4.0",
"express": "^5.1.0",
"express-ws": "^5.0.2"
}