pac-maker

PAC generator & maintenance tool

Usage no npm install needed!

<script type="module">
  import pacMaker from 'https://cdn.skypack.dev/pac-maker';
</script>

README

pac-maker

npm package node-current Test

Proxy Auto Configuration (PAC) file generator & maintenance tool.

Features:

  • Generate PAC files from various hostname sources.
  • Load a PAC file and use it in node.
  • Serve the PAC with http and watch for source change.
  • Show what hosts in browser history will be proxied by the PAC.

Usage

Just use pre-generated PAC file: proxy.pac

Install

pac-maker requires NodeJS >= 16, older versions are not tested.

npm install pac-maker

Configuration

pac-maker loads config file from working directory, default is pac.config.js, it can be specified by --config=<path> .

config file should export a configuration object:

import { builtinList, gfwlist, ofArray } from "pac-maker";

export default {
    path: "dist/proxy.pac",
    direct: "DIRECT",
    sources: {
        "SOCKS5 localhost:2080": [
            gfwlist(),
            builtinList("default"),
            builtinList("forbidden"),
            ofArray(["google.com"]),
        ],
    },
};

There are some built-in sources in pac-maker:

  • gfwlist Fetch hostnames from gfwlist.

  • hostnameFile Read hostnames from a file, for hostname file example, see the list directory.

  • builtinList Read hostnames from a file in the list directory.

  • ofArray Just use an array of hostnames

CLI commands

Generate a PAC file:

node bin/pac-maker.js generate [--config=<path>] [--watch]
  • --watch After the initial build, pac-maker will continue to watch for updates in any of the sources.

Find what hosts will be proxied by the PAC in browser history, support Chrome, Firefox, and Edge:

node bin/pac-maker.js analyze [--config=<path>] [--json=<path>]
  • --json Save matched rules to this file, default is matches.json.

Serve the PAC file with http, and update when source have changes:

node bin/pac-maker.js serve [--config=<file>] [--host=<host>] [--port=<port>]
  • --host By default, the server will accept connections from all addresses, It is possible to listen to just one selected interface using the host parameter.

  • --port The port number that http server to listened on, default is 7568.

JavaScript API

pac-maker exports some useful functions that allows you to play with PAC inside your own JavaScript program.

This package is pure ESM, It cannot be require()'d from CommonJS.

buildPAC

Create a PAC script from rules, use the built-in template template/default.js.

The function takes two parameters, first is a rules object which key is a proxy string , and value is a hostname array. the second parameter will be returned from FindProxyForURL if no hostname matched, default is DIRECT.

import { writeFileSync } from "fs";
import { buildPAC } from "pac-maker";

const rules = {
    "HTTP 192.168.0.1:80": ["foo.com", "bar.com"],
    "SOCKS5 localhost:1080": ["example.com"],
};

writeFileSync("proxy.pac", buildPAC(rules));

loadPAC

Load and execute PAC script, return an object includes all global variables defined in the PAC.

import { readFileSync } from "fs";
import { loadPAC } from "pac-maker";

const pac = loadPAC(readFileSync("proxy.pac", "utf8"));

console.log(pac.FindProxyForURL("", "example.com"));

commands

All commands supported by pac-maker can also be called in JavaScript code.

import { commands } from "pac-maker";
import config from "./pac.config.js";

commands.serve({ host: "localhost", port: 12345 }, config);

Run tests

To run unit tests, you need enable experimental vm modules.

set NODE_OPTIONS=--experimental-vm-modules
pnpm test

NOTE: Some tests may fail with the error Provided module is not an instance of Module , that is a bug in v8.