Spaces:
Sleeping
Sleeping
Update server.js
Browse files
server.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const path = require('path');
|
| 3 |
const fs = require('fs');
|
| 4 |
-
const cors = require("cors")
|
| 5 |
-
const cookieParser = require('cookie-parser')
|
| 6 |
-
const puppeteer = require('puppeteer-extra');
|
| 7 |
-
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
|
| 8 |
-
const fetch = require("node-fetch");
|
| 9 |
|
| 10 |
-
puppeteer
|
| 11 |
|
| 12 |
const app = express();
|
| 13 |
const PORT = 7860;
|
|
@@ -15,7 +10,7 @@ const PORT = 7860;
|
|
| 15 |
// 中间件
|
| 16 |
app.use(express.json({ limit: '40mb' }));
|
| 17 |
app.use(express.static('.'));
|
| 18 |
-
|
| 19 |
// CORS支持
|
| 20 |
app.use((req, res, next) => {
|
| 21 |
res.header('Access-Control-Allow-Origin', '*');
|
|
@@ -93,187 +88,199 @@ app.get('/location', async (req, res) => {
|
|
| 93 |
}
|
| 94 |
});
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
async function captureHTML(url) {
|
| 98 |
-
let browser;
|
| 99 |
try {
|
| 100 |
browser = await puppeteer.launch({
|
| 101 |
-
executablePath:
|
| 102 |
-
headless
|
| 103 |
-
args: [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
});
|
| 105 |
|
| 106 |
-
|
| 107 |
|
|
|
|
| 108 |
await page.setUserAgent(
|
| 109 |
-
|
| 110 |
-
|
| 111 |
);
|
| 112 |
|
| 113 |
await page.evaluateOnNewDocument(() => {
|
| 114 |
-
Object.defineProperty(navigator,
|
| 115 |
-
Object.defineProperty(navigator,
|
| 116 |
-
Object.defineProperty(navigator,
|
| 117 |
window.chrome = { runtime: {} };
|
| 118 |
});
|
| 119 |
|
| 120 |
-
await page.evaluateOnNewDocument(() => {
|
| 121 |
-
const metas = document.getElementsByTagName("meta");
|
| 122 |
-
for (let i = metas.length - 1; i >= 0; i--) {
|
| 123 |
-
if (metas[i].httpEquiv && metas[i].httpEquiv.toLowerCase() === "refresh") {
|
| 124 |
-
metas[i].remove();
|
| 125 |
-
}
|
| 126 |
-
}
|
| 127 |
-
});
|
| 128 |
-
|
| 129 |
await page.setRequestInterception(true);
|
| 130 |
-
page.on(
|
| 131 |
-
const
|
| 132 |
-
if (
|
| 133 |
-
|
| 134 |
-
} else {
|
| 135 |
-
request.continue();
|
| 136 |
-
}
|
| 137 |
});
|
| 138 |
|
| 139 |
-
await page.goto(url, { waitUntil:
|
| 140 |
-
|
| 141 |
-
await page.waitForNetworkIdle();
|
| 142 |
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
await browser.close();
|
| 146 |
-
return html
|
| 147 |
-
} catch (e) {
|
| 148 |
-
if (browser) await browser.close();
|
| 149 |
-
console.error(e);
|
| 150 |
-
throw e;
|
| 151 |
-
}
|
| 152 |
-
}
|
| 153 |
-
|
| 154 |
-
app.get("/api/bypass", async (req, res) => {
|
| 155 |
-
const { url } = req.query;
|
| 156 |
-
if (!url) {
|
| 157 |
-
return res.status(400).json({ error: "URL is required" });
|
| 158 |
-
}
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
let browser;
|
| 171 |
-
try {
|
| 172 |
-
browser = await puppeteer.launch({
|
| 173 |
-
executablePath: '/opt/google/chrome/chrome',
|
| 174 |
-
headless: 'new',
|
| 175 |
-
args: [
|
| 176 |
-
'--no-sandbox',
|
| 177 |
-
'--disable-setuid-sandbox',
|
| 178 |
-
'--disable-gpu',
|
| 179 |
-
'--no-zygote',
|
| 180 |
-
],
|
| 181 |
-
});
|
| 182 |
|
| 183 |
-
const
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
);
|
| 188 |
-
|
| 189 |
-
await page.evaluateOnNewDocument(() => {
|
| 190 |
-
Object.defineProperty(navigator, 'webdriver', { get: () => false });
|
| 191 |
-
Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3] });
|
| 192 |
-
Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
|
| 193 |
-
window.chrome = { runtime: {} };
|
| 194 |
-
});
|
| 195 |
|
| 196 |
-
await page.
|
| 197 |
-
const
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
metas[i].remove();
|
| 201 |
-
}
|
| 202 |
}
|
| 203 |
-
});
|
| 204 |
|
| 205 |
-
await page.
|
| 206 |
-
|
| 207 |
-
page.on("request", (req) => {
|
| 208 |
-
const reqUrl = req.url();
|
| 209 |
-
if (
|
| 210 |
-
reqUrl.includes("disable-devtool") ||
|
| 211 |
-
reqUrl.includes("shoagloumtoamir.net") ||
|
| 212 |
-
["image", "stylesheet", "font", "media"].includes(req.resourceType())
|
| 213 |
-
) {
|
| 214 |
-
req.abort();
|
| 215 |
-
} else {
|
| 216 |
-
req.continue();
|
| 217 |
-
}
|
| 218 |
-
});
|
| 219 |
|
| 220 |
-
let
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
};
|
| 228 |
-
}
|
| 229 |
-
});
|
| 230 |
|
| 231 |
-
|
|
|
|
|
|
|
| 232 |
|
| 233 |
-
|
| 234 |
-
|
|
|
|
|
|
|
| 235 |
try {
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
} catch {
|
|
|
|
|
|
|
| 244 |
}
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
}
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
token = bodyJson.token || null;
|
| 255 |
-
} catch {}
|
| 256 |
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
-
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
return {
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
| 266 |
}
|
| 267 |
-
}
|
| 268 |
|
| 269 |
app.get('/api/cinex', async (req, res) => {
|
| 270 |
const { url } = req.query;
|
| 271 |
if (!url) {
|
| 272 |
return res.status(400).json({ error: 'URL is required' });
|
| 273 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
| 277 |
});
|
| 278 |
|
| 279 |
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const path = require('path');
|
| 3 |
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
const puppeteer = require('puppeteer-core');
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const PORT = 7860;
|
|
|
|
| 10 |
// 中间件
|
| 11 |
app.use(express.json({ limit: '40mb' }));
|
| 12 |
app.use(express.static('.'));
|
| 13 |
+
|
| 14 |
// CORS支持
|
| 15 |
app.use((req, res, next) => {
|
| 16 |
res.header('Access-Control-Allow-Origin', '*');
|
|
|
|
| 88 |
}
|
| 89 |
});
|
| 90 |
|
| 91 |
+
async function captureGDrivePayloadAndCookie(url, opts = {}) {
|
| 92 |
+
const {
|
| 93 |
+
chromePath = '/opt/google/chrome/chrome',
|
| 94 |
+
headless = 'new',
|
| 95 |
+
buttonSelector = '#gdriveButton',
|
| 96 |
+
fallbackSelector = '.google-download',
|
| 97 |
+
waitForButtonMs = 60000,
|
| 98 |
+
waitForPostMs = 20000,
|
| 99 |
+
} = opts;
|
| 100 |
+
|
| 101 |
+
let browser = null;
|
| 102 |
+
let page = null;
|
| 103 |
|
|
|
|
|
|
|
| 104 |
try {
|
| 105 |
browser = await puppeteer.launch({
|
| 106 |
+
executablePath: chromePath,
|
| 107 |
+
headless,
|
| 108 |
+
args: [
|
| 109 |
+
'--no-sandbox',
|
| 110 |
+
'--disable-setuid-sandbox',
|
| 111 |
+
'--disable-web-security',
|
| 112 |
+
'--disable-gpu',
|
| 113 |
+
'--no-zygote',
|
| 114 |
+
],
|
| 115 |
});
|
| 116 |
|
| 117 |
+
page = await browser.newPage();
|
| 118 |
|
| 119 |
+
await page.setViewport({ width: 1280, height: 900 });
|
| 120 |
await page.setUserAgent(
|
| 121 |
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' +
|
| 122 |
+
'(KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'
|
| 123 |
);
|
| 124 |
|
| 125 |
await page.evaluateOnNewDocument(() => {
|
| 126 |
+
Object.defineProperty(navigator, 'webdriver', { get: () => false });
|
| 127 |
+
Object.defineProperty(navigator, 'plugins', { get: () => [1, 2, 3] });
|
| 128 |
+
Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'] });
|
| 129 |
window.chrome = { runtime: {} };
|
| 130 |
});
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
await page.setRequestInterception(true);
|
| 133 |
+
page.on('request', (req) => {
|
| 134 |
+
const rt = req.resourceType();
|
| 135 |
+
if (['image', 'stylesheet', 'font', 'media'].includes(rt)) req.abort();
|
| 136 |
+
else req.continue();
|
|
|
|
|
|
|
|
|
|
| 137 |
});
|
| 138 |
|
| 139 |
+
await page.goto(url, { waitUntil: 'load', timeout: 60000 });
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
await page.waitForFunction(() => document.readyState === 'complete', { timeout: 30000 }).catch(() => { /* ignore */ });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
let foundSelector = null;
|
| 144 |
+
try {
|
| 145 |
+
console.log('⏳ Waiting for primary selector:', buttonSelector);
|
| 146 |
+
await page.waitForSelector(buttonSelector, { visible: true, timeout: waitForButtonMs });
|
| 147 |
+
foundSelector = buttonSelector;
|
| 148 |
+
} catch (errPrimary) {
|
| 149 |
+
console.log('primary selector not found, trying fallback:', fallbackSelector);
|
| 150 |
+
try {
|
| 151 |
+
await page.waitForSelector(fallbackSelector, { visible: true, timeout: Math.max(5000, waitForButtonMs / 4) });
|
| 152 |
+
foundSelector = fallbackSelector;
|
| 153 |
+
} catch (errFallback) {
|
| 154 |
+
throw new Error(`Button not found using selectors "${buttonSelector}" or "${fallbackSelector}"`);
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
|
| 158 |
+
console.log('✅ Found button selector:', foundSelector);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
const postRequestPromise = page.waitForRequest(
|
| 161 |
+
(req) => {
|
| 162 |
+
try {
|
| 163 |
+
if (req.method() !== 'POST') return false;
|
| 164 |
+
const body = req.postData() || '';
|
| 165 |
+
if (body.includes('"gdrive"') || body.includes('gdrive') || /token\s*[:=]/i.test(body)) return true;
|
| 166 |
+
} catch (e) {
|
| 167 |
+
}
|
| 168 |
+
return false;
|
| 169 |
+
},
|
| 170 |
+
{ timeout: waitForPostMs }
|
| 171 |
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
await page.evaluate((sel) => {
|
| 174 |
+
const el = document.querySelector(sel);
|
| 175 |
+
if (el) {
|
| 176 |
+
el.scrollIntoView({ behavior: 'auto', block: 'center', inline: 'center' });
|
|
|
|
|
|
|
| 177 |
}
|
| 178 |
+
}, foundSelector);
|
| 179 |
|
| 180 |
+
await page.click(foundSelector);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
let matchedRequest;
|
| 183 |
+
try {
|
| 184 |
+
matchedRequest = await postRequestPromise;
|
| 185 |
+
} catch (err) {
|
| 186 |
+
// no post captured
|
| 187 |
+
throw new Error('Timed out waiting for POST request after clicking the button');
|
| 188 |
+
}
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
+
const headers = matchedRequest.headers() || {};
|
| 191 |
+
const cookieHeader = headers.cookie || null;
|
| 192 |
+
const rawBody = matchedRequest.postData() || '';
|
| 193 |
|
| 194 |
+
let parsedPayload = null;
|
| 195 |
+
try {
|
| 196 |
+
parsedPayload = JSON.parse(rawBody);
|
| 197 |
+
} catch (e) {
|
| 198 |
try {
|
| 199 |
+
const params = new URLSearchParams(rawBody);
|
| 200 |
+
if ([...params].length > 0) {
|
| 201 |
+
parsedPayload = {};
|
| 202 |
+
for (const [k, v] of params.entries()) parsedPayload[k] = v;
|
| 203 |
+
} else {
|
| 204 |
+
parsedPayload = { raw: rawBody };
|
| 205 |
+
}
|
| 206 |
+
} catch (e2) {
|
| 207 |
+
parsedPayload = { raw: rawBody };
|
| 208 |
+
}
|
| 209 |
}
|
| 210 |
|
| 211 |
+
await page.close();
|
| 212 |
+
await browser.close();
|
| 213 |
+
|
| 214 |
+
return {
|
| 215 |
+
success: true,
|
| 216 |
+
cookies: cookieHeader,
|
| 217 |
+
token: parsedPayload.token,
|
| 218 |
+
requestUrl: matchedRequest.url(),
|
| 219 |
+
};
|
| 220 |
+
} catch (err) {
|
| 221 |
+
console.error('captureGDrivePayloadAndCookie error:', err.message || err);
|
| 222 |
+
if (page) {
|
| 223 |
+
try { await page.close(); } catch {}
|
| 224 |
+
}
|
| 225 |
+
if (browser) {
|
| 226 |
+
try { await browser.close(); } catch {}
|
| 227 |
}
|
| 228 |
+
return { success: false, error: String(err.message || err) };
|
| 229 |
+
}
|
| 230 |
+
}
|
| 231 |
|
| 232 |
+
app.get('/api/bypass', async (req, res) => {
|
| 233 |
+
const { url } = req.query;
|
| 234 |
+
if (!url) return res.status(400).json({ error: 'URL is required' });
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
try {
|
| 237 |
+
const result = await captureGDrivePayloadAndCookie(url, {
|
| 238 |
+
chromePath: '/opt/google/chrome/chrome',
|
| 239 |
+
headless: 'new',
|
| 240 |
+
buttonSelector: '#gdriveButton',
|
| 241 |
+
fallbackSelector: '.google-download',
|
| 242 |
+
waitForButtonMs: 60000,
|
| 243 |
+
waitForPostMs: 20000,
|
| 244 |
+
});
|
| 245 |
|
| 246 |
+
if (!result.success) {
|
| 247 |
+
return res.status(500).json({ error: result.error || 'failed' });
|
| 248 |
+
}
|
| 249 |
|
| 250 |
+
return res.json({
|
| 251 |
+
cookie: result.cookieHeader,
|
| 252 |
+
payload: result.payload,
|
| 253 |
+
requestUrl: result.requestUrl,
|
| 254 |
+
});
|
| 255 |
+
} catch (err) {
|
| 256 |
+
console.error('/api/bypass error:', err);
|
| 257 |
+
return res.status(500).json({ error: 'Internal error' });
|
| 258 |
}
|
| 259 |
+
});
|
| 260 |
|
| 261 |
app.get('/api/cinex', async (req, res) => {
|
| 262 |
const { url } = req.query;
|
| 263 |
if (!url) {
|
| 264 |
return res.status(400).json({ error: 'URL is required' });
|
| 265 |
}
|
| 266 |
+
const result = await captureGDrivePayloadAndCookie(url, {
|
| 267 |
+
chromePath: '/opt/google/chrome/chrome',
|
| 268 |
+
headless: 'new',
|
| 269 |
+
buttonSelector: '#gdriveButton',
|
| 270 |
+
fallbackSelector: '.google-download',
|
| 271 |
+
waitForButtonMs: 60000,
|
| 272 |
+
waitForPostMs: 20000,
|
| 273 |
+
});
|
| 274 |
+
|
| 275 |
+
if (!result.success) {
|
| 276 |
+
return res.status(500).json({ error: result.error || 'failed' });
|
| 277 |
+
}
|
| 278 |
|
| 279 |
+
return res.json({
|
| 280 |
+
cookie: result.cookieHeader,
|
| 281 |
+
payload: result.payload,
|
| 282 |
+
requestUrl: result.requestUrl,
|
| 283 |
+
});
|
| 284 |
});
|
| 285 |
|
| 286 |
|