Add simple proxy for replacing inline extension with 4chan X.

This commit is contained in:
ccd0 2015-07-30 21:05:17 -07:00
parent f8fef832bd
commit c5eb74ba5f

41
tools/proxy.py Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python3
import http.server, http.client, sys, re
proxyConfig = b'''function FindProxyForURL(url, host) {
if (/^http:\/\/s\.4cdn\.org\/(js\/extension\.min\.\d+\.js)?$/.test(url)) {
return 'PROXY localhost:8000';
}
return 'DIRECT';
}
'''
class ExtensionReplacer(http.server.BaseHTTPRequestHandler):
def do_HEAD(self):
self.do_GET()
def do_GET(self):
if self.path == '/proxy.pac':
self.send_response(200, 'OK')
self.send_header('Content-Length', len(proxyConfig))
self.end_headers()
if self.command != 'HEAD':
self.wfile.write(proxyConfig)
elif re.match(r'http://s\.4cdn\.org/js/extension.min.\d+.js$', self.path):
self.send_response(302, 'Found')
self.send_header('Location', 'https://ccd0.github.io/4chan-x/builds/4chan-X.user.js')
self.end_headers()
else:
conn = http.client.HTTPConnection('s.4cdn.org')
conn.request(self.command, self.path, headers=self.headers)
response = conn.getresponse()
self.send_response(response.status, response.reason)
for header, value in response.getheaders():
if header not in ('Date', 'Connection'):
self.send_header(header, value)
self.end_headers()
self.wfile.write(response.read())
conn.close()
port = int(sys.argv[1]) if 1 < len(sys.argv) else 8000
httpd = http.server.HTTPServer(('localhost', port), ExtensionReplacer)
httpd.serve_forever()