108 lines
3.3 KiB
JavaScript
108 lines
3.3 KiB
JavaScript
require("dotenv").config();
|
|
const Discord = require("discord.js");
|
|
const colors = require("colors");
|
|
const fs = require("fs");
|
|
const ws = require("ws");
|
|
const wsServer = process.env.WS_SERVER || "wss://hookproxy.kcadev.org/client";
|
|
|
|
if (!process.env.CLIENT_ID) {
|
|
const randomString = [...Array(64)]
|
|
.map(() => Math.random().toString(36)[2])
|
|
.join("");
|
|
const envContent = `CLIENT_ID=${randomString}\n`;
|
|
|
|
fs.writeFileSync(".env", envContent);
|
|
console.log("Generated .env file with a random CLIENT_ID".green);
|
|
|
|
// Reload environment variables
|
|
require("dotenv").config();
|
|
}
|
|
|
|
const wsMain = () => {
|
|
const wsClient = new ws(wsServer, {
|
|
headers: {
|
|
"User-Agent": "DiscordHookProxyClient v1.0",
|
|
"Client-ID": process.env.CLIENT_ID,
|
|
}
|
|
});
|
|
let pingTimer;
|
|
wsClient.on("open", () => {
|
|
console.log("WebSocket connection established".green);
|
|
// Send periodic pings to keep the connection alive
|
|
pingTimer = setInterval(() => {
|
|
if (wsClient.readyState === ws.OPEN) {
|
|
wsClient.send("ping");
|
|
}
|
|
}, 15000 + (Math.random() * 2 - 1).toFixed(2) * 1000); // Send a ping every 30 seconds
|
|
});
|
|
|
|
wsClient.on("message", (rawdata) => {
|
|
let message
|
|
try {
|
|
message = JSON.parse(rawdata);
|
|
|
|
if (!message || !message.creds?.id || !message.creds?.token || !message.data) {
|
|
console.error("Invalid message format received".red);
|
|
console.log("Expected format: { creds: { id: 'webhook_id', token: 'webhook_token' }, data: 'message_content' }".yellow);
|
|
console.log("Received message:", rawdata.yellow);
|
|
return;
|
|
}
|
|
|
|
const webhook = new Discord.WebhookClient({
|
|
id: message.creds.id,
|
|
token: message.creds.token,
|
|
});
|
|
|
|
webhook.send(message.data).then(() => {
|
|
console.log(`Message sent to webhook ${message.creds.id}`.green);
|
|
}).catch((error) => {
|
|
console.error(`Error sending message to webhook ${message.creds.id}:`, error.message.red);
|
|
});
|
|
} catch (error) {
|
|
console.error("Error parsing WebSocket message:", error.message.red);
|
|
return;
|
|
}
|
|
});
|
|
|
|
|
|
wsClient.on("error", (error) => {
|
|
console.error("WebSocket error:", error.message.red);
|
|
});
|
|
|
|
wsClient.on("close", (code, reason) => {
|
|
if (pingTimer) {
|
|
clearInterval(pingTimer);
|
|
}
|
|
console.log(`WebSocket connection closed (${code} ${reason}). Reconnecting...`.yellow);
|
|
setTimeout(wsMain, 5000); // Reconnect after 5 seconds
|
|
});
|
|
};
|
|
|
|
|
|
// Start the WebSocket client
|
|
wsMain();
|
|
|
|
console.log("Discord Hook Proxy Client started".blue);
|
|
console.log("Listening for messages...".blue);
|
|
console.log(`To send a webhook message, send a Discord webhook message to https://hookproxy.kcadev.org/${process.env.CLIENT_ID}/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN`.blue);
|
|
console.log(`NEVER SHARE YOUR CLIENT ID OR WEBHOOK TOKENS WITH ANYONE!`.red.bold);
|
|
|
|
console.log(`This program must be left running to receive messages.`.yellow);
|
|
|
|
console.log(`Press Ctrl+C to exit.`.yellow);
|
|
|
|
// Handle graceful shutdown
|
|
process.on("SIGINT", () => {
|
|
console.log("\nShutting down gracefully...".yellow);
|
|
process.exit(0);
|
|
});
|
|
|
|
process.on("uncaughtException", (error) => {
|
|
console.error("Uncaught Exception:", error.message.red);
|
|
});
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
console.error("Unhandled Rejection at:", promise, "reason:", reason.message.red);
|
|
});
|
|
process.on("warning", (warning) => {
|
|
console.warn("Warning:", warning.message.yellow);
|
|
}); |