Last active
March 14, 2017 11:19
-
-
Save xbdotl/d474c3f7dcf278657f2333356f243837 to your computer and use it in GitHub Desktop.
pac generator for china ip list
This file contains hidden or 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
| javascript: !function() { | |
| var CIDR_IP_LIST_URL = 'https://raw.githubusercontent.com/17mon/china_ip_list/master/china_ip_list.txt'; | |
| var PROXY_URL = 'Change this to your proxy address.'; | |
| var PAC_TMPL = [ | |
| '\'use strict\';', | |
| '', | |
| '// template literals', | |
| `var autoproxy = '${PROXY_URL}';`, | |
| '', | |
| 'var direct = \'DIRECT\';', | |
| 'var blackhole = \'0.0.0.0:65535\';', | |
| '', | |
| 'function FindProxyForLocalDirect(url, host){', | |
| ' if (', | |
| ' // auto generated', | |
| '__PLACE_HOLDER__', | |
| ' // end auto generated', | |
| ' isPlainHostName(host) ||', | |
| ' isInNet(host, "10.0.0.0", "255.0.0.0") ||', | |
| ' isInNet(host, "192.168.0.0", "255.255.0.0") ||', | |
| ' isInNet(host, "127.0.0.0", "255.0.0.0") ||', | |
| ' isInNet(host, "172.16.0.0", "255.255.0.0")', | |
| ' ){', | |
| ' return true;', | |
| ' }', | |
| ' return false;', | |
| '};', | |
| 'function FindProxyForURL(url, host) {', | |
| ' if(FindProxyForLocalDirect(url, host)){', | |
| ' return direct;', | |
| ' }', | |
| ' return autoproxy;', | |
| '};' | |
| ].join('\n'); | |
| fetch(CIDR_IP_LIST_URL).then(function(data) { | |
| return data.text(); | |
| }).then(function(data) { | |
| return data.split('\n'); | |
| }).then(function(data) { | |
| return data.map(function(i) { | |
| var arr = i.split('/'); | |
| var mask = cidrToMask(arr[1]); | |
| return ' isInNet(host, "' + arr[0] + '", "' + mask + '") ||' | |
| }).join('\n'); | |
| }).then(function(data) { | |
| var pac = PAC_TMPL.replace('__PLACE_HOLDER__', data); | |
| console.log(pac); | |
| return window.location.href = ('data:application/x-javascript;base64, ' + btoa(pac)); | |
| }).catch(function(err) { | |
| console.error(err); | |
| alert('Generate PAC config failed! See console error logs.'); | |
| return ''; | |
| }); | |
| function cidrToMask(cidr) { | |
| var tmp = (Math.pow(2, 32) - Math.pow(2, 32 - cidr)).toString(16).padEnd(8, '0').split(''); | |
| tmp.splice(2, -1, '.'); | |
| tmp.splice(5, -1, '.'); | |
| tmp.splice(8, -1, '.'); | |
| return tmp.join('').split('.').map(function(i) { | |
| return parseInt(i, 16); | |
| }).join('.') | |
| } | |
| }(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment