Skip to content

Instantly share code, notes, and snippets.

@holys
Forked from iwinux/proxy-pac.js
Created November 5, 2012 17:08
Show Gist options
  • Save holys/4018360 to your computer and use it in GitHub Desktop.
Save holys/4018360 to your computer and use it in GitHub Desktop.
PAC that uses a white list
/*global isPlainHostName, isInNet*/
var proxy = "SOCKS5 127.0.0.1:9999; SOCKS 127.0.0.1:9999; DIRECT";
var noProxy = "DIRECT";
var localhosts = [
['192.168.0.0', '255.255.0.0'],
['10.0.0.0', '255.0.0.0'],
['172.0.0.0', '255.0.0.0'],
['127.0.0.0', '255.255.255.0']
];
var gi, whiteListed = {}, whiteList = [
"alipay.com",
"alipayobjects.com",
"baidu.com",
"com.cn",
"douban.com",
"edu.cn",
"feidee.com",
"icast.cn",
"kandian.com",
"mmcdn.cn",
"mmstat.com",
"renren.com",
"sinaapp.com",
"sinaimg.cn",
"sinajs.cn",
"tanx.com",
"taobao.com",
"taobaocdn.com",
"tbcdn.cn",
"v2ex.com",
"weibo.com"
];
for (gi = 0; gi < whiteList.length; gi += 1) { whiteListed[whiteList[gi]] = true; }
function isLocal(host) {
var i, ip, netmask;
if (isPlainHostName(host)) { return true; }
for (i = 0; i < localhosts.length; i++) {
ip = localhosts[i][0];
netmask = localhosts[i][1];
if (isInNet(host, ip, netmask)) { return true; }
}
return false;
}
function hostToDomain(host) {
var dotpos = host.lastIndexOf(".");
if (dotpos === -1) { return host; }
// Find the second last dot
dotpos = host.lastIndexOf(".", dotpos - 1);
if (dotpos === -1) { return host; }
return host.substring(dotpos + 1);
}
function FindProxyForURL(url, host) {
if (isLocal(host)) { return noProxy; }
return (whiteListed[hostToDomain(host)] ? noProxy : proxy);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment