114 lines
3.2 KiB
JavaScript
114 lines
3.2 KiB
JavaScript
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 }));
|
|
app.use(express.json());
|
|
app.use(express.static('static'));
|
|
expressWs(app);
|
|
|
|
const hookEvent = new EventEmitter();
|
|
|
|
cidsInUse = new Set();
|
|
|
|
// Example route
|
|
app.ws('/client', (ws, req) => {
|
|
cid = req.headers["client-id"];
|
|
if (!cid) {
|
|
ws.close(1008, "No cid provided");
|
|
return;
|
|
}
|
|
if (cidsInUse.has(cid)) {
|
|
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,
|
|
token: data.hook_token
|
|
},
|
|
data: data.data
|
|
}
|
|
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});;
|
|
res.status(200).json({ status: 'ok' });
|
|
});
|
|
|
|
app.get('/: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) {
|
|
console.log(`Invalid request: cid is required`.red);
|
|
return res.status(400).json({ error: 'cid is required' });
|
|
}
|
|
if (!cidsInUse.has(cid)) {
|
|
console.log(`No client connected with cid: ${cid}`.red);
|
|
return res.status(504).json({ error: 'no client connected' });
|
|
}
|
|
|
|
res.status(200).json({
|
|
status: 'ok',
|
|
message: 'Client is connected and ready to receive your webhooks!',
|
|
hook_id: hookId,
|
|
hook_token: hookToken
|
|
});
|
|
});
|
|
|
|
app.get('/:cid', (req, res) => {
|
|
const cid = req.params.cid;
|
|
|
|
if (!cid) {
|
|
console.log(`Invalid request: cid is required`.red);
|
|
return res.status(400).json({ error: 'cid is required' });
|
|
}
|
|
if (!cidsInUse.has(cid)) {
|
|
console.log(`No client connected with cid: ${cid}`.red);
|
|
return res.status(504).json({ error: 'no client connected' });
|
|
}
|
|
|
|
res.status(200).json({
|
|
status: 'ok',
|
|
message: 'Client is connected and ready to receive your webhooks!',
|
|
cid: cid
|
|
});
|
|
});
|
|
|
|
const PORT = process.env.SERVER_PORT || 3000;
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is listening on port ${PORT}`.green);
|
|
}); |