diff --git a/routes/index.js b/routes/index.js index 8d88ef3..c24d585 100644 --- a/routes/index.js +++ b/routes/index.js @@ -21,7 +21,7 @@ router.post('/rsCookie', async (req, res) => { let cookie = req.body['cookieBase64']; let userAgent = req.body['userAgentBase64']; console.log(`${uuid};接收到 ${areaName} 请求:${url}`) - fs.writeFileSync(`./back/${uuid}.url`, `${url}\n ${userAgent}\n ${cookie} \n`); + // fs.writeFileSync(`./back/${uuid}.url`, `${url}\n ${userAgent}\n ${cookie} \n`); if (url == null || url === '') { return res.status(500).send('error url') } @@ -105,22 +105,29 @@ async function handle(url, htmlStr, jsText, cookie, userAgent, uuid, loadHtmlJs) if (cookie != null) { let cookieList = CookieStr2List(cookie); console.log(`${uuid};cookie 加载长度--->`, cookieList, baseUrl) - fs.writeFileSync(`./back/${uuid}.cookie`, cookie) + // fs.writeFileSync(`./back/${uuid}.cookie`, cookie) for (let i = 0; i < cookieList.length; i++) { cookieJar.setCookieSync(cookieList[i], baseUrl); } } + // 设置cookie的回掉 + const superSetCookie = cookieJar.setCookie; + cookieJar.setCookie = function (cookie, currentUrl, options, callback) { + console.log(`${uuid};正在设置 Cookie:`, cookie, currentUrl); + // store.set(uuid, cookie) + return superSetCookie.call(this, cookie, currentUrl, options, callback); + }; // 加载dom let dom = await jsDom(htmlStr); // console.log('html 加载长度--->', dom.serialize()) console.log(`${uuid};html 加载长度--->`, dom.serialize().length) - fs.writeFileSync(`./back/${uuid}.html`, dom.serialize()) + // fs.writeFileSync(`./back/${uuid}.html`, dom.serialize()) window = dom.window - // js执行成功后的瑞树会跳转页面 会触发onbeforeunload钩子 + // js执行成功后会跳转页面 会触发onbeforeunload钩子 window.onbeforeunload = async (url) => { const cookies = cookieJar.getCookieStringSync(baseUrl); - // console.debug(`${url} 生成cookie:`, cookies); + console.debug(`${url} 生成cookie:`, cookies); store.set(uuid, cookies) // window.close(); } @@ -134,33 +141,24 @@ async function handle(url, htmlStr, jsText, cookie, userAgent, uuid, loadHtmlJs) js = jsText; } console.log(`${uuid};js 加载长度--->`, js.length) - fs.writeFileSync(`./back/${uuid}.js`, js) + // fs.writeFileSync(`./back/${uuid}.js`, js) // 执行 js let script = new Script(js); let internalVMContext = dom.getInternalVMContext(); script.runInContext(internalVMContext); // 等待 onbeforeunload 钩子触发后的回掉 - let timeout = -1; - let reTry = -1; - if (process.env.NODE_ENV === 'prod') { - timeout = 100 - reTry = 10 - } else { - timeout = 100 - reTry = 10 - } - let val = await store.waitGetAndDelete(uuid, timeout, reTry) + // let val = await store.waitGetAndDelete(uuid, 100, 10) // bug 关闭后 部分参数是需要永久保存在内存中的 比如 cookie 实例 下次调用会报错 // internalVMContext.close() // window.close() // dom = null - if (val != null) { - return val; - } - console.log(`${uuid} 执行超时`) + // if (val != null) { + // return val; + // } + // console.log(`${uuid} 执行超时`) return cookieJar.getCookieStringSync(baseUrl); // throw new Error(`${uuid}执行超时`) } diff --git a/test/rs6/yunnan.js b/test/rs6/yunnan.js index f229e88..386036b 100644 --- a/test/rs6/yunnan.js +++ b/test/rs6/yunnan.js @@ -4,32 +4,49 @@ const {jsdomFromText, browser, jsdomFromUrl} = require("sdenv"); const {Script} = require("vm"); let fs = require('fs'); const express = require("express"); -const cheerio = require('cheerio'); +// const cheerio = require('cheerio'); -const app = express(); +// const app = express(); var baseUrl = "https://app.yunnan.chinatax.gov.cn" let url = `https://app.yunnan.chinatax.gov.cn/xxmh/html/dfts/index_frame.html`; -async function extracted(cookieJar) { - const [jsdomer] = await jsdomFromUrl({ - proxy: 'http://127.0.0.1:8080', - strictSSL: false, - userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', - }, cookieJar); - const dom = await jsdomer(url); - - console.log(dom.serialize()) +// async function extracted(cookieJar) { +// const [jsdomer] = await jsdomFromUrl({ +// // proxy: 'http://127.0.0.1:8080', +// strictSSL: false, +// userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36', +// }, cookieJar); +// const dom = await jsdomer(url); +// +// console.log(dom.serialize()) +// } +function loadJs(window, jsText) { + // 加载js + let js = ''; + // 加载 页面上的js + const allScript = window.document.querySelectorAll('script[r="m"]'); + for (let i = 0; i < allScript.length; i++) { + const script = allScript[i]; + let attr = script.textContent; + if (attr) { + js += attr + } else { + js += jsText + } + js += ";\n" + } + return js; } -function loadPages(urlBase64, jsBase64, htmlBase64, cookieBase64, userAgentBase64, ts) { +async function loadPages(urlBase64, jsBase64, htmlBase64, cookieBase64, userAgentBase64, ts) { const htmlText = htmlBase64; const jsText = jsBase64; const [jsDom, cookieJar] = jsdomFromText({ - proxy: 'http://127.0.0.1:8080', + // proxy: 'http://127.0.0.1:8080', url: url, referrer: url, contentType: "text/html", @@ -41,11 +58,11 @@ function loadPages(urlBase64, jsBase64, htmlBase64, cookieBase64, userAgentBase6 window = dom.window // 加载 window.$_ts - let $ = cheerio.load(htmlText); - let nextAll = $('script[r=m]'); + // let $ = cheerio.load(htmlText); + // let nextAll = $('script[r=m]'); - let trendJs = $('script').first().html(); - let js = trendJs + ";\n" + jsText + // let trendJs = $('script').first().html(); + // let js = trendJs + ";\n" + jsText // nextAll.each((index, element) => { // let attr = $(element).html(); // if (attr) { @@ -59,32 +76,31 @@ function loadPages(urlBase64, jsBase64, htmlBase64, cookieBase64, userAgentBase6 cookieJar.setCookieSync(cookieBase64, baseUrl); - window.onbeforeunload = async (url) => { const cookies = cookieJar.getCookieStringSync(baseUrl); console.debug(`${url} 生成cookie:`, cookies); - let fff = await fetch("https://app.yunnan.chinatax.gov.cn/xxmh/html/dfts/index_frame.html", { - "headers": { - "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-language": "zh-CN,zh;q=0.9", - "cache-control": "no-cache", - "pragma": "no-cache", - "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"", - "sec-ch-ua-mobile": "?0", - "sec-ch-ua-platform": "\"Linux\"", - "sec-fetch-dest": "document", - "sec-fetch-mode": "navigate", - "sec-fetch-site": "same-origin", - "upgrade-insecure-requests": "1", - "cookie": cookies, - "Referer": "https://app.yunnan.chinatax.gov.cn/xxmh/html/dfts/index_frame.html", - }, "body": null, "method": "GET" - }); - - console.log(fff.status, fff.statusText) + // let fff = await fetch("https://app.yunnan.chinatax.gov.cn/xxmh/html/dfts/index_frame.html", { + // "headers": { + // "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-language": "zh-CN,zh;q=0.9", + // "cache-control": "no-cache", + // "pragma": "no-cache", + // "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"", + // "sec-ch-ua-mobile": "?0", + // "sec-ch-ua-platform": "\"Linux\"", + // "sec-fetch-dest": "document", + // "sec-fetch-mode": "navigate", + // "sec-fetch-site": "same-origin", + // "upgrade-insecure-requests": "1", + // "cookie": cookies, + // "Referer": "https://app.yunnan.chinatax.gov.cn/xxmh/html/dfts/index_frame.html", + // }, "body": null, "method": "GET" + // }); + // + // console.log(fff.status, fff.statusText) window.close(); @@ -92,11 +108,34 @@ function loadPages(urlBase64, jsBase64, htmlBase64, cookieBase64, userAgentBase6 browser(dom.window, 'chrome'); + let js = loadJs(window, jsText); let script = new Script(js); let internalVMContext = dom.getInternalVMContext(); // 执行 js script.runInContext(internalVMContext); + + const cookies = cookieJar.getCookieStringSync(baseUrl); + + let fff = await fetch("https://app.yunnan.chinatax.gov.cn/xxmh/html/dfts/index_frame.html", { + "headers": { + "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-language": "zh-CN,zh;q=0.9", + "cache-control": "no-cache", + "pragma": "no-cache", + "sec-ch-ua": "\"Not/A)Brand\";v=\"8\", \"Chromium\";v=\"126\", \"Google Chrome\";v=\"126\"", + "sec-ch-ua-mobile": "?0", + "sec-ch-ua-platform": "\"Linux\"", + "sec-fetch-dest": "document", + "sec-fetch-mode": "navigate", + "sec-fetch-site": "same-origin", + "upgrade-insecure-requests": "1", + "cookie": cookies, + "Referer": "https://app.yunnan.chinatax.gov.cn/xxmh/html/dfts/index_frame.html", + }, "body": null, "method": "GET" + }); + + console.log(fff.status, fff.statusText) } @@ -104,7 +143,7 @@ async function test() { let response = await fetch(url, {"method": "GET"}); // 返回页面内容 let htmlStr = await response.text(); - // console.log(htmlStr) + console.log(htmlStr) // 页面返回cookie @@ -114,7 +153,7 @@ async function test() { let staticJs = fs.readFileSync('/home/liu/桌面/MO5zzCMcub4d.b4c45da.js'); - loadPages(url, staticJs.toString('utf8'), htmlStr, cookies, "") + await loadPages(url, staticJs.toString('utf8'), htmlStr, cookies, "") }