English | 中文
To convert SwitchyOmega rule lists (
*.sorl) to PAC script.
Installation:
npm install sorl2pacConversion:
import { sorl2pac } from "sorl2pac";
const pacSource = sorl2pac(sorlText, {
matched: "DIRECT",
unmatched: "PROXY 127.0.0.1:7890",
});The function returns a complete PAC script. It contains the standard FindProxyForURL(url, host) entry point, which uses an internal match(host) helper to evaluate rules (see Matching Behavior). The main logic looks like this:
function FindProxyForURL(url, host) {
return match(host) ? "DIRECT" : "PROXY 127.0.0.1:7890";
}function sorl2pac(
sorl: string,
results: {
matched: string;
unmatched: string;
},
): string;sorlmust be a string containing a supported SwitchyOmega rule list.results.matchedis returned byFindProxyForURL()whenmatch(host)istrue.results.unmatchedis returned whenmatch(host)isfalse.match(host)is generated inside the PAC script. It is not an extra package export.
sorl2pac() throws TypeError for invalid API arguments and SyntaxError for unsupported rule-list syntax.
This package intentionally supports a small SwitchyOmega new-format subset:
[SwitchyOmega Conditions]- blank lines
;comment lines@notelines- bare
HostWildcardConditionrules, such as*.example.com - host wildcard prefixes:
HostWildcard:,HostWildcardCondition:,Host:,H:,W:,HW: !exclusive rules
Unsupported syntax fails instead of being ignored, including old #BEGIN rule lists, @with result, +profile, URL conditions, IP conditions, time conditions, and AutoProxy syntax.
- Rules are checked from top to bottom.
- A normal rule match returns
true. - An exclusive rule match returns
false. - No match returns
false. - An empty rule list is valid and always returns
false. *.abc.commatches bothabc.comandsub.abc.com.**.abc.commatchessub.abc.combut notabc.com.
Because matched and unmatched can be set to any proxy result strings, callers can implement whitelist or blacklist proxy behavior.
const whitelistPac = sorl2pac(sorlText, {
matched: "DIRECT",
unmatched: "PROXY 127.0.0.1:7890",
});
const blacklistPac = sorl2pac(sorlText, {
matched: "PROXY 127.0.0.1:7890",
unmatched: "DIRECT",
});Any code contributed to this project is considered authorized for commercial use by the project authors and their affiliated companies and distributed under this project's license.
任何贡献到本项目的代码,均视为授权本项目作者及其关联公司用于商业用途,并可按本项目协议进行分发。
MIT