forked from qwj418/Flora_Pac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflora_pac
More file actions
executable file
·146 lines (127 loc) · 4.5 KB
/
flora_pac
File metadata and controls
executable file
·146 lines (127 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
# Flora_Pac by @leaskh
# www.leaskh.com, i@leaskh.com
# Based on chnroutes project (by Numb.Majority@gmail.com)
import re
import urllib2
import argparse
import math
import socket
import struct
def ip2long(ip):
packedIP = socket.inet_aton(ip)
return struct.unpack("!L", packedIP)[0]
def generate_pac(proxy):
results = fetch_ip_data()
pacfile = 'flora_pac.pac'
rfile = open(pacfile, 'w')
results.insert(0, (ip2long('127.0.0.1'), ip2long('255.0.0.0'), 0))
results.insert(1, (ip2long('10.0.0.0'), ip2long('255.0.0.0'), 0))
results.insert(2, (ip2long('172.16.0.0'), ip2long('255.240.0.0'), 0))
results.insert(3, (ip2long('192.168.0.0'), ip2long('255.255.0.0'), 0))
strLines = (
"// Flora_Pac by @leaskh"
"\n// www.leaskh.com, i@leaskh.com"
"\n"
"\nfunction FindProxyForURL(url, host) {"
"\n"
"\n var list = ["
)
intLines = 0
for ip,mask,_ in results:
if intLines > 0:
strLines = strLines + ','
intLines = intLines + 1
strLines = strLines + "\n [%d, %d]"%(ip, mask)
strLines = strLines + (
"\n ];"
"\n"
"\n function convert_addr(ipchars) {"
"\n var bytes = ipchars.split('.');"
"\n var result = ((bytes[0] & 0xff) << 24) |"
"\n ((bytes[1] & 0xff) << 16) |"
"\n ((bytes[2] & 0xff) << 8) |"
"\n (bytes[3] & 0xff);"
"\n return result;"
"\n }"
"\n"
"\n function isInNet(ipaddr, pattern, mask) {"
"\n return (ipaddr & mask) === (pattern & mask);"
"\n }"
"\n"
"\n if (isPlainHostName(host)"
"\n || (host === '127.0.0.1')"
"\n || (host === 'localhost')"
"\n || (/\\b([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\\b/.test(host))) {"
"\n return 'DIRECT';"
"\n }"
"\n"
"\n var strIp = dnsResolve(host);"
"\n if (!strIp) {"
"\n return 'DIRECT';"
"\n }"
"\n"
"\n var intIp = convert_addr(strIp);"
"\n for (var i = 0; i < list.length; i++) {"
"\n if (isInNet(intIp, list[i][0], list[i][1])) {"
"\n return 'DIRECT';"
"\n }"
"\n }"
"\n"
"\n return '%s';"
"\n"
"\n}"
"\n"%(proxy)
)
rfile.write(strLines)
rfile.close()
print ("Rules: %d items.\n"
"Usage: Use the newly created %s as your web browser's automatic "
"proxy configuration (.pac) file."%(intLines, pacfile))
def fetch_ip_data():
#fetch data from apnic
print "Fetching data from apnic.net, it might take a few minutes, please wait..."
url=r'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest'
#url=r'http://flora/delegated-apnic-latest' #debug
data=urllib2.urlopen(url).read()
cnregex=re.compile(r'apnic\|cn\|ipv4\|[0-9\.]+\|[0-9]+\|[0-9]+\|a.*',re.IGNORECASE)
cndata=cnregex.findall(data)
results=[]
prev_net=''
for item in cndata:
unit_items=item.split('|')
starting_ip=unit_items[3]
num_ip=int(unit_items[4])
imask=0xffffffff^(num_ip-1)
#convert to string
imask=hex(imask)[2:]
mask=[0]*4
mask[0]=imask[0:2]
mask[1]=imask[2:4]
mask[2]='0' #imask[4:6]
mask[3]='0' #imask[6:8]
#convert str to int
mask=[ int(i,16 ) for i in mask]
mask="%d.%d.%d.%d"%tuple(mask)
#mask in *nix format
mask2=32-int(math.log(num_ip,2))
ip=starting_ip.split('.')
ip[2] = '0'
ip[3] = '0'
starting_ip = '.'.join(ip)
if starting_ip != prev_net:
results.append((ip2long(starting_ip), ip2long(mask), mask2))
prev_net = starting_ip
return results
if __name__=='__main__':
parser=argparse.ArgumentParser(description="Generate proxy auto-config rules.")
parser.add_argument('-x', '--proxy',
dest = 'proxy',
default = 'SOCKS 127.0.0.1:8964',
nargs = '?',
help = "Proxy Server, examples: "
"SOCKS 127.0.0.1:8964; "
"SOCKS5 127.0.0.1:8964; "
"PROXY 127.0.0.1:8964")
args = parser.parse_args()
generate_pac(args.proxy)