Last active
October 20, 2016 12:02
-
-
Save bramblex/530678142758cc01913e4f0d794460d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Update At 2016-10-20 19:59:47; | |
var proxy = "SOCKS5 127.0.0.1:1080; SOCKS 127.0.0.1:1080; DIRECT;"; | |
var rules = { | |
"amazonaws.com": true, | |
"phonegap.com": true, | |
"wordpress.com": true, | |
"google.com": true, | |
"gmail.com": true, | |
"gstatic.com": true, | |
"googleusercontent.com": true, | |
"googleapis.com": true, | |
"goo.gl": true, | |
"googlecode.com": true, | |
"google-analytics.com": true, | |
"googlevideo.com": true, | |
"youtube.com": true, | |
"ytimg.com": true, | |
"ggpht.com": true, | |
"facebook.com": true, | |
"facebook.net": true, | |
"akamaihd.net": true, | |
"wikipedia.org": true, | |
"redtube.com": true, | |
"live.com": true, | |
"dropbox.com": true, | |
"sourceforge.net": true, | |
"github.com": true, | |
"github.io": true, | |
"githubusercontent.com": true, | |
"disqus.com": true, | |
"w.org": true, | |
"twitter.com": true, | |
"twimg.com": true, | |
"eurekavpt.com": true, | |
"teamviewer.com": true, | |
"reddit.com": true, | |
"redditstatic.com": true, | |
"redditmedia.com": true, | |
"pastie.org": true, | |
"haskell.org": true, | |
"npmjs.com": true, | |
"pixnet.net": true, | |
"cilibaba.net": true, | |
"cloudfront.net": true, | |
"tampermonkey.net": true, | |
"stackoverflow.com": true, | |
"leetcode.com": true, | |
"slideshare.net": true, | |
"googletagservices.com": true, | |
"doubleclick.net": true, | |
"hola.org": true, | |
"google.co.jp": true, | |
"google.com.hk": true, | |
"livecoding.tv": true, | |
"sequelizejs.com": true, | |
"91zll.com": true, | |
"123systems.net": true, | |
"jetbrains.com": true, | |
"jetbrains.org": true, | |
"npmjs.org": true, | |
"persona.org": true, | |
"mozilla.org": true, | |
"mzl.la": true, | |
"ruanyifeng.com": true, | |
"mailchimp.com": true, | |
"apple.com": true, | |
"quora.com": true, | |
"quoracdn.net": true, | |
"withgoogle.com": true, | |
"jsdelivr.net": true, | |
"rawgit.com": true, | |
"vuejs.org": true, | |
"blogger.com": true, | |
"disquscdn.com": true, | |
"eslint.org": true, | |
"wordreference.com": true, | |
"html2jade.org": true, | |
"nodejs.org": true, | |
"appstorm.net": true, | |
"nwjs.io": true, | |
"chrome.com": true | |
}; | |
var getRootDomain = function getRootDomain(url) { | |
var subdomian_map = {"com":true, "org":true, "net":true, "edu":true, "gov":true, "ac":true, "co":true} | |
var domian_list = url.match(/^(https?\:\/\/|)([^\/?#]+)(?:[\/?#]|$)/i)[2].split('.'); | |
return subdomian_map[domian_list[domian_list.length-2]] | |
? domian_list.slice(-3).join('.') | |
: domian_list.slice(-2).join('.'); | |
}; | |
var FindProxyForURL = function FindProxyForURL(url, host) { | |
var root_domain = getRootDomain(url); | |
return rules[root_domain] ? proxy : 'DIRECT;'; | |
}; | |
var runIfNode = function (){ | |
if (typeof module !== 'undefined' && module.exports){ | |
var ncr = function ncr(router){ | |
var path = process.argv.slice(2); | |
var current, i; | |
var next = router; | |
for(i=0; i<path.length; i++){ | |
current = next; | |
next = next[path[i]]; | |
if (typeof next === 'object') continue; else break; | |
} | |
if (typeof next === 'function'){ | |
var args_needed = next.toString().match(/^(function|)\s*\S*\s*\((.*?)\)/)[2] | |
.split(',').filter(function(item){return item.trim() !== ''}); | |
var args = path.slice(i+1); | |
if (args_needed.length !== 0 && args.length !== args_needed.length ){ | |
throw new Error('Command "'+path[i]+'" need agruments: '+args_needed.map(function(item){return '<'+item+'>'}).join(' ')); | |
} else { | |
return next.apply(current, args); | |
} | |
} else if (typeof next === 'object'){ | |
if (typeof next['?'] === 'function') return next['?'].apply(next, path.slice(i+1)); | |
throw new Error('Need more arguments, like '+JSON.stringify(Object.keys(next).filter(function(item){return item !== '?'}))) | |
} else { | |
throw new Error('Command "'+path[i]+'" not found. Do you mean '+JSON.stringify(Object.keys(current).filter(function(item){return item !== '?'}))) | |
} | |
}; | |
var refreshPac = function refreshPac(){ | |
var date_comment = '// Update At ' + (new Date()).toLocaleString(); | |
var proxy_define = 'var proxy = ' + JSON.stringify(proxy); | |
var rules_define = 'var rules = ' + JSON.stringify(rules, null, 4); | |
var getRootDomain_define = 'var getRootDomain = ' + getRootDomain.toString(); | |
var FindProxyForURL_define = 'var FindProxyForURL = ' + FindProxyForURL.toString(); | |
var runIfNode_define = 'var runIfNode = ' + runIfNode.toString(); | |
var code = [ | |
date_comment, | |
proxy_define, | |
rules_define, | |
getRootDomain_define, | |
FindProxyForURL_define, | |
runIfNode_define, | |
'runIfNode()' | |
].join(';\n\n') + ';'; | |
require('fs').writeFileSync(__filename, code, 'utf8'); | |
}; | |
ncr({ | |
proxy: { | |
show: function(){ | |
console.log(proxy); | |
}, | |
set: function(pac_proxy){ | |
proxy = pac_proxy; | |
refreshPac(); | |
console.log('proxy set:', pac_proxy); | |
} | |
}, | |
rule: { | |
list: function(){ | |
Object.keys(rules).sort().forEach(function(rule){console.log(rule)}); | |
}, | |
add: function(url_or_rule){ | |
rules[getRootDomain(url_or_rule)] = true; | |
refreshPac(); | |
console.log('rules add:', getRootDomain(url_or_rule)); | |
}, | |
rm: function(url_or_rule){ | |
delete rules[getRootDomain(url_or_rule)]; | |
refreshPac(); | |
console.log('rules rm:', getRootDomain(url_or_rule)); | |
} | |
}, | |
help: function(){ | |
console.log([ | |
'Usage: ', | |
'\tproxy set <pac_proxy>: set proxy', | |
'\tproxy show: show current proxy', | |
'\trule list: list all rules', | |
'\trule add <url_or_rule>: add rules', | |
'\trule rm <url_or_rule>: rm rules' | |
].join('\n')); | |
}, | |
'?': function(){ | |
this.help(); | |
process.exit(1); | |
} | |
}) | |
} | |
}; | |
runIfNode(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment