t8ig / index.js
ulduldp's picture
Update index.js
c6d4d15 verified
const express = require("express");
const axios = require("axios");
const app = express();
// ===== CONFIG =====
const API_URL = "https://pastebin.com/raw/B0EPV9kB"; // <-- apna API endpoint
const VISIT_DELAY_MS = 5000; // 5 sec delay per URL
const INTERVAL_MS = 12 * 60 * 60 * 1000; // 12 hours
// ===== STATUS STORAGE =====
let status = {
running: false,
lastStartTime: null,
lastEndTime: null,
totalUrls: 0,
logs: []
};
// Realistic User Agents
const USER_AGENTS = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36",
"Mozilla/5.0 (Linux; Android 10) AppleWebKit/537.36 Chrome/120.0.0.0 Mobile Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 Version/17.0 Safari/605.1.15"
];
function getRandomUA() {
return USER_AGENTS[Math.floor(Math.random() * USER_AGENTS.length)];
}
async function fetchUrls() {
console.log("Started fetching urls");
try {
const res = await axios.get(API_URL, {
timeout: 15000,
headers: { "Accept": "application/json" }
});
console.log(res.data);
console.log(!Array.isArray(res.data));
if (!Array.isArray(res.data)) return [];
return res.data;
} catch (err) {
status.logs.push({
type: "error",
message: `API Fetch Failed: ${err.message}`,
time: new Date().toISOString()
});
return [];
}
}
async function visitUrl(url) {
try {
const start = Date.now();
await axios.get(url, {
timeout: 20000,
headers: {
"User-Agent": getRandomUA(),
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language": "en-US,en;q=0.9",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1"
}
});
const time = Date.now() - start;
return {
type: "success",
url,
responseTime: time,
message: "Visited successfully",
time: new Date().toISOString()
};
} catch (err) {
return {
type: "failed",
url,
message: err.message,
time: new Date().toISOString()
};
}
}
async function runJob() {
console.log('Running Runjib()');
status.running = true;
status.lastStartTime = new Date().toISOString();
status.logs = []; // reset logs
const urls = await fetchUrls();
status.totalUrls = urls.length;
if (!urls.length) {
status.logs.push({
type: "warning",
message: "No URLs received from API",
time: new Date().toISOString()
});
status.running = false;
status.lastEndTime = new Date().toISOString();
return;
}
for (const url of urls) {
const result = await visitUrl(url);
status.logs.push(result);
await new Promise(r => setTimeout(r, VISIT_DELAY_MS));
}
status.running = false;
status.lastEndTime = new Date().toISOString();
}
// ===== STATUS ROUTE =====
app.get("/url-pinger/stat", (req, res) => {
res.json({
running: status.running,
lastStartTime: status.lastStartTime,
lastEndTime: status.lastEndTime,
totalUrls: status.totalUrls,
history: status.logs
});
});
app.get("/", (req, res) => {
res.send("URL Pinger Server Running...");
});
app.listen(7860, () => {
console.log("Server running on http://localhost:7860");
});
// Run immediately
runJob();
console.log("Called Runjob()");
// Repeat every 12 hours
setInterval(runJob, INTERVAL_MS);