Skip to content

Instantly share code, notes, and snippets.

@wangjiezhe
Created November 24, 2016 10:26
Show Gist options
  • Save wangjiezhe/8a6f1462bb7ccd7df052586c03c17d6e to your computer and use it in GitHub Desktop.
Save wangjiezhe/8a6f1462bb7ccd7df052586c03c17d6e to your computer and use it in GitHub Desktop.
Pac for domestic ip in PKU
#!/usr/bin/env python
import re
from urllib.request import urlopen
url = 'https://its.pku.edu.cn/oper/liebiao.jsp'
with urlopen(url) as response:
the_page = response.read().decode('gbk')
loc1 = the_page.find('netmask')
loc1 += 7
while not the_page[loc1].isdigit():
loc1 += 1
loc2 = the_page.find('</pre>')
addresses = the_page[loc1:loc2]
addrlist = addresses.split('\n')
with open('cernet.pac', 'w') as f:
f.write('''function FindProxyForURL(url, host)
{
if( isPlainHostName(host) || !isResolvable(host) ) return "DIRECT";
rip=dnsResolve(host);
if( isInNet(rip,"127.0.0.0","255.0.0.0") ||
''')
rule = re.compile(r'^\s*(?P<netnum>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(?P<hostbits>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*(?P<netmark>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*')
for line in addrlist:
matchlist = rule.search(line)
if(len(line) == 0):
continue
line4write = 'isInNet(rip,\"%s\",\"%s\") ||\n' % (matchlist.group('netnum'), matchlist.group('netmark'))
f.write(line4write)
f.write('''(shExpMatch(host, "*qq.com")) ||
(shExpMatch(host, "*taobao.com")) )return "DIRECT";
else return "PROXY 127.0.0.1:8087; DIRECT";
}
''')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment