A powerful library to parse proxy auto-config (PAC) files in Node.js 一个强大的Node.js库,用于解析代理自动配置(PAC)文件
Pacparser is a Node.js library that allows you to parse and execute proxy auto-config (PAC) files. It runs PAC scripts in a secure Node.js VM environment and provides a simple API to find the appropriate proxy configuration for any given URL. Pacparser是一个Node.js库,允许您解析和执行代理自动配置(PAC)文件。 它在安全的Node.js VM环境中运行PAC脚本,并提供简单的API来为任何给定URL找到合适的代理配置。
PAC files are widely used to determine proxy settings based on URL patterns, domain names, and other criteria. Pacparser makes it easy to integrate PAC file support into your Node.js applications. PAC文件广泛用于根据URL模式、域名和其他标准确定代理设置。 Pacparser使将PAC文件支持集成到您的Node.js应用程序中变得容易。
Simple API that integrates seamlessly with your Node.js applications 简单的API,可与您的Node.js应用程序无缝集成
Load PAC files from local paths or remote URLs 从本地路径或远程URL加载PAC文件
Use pacparser directly from the command line 直接从命令行使用pacparser
Install Pacparser using npm or pnpm. It can be used as a library in your Node.js projects or as a command-line tool. 使用npm或pnpm安装Pacparser。它可以作为库在您的Node.js项目中使用,也可以作为命令行工具使用。
npm install pacparser # Use in Node.js
npm install pacparser -g # Use as a command line tool
Pacparser is easy to use. Create an instance, load your PAC file (from a path, URL, or directly as a string), and then find the appropriate proxy for any URL. Pacparser使用简单。创建一个实例,加载您的PAC文件(从路径、URL或直接作为字符串), 然后为任何URL找到合适的代理。
import Pacparser from "pacparser";
const pacParser = new Pacparser();
pacParser.parsePac("https://proxy.example.com/proxy.pac");
const proxy = await pacParser.findProxy("https://direct.mozilla.org");
import Pacparser from "pacparser";
import path from "path";
const pacParser = Pacparser.create(path.join(__dirname, "./proxy.pac"));
await pacParser.findProxy("https://direct.mozilla.org"); // returns "DIRECT"
import Pacparser from "pacparser";
const pacScript = `
function FindProxyForURL(url, host) {
if (isPlainHostName(host)) {
return "DIRECT";
} else {
return "PROXY proxy.example.com:8080";
}
}
`;
const pacParser = new Pacparser();
pacParser.parsePac(pacScript);
const proxy = await pacParser.findProxy("https://example.com");
The main class for parsing and executing PAC files. 用于解析和执行PAC文件的主类。
new Pacparser(pac? string)
构造函数:
new Pacparser(pac? string)
Creates a new Pacparser instance. 创建一个新的Pacparser实例。
pac
(optional): PAC file
path, URL, or PAC script string
pac
(可选):PAC文件路径、URL或PAC脚本字符串
Pacparser.create(pac? string)
静态方法:
Pacparser.create(pac? string)
Alternative way to create a new Pacparser instance. 创建新Pacparser实例的另一种方式。
pac
(optional): PAC file
path, URL, or PAC script string
pac
(可选):PAC文件路径、URL或PAC脚本字符串
parsePac(pac: string)
方法:
parsePac(pac: string)
Switches the PAC source (filepath, URL, or PAC script). 切换PAC源(文件路径、URL或PAC脚本)。
pac
: PAC file path, URL, or
PAC script string
pac
:PAC文件路径、URL或PAC脚本字符串
findProxy(url: string, host?: string)
方法:
findProxy(url: string, host?: string)
Finds the appropriate proxy configuration for a given URL. 为给定URL找到合适的代理配置。
url
: The target URL
url
:目标URL
host
(optional): The hostname
(defaults to URL's hostname)
host
(可选):主机名(默认为URL的主机名)
reload()
方法: reload()
Reloads the current PAC script. 重新加载当前的PAC脚本。
cleanup()
方法: cleanup()
Cleans up the Pacparser instance and releases resources. 清理Pacparser实例并释放资源。
getPacCode()
方法: getPacCode()
Gets the current PAC script code. 获取当前的PAC脚本代码。
getPacSource()
方法: getPacSource()
Get pac source in pacparser instance. 获取当前的PAC资源。
Pacparser supports all standard PAC built-in functions: Pacparser支持所有标准的PAC内置函数:
isPlainHostName(host)
dnsDomainIs(host, domain)
localHostOrDomainIs(host, hostdom)
isResolvable(host)
isInNet(host, pattern, mask)
dnsResolve(host)
convert_addr(ipaddr)
myIpAddress()
dnsDomainLevels(host)
shExpMatch(str, shexp)
weekdayRange(wd1, wd2?, gmt?)
dateRange(...args)
timeRange(...args)
alter(str)
When installed globally, Pacparser provides a CLI tool that can be used with either
pacparser
or its shortcut
pap
.
全局安装后,Pacparser提供了一个CLI工具,可以通过pacparser
或其快捷方式pap
使用。
pacparser exec --pac './proxy.pac' --findproxy https://www.google.com
# Or use the shortcut command
pap exec -p './proxy.pac' -f https://www.google.com
pap exec -p 'function FindProxyForURL(url, host) {
if (isPlainHostName(host)) {
return "DIRECT";
} else {
return "PROXY w3proxy.mozilla.org:8080; DIRECT";
}
}' -f https://www.google.com
pap exec -p http://localhost:3000/proxy.pac -f https://www.google.com