Skip to content

Instantly share code, notes, and snippets.

@iwinux
Last active October 12, 2015 10:28
Show Gist options
  • Save iwinux/4012929 to your computer and use it in GitHub Desktop.
Save iwinux/4012929 to your computer and use it in GitHub Desktop.
PAC that uses a white list
/*global isPlainHostName, isInNet*/
// inspired by http://chenyufei.info/blog/2012-03-18/pac-and-debug
var proxy = "SOCKS5 127.0.0.1:9999; SOCKS 127.0.0.1:9999; DIRECT"
var noProxy = "DIRECT"
var localhosts = [
['0.0.0.0', '255.255.255.255']
, ['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 = [
"126.net"
, "163.com"
, "netease.com"
, "alipay.com"
, "alipayobjects.com"
, "baidu.com"
, "martianz.cn"
, "martianlaboratory.com"
, "bdimg.com"
, "beansmile.com"
, "campfirenow.com"
, "alimama.com"
, "com.cn"
, "diandian.com"
, "douban.com"
, "edu.cn"
, "feidee.com"
, "google-analytics.com"
, "guokr.com"
, "iask.com"
, "doitim.com"
, "icast.cn"
, "imrworldwide.com"
, "kandian.com"
, "libdd.com"
, "mmcdn.cn"
, "mmstat.com"
, "newrelic.com"
, "renren.com"
, "rrfmn.com"
, "rrimg.com"
, "sinaapp.com"
, "sinaimg.cn"
, "sinajs.cn"
, "sohu.com"
, "tanx.com"
, "taobao.com"
, "taobaocdn.com"
, "tbcdn.cn"
, "tdimg.com"
, "tianyaui.com"
, "v2ex.com"
, "weibo.com"
, "workflowy.com"
, "xiaonei.com"
, "xnimg.cn"
, "ydstatic.com"
, "youdao.com"
, "qq.com"
, "zhi.hu"
, "zhihu.com"
, "miaozhen.com"
, "alimama.cn"
, "kankan.com"
, "sandai.net"
, "youku.com"
, "adroll.com"
, "tudouui.com"
, "tudou.com"
, "voicefive.com"
, "stfile.com"
, "xunlei.com"
, "zhimg.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