mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-09-24 23:12:34 +08:00
* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0. - [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases) - [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0) --- updated-dependencies: - dependency-name: eslint-plugin-unicorn dependency-version: 68.0.0 dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * style: fix no-useless-coercion * style: fix prefer-boolean-return * style: fix no-useless-logical-operand * style: fix prefer-single-replace * style: fix consistent-conditional-object-spread * style: fix no-useless-else * style: fix prefer-unicode-code-point-escapes * style: fix operator-assignment * style: fix prefer-continue * style: fix prefer-math-constants * style: fix prefer-hoisting-branch-code * style: fix prefer-array-slice * style: fix consistent-class-member-order * style: fix no-return-array-push * style: fix prefer-promise-with-resolvers * style: fix no-array-concat-in-loop * style: fix no-loop-iterable-mutation * style: fix no-invalid-argument-count * style: fix no-duplicate-if-branches * style: fix prefer-else-if * style: fix no-unreadable-for-of-expression * style: fix logical-assignment-operators * style: fix default-export-style * style: revert false positve in prefer-type-literal-last in #22274 * style: fix arrow-body-style * style: add v68 recommended preset * style: refactor GraphQL queries to use template literals for better readability --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tony <TonyRL@users.noreply.github.com>
193 lines
6.8 KiB
TypeScript
193 lines
6.8 KiB
TypeScript
import type { Browser, BrowserContext, BrowserContextOptions, LaunchOptions, Page } from 'patchright';
|
|
import { chromium } from 'patchright';
|
|
|
|
import { config } from '@/config';
|
|
|
|
import logger from './logger';
|
|
import proxy from './proxy';
|
|
|
|
type GotoOptions = Parameters<Page['goto']>[1];
|
|
|
|
type ProxyState = NonNullable<ReturnType<typeof proxy.getCurrentProxy>>;
|
|
|
|
const proxyServerFromUrl = (proxyUrl: URL) => {
|
|
const protocol = proxyUrl.protocol.replace('socks5h:', 'socks5:').replace('socks4a:', 'socks4:');
|
|
return `${protocol}//${proxyUrl.host}`;
|
|
};
|
|
|
|
const getProxyOptions = (currentProxy: ProxyState | null | undefined) => {
|
|
if (!currentProxy) {
|
|
return {};
|
|
}
|
|
|
|
const username = currentProxy.urlHandler?.username;
|
|
const password = currentProxy.urlHandler?.password;
|
|
if (username || password) {
|
|
if (currentProxy.urlHandler.protocol !== 'http:') {
|
|
logger.warn('SOCKS/HTTPS proxy with authentication is not supported by playwright, continue without proxy');
|
|
return {};
|
|
}
|
|
|
|
return {
|
|
proxy: {
|
|
password: decodeURIComponent(password ?? ''),
|
|
server: proxyServerFromUrl(currentProxy.urlHandler),
|
|
username: decodeURIComponent(username ?? ''),
|
|
},
|
|
} satisfies Pick<LaunchOptions, 'proxy'>;
|
|
}
|
|
|
|
return {
|
|
proxy: {
|
|
server: currentProxy.uri.replace('socks5h://', 'socks5://').replace('socks4a://', 'socks4://').replace(/\/$/, ''),
|
|
},
|
|
} satisfies Pick<LaunchOptions, 'proxy'>;
|
|
};
|
|
|
|
// Patchright already patches playwright's default args (e.g. injects --disable-blink-features=AutomationControlled and strips --enable-automation)
|
|
const COMMON_LAUNCH_ARGS = ['--no-sandbox', '--disable-setuid-sandbox', '--window-position=0,0', '--ignore-certificate-errors', '--ignore-certificate-errors-spki-list'];
|
|
|
|
const getLaunchOptions = (currentProxy?: ProxyState | null): LaunchOptions => ({
|
|
args: COMMON_LAUNCH_ARGS,
|
|
executablePath: config.chromiumExecutablePath || undefined,
|
|
headless: true,
|
|
...getProxyOptions(currentProxy),
|
|
});
|
|
|
|
type BrowserlessLaunchOptions = Pick<LaunchOptions, 'args' | 'headless' | 'proxy'>;
|
|
type BrowserlessCdpLaunchOptions = Omit<BrowserlessLaunchOptions, 'proxy'> & { stealth?: boolean };
|
|
|
|
const toBrowserlessLaunchOptions = (currentProxy?: ProxyState | null): BrowserlessLaunchOptions => ({
|
|
args: COMMON_LAUNCH_ARGS,
|
|
headless: true,
|
|
...getProxyOptions(currentProxy),
|
|
});
|
|
|
|
// CDP accepts `stealth` but NOT a `proxy` object.
|
|
const toBrowserlessCDPLaunchOptions = (currentProxy?: ProxyState | null): BrowserlessCdpLaunchOptions => {
|
|
let proxyServerArgs: string[] = [];
|
|
|
|
if (currentProxy) {
|
|
if (currentProxy.urlHandler?.username || currentProxy.urlHandler?.password) {
|
|
logger.warn('Proxy authentication is not supported over CDP (--proxy-server), continue without proxy');
|
|
} else {
|
|
const server = currentProxy.uri.replace('socks5h://', 'socks5://').replace('socks4a://', 'socks4://').replace(/\/$/, '');
|
|
proxyServerArgs = [`--proxy-server=${server}`];
|
|
}
|
|
}
|
|
|
|
return {
|
|
args: [...COMMON_LAUNCH_ARGS, ...proxyServerArgs],
|
|
headless: true,
|
|
stealth: true,
|
|
};
|
|
};
|
|
|
|
const getContextOptions = (): BrowserContextOptions => ({
|
|
ignoreHTTPSErrors: true,
|
|
userAgent: config.ua,
|
|
});
|
|
|
|
// CDP > WS > local
|
|
const launchBrowser = async (currentProxy?: ProxyState | null) => {
|
|
let browser: Browser;
|
|
if (config.playwrightCDPEndpoint) {
|
|
browser = await chromium.connectOverCDP(getBrowserlessEndpoint(config.playwrightCDPEndpoint, toBrowserlessCDPLaunchOptions(currentProxy)));
|
|
} else if (config.playwrightWSEndpoint) {
|
|
browser = await chromium.connect(getBrowserlessEndpoint(config.playwrightWSEndpoint, toBrowserlessLaunchOptions(currentProxy)));
|
|
} else {
|
|
browser = await chromium.launch(getLaunchOptions(currentProxy));
|
|
}
|
|
const context = await browser.newContext(getContextOptions());
|
|
return { browser, context };
|
|
};
|
|
|
|
const getBrowserlessEndpoint = (endpoint: string, launchOptions: BrowserlessLaunchOptions) => {
|
|
const endpointURL = new URL(endpoint);
|
|
endpointURL.searchParams.set('launch', JSON.stringify(launchOptions));
|
|
return endpointURL.href;
|
|
};
|
|
|
|
const scheduleClose = (browser: Browser, timeout = 30000) => {
|
|
setTimeout(() => {
|
|
void browser.close();
|
|
}, timeout);
|
|
};
|
|
|
|
/**
|
|
* @returns Playwright browser context (native `newPage()` shares state across calls)
|
|
*/
|
|
export default async function outPlaywright() {
|
|
const currentProxy = proxy.getCurrentProxy();
|
|
const { browser, context } = await launchBrowser(currentProxy && proxy.proxyObj.url_regex === '.*' ? currentProxy : null);
|
|
scheduleClose(browser);
|
|
return context;
|
|
}
|
|
|
|
// No-op in Node.js environment (used by Worker build via alias)
|
|
export const setBrowserBinding = (_binding: any) => {};
|
|
|
|
/**
|
|
* @returns Playwright page
|
|
*/
|
|
export const getPlaywrightPage = async (
|
|
url: string,
|
|
instanceOptions: {
|
|
closeTimeout?: number;
|
|
gotoConfig?: GotoOptions;
|
|
noGoto?: boolean;
|
|
onBeforeLoad?: (page: Page, context?: BrowserContext) => Promise<void> | void;
|
|
} = {}
|
|
) => {
|
|
let allowProxy = false;
|
|
const proxyRegex = new RegExp(proxy.proxyObj.url_regex);
|
|
let urlHandler: URL | undefined;
|
|
try {
|
|
urlHandler = new URL(url);
|
|
} catch {
|
|
// ignore invalid URLs such as about:blank
|
|
}
|
|
|
|
if (proxyRegex.test(url) && url.startsWith('http') && !(urlHandler && urlHandler.host === proxy.proxyUrlHandler?.host)) {
|
|
allowProxy = true;
|
|
}
|
|
|
|
const currentProxy = proxy.getCurrentProxy();
|
|
const currentProxyState = currentProxy && allowProxy ? currentProxy : null;
|
|
const hasProxy = Boolean(getProxyOptions(currentProxyState).proxy);
|
|
const { browser, context } = await launchBrowser(currentProxyState);
|
|
scheduleClose(browser, instanceOptions.closeTimeout);
|
|
const page = await context.newPage();
|
|
|
|
if (hasProxy && currentProxyState) {
|
|
logger.debug(`Proxying request in playwright via ${currentProxyState.uri}: ${url}`);
|
|
}
|
|
|
|
if (instanceOptions.onBeforeLoad) {
|
|
await instanceOptions.onBeforeLoad(page, context);
|
|
}
|
|
|
|
if (!instanceOptions.noGoto) {
|
|
try {
|
|
await page.goto(url, instanceOptions.gotoConfig || { waitUntil: 'domcontentloaded' });
|
|
} catch (error) {
|
|
if (hasProxy && currentProxyState && proxy.multiProxy) {
|
|
logger.warn(`Playwright navigation failed with proxy ${currentProxyState.uri}, marking as failed: ${error}`);
|
|
proxy.markProxyFailed(currentProxyState.uri);
|
|
throw error;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
return {
|
|
context,
|
|
destroy: async () => {
|
|
await context.close();
|
|
},
|
|
page,
|
|
};
|
|
};
|
|
|
|
export { type Page } from 'patchright';
|