CoolFace
Apppublic

ReaperCV/upt

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
server.js157 linesDownload Raw Back to root
1const express = require("express");2const axios = require("axios");3const fs = require("fs");4const UserAgent = require("user-agents");5const puppeteer = require("puppeteer");6 7const app = express();8const port = 7860;9let activeIntervals = new Map();10 11app.use(express.json());12 13function loadUrls() {14    try {15        const data = fs.readFileSync("urls.json", "utf8");16        return JSON.parse(data).urls || [];17    } catch {18        return [];19    }20}21 22function saveUrls(urls) {23    fs.writeFileSync("urls.json", JSON.stringify({ urls }, null, 4));24}25 26function getHeaders(url) {27    return {28        "User-Agent": new UserAgent().toString(),29        "Referer": url,30        "Cache-Control": "no-cache",31        "Accept-Language": "en-US,en;q=0.9",32    };33}34 35function getRandomInterval() {36    return Math.floor(Math.random() * (7000 - 3000 + 1)) + 3000;37}38 39async function pingUrl(url) {40    // Randomly decide whether to use Puppeteer (20% chance)41    if (Math.random() < 0.2) {42        return await pingWithPuppeteer(url);43    }44 45    try {46        await axios.get(url, { headers: getHeaders(url) });47    } catch {48        // If request fails, fallback to Puppeteer49        await pingWithPuppeteer(url);50    }51}52 53async function pingWithPuppeteer(url) {54    try {55        const browser = await puppeteer.launch({ headless: "new", executablePath: "/usr/bin/google-chrome" });56        const page = await browser.newPage();57        await page.setUserAgent(new UserAgent().toString());58        await page.goto(url, { waitUntil: "domcontentloaded", timeout: 10000 });59 60        // Random interactions61        const elements = await page.$$("a, button, input, textarea, select");62        if (elements.length > 0) {63            const randomElement = elements[Math.floor(Math.random() * elements.length)];64            const tagName = await (await randomElement.getProperty("tagName")).jsonValue();65            66            if (tagName === "BUTTON" || tagName === "A") {67                await randomElement.click();68            } else if (tagName === "INPUT" || tagName === "TEXTAREA") {69                await randomElement.type("Test", { delay: 100 });70            } else {71                await page.hover(randomElement);72            }73        }74 75        await browser.close();76    } catch {}77}78 79function startPinging() {80    const urls = loadUrls();81    activeIntervals.forEach(clearInterval);82    activeIntervals.clear();83 84    urls.forEach((url) => {85        const intervalTime = getRandomInterval();86        const interval = setInterval(() => pingUrl(url), intervalTime);87        activeIntervals.set(url, interval);88    });89}90 91app.get("/", (req, res) => {92    res.send(`<h1>Ping Server Running ๐Ÿš€</h1>`);93});94 95app.get("/reload", (req, res) => {96    startPinging();97    res.json({ message: "Reloaded URLs" });98});99 100app.get("/list", (req, res) => {101    res.json({ urls: Array.from(activeIntervals.keys()) });102});103 104app.post("/add", (req, res) => {105    const url = req.body.url;106    if (!url) return res.status(400).json({ error: "Invalid URL" });107 108    const urls = loadUrls();109    if (urls.includes(url)) return res.json({ message: "URL already exists" });110 111    urls.push(url);112    saveUrls(urls);113    startPinging();114 115    res.json({ message: `Added and started pinging ${url}` });116});117 118// **Add URL via Query Parameter**119app.get("/add", (req, res) => {120    const url = req.query.url;121    if (!url) return res.status(400).json({ error: "URL query parameter missing" });122 123    const urls = loadUrls();124    if (urls.includes(url)) return res.json({ message: "URL already exists" });125 126    urls.push(url);127    saveUrls(urls);128    startPinging();129 130    res.json({ message: `Added and started pinging ${url}` });131});132 133app.post("/remove", (req, res) => {134    const url = req.body.url;135    if (!url) return res.status(400).json({ error: "Invalid URL" });136 137    let urls = loadUrls();138    if (!urls.includes(url)) return res.status(404).json({ error: "URL not found" });139 140    urls = urls.filter((u) => u !== url);141    saveUrls(urls);142 143    if (activeIntervals.has(url)) {144        clearInterval(activeIntervals.get(url));145        activeIntervals.delete(url);146    }147 148    res.json({ message: `Removed and stopped pinging ${url}` });149});150 151process.on("SIGINT", () => {152    activeIntervals.forEach(clearInterval);153    process.exit();154});155 156startPinging();157app.listen(port, () => console.log(`๐Ÿš€ Server running on port ${port}`));