Update server.js
Browse files
server.js
CHANGED
|
@@ -1,68 +1,148 @@
|
|
| 1 |
const express = require("express");
|
| 2 |
-
const
|
|
|
|
| 3 |
const cheerio = require("cheerio");
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
const app = express();
|
| 6 |
const PORT = 7860;
|
| 7 |
|
| 8 |
-
//
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
const timeAgo = $(el).find("p.p-0.m-0.small").text().trim();
|
| 31 |
-
const link = $(el).find("a.btn.btn-primary").attr("href");
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
});
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
}
|
| 44 |
|
| 45 |
-
// Scraper function for messages
|
| 46 |
async function scrapeMessages(number) {
|
| 47 |
-
const
|
| 48 |
-
const
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
// Endpoint: UK numbers
|
|
@@ -70,7 +150,6 @@ app.get(["/uk", "/uk/:page"], async (req, res) => {
|
|
| 70 |
try {
|
| 71 |
const page = parseInt(req.params.page) || 1;
|
| 72 |
const numbers = await scrapeUK(page);
|
| 73 |
-
|
| 74 |
res.json({
|
| 75 |
country: "United Kingdom",
|
| 76 |
page,
|
|
@@ -88,7 +167,6 @@ app.get("/msg/:number", async (req, res) => {
|
|
| 88 |
try {
|
| 89 |
const number = req.params.number;
|
| 90 |
const messages = await scrapeMessages(number);
|
| 91 |
-
|
| 92 |
res.json({
|
| 93 |
number,
|
| 94 |
count: messages.length,
|
|
@@ -100,7 +178,25 @@ app.get("/msg/:number", async (req, res) => {
|
|
| 100 |
}
|
| 101 |
});
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
// Start server
|
| 104 |
app.listen(PORT, () => {
|
| 105 |
console.log(`Server running on http://localhost:${PORT}`);
|
|
|
|
| 106 |
});
|
|
|
|
| 1 |
const express = require("express");
|
| 2 |
+
const puppeteer = require("puppeteer-extra");
|
| 3 |
+
const StealthPlugin = require("puppeteer-extra-plugin-stealth");
|
| 4 |
const cheerio = require("cheerio");
|
| 5 |
|
| 6 |
+
// Add stealth plugin
|
| 7 |
+
puppeteer.use(StealthPlugin());
|
| 8 |
+
|
| 9 |
const app = express();
|
| 10 |
const PORT = 7860;
|
| 11 |
|
| 12 |
+
// Browser instance management
|
| 13 |
+
let browser = null;
|
| 14 |
+
|
| 15 |
+
async function getBrowser() {
|
| 16 |
+
if (!browser) {
|
| 17 |
+
browser = await puppeteer.launch({
|
| 18 |
+
headless: "new", // Use new headless mode
|
| 19 |
+
args: [
|
| 20 |
+
'--no-sandbox',
|
| 21 |
+
'--disable-setuid-sandbox',
|
| 22 |
+
'--disable-dev-shm-usage',
|
| 23 |
+
'--disable-accelerated-2d-canvas',
|
| 24 |
+
'--no-first-run',
|
| 25 |
+
'--no-zygote',
|
| 26 |
+
'--disable-gpu',
|
| 27 |
+
'--disable-web-security',
|
| 28 |
+
'--disable-features=VizDisplayCompositor'
|
| 29 |
+
]
|
| 30 |
+
});
|
| 31 |
+
}
|
| 32 |
+
return browser;
|
| 33 |
+
}
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
// Enhanced page setup for maximum stealth
|
| 36 |
+
async function setupPage(browser) {
|
| 37 |
+
const page = await browser.newPage();
|
| 38 |
+
|
| 39 |
+
// Set viewport to common resolution
|
| 40 |
+
await page.setViewport({ width: 1366, height: 768 });
|
| 41 |
+
|
| 42 |
+
// Set realistic user agent
|
| 43 |
+
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
|
| 44 |
+
|
| 45 |
+
// Set additional headers
|
| 46 |
+
await page.setExtraHTTPHeaders({
|
| 47 |
+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
| 48 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 49 |
+
'Accept-Encoding': 'gzip, deflate, br',
|
| 50 |
+
'DNT': '1',
|
| 51 |
+
'Connection': 'keep-alive',
|
| 52 |
+
'Upgrade-Insecure-Requests': '1',
|
| 53 |
});
|
| 54 |
+
|
| 55 |
+
return page;
|
| 56 |
+
}
|
| 57 |
|
| 58 |
+
// Scraper function for numbers list with stealth
|
| 59 |
+
async function scrapeUK(pageNum = 1) {
|
| 60 |
+
const browser = await getBrowser();
|
| 61 |
+
const page = await setupPage(browser);
|
| 62 |
+
|
| 63 |
+
try {
|
| 64 |
+
const url = pageNum === 1
|
| 65 |
+
? "https://getsms.cc/temporary-phone-numbers/UK"
|
| 66 |
+
: `https://getsms.cc/temporary-phone-numbers/UK/${pageNum}`;
|
| 67 |
+
|
| 68 |
+
// Navigate with realistic options
|
| 69 |
+
await page.goto(url, {
|
| 70 |
+
waitUntil: 'networkidle2',
|
| 71 |
+
timeout: 30000
|
| 72 |
+
});
|
| 73 |
+
|
| 74 |
+
// Add random delay to mimic human behavior
|
| 75 |
+
await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000));
|
| 76 |
+
|
| 77 |
+
// Get page content
|
| 78 |
+
const content = await page.content();
|
| 79 |
+
const $ = cheerio.load(content);
|
| 80 |
+
|
| 81 |
+
const results = [];
|
| 82 |
+
$(".card").each((i, el) => {
|
| 83 |
+
const number = $(el).find("p.p-0.m-0.font-weight-bold").text().trim();
|
| 84 |
+
const timeAgo = $(el).find("p.p-0.m-0.small").text().trim();
|
| 85 |
+
const link = $(el).find("a.btn.btn-primary").attr("href");
|
| 86 |
+
|
| 87 |
+
if (number && timeAgo && link) {
|
| 88 |
+
results.push({
|
| 89 |
+
number,
|
| 90 |
+
timeAgo,
|
| 91 |
+
link: `https://getsms.cc${link}`,
|
| 92 |
+
});
|
| 93 |
+
}
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
return results;
|
| 97 |
+
} catch (error) {
|
| 98 |
+
console.error('Error scraping UK numbers:', error);
|
| 99 |
+
throw error;
|
| 100 |
+
} finally {
|
| 101 |
+
await page.close();
|
| 102 |
+
}
|
| 103 |
}
|
| 104 |
|
| 105 |
+
// Scraper function for messages with stealth
|
| 106 |
async function scrapeMessages(number) {
|
| 107 |
+
const browser = await getBrowser();
|
| 108 |
+
const page = await setupPage(browser);
|
| 109 |
+
|
| 110 |
+
try {
|
| 111 |
+
const url = `https://getsms.cc/info/${number}`;
|
| 112 |
+
|
| 113 |
+
// Navigate with realistic options
|
| 114 |
+
await page.goto(url, {
|
| 115 |
+
waitUntil: 'networkidle2',
|
| 116 |
+
timeout: 30000
|
| 117 |
+
});
|
| 118 |
+
|
| 119 |
+
// Add random delay
|
| 120 |
+
await new Promise(resolve => setTimeout(resolve, Math.random() * 2000 + 1000));
|
| 121 |
+
|
| 122 |
+
// Get page content
|
| 123 |
+
const content = await page.content();
|
| 124 |
+
const $ = cheerio.load(content);
|
| 125 |
+
|
| 126 |
+
const messages = [];
|
| 127 |
+
$(".direct-chat-msg").each((i, el) => {
|
| 128 |
+
const from = $(el).find(".direct-chat-name").text().trim();
|
| 129 |
+
const timeAgo = $(el).find("time").text().trim();
|
| 130 |
+
const text = $(el).find(".direct-chat-text").text().trim();
|
| 131 |
+
|
| 132 |
+
// Skip ads/empty messages
|
| 133 |
+
if (from && text) {
|
| 134 |
+
messages.push({ from, timeAgo, text });
|
| 135 |
+
}
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
// Return latest 3 (newest first)
|
| 139 |
+
return messages.slice(-3).reverse();
|
| 140 |
+
} catch (error) {
|
| 141 |
+
console.error('Error scraping messages:', error);
|
| 142 |
+
throw error;
|
| 143 |
+
} finally {
|
| 144 |
+
await page.close();
|
| 145 |
+
}
|
| 146 |
}
|
| 147 |
|
| 148 |
// Endpoint: UK numbers
|
|
|
|
| 150 |
try {
|
| 151 |
const page = parseInt(req.params.page) || 1;
|
| 152 |
const numbers = await scrapeUK(page);
|
|
|
|
| 153 |
res.json({
|
| 154 |
country: "United Kingdom",
|
| 155 |
page,
|
|
|
|
| 167 |
try {
|
| 168 |
const number = req.params.number;
|
| 169 |
const messages = await scrapeMessages(number);
|
|
|
|
| 170 |
res.json({
|
| 171 |
number,
|
| 172 |
count: messages.length,
|
|
|
|
| 178 |
}
|
| 179 |
});
|
| 180 |
|
| 181 |
+
// Graceful shutdown
|
| 182 |
+
process.on('SIGINT', async () => {
|
| 183 |
+
console.log('Shutting down gracefully...');
|
| 184 |
+
if (browser) {
|
| 185 |
+
await browser.close();
|
| 186 |
+
}
|
| 187 |
+
process.exit(0);
|
| 188 |
+
});
|
| 189 |
+
|
| 190 |
+
process.on('SIGTERM', async () => {
|
| 191 |
+
console.log('Shutting down gracefully...');
|
| 192 |
+
if (browser) {
|
| 193 |
+
await browser.close();
|
| 194 |
+
}
|
| 195 |
+
process.exit(0);
|
| 196 |
+
});
|
| 197 |
+
|
| 198 |
// Start server
|
| 199 |
app.listen(PORT, () => {
|
| 200 |
console.log(`Server running on http://localhost:${PORT}`);
|
| 201 |
+
console.log('Using Puppeteer with stealth mode to avoid detection');
|
| 202 |
});
|