test: puppeteer
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { config } from '@/config';
|
||||
|
||||
const proxyIsPAC = config.pacUri || config.pacScript;
|
||||
|
||||
import pacProxy from './pac-proxy';
|
||||
import unifyProxy from './unify-proxy';
|
||||
|
||||
let proxyUri: string | undefined;
|
||||
let proxyObj: Record<string, any> | undefined;
|
||||
let proxyUrlHandler: URL | null = null;
|
||||
if (proxyIsPAC) {
|
||||
const proxy = pacProxy(config.pacUri, config.pacScript, config.proxy);
|
||||
proxyUri = proxy.proxyUri;
|
||||
proxyObj = proxy.proxyObj;
|
||||
proxyUrlHandler = proxy.proxyUrlHandler;
|
||||
} else {
|
||||
const proxy = unifyProxy(config.proxyUri, config.proxy);
|
||||
proxyUri = proxy.proxyUri;
|
||||
proxyObj = proxy.proxyObj;
|
||||
proxyUrlHandler = proxy.proxyUrlHandler;
|
||||
}
|
||||
|
||||
let agent = null;
|
||||
if (proxyIsPAC) {
|
||||
const { PacProxyAgent } = require('pac-proxy-agent');
|
||||
agent = new PacProxyAgent(`pac+${proxyUri}`);
|
||||
} else if (proxyUri) {
|
||||
if (proxyUri.startsWith('http')) {
|
||||
const { HttpsProxyAgent } = require('https-proxy-agent');
|
||||
agent = new HttpsProxyAgent(proxyUri);
|
||||
} else if (proxyUri.startsWith('socks')) {
|
||||
const { SocksProxyAgent } = require('socks-proxy-agent');
|
||||
agent = new SocksProxyAgent(proxyUri);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
agent,
|
||||
proxyUri,
|
||||
proxyObj,
|
||||
proxyUrlHandler,
|
||||
};
|
||||
@@ -0,0 +1,72 @@
|
||||
import { type Config } from '@/config';
|
||||
import logger from '@/utils/logger';
|
||||
|
||||
const possibleProtocol = ['http', 'https', 'ftp', 'file', 'data'];
|
||||
|
||||
const pacProxy = (pacUri: Config['pacUri'], pacScript: Config['pacScript'], proxyObj: Config['proxy']) => {
|
||||
let pacUrlHandler = null;
|
||||
|
||||
// Validate PAC_URI / PAC_SCRIPT
|
||||
if (pacScript) {
|
||||
if (typeof pacScript === 'string') {
|
||||
pacUri = 'data:text/javascript;charset=utf-8,' + encodeURIComponent(pacScript);
|
||||
} else {
|
||||
logger.error('Invalid PAC_SCRIPT, use PAC_URI instead');
|
||||
}
|
||||
}
|
||||
if (pacUri && typeof pacUri === 'string') {
|
||||
try {
|
||||
pacUrlHandler = new URL(pacUri);
|
||||
} catch (error: any) {
|
||||
pacUri = undefined;
|
||||
pacUrlHandler = null;
|
||||
logger.error(`Parse PAC_URI error: ${error.stack}`);
|
||||
}
|
||||
} else {
|
||||
pacUri = undefined;
|
||||
}
|
||||
|
||||
// Check if PAC_URI has the right protocol
|
||||
if (pacUri && (!pacUrlHandler?.protocol || !possibleProtocol.includes(pacUrlHandler.protocol.replace(':', '')))) {
|
||||
logger.error(`Unsupported PAC protocol: ${pacUrlHandler?.protocol?.replace(':', '')}, expect one of ${possibleProtocol.join(', ')}`);
|
||||
pacUri = undefined;
|
||||
pacUrlHandler = null;
|
||||
}
|
||||
|
||||
// Validate proxyObj
|
||||
if (pacUrlHandler) {
|
||||
proxyObj.host = pacUrlHandler.hostname;
|
||||
proxyObj.port = pacUrlHandler.port;
|
||||
proxyObj.protocol = pacUrlHandler.protocol.replace(':', '');
|
||||
} else {
|
||||
proxyObj.protocol = proxyObj.host = proxyObj.port = proxyObj.auth = undefined;
|
||||
}
|
||||
|
||||
// Validate PROXY_AUTH
|
||||
if (proxyObj.auth && pacUrlHandler) {
|
||||
let promptProxyUri = false;
|
||||
if (pacUrlHandler.username || pacUrlHandler.password) {
|
||||
logger.warn('PAC_URI contains username and/or password, ignoring PROXY_AUTH');
|
||||
proxyObj.auth = undefined;
|
||||
} else if (['http:', 'https:'].includes(pacUrlHandler.protocol)) {
|
||||
logger.info('PROXY_AUTH is set and will be used for requests from Node.js. However, requests from puppeteer will not use it');
|
||||
promptProxyUri = true;
|
||||
} else {
|
||||
logger.warn(`PROXY_AUTH is only supported by HTTP(S) proxies, but got ${pacUrlHandler.protocol}, ignoring`);
|
||||
proxyObj.auth = undefined;
|
||||
promptProxyUri = true;
|
||||
}
|
||||
if (promptProxyUri) {
|
||||
logger.info('To get rid of this, set PAC_URI like protocol://username:password@host:port and clear PROXY_{AUTH,PROTOCOL,HOST,PORT}');
|
||||
}
|
||||
}
|
||||
|
||||
// Compatible with unify-proxy
|
||||
return {
|
||||
proxyUri: pacUri,
|
||||
proxyObj,
|
||||
proxyUrlHandler: pacUrlHandler,
|
||||
};
|
||||
};
|
||||
|
||||
export default pacProxy;
|
||||
@@ -0,0 +1,113 @@
|
||||
import { type Config } from '@/config';
|
||||
import logger from '@/utils/logger';
|
||||
|
||||
const defaultProtocol = 'http';
|
||||
const possibleProtocol = ['http', 'https', 'socks', 'socks4', 'socks4a', 'socks5', 'socks5h'];
|
||||
|
||||
const unifyProxy = (proxyUri: Config['proxyUri'], proxyObj: Config['proxy']) => {
|
||||
proxyObj = proxyObj || {};
|
||||
const [oriProxyUri, oriProxyObj] = [proxyUri, proxyObj];
|
||||
proxyObj = { ...proxyObj };
|
||||
|
||||
let proxyUrlHandler: URL | null = null;
|
||||
|
||||
// PROXY_URI
|
||||
if (proxyUri && typeof proxyUri === 'string') {
|
||||
if (!proxyUri.includes('://')) {
|
||||
logger.warn(`PROXY_URI contains no protocol, assuming ${defaultProtocol}`);
|
||||
proxyUri = `${defaultProtocol}://${proxyUri}`;
|
||||
}
|
||||
try {
|
||||
proxyUrlHandler = new URL(proxyUri);
|
||||
} catch (error: any) {
|
||||
logger.error(`Parse PROXY_URI error: ${error.stack}`);
|
||||
}
|
||||
}
|
||||
|
||||
// PROXY_{PROTOCOL,HOST,PORT}
|
||||
if (proxyObj.protocol || proxyObj.host || proxyObj.port) {
|
||||
if (proxyUrlHandler) {
|
||||
logger.warn('PROXY_URI is set, ignoring PROXY_{PROTOCOL,HOST,PORT}');
|
||||
} else if (proxyObj.host) {
|
||||
let tempProxyStr = proxyObj.host;
|
||||
|
||||
if (tempProxyStr.includes('://')) {
|
||||
logger.warn('PROXY_HOST contains protocol, ignoring PROXY_PROTOCOL');
|
||||
} else if (proxyObj.protocol) {
|
||||
tempProxyStr = `${proxyObj.protocol}://${tempProxyStr}`;
|
||||
} else {
|
||||
logger.warn(`PROXY_PROTOCOL is not set, assuming '${defaultProtocol}'`);
|
||||
tempProxyStr = `${defaultProtocol}://${tempProxyStr}`;
|
||||
}
|
||||
try {
|
||||
proxyUrlHandler = new URL(tempProxyStr);
|
||||
if (proxyUrlHandler.port && proxyObj.port) {
|
||||
logger.warn('PROXY_HOST contains port, ignoring PROXY_PORT');
|
||||
} else if (proxyObj.port) {
|
||||
if (Number.parseInt(proxyObj.port)) {
|
||||
proxyUrlHandler.port = proxyObj.port;
|
||||
} else {
|
||||
logger.warn(`PROXY_PORT is not a number, ignoring`);
|
||||
}
|
||||
} else {
|
||||
logger.warn('PROXY_PORT is not set, leaving proxy agent to determine');
|
||||
}
|
||||
} catch (error: any) {
|
||||
logger.error(`Parse PROXY_HOST error: ${error.stack}`);
|
||||
}
|
||||
} else {
|
||||
logger.warn('Either PROXY_{PROTOCOL,PORT} is set, but PROXY_HOST is missing, ignoring');
|
||||
}
|
||||
}
|
||||
|
||||
// PROXY_AUTH
|
||||
if (proxyObj.auth && proxyUrlHandler) {
|
||||
let promptProxyUri = false;
|
||||
if (proxyUrlHandler.username || proxyUrlHandler.password) {
|
||||
logger.warn('PROXY_URI contains username and/or password, ignoring PROXY_AUTH');
|
||||
proxyObj.auth = undefined;
|
||||
} else if (['http:', 'https:'].includes(proxyUrlHandler.protocol)) {
|
||||
logger.info('PROXY_AUTH is set and will be used for requests from Node.js. However, requests from puppeteer will not use it');
|
||||
promptProxyUri = true;
|
||||
} else {
|
||||
logger.warn(`PROXY_AUTH is only supported by HTTP(S) proxies, but got ${proxyUrlHandler.protocol}, ignoring`);
|
||||
proxyObj.auth = undefined;
|
||||
promptProxyUri = true;
|
||||
}
|
||||
if (promptProxyUri) {
|
||||
logger.info('To get rid of this, set PROXY_URI like protocol://username:password@host:port and clear PROXY_{AUTH,PROTOCOL,HOST,PORT}');
|
||||
}
|
||||
}
|
||||
|
||||
// is proxy enabled and valid?
|
||||
let isProxyValid = false;
|
||||
if (proxyUrlHandler) {
|
||||
const protocol = proxyUrlHandler.protocol.replace(':', '');
|
||||
if (possibleProtocol.includes(protocol)) {
|
||||
if (protocol !== 'http' && (proxyUrlHandler.username || proxyUrlHandler.password)) {
|
||||
logger.warn("PROXY_URI is an HTTPS/SOCKS proxy with authentication, which is not supported by puppeteer (ignore if you don't need it)");
|
||||
logger.info('To get rid of this, consider using an HTTP proxy instead');
|
||||
}
|
||||
proxyObj.protocol = protocol;
|
||||
proxyObj.host = proxyUrlHandler.hostname;
|
||||
proxyObj.port = proxyUrlHandler.port;
|
||||
// trailing slash will cause puppeteer to throw net::ERR_NO_SUPPORTED_PROXIES, trim it
|
||||
proxyUri = proxyUrlHandler.href.endsWith('/') ? proxyUrlHandler.href.slice(0, -1) : proxyUrlHandler.href;
|
||||
isProxyValid = true;
|
||||
} else {
|
||||
logger.error(`Unsupported proxy protocol: ${protocol}, expect one of ${possibleProtocol.join(', ')}`);
|
||||
}
|
||||
}
|
||||
if (!isProxyValid) {
|
||||
if ((oriProxyUri && typeof oriProxyUri === 'string') || oriProxyObj.protocol || oriProxyObj.host || oriProxyObj.port || oriProxyObj.auth) {
|
||||
logger.error('Proxy is disabled due to misconfiguration');
|
||||
}
|
||||
proxyObj.protocol = proxyObj.host = proxyObj.port = proxyObj.auth = undefined;
|
||||
proxyUri = undefined;
|
||||
proxyUrlHandler = null;
|
||||
}
|
||||
|
||||
return { proxyUri, proxyObj, proxyUrlHandler };
|
||||
};
|
||||
|
||||
export default unifyProxy;
|
||||
Reference in New Issue
Block a user