Spaces:
Sleeping
Sleeping
| const express = require('express'); | |
| const path = require('path'); | |
| const fs = require('fs'); | |
| const cors = require("cors") | |
| const cookieParser = require('cookie-parser') | |
| const puppeteer = require('puppeteer-extra'); | |
| const StealthPlugin = require('puppeteer-extra-plugin-stealth'); | |
| const fetch = require("node-fetch"); | |
| puppeteer.use(StealthPlugin()); | |
| const app = express(); | |
| const PORT = 7860; | |
| // 中间件 | |
| app.use(express.json({ limit: '40mb' })); | |
| app.use(express.static('.')); | |
| app.use(cors()); | |
| // CORS支持 | |
| app.use((req, res, next) => { | |
| res.header('Access-Control-Allow-Origin', '*'); | |
| res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); | |
| res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization'); | |
| if (req.method === 'OPTIONS') { | |
| res.sendStatus(200); | |
| } else { | |
| next(); | |
| } | |
| }); | |
| async function redr(url, method = 'GET', postData = null) { | |
| const browser = await puppeteer.launch({ | |
| executablePath: "/opt/google/chrome/chrome", | |
| headless: true, | |
| }); | |
| const page = await browser.newPage(); | |
| await page.setExtraHTTPHeaders({ | |
| 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', | |
| 'accept-encoding': 'gzip, deflate, br, zstd', | |
| 'accept-language': 'en-US,en;q=0.9', | |
| 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Mobile Safari/537.36' | |
| }); | |
| try { | |
| let response; | |
| if (method === 'POST' && postData) { | |
| response = await page.goto(url, { | |
| waitUntil: 'load', | |
| timeout: 0, | |
| method: 'POST', | |
| postData: JSON.stringify(postData), | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| } | |
| }); | |
| } else { | |
| response = await page.goto(url, { waitUntil: 'load', timeout: 10000 }); | |
| } | |
| const finalUrl = page.url(); | |
| console.log('Page URL after redirect (if any):', finalUrl); | |
| const content = await page.content(); | |
| return content; | |
| } catch (error) { | |
| console.error('Error scraping page:', error); | |
| return `Error: ${error.message}`; | |
| } finally { | |
| await browser.close(); | |
| } | |
| } | |
| app.get('/location', async (req, res) => { | |
| const url = req.query.url; | |
| const method = req.query.method || 'GET'; | |
| if (!url) { | |
| return res.status(400).send('URL query parameter is required'); | |
| } | |
| try { | |
| const pageContent = await redr(url, method); | |
| res.send(pageContent); | |
| } catch (error) { | |
| res.status(500).send('Failed to scrape the page'); | |
| } | |
| }); | |
| async function captureHTML(url) { | |
| let browser; | |
| try { | |
| browser = await puppeteer.launch({ | |
| executablePath: "/opt/google/chrome/chrome", // adjust if needed | |
| headless: "new", | |
| args: ['--disable-gpu', '--no-sandbox', '--disable-setuid-sandbox', '--ignoreHTTPSErrors'], | |
| }); | |
| const page = await browser.newPage(); | |
| await page.setUserAgent( | |
| "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + | |
| "(KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36" | |
| ); | |
| await page.evaluateOnNewDocument(() => { | |
| Object.defineProperty(navigator, "webdriver", { get: () => false }); | |
| Object.defineProperty(navigator, "plugins", { get: () => [1, 2, 3] }); | |
| Object.defineProperty(navigator, "languages", { get: () => ["en-US", "en"] }); | |
| window.chrome = { runtime: {} }; | |
| }); | |
| await page.evaluateOnNewDocument(() => { | |
| const metas = document.getElementsByTagName("meta"); | |
| for (let i = metas.length - 1; i >= 0; i--) { | |
| if (metas[i].httpEquiv && metas[i].httpEquiv.toLowerCase() === "refresh") { | |
| metas[i].remove(); | |
| } | |
| } | |
| }); | |
| await page.setRequestInterception(true); | |
| page.on("request", (request) => { | |
| const reqUrl = request.url(); | |
| if (reqUrl.includes("disable-devtool") || reqUrl.includes("shoagloumtoamir.net")) { | |
| request.abort(); | |
| } else { | |
| request.continue(); | |
| } | |
| }); | |
| await page.goto(url, { waitUntil: "load", timeout: 60000 }); | |
| await page.waitForNetworkIdle(); | |
| const html = await page.content(); | |
| await browser.close(); | |
| return html | |
| } catch (e) { | |
| if (browser) await browser.close(); | |
| console.error(e); | |
| throw e; | |
| } | |
| } | |
| app.get("/api/bypass", async (req, res) => { | |
| const { url } = req.query; | |
| if (!url) { | |
| return res.status(400).json({ error: "URL is required" }); | |
| } | |
| try { | |
| const html = await captureHTML(url); | |
| res.setHeader("Content-Type", "text/html; charset=utf-8"); | |
| res.send(html); | |
| } catch (err) { | |
| res.status(500).json({ error: "Failed to capture HTML" }); | |
| } | |
| }); | |
| async function capturetokenandcookies(url) { | |
| let browser; | |
| try { | |
| browser = await puppeteer.launch({ | |
| executablePath: '/opt/google/chrome/chrome', | |
| headless: 'new', | |
| args: [ | |
| '--no-sandbox', | |
| '--disable-setuid-sandbox', | |
| '--disable-gpu', | |
| '--no-zygote', | |
| ], | |
| }); | |
| const page = await browser.newPage(); | |
| await page.setViewport({ width: 1280, height: 800 }); | |
| await page.setUserAgent( | |
| 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' | |
| ); | |
| await page.evaluateOnNewDocument(() => { | |
| Object.defineProperty(navigator, 'webdriver', { get: () => false }); | |
| Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3] }); | |
| Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] }); | |
| window.chrome = { runtime: {} }; | |
| }); | |
| await page.evaluateOnNewDocument(() => { | |
| const metas = document.getElementsByTagName('meta'); | |
| for (let i = metas.length - 1; i >= 0; i--) { | |
| if (metas[i].httpEquiv && metas[i].httpEquiv.toLowerCase() === 'refresh') { | |
| metas[i].remove(); | |
| } | |
| } | |
| }); | |
| await page.setRequestInterception(true); | |
| page.on("request", (req) => { | |
| const reqUrl = req.url(); | |
| if ( | |
| reqUrl.includes("disable-devtool") || | |
| reqUrl.includes("shoagloumtoamir.net") || | |
| ["image", "stylesheet", "font", "media"].includes(req.resourceType()) | |
| ) { | |
| req.abort(); | |
| } else { | |
| req.continue(); | |
| } | |
| }); | |
| let capturedRequest = null; | |
| page.on('request', (request) => { | |
| if (request.method() === 'POST' && !capturedRequest) { | |
| capturedRequest = { | |
| url: request.url(), | |
| headers: request.headers(), | |
| body: request.postData(), | |
| }; | |
| } | |
| }); | |
| await page.goto(url, { waitUntil: 'domcontentloaded', timeout: 60000 }); | |
| const buttons = ['#gdriveButton']; | |
| for (const selector of buttons) { | |
| try { | |
| await page.waitForSelector(selector, { timeout: 8000 }); | |
| await Promise.all([ | |
| page.waitForNetworkIdle(), | |
| page.click(selector), | |
| ]); | |
| await page.waitForTimeout(3000); | |
| if (capturedRequest) break; | |
| } catch {} | |
| } | |
| if (!capturedRequest) { | |
| await page.close(); | |
| process.exit() | |
| } | |
| let token = null; | |
| try { | |
| const bodyJson = JSON.parse(capturedRequest.body); | |
| token = bodyJson.token || null; | |
| } catch {} | |
| const cookies = capturedRequest.headers.cookie || null; | |
| await page.close(); | |
| return { token, cookies }; | |
| } catch (e) { | |
| if (browser) await browser.close(); | |
| console.log(e) | |
| process.exit(1) | |
| } | |
| } | |
| app.get('/api/cinex', async (req, res) => { | |
| const { url } = req.query; | |
| if (!url) { | |
| return res.status(400).json({ error: 'URL is required' }); | |
| } | |
| const result = await capturetokenandcookies(url); | |
| res.json(result); | |
| }); | |
| // 健康检查端点 | |
| app.get('/', (req, res) => { | |
| res.json({ status: 'hii', timestamp: new Date().toISOString() }); | |
| }); | |
| // 启动服务器 | |
| app.listen(PORT, () => { | |
| console.log(`Puppeteer服务器运行在 http://localhost:${PORT}`); | |
| console.log('API端点:'); | |
| console.log(' POST /api/generate-pdf - 生成PDF'); | |
| console.log(' POST /api/generate-images - 生成图片'); | |
| console.log(' GET /api/health - 健康检查'); | |
| }); | |
| module.exports = app; |