UNPKG

rsshub

Version:
124 lines (122 loc) 4.94 kB
import { t as config } from "./config-MQ-7ebJ_.mjs"; import { t as logger_default } from "./logger-C4avouvV.mjs"; import { t as proxy_default } from "./proxy-CjvoY5ys.mjs"; import { anonymizeProxy } from "proxy-chain"; import puppeteer from "rebrowser-puppeteer"; //#region lib/utils/puppeteer.ts /** * @deprecated use getPage instead * @returns Puppeteer browser */ const outPuppeteer = async () => { const options = { args: [ "--no-sandbox", "--disable-setuid-sandbox", "--disable-blink-features=AutomationControlled", "--window-position=0,0", "--ignore-certificate-errors", "--ignore-certificate-errors-spki-list", `--user-agent=${config.ua}` ], headless: true, ignoreHTTPSErrors: true }; const insidePuppeteer = puppeteer; const currentProxy = proxy_default.getCurrentProxy(); if (currentProxy && proxy_default.proxyObj.url_regex === ".*") if (currentProxy.urlHandler?.username || currentProxy.urlHandler?.password) if (currentProxy.urlHandler.protocol === "http:") options.args.push(`--proxy-server=${await anonymizeProxy(currentProxy.uri)}`); else logger_default.warn("SOCKS/HTTPS proxy with authentication is not supported by puppeteer, continue without proxy"); else options.args.push(`--proxy-server=${currentProxy.uri.replace("socks5h://", "socks5://").replace("socks4a://", "socks4://")}`); const browser = await (config.puppeteerWSEndpoint ? insidePuppeteer.connect({ browserWSEndpoint: config.puppeteerWSEndpoint }) : insidePuppeteer.launch(config.chromiumExecutablePath ? { executablePath: config.chromiumExecutablePath, ...options } : options)); setTimeout(async () => { await browser.close(); }, 3e4); return browser; }; var puppeteer_default = outPuppeteer; /** * @returns Puppeteer page */ const getPuppeteerPage = async (url, instanceOptions = {}) => { const options = { args: [ "--no-sandbox", "--disable-setuid-sandbox", "--disable-blink-features=AutomationControlled", "--window-position=0,0", "--ignore-certificate-errors", "--ignore-certificate-errors-spki-list", `--user-agent=${config.ua}` ], headless: true, ignoreHTTPSErrors: true }; const insidePuppeteer = puppeteer; let allowProxy = false; const proxyRegex = new RegExp(proxy_default.proxyObj.url_regex); let urlHandler; try { urlHandler = new URL(url); } catch {} if (proxyRegex.test(url) && url.startsWith("http") && !(urlHandler && urlHandler.host === proxy_default.proxyUrlHandler?.host)) allowProxy = true; let hasProxy = false; let currentProxyState = null; const currentProxy = proxy_default.getCurrentProxy(); if (currentProxy && allowProxy) { currentProxyState = currentProxy; if (currentProxy.urlHandler?.username || currentProxy.urlHandler?.password) if (currentProxy.urlHandler.protocol === "http:") { const urlObj = new URL(currentProxy.uri); urlObj.username = ""; urlObj.password = ""; options.args.push(`--proxy-server=${urlObj.toString().replace(/\/$/, "")}`); hasProxy = true; } else logger_default.warn("SOCKS/HTTPS proxy with authentication is not supported by puppeteer, continue without proxy"); else { options.args.push(`--proxy-server=${currentProxy.uri.replace("socks5h://", "socks5://").replace("socks4a://", "socks4://")}`); hasProxy = true; } } let browser; if (config.puppeteerWSEndpoint) { const endpointURL = new URL(config.puppeteerWSEndpoint); endpointURL.searchParams.set("launch", JSON.stringify(options)); endpointURL.searchParams.set("stealth", "true"); const endpoint = endpointURL.toString(); browser = await insidePuppeteer.connect({ browserWSEndpoint: endpoint }); } else browser = await insidePuppeteer.launch(config.chromiumExecutablePath ? { executablePath: config.chromiumExecutablePath, ...options } : options); setTimeout(async () => { await browser.close(); }, 3e4); const page = await browser.newPage(); if (hasProxy && currentProxyState) logger_default.debug(`Proxying request in puppeteer via ${currentProxyState.uri}: ${url}`); if (hasProxy && currentProxyState && (currentProxyState.urlHandler?.username || currentProxyState.urlHandler?.password)) await page.authenticate({ username: currentProxyState.urlHandler?.username, password: currentProxyState.urlHandler?.password }); if (instanceOptions.onBeforeLoad) await instanceOptions.onBeforeLoad(page, browser); if (!instanceOptions.noGoto) try { await page.goto(url, instanceOptions.gotoConfig || { waitUntil: "domcontentloaded" }); } catch (error) { if (hasProxy && currentProxyState && proxy_default.multiProxy) { logger_default.warn(`Puppeteer navigation failed with proxy ${currentProxyState.uri}, marking as failed: ${error}`); proxy_default.markProxyFailed(currentProxyState.uri); throw error; } throw error; } return { page, destory: async () => { await browser.close(); }, browser }; }; //#endregion export { puppeteer_default as n, getPuppeteerPage as t };