Skip to content

Instantly share code, notes, and snippets.

@beetcb
Last active February 6, 2024 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beetcb/c3d79c664a56186b8376d814dc65598f to your computer and use it in GitHub Desktop.
Save beetcb/c3d79c664a56186b8376d814dc65598f to your computer and use it in GitHub Desktop.
// see -> https://en.wikipedia.org/wiki/Proxy_auto-config#:~:text=A%20proxy%20auto%2Dconfig%20(PAC,for%20fetching%20a%20given%20URL.
// 定义 findProxyForURL 函数,它接受初始化状态和配置文件
const findProxyForURL = (init, profiles) => (url, host) => {
let result = init;
const scheme = url.substr(0, url.indexOf(":"));
do {
result = profiles[result];
if (typeof result === "function") {
result = result(url, host, scheme);
}
} while (typeof result !== "string" || result.charCodeAt(0) === 43);
return result;
};
// 定义代理配置文件
const proxyProfiles = {
"+auto switch": (url, host, scheme) => {
// 如果请求的 host 是本地地址,则直接连接
if (/^127\.0\.0\.1$/.test(host) || /^::1$/.test(host) || /^localhost$/.test(host)) {
return "DIRECT";
};
// 定义一系列的规则,如果请求的 host 符合规则,则返回对应的代理配置
var rules = [
{ pattern: /^passport\.woa\.com$/, proxy: "+ioa-pac" },
{ pattern: /(?:^|\.)figma\.com$/, proxy: "+ioa-pac" },
{ pattern: /(?:^|\.)openai\.com$/, proxy: "+clash" },
{ pattern: /(?:^|\.)sentry\.io$/, proxy: "+clash" },
{ pattern: /(?:^|\.)auth0\.com$/, proxy: "+clash" },
{ pattern: /(?:^|\.)bing\.com$/, proxy: "+clash" },
{ pattern: /(?:^|\.)live\.com$/, proxy: "+clash" },
{ pattern: /(?:^|\.)office\.net$/, proxy: "+clash" },
{ pattern: /(?:^|\.)netflix\.com$/, proxy: "+clash" },
{ pattern: /^glados\.network$/, proxy: "+clash" },
{ pattern: /^bard\.google\.com$/, proxy: "+clash" },
{ pattern: /(?:^|\.)tencent\.com$/, proxy: "+whistle" },
{ pattern: /(?:^|\.)woa\.com$/, proxy: "+whistle" },
{ pattern: /(?:^|\.)tencentcloud\.com$/, proxy: "+whistle" },
{ pattern: /(?:^|\.)qq\.com$/, proxy: "+whistle" },
{ pattern: /(?:^|\.)tencent-cloud\./, proxy: "+whistle" },
{ pattern: /(?:^|\.)cloud\.tencent\./, proxy: "+whistle" },
{ pattern: /^oa\.com$/, proxy: "+whistle" },
{ pattern: /^static\.cloudcachetci\.com$/, proxy: "+whistle" },
{ pattern: /^cloudcache\.tencent.*\./, proxy: "+whistle" },
{ pattern: /^imgcache\.qq\./, proxy: "+whistle" }
];
// 使用 find 方法查找第一个匹配的规则
const rule = rules.find(r => r.pattern.test(host));
return rule ? rule.proxy : "+ioa-pac";
},
"+clash": (url, host, scheme) => {
return "PROXY 127.0.0.1:7890";
},
"+ioa-pac": (url, host, scheme) => {
return "PROXY 127.0.0.1:12639";
},
"+whistle": (url, host, scheme) => {
return "PROXY 127.0.0.1:8899";
}
};
// 自动切流模式
const pacFunction = findProxyForURL("+auto switch", proxyProfiles);
function FindProxyForURL(url, host) {
return pacFunction(url, host);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment