Repository navigation
Expand file tree
/
Copy pathpac_server.py
More file actions
341 lines (301 loc) · 15.3 KB
/
Copy pathpac_server.py
File metadata and controls
341 lines (301 loc) · 15.3 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python
# coding:utf-8
import os
import base64
import time
import re
import _thread
import urllib.request, urllib.error, urllib.parse
import urllib.parse
import simple_http_server
from xlog import getLogger
xlog = getLogger("gae_proxy")
from .config import config
default_pacfile = os.path.join(os.path.dirname(os.path.abspath(__file__)), config.PAC_FILE)
user_pacfile = os.path.join(config.DATA_PATH, config.PAC_FILE)
current_path = os.path.dirname(os.path.abspath(__file__))
root_path = os.path.abspath( os.path.join(current_path, os.pardir, os.pardir))
data_root = os.path.join(root_path, 'data')
gae_proxy_listen = "GOAGENT_LISTEN"
pac_listen = "PAC_LISTEN"
def get_serving_pacfile():
if not os.path.isfile(user_pacfile):
serving_pacfile = default_pacfile
else:
serving_pacfile = user_pacfile
return serving_pacfile
def get_opener():
autoproxy = '127.0.0.1:%s' % config.LISTEN_PORT
import ssl
if getattr(ssl, "create_default_context", None):
cafile = os.path.join(data_root, "gae_proxy", "CA.crt")
context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH,
cafile=cafile)
https_handler = urllib.request.HTTPSHandler(context=context)
opener = urllib.request.build_opener(urllib.request.ProxyHandler({'http': autoproxy, 'https': autoproxy}), https_handler)
else:
opener = urllib.request.build_opener(urllib.request.ProxyHandler({'http': autoproxy, 'https': autoproxy}))
return opener
class PacUtil(object):
"""GAEProxy Pac Util"""
@staticmethod
def update_pacfile(filename):
opener = get_opener()
listen_ip = config.LISTEN_IP
autoproxy = gae_proxy_listen
blackhole = pac_listen
default = 'DIRECT'
if config.PAC_ADBLOCK:
try:
xlog.info('try download %r to update_pacfile(%r)', config.PAC_ADBLOCK, filename)
adblock_content = opener.open(config.PAC_ADBLOCK).read()
except Exception as e:
xlog.warn("pac_update download adblock fail:%r", e)
return
try:
xlog.info('try download %r to update_pacfile(%r)', config.PAC_GFWLIST, filename)
pac_content = opener.open(config.PAC_GFWLIST).read().decode('iso-8859-1')
except Exception as e:
xlog.warn("pac_update download gfwlist fail:%r", e)
return
content = ''
need_update = True
with open(get_serving_pacfile(), 'rb') as fp:
content = fp.read().decode('iso-8859-1')
try:
placeholder = '// AUTO-GENERATED RULES, DO NOT MODIFY!'
content = content[:content.index(placeholder)+len(placeholder)]
content = re.sub(r'''blackhole\s*=\s*['"]PROXY [\.\w:]+['"]''', 'blackhole = \'PROXY %s\'' % blackhole, content)
content = re.sub(r'''autoproxy\s*=\s*['"]PROXY [\.\w:]+['"]''', 'autoproxy = \'PROXY %s\'' % autoproxy, content)
if content.startswith('//'):
line = '// Proxy Auto-Config file generated by autoproxy2pac, %s\r\n' % time.strftime('%Y-%m-%d %H:%M:%S')
content = line + '\r\n'.join(content.splitlines()[1:])
except ValueError:
need_update = False
try:
if config.PAC_ADBLOCK:
xlog.info('%r downloaded, try convert it with adblock2pac', config.PAC_ADBLOCK)
jsrule = PacUtil.adblock2pac(adblock_content, 'FindProxyForURLByAdblock', blackhole, default)
content += '\r\n' + jsrule + '\r\n'
xlog.info('%r downloaded and parsed', config.PAC_ADBLOCK)
else:
content += '\r\nfunction FindProxyForURLByAdblock(url, host) {return "DIRECT";}\r\n'
except Exception as e:
need_update = False
xlog.exception('update_pacfile failed: %r', e)
return
try:
autoproxy_content = base64.b64decode(pac_content).decode('iso-8859-1')
xlog.info('%r downloaded, try convert it with autoproxy2pac', config.PAC_GFWLIST)
jsrule = PacUtil.autoproxy2pac(autoproxy_content, 'FindProxyForURLByAutoProxy', autoproxy, default)
content += '\r\n' + jsrule + '\r\n'
xlog.info('%r downloaded and parsed', config.PAC_GFWLIST)
except Exception as e:
need_update = False
xlog.exception('update_pacfile failed: %r', e)
return
if need_update:
with open(user_pacfile, 'wb') as fp:
fp.write(content.encode())
xlog.info('%r successfully updated', user_pacfile)
serving_pacfile = user_pacfile
@staticmethod
def autoproxy2pac(content, func_name='FindProxyForURLByAutoProxy', proxy=gae_proxy_listen, default='DIRECT', indent=4):
"""Autoproxy to Pac, based on https://github.com/iamamac/autoproxy2pac"""
jsLines = []
for line in content.splitlines()[1:]:
if line and not line.startswith("!"):
use_proxy = True
if line.startswith("@@"):
line = line[2:]
use_proxy = False
return_proxy = 'PROXY %s' % proxy if use_proxy else default
if line.startswith('/') and line.endswith('/'):
jsLine = 'if (/%s/i.test(url)) return "%s";' % (line[1:-1], return_proxy)
elif line.startswith('||'):
domain = line[2:].lstrip('.')
if len(jsLines) > 0 and ('host.indexOf(".%s") >= 0' % domain in jsLines[-1] or 'host.indexOf("%s") >= 0' % domain in jsLines[-1]):
jsLines.pop()
jsLine = 'if (dnsDomainIs(host, ".%s") || host == "%s") return "%s";' % (domain, domain, return_proxy)
elif line.startswith('|'):
jsLine = 'if (url.indexOf("%s") == 0) return "%s";' % (line[1:], return_proxy)
elif '*' in line:
jsLine = 'if (shExpMatch(url, "*%s*")) return "%s";' % (line.strip('*'), return_proxy)
elif '/' not in line:
jsLine = 'if (host.indexOf("%s") >= 0) return "%s";' % (line, return_proxy)
else:
jsLine = 'if (url.indexOf("%s") >= 0) return "%s";' % (line, return_proxy)
jsLine = ' ' * indent + jsLine
if use_proxy:
jsLines.append(jsLine)
else:
jsLines.insert(0, jsLine)
function = 'function %s(url, host) {\r\n%s\r\n%sreturn "%s";\r\n}' % (func_name, '\n'.join(jsLines), ' '*indent, default)
return function
@staticmethod
def urlfilter2pac(content, func_name='FindProxyForURLByUrlfilter', proxy='127.0.0.1:8086', default='DIRECT', indent=4):
"""urlfilter.ini to Pac, based on https://github.com/iamamac/autoproxy2pac"""
jsLines = []
for line in content[content.index('[exclude]'):].splitlines()[1:]:
if line and not line.startswith(';'):
use_proxy = True
if line.startswith("@@"):
line = line[2:]
use_proxy = False
return_proxy = 'PROXY %s' % proxy if use_proxy else default
if '*' in line:
jsLine = 'if (shExpMatch(url, "%s")) return "%s";' % (line, return_proxy)
else:
jsLine = 'if (url == "%s") return "%s";' % (line, return_proxy)
jsLine = ' ' * indent + jsLine
if use_proxy:
jsLines.append(jsLine)
else:
jsLines.insert(0, jsLine)
function = 'function %s(url, host) {\r\n%s\r\n%sreturn "%s";\r\n}' % (func_name, '\n'.join(jsLines), ' '*indent, default)
return function
@staticmethod
def adblock2pac(content, func_name='FindProxyForURLByAdblock', proxy='127.0.0.1:8086', default='DIRECT', indent=4):
"""adblock list to Pac, based on https://github.com/iamamac/autoproxy2pac"""
jsLines = []
for line in content.splitlines()[1:]:
if not line or line.startswith('!') or '##' in line or '#@#' in line:
continue
use_proxy = True
use_start = False
use_end = False
use_domain = False
use_postfix = []
if '$' in line:
posfixs = line.split('$')[-1].split(',')
if any('domain' in x for x in posfixs):
continue
if 'image' in posfixs:
use_postfix += ['.jpg', '.gif']
elif 'script' in posfixs:
use_postfix += ['.js']
else:
continue
line = line.split('$')[0]
if line.startswith("@@"):
line = line[2:]
use_proxy = False
if '||' == line[:2]:
line = line[2:]
if '/' not in line:
use_domain = True
else:
if not line.startswith('http://'):
line = 'http://' + line
use_start = True
elif '|' == line[0]:
line = line[1:]
if not line.startswith('http://'):
line = 'http://' + line
use_start = True
if line[-1] in ('^', '|'):
line = line[:-1]
if not use_postfix:
use_end = True
return_proxy = 'PROXY %s' % proxy if use_proxy else default
line = line.replace('^', '*').strip('*')
if use_start and use_end:
if '*' in line:
jsLine = 'if (shExpMatch(url, "%s")) return "%s";' % (line, return_proxy)
else:
jsLine = 'if (url == "%s") return "%s";' % (line, return_proxy)
elif use_start:
if '*' in line:
if use_postfix:
jsCondition = ' || '.join('shExpMatch(url, "%s*%s")' % (line, x) for x in use_postfix)
jsLine = 'if (%s) return "%s";' % (jsCondition, return_proxy)
else:
jsLine = 'if (shExpMatch(url, "%s*")) return "%s";' % (line, return_proxy)
else:
jsLine = 'if (url.indexOf("%s") == 0) return "%s";' % (line, return_proxy)
elif use_domain and use_end:
if '*' in line:
jsLine = 'if (shExpMatch(host, "%s*")) return "%s";' % (line, return_proxy)
else:
jsLine = 'if (host == "%s") return "%s";' % (line, return_proxy)
elif use_domain:
if line.split('/')[0].count('.') <= 1:
if use_postfix:
jsCondition = ' || '.join('shExpMatch(url, "http://*.%s*%s")' % (line, x) for x in use_postfix)
jsLine = 'if (%s) return "%s";' % (jsCondition, return_proxy)
else:
jsLine = 'if (shExpMatch(url, "http://*.%s*")) return "%s";' % (line, return_proxy)
else:
if '*' in line:
if use_postfix:
jsCondition = ' || '.join('shExpMatch(url, "http://%s*%s")' % (line, x) for x in use_postfix)
jsLine = 'if (%s) return "%s";' % (jsCondition, return_proxy)
else:
jsLine = 'if (shExpMatch(url, "http://%s*")) return "%s";' % (line, return_proxy)
else:
if use_postfix:
jsCondition = ' || '.join('shExpMatch(url, "http://%s*%s")' % (line, x) for x in use_postfix)
jsLine = 'if (%s) return "%s";' % (jsCondition, return_proxy)
else:
jsLine = 'if (url.indexOf("http://%s") == 0) return "%s";' % (line, return_proxy)
else:
if use_postfix:
jsCondition = ' || '.join('shExpMatch(url, "*%s*%s")' % (line, x) for x in use_postfix)
jsLine = 'if (%s) return "%s";' % (jsCondition, return_proxy)
else:
jsLine = 'if (shExpMatch(url, "*%s*")) return "%s";' % (line, return_proxy)
jsLine = ' ' * indent + jsLine
if use_proxy:
jsLines.append(jsLine)
else:
jsLines.insert(0, jsLine)
function = 'function %s(url, host) {\r\n%s\r\n%sreturn "%s";\r\n}' % (func_name, '\n'.join(jsLines), ' '*indent, default)
return function
class PACServerHandler(simple_http_server.HttpServerHandler):
onepixel = 'GIF89a\x01\x00\x01\x00\x80\xff\x00\xc0\xc0\xc0\x00\x00\x00!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;'
def address_string(self):
return '%s:%s' % self.client_address[:2]
def do_CONNECT(self):
self.wfile.write(b'HTTP/1.1 403\r\nConnection: close\r\n\r\n')
def do_GET(self):
xlog.info('PAC from:%s %s %s ', self.address_string(), self.command, self.path)
path = urllib.parse.urlparse(self.path).path # '/proxy.pac'
filename = os.path.normpath('./' + path) # proxy.pac
if self.path.startswith(('http://', 'https://')):
data = 'HTTP/1.1 200\r\nCache-Control: max-age=86400\r\nExpires:Oct, 01 Aug 2100 00:00:00 GMT\r\nConnection: close\r\n'
if filename.endswith(('.jpg', '.gif', '.jpeg', '.bmp')):
data += 'Content-Type: image/gif\r\n\r\n' + self.onepixel
else:
data += '\r\n This is the Pac server, not proxy port, use 8087 as proxy port.'
self.wfile.write(data.encode())
xlog.info('%s "%s %s HTTP/1.1" 200 -', self.address_string(), self.command, self.path)
return
# check for '..', which will leak file
if re.search(r'(\.{2})', self.path) is not None:
self.wfile.write(b'HTTP/1.1 404\r\n\r\n')
xlog.warn('%s %s %s haking', self.address_string(), self.command, self.path )
return
if filename != 'proxy.pac':
xlog.warn("pac_server GET %s fail", filename)
self.wfile.write(b'HTTP/1.1 404\r\n\r\n')
return
mimetype = 'text/plain'
if self.path.endswith('.pac?flush') or time.time() - os.path.getmtime(get_serving_pacfile()) > config.PAC_EXPIRED:
_thread.start_new_thread(PacUtil.update_pacfile, (user_pacfile,))
pac_filename = get_serving_pacfile()
with open(pac_filename, 'rb') as fp:
data = fp.read().decode('iso-8859-1')
host = self.headers['Host']
host, _, port = host.rpartition(":")
gae_proxy_proxy = host + ":" + str(config.LISTEN_PORT)
pac_proxy = host + ":" + str(config.PAC_PORT)
data = data.replace(gae_proxy_listen, gae_proxy_proxy)
data = data.replace(pac_listen, pac_proxy)
self.wfile.write(('HTTP/1.1 200\r\nContent-Type: %s\r\nContent-Length: %s\r\n\r\n' % (mimetype, len(data))).encode())
self.wfile.write(data.encode())
def send_file(self, filename, mimetype):
with open(filename, 'rb') as fp:
data = fp.read()
if data:
self.wfile.write(('HTTP/1.1 200\r\nContent-Type: %s\r\nContent-Length: %s\r\n\r\n' % (mimetype, len(data))).encode())
self.wfile.write(data)