From 71c0bc4a0c9cc1a9901daa96074b07065c1e4a5c Mon Sep 17 00:00:00 2001 From: liudongqi Date: Thu, 5 Sep 2024 16:57:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/index.js | 60 +++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/routes/index.js b/routes/index.js index 80ad0fe..c436869 100644 --- a/routes/index.js +++ b/routes/index.js @@ -18,7 +18,6 @@ router.post('/rsCookie', async (req, res) => { let url = req.body['url']; let areaName = req.body['areaName']; let htmlStr = req.body['htmlBase64']; - let jsStr = req.body['jsBase64']; let cookie = req.body['cookieBase64']; let userAgent = req.body['userAgentBase64']; console.log(`${uuid};接收到 ${areaName} 请求:${url}`) @@ -29,17 +28,13 @@ router.post('/rsCookie', async (req, res) => { return res.status(500).send('error html') } - let cookies = await handle(url, uuid, areaName, - Buffer.from(htmlStr, 'base64').toString('utf-8'), - cookie != null && cookie !== "" ? Buffer.from(cookie, 'base64').toString('utf-8') : null, - userAgent != null && userAgent !== "" ? Buffer.from(userAgent, 'base64').toString('utf-8') : null, - ) + let cookies = await handle(url, uuid, areaName, Buffer.from(htmlStr, 'base64').toString('utf-8'), cookie != null && cookie !== "" ? Buffer.from(cookie, 'base64').toString('utf-8') : null, userAgent != null && userAgent !== "" ? Buffer.from(userAgent, 'base64').toString('utf-8') : null,) - console.log(`${uuid};返回cookie ---->`, cookies.split('; ')) + console.log(`${uuid};返回cookie ---->`, cookies) res.status(200).send(cookies); } catch (e) { - console.error(e.message) + console.error(e.stack) return res.status(500).send(e.message) } finally { console.log(`${uuid};rsCookie ${new Date() - start} ms`) @@ -59,34 +54,39 @@ async function handle(url, uuid, areaName, htmlStr, cookie, userAgent) { // 获取 origin let baseUrl = new URL(url).origin; // 初始化 jsDom 和 cookieJar - const [jsDom, cookieJar] = await jsdomFromText({ - url: url, - referrer: url, - userAgent: userAgent, - contentType: "text/html", - runScripts: "outside-only", // runScripts: 'dangerously'/'outside-only' + const [jsDom, cookieJar] = jsdomFromText({ + url: url, referrer: url, userAgent: userAgent, contentType: "text/html", runScripts: "outside-only", // runScripts: 'dangerously'/'outside-only' }) + // 加载dom + let dom = await jsDom(htmlStr); + console.log(`${uuid};html 加载长度--->`, dom.serialize().length) + window = dom.window + // ------------------------------------------------ param ---------------------------------------------------------- + // 标志判断cookie是否生成 + window[uuid] = false + + + // ------------------------------------------------ function ------------------------------------------------------- + // js执行成功后会跳转页面 会触发onbeforeunload钩子 + window.onbeforeunload = async (url) => { + console.debug(`${url} 页面回调完成`); + window[uuid] = true + } + // 设置 cookie if (cookie != null) { let cookieList = CookieStr2List(cookie); - console.log(`${uuid};cookie 加载长度--->`, cookieList, baseUrl) + console.log(`${uuid};cookie 加载长度--->`, cookieList.length, baseUrl) for (let i = 0; i < cookieList.length; i++) { cookieJar.setCookieSync(cookieList[i], baseUrl); } } - // 加载dom - let dom = await jsDom(htmlStr); - console.log(`${uuid};html 加载长度--->`, dom.serialize().length) - window = dom.window - // 标志判断cookie是否生成 - window[uuid] = false // 方案1 通过监听cookie 判断cookie是否生成 - cookieJar.delete const superSetCookie = cookieJar.setCookie; // 设置 setCookie 代理 cookieJar.setCookie = function (cookie, currentUrl, options, callback) { - console.log(`${uuid};正在设置 Cookie:`, cookie, currentUrl); + console.debug(`${uuid};正在设置 Cookie:`, cookie, currentUrl); let call = superSetCookie.call(this, cookie, currentUrl, options, callback); // 设置标志可取标志 if (cookie.includes('YqQ7a3SgknV8P')) { @@ -95,12 +95,7 @@ async function handle(url, uuid, areaName, htmlStr, cookie, userAgent) { return call; }; - // js执行成功后会跳转页面 会触发onbeforeunload钩子 - window.onbeforeunload = async (url) => { - console.debug(`${url} 页面回调完成`); - window[uuid] = true - } - // 初始化浏览器 + // ------------------------------------------------ 实例化浏览器 ----------------------------------------------------- browser(window, 'chrome'); // 加载js let js = await JsUtil.loadJs(window.document, areaName, cookie); @@ -112,16 +107,17 @@ async function handle(url, uuid, areaName, htmlStr, cookie, userAgent) { let internalVMContext = dom.getInternalVMContext(); script.runInContext(internalVMContext, {timeout: 1000}); - + // 等待cookie 被设置 for (let i = 0; i < 10; i++) { if (window[uuid]) { break; } await sleep(100) } - + // 获取cookie let resCookie = cookieJar.getCookieStringSync(baseUrl); - window.close() + // 关闭 + dom.window.close() return resCookie; }