Pacparser Pacparser

A powerful library to parse proxy auto-config (PAC) files in Node.js 一个强大的Node.js库,用于解析代理自动配置(PAC)文件

What is Pacparser? 什么是Pacparser?

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应用程序中变得容易。

Easy Integration 易于集成

Simple API that integrates seamlessly with your Node.js applications 简单的API,可与您的Node.js应用程序无缝集成

URL & File Support 支持URL和文件

Load PAC files from local paths or remote URLs 从本地路径或远程URL加载PAC文件

CLI Included 包含命令行工具

Use pacparser directly from the command line 直接从命令行使用pacparser

Installation 安装

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

Basic Usage 基本用法

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找到合适的代理。

Loading from a 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");

Loading from a File 从文件加载

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"

Using PAC Script Directly 直接使用PAC脚本

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");

API Documentation API文档

Pacparser Class Pacparser类

The main class for parsing and executing PAC files. 用于解析和执行PAC文件的主类。

Constructor: 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脚本字符串

Static Method: 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脚本字符串
  • Returns: A new Pacparser instance 返回:一个新的Pacparser实例

Method: 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脚本字符串
  • Returns: The Pacparser instance (allows chaining) 返回:Pacparser实例(支持链式调用)

Method: 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的主机名)
  • Returns: Promise that resolves to proxy configuration string (e.g., "PROXY proxy:port; DIRECT") 返回:Promise,解析为代理配置字符串(例如,"PROXY proxy:port; DIRECT")

Method: reload() 方法: reload()

Reloads the current PAC script. 重新加载当前的PAC脚本。

  • Returns: Promise that resolves when reload is complete 返回:当重新加载完成时解析的Promise

Method: cleanup() 方法: cleanup()

Cleans up the Pacparser instance and releases resources. 清理Pacparser实例并释放资源。

Method: getPacCode() 方法: getPacCode()

Gets the current PAC script code. 获取当前的PAC脚本代码。

  • Returns: PAC script string 返回:PAC脚本字符串

Method: getPacSource() 方法: getPacSource()

Get pac source in pacparser instance. 获取当前的PAC资源。

  • Returns: PAC source string 返回:PAC资源

PAC Built-in Functions 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)

Command Line Interface 命令行界面

When installed globally, Pacparser provides a CLI tool that can be used with either pacparser or its shortcut pap. 全局安装后,Pacparser提供了一个CLI工具,可以通过pacparser或其快捷方式pap使用。

Basic CLI Usage 基本CLI用法

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

Using a PAC Script Directly 直接使用PAC脚本

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

Using a PAC URL 使用PAC URL

pap exec -p http://localhost:3000/proxy.pac -f https://www.google.com