Skip to content

Instantly share code, notes, and snippets.

@janlay
Last active February 29, 2024 20:23
Show Gist options
  • Save janlay/4527678 to your computer and use it in GitHub Desktop.
Save janlay/4527678 to your computer and use it in GitHub Desktop.
A white-list based PAC.
/*
* A white-list based PAC without regexp, by @janlay
* It's just simple and fast.
* Last update: Oct 20, 2015
* Special thanks to @Paveo
*/
function FindProxyForURL(url, host) {
// REPLACE PROXY WITH YOUR OWN'S
var PROXY = "SOCKS 127.0.0.1:8801;SOCKS5 127.0.0.1:8801;PROXY 127.0.0.1:8800";
var BLACKHOLE = "127.0.0.2";
var DEFAULT = "DIRECT";
var parts = host.split('.'),
firstPart = parts[0],
// always use proxy, even if inHosts or domains are matched
overrideDomains = ['google.com', 'twitter.com', 'twimg.com', 'amazonaws.com', 'facebook.com'],
// domain/host starts with
prefixes = ['cn', 'china'],
// indexOf searching
inHosts = ['51', '58', '86', '91', '99', '100', '123', '126', '163', '168', '360', 'cn', 'bj', 'zj', 'ali', 'mayi', 'taobao', 'qq', 'tencent', 'baidu', 'china', 'local'],
// privacy trackers
blockedHosts = ['mmstat.com', 'googlesyndication.com', '127.net'],
// domains end with
domains = ['cn', 'img.com', '39.net', 'apple.com', 'baixing.com', 'go2map.com', 'blueidea.com', 'caing.com', 'ccb.com', 'comsenz.com', 'csdn.net', 'ctrip.com', 'dangdang.com', 'dianping.com', 'dingtalk.com', 'discuz.net', 'donews.com', 'douban.com', 'dream4ever.org', 'eastmoney.com', 'et8.org', 'et8.net', 'ecitic.com', 'fastspring.com', 'fengniao.com', 'futu5.com', 'ganji.com', 'gfan.com', 'gfw.io', 'gougou.com', 'hi-pda.com', 'huaban.com', 'huanqiu.com', 'hudong.com', 'iciba.com', 'ihg.com', 'img-space.com', 'infzm.com', 'ip138.com', 'jandan.net', 'jd.com', 'jiepang.com', 'ku6.com', 'lampdrive.com', 'live.net', 'etao.com', 'mapabc.com', 'mapbar.com', 'meituan.com', 'mi.com', 'miwifi.com', 'microsoft.com', 'onenote.com', 'macpaw.com', 'mozilla.org', 'mop.com', 'mtime.com', 'mydrivers.com', 'netease.com', 'nuomi.com', 'onlinedown.net', 'paipai.com', 'qiyi.com', 'qunar.com', 'renren.com', 'sanguosha.com', 'sdo.com', 'sf-express.com', 'iask.com', 'sogou.com', 'sohu.com', 'soso.com', 'soufun.com', 'stackoverflow.com', 'superuser.com', 'tenpay.com', 'tgbus.com', 'tmall.com', 'tudou.com', 'tudouui.com', 'uusee.com', 'verycd.com', 'weibo.com', 'weiphone.com', 'xiami.com', 'xiami.net', 'xinhuanet.com', 'xinnet.com', 'xunlei.com', 'yesky.com', 'yihaodian.com', 'ynet.com', 'youdao.com', 'youku.com', 'yupoo.com', 'zaobao.com', 'zhaopin.com', 'zhihu.com', 'my.cl.ly', 'synacast.com', 'xiachufang.com', 'wandoujia.com', 'chdbits.org', 'hdwing.com', 'zhihu.com', 'zhi.hu', 'join.me', 'imgur.com', 'images-amazon.com', 'smzdm.com', 'ycombinator.com', 'v2ex.com', 'verisign.com', 'laiwang.com', 'hiwifi.com', 'tanx.com'];
// ignore local host name. eg: http://localhost
if (isPlainHostName(host)) return DEFAULT;
// force proxy by url. eg: http://foo.com/?bar=1&fuckgfw
if (url.indexOf('fuckgfw') > 0) return PROXY;
// bypass plain IP
var lastChar = host.substring(host.length - 1);
if (lastChar >= '0' && lastChar <= '9')
return DEFAULT;
var i, len;
// block privacy trackers
for (i = 0, len = blockedHosts.length; i < len; i++)
if (host.substr(-blockedHosts[i].length) === blockedHosts[i]) return BLACKHOLE;
// force proxy by domain. eg: http://cn.nytimes.com
for (i = 0, len = overrideDomains.length; i < len; i++)
if (host.substr(-overrideDomains[i].length) === overrideDomains[i]) return PROXY;
// domain/ip prefix. eg: http://60.1.2.3
for (i = 0, len = prefixes.length; i < len; i++)
if (prefixes[i] === firstPart) return DEFAULT;
// match main domain. eg: http://www.verycd.com, http://ip138.com/
for (i = 0, len = domains.length; i < len; i++)
if (host.substr(-domains[i].length) === domains[i]) return DEFAULT;
// search pattern in domains. eg: https://www.51job.com, https://www.alipay.com
for (i = 0, len = inHosts.length; i < len; i++)
if (host.indexOf(inHosts[i]) >= 0) return DEFAULT;
// for all other host, default to proxy.
return PROXY;
}
@chrisyip
Copy link

配合 isInNet()bestroutetb 或者 chnroutes 会更好,刚好 bestroutetb 的表是 ip 和 mask 的配对,简单的脚本就可以转换过去。

@Leask
Copy link

Leask commented Oct 18, 2013

使用 isInNet+ chnroutes 基本上一劳永逸的方法,我有一个简单的实现 https://github.com/Leask/Flora_Pac w/ @chrisyip

@janlay
Copy link
Author

janlay commented Oct 18, 2013

这个 PAC 的设计目标是快,从而达到节约 CPU 的目的。楼上两位提供的方案从方向上来说,不是我追求的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment