#!/usr/bin/env python from SimpleHTTPProxy import SimpleHTTPProxyHandler, test import re class RemoveIframeProxyHandler(SimpleHTTPProxyHandler): def response_handler(self, req, reqbody, res, resbody): content_type = res.headers.get('Content-Type', '') if content_type.startswith('text/html'): return re.sub(r'<iframe[\s\S]+?</iframe>', '<iframe removed>', resbody) if __name__ == '__main__': test(HandlerClass=RemoveIframeProxyHandler)
version_table = {10: "HTTP/1.0", 11: "HTTP/1.1", 9: "HTTP/0.9"} def save_handler(self, req, reqbody, res, resbody): # limit the length of showing reqbody if reqbody and len(reqbody) > 8192: reqbody = reqbody[:8192] + " [...]" text = """---- %s %s %r %s %d %s %s """ % ( req.requestline, str(req.headers), reqbody, self.version_table[res.version], res.status, res.reason, str(res.headers), ) # print text.replace('\n', '\x1bE') # use NEL control character for line break (useful for grep) print text sys.stdout.flush() if __name__ == "__main__": test(HandlerClass=CatHeadersProxyHandler)
#!/usr/bin/env python from SimpleHTTPProxy import SimpleHTTPProxyHandler, test import os class SaveImagesProxyHandler(SimpleHTTPProxyHandler): def url2path(self, url): schema, _, urlpath = self.path.split('/', 2) fpath = "SaveImagesProxy/%s/%s" % (schema.rstrip(':'), urlpath.split('?')[0]) fdir, fname = os.path.split(fpath) if not fname: fname = 'index.html' fpath = os.path.join(fdir, fname) return fpath def save_handler(self, req, reqbody, res, resbody): content_type = res.headers.get('Content-Type', '') if content_type.startswith('image/'): fpath = self.url2path(self.path) fdir = os.path.dirname(fpath) if not os.path.isdir(fdir): os.makedirs(fdir) with open(fpath, 'wb') as f: f.write(resbody) if __name__ == '__main__': test(HandlerClass=SaveImagesProxyHandler)
self.connection = ssl.wrap_socket( self.connection, keyfile=self.keyfile, certfile=certpath, server_side=True ) else: self.connection = ssl.wrap_socket( self.connection, keyfile=self.keyfile, certfile=self.certfile, server_side=True ) self.rfile = self.connection.makefile("rb", self.rbufsize) self.wfile = self.connection.makefile("wb", self.wbufsize) self.https_origin = req.path.rstrip("/") return True elif req.command == "GET" and req.path == "http://proxy.test/": with open(self.ca_certfile, "rb") as f: data = f.read() self.send_response(200) self.send_header("Content-Type", "application/x-x509-ca-cert") self.send_header("Content-Length", len(data)) self.send_header("Connection", "close") self.end_headers() self.wfile.write(data) return True else: if hasattr(self, "https_origin"): req.path = self.https_origin + req.path if __name__ == "__main__": test(HandlerClass=SSLBumpProxyHandler)
#!/usr/bin/env python # -*- coding: utf-8 -*- from SimpleHTTPProxy import SimpleHTTPProxyHandler, test import socket class SendSSTPProxyHandler(SimpleHTTPProxyHandler): version_table = {10: 'HTTP/1.0', 11: 'HTTP/1.1', 9: 'HTTP/0.9'} def save_handler(self, req, reqbody, res, resbody): reqmsg = r"%s\nReferer: %s" % (req.requestline, req.headers.get('Referer', '')) resmsg = r"%s %d %s\nContent-Type: %s\nContent-Encoding: %s\nContent-Length: %s" % (self.version_table[res.version], res.status, res.reason, res.headers.get('Content-Type', ''), res.headers.get('Content-Encoding', ''), res.headers.get('Content-Length', '')) s = socket.create_connection(('localhost', 9801)) text = r"""SEND SSTP/1.1 Sender: SendSSTPProxy Script: \v\_q\h\s0%s\u%s\e Option: nodescript,notranslate Charset: UTF-8 """ % (reqmsg, resmsg) s.sendall(text.replace("\n", "\r\n")) s.close() if __name__ == '__main__': test(HandlerClass=SendSSTPProxyHandler)
#!/usr/bin/env python from SimpleHTTPProxy import SimpleHTTPProxyHandler, test class ChangeUAProxyHandler(SimpleHTTPProxyHandler): def request_handler(self, req, reqbody): req.headers[ 'User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25' # req.headers['User-Agent'] = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' if __name__ == '__main__': test(HandlerClass=ChangeUAProxyHandler)
http_url = re.sub(r'^https://', 'http://', location) self.forward_table[http_url] = location res.headers['Location'] = http_url content_type = res.headers.get('Content-Type', '') if content_type.startswith('text/html') or content_type.startswith('text/css') or content_type.startswith('text/javascript'): re_url = r'((["\'])\s*)https://([\w\-.~%!$&\'()*+,;=:@/?#]+)(\s*\2)' # based on RFC 3986 def replace_method(m): raw_path = m.group(3).replace('&', '&') self.forward_table['http://' + raw_path] = 'https://' + raw_path return '%shttp://%s%s' % (m.group(1), m.group(3), m.group(4)) resbody = re.sub(re_url, replace_method, resbody) return resbody def ssl_request_handler(self, req, reqbody): # override here # return True if you sent the response here and the proxy should not connect to the upstream server # return replaced reqbody (other than None and True) if you did pass def ssl_response_handler(self, req, reqbody, res, resbody): # override here # return True if you sent the response here and the proxy should not connect to the upstream server # return replaced resbody (other than None and True) if you did pass if __name__ == '__main__': test(HandlerClass=SSLStripProxyHandler)
from SimpleHTTPProxy import SimpleHTTPProxyHandler, test import sys class CatHeadersProxyHandler(SimpleHTTPProxyHandler): version_table = {10: 'HTTP/1.0', 11: 'HTTP/1.1', 9: 'HTTP/0.9'} def save_handler(self, req, reqbody, res, resbody): # limit the length of showing reqbody if reqbody and len(reqbody) > 8192: reqbody = reqbody[:8192] + ' [...]' text = """---- %s %s %r %s %d %s %s """ % (req.requestline, str(req.headers), reqbody, self.version_table[res.version], res.status, res.reason, str( res.headers)) # print text.replace('\n', '\x1bE') # use NEL control character for line break (useful for grep) print text sys.stdout.flush() if __name__ == '__main__': test(HandlerClass=CatHeadersProxyHandler)
#!/usr/bin/env python from SimpleHTTPProxy import SimpleHTTPProxyHandler, test import re class DenyProxyHandler(SimpleHTTPProxyHandler): def request_handler(self, req, reqbody): m = re.match(r'https?://[^/]*google-analytics\.com/', req.path) if m: self.send_error(503) return True if __name__ == '__main__': test(HandlerClass=DenyProxyHandler)
#!/usr/bin/env python from SimpleHTTPProxy import SimpleHTTPProxyHandler, test import re class StripAmazonProxyHandler(SimpleHTTPProxyHandler): def request_handler(self, req, reqbody): m = re.match(r'http://www\.amazon\.co\.jp/[^?]+?/dp/([A-Z0-9]{10})', req.path) if m: redirect_url = "http://www.amazon.co.jp/dp/%s" % m.group(1) if redirect_url == req.path: return self.send_response(302) self.send_header('Location', redirect_url) self.send_header('Connection', 'close') self.end_headers() return True if __name__ == '__main__': test(HandlerClass=StripAmazonProxyHandler)
#!/usr/bin/env python from SimpleHTTPProxy import SimpleHTTPProxyHandler, test class ChangeUAProxyHandler(SimpleHTTPProxyHandler): def request_handler(self, req, reqbody): req.headers['User-Agent'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25' # req.headers['User-Agent'] = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' if __name__ == '__main__': test(HandlerClass=ChangeUAProxyHandler)
from SimpleHTTPProxy import SimpleHTTPProxyHandler, test import socket class SendSSTPProxyHandler(SimpleHTTPProxyHandler): version_table = {10: 'HTTP/1.0', 11: 'HTTP/1.1', 9: 'HTTP/0.9'} def save_handler(self, req, reqbody, res, resbody): reqmsg = r"%s\nReferer: %s" % (req.requestline, req.headers.get('Referer', '')) resmsg = r"%s %d %s\nContent-Type: %s\nContent-Encoding: %s\nContent-Length: %s" % ( self.version_table[res.version], res.status, res.reason, res.headers.get('Content-Type', ''), res.headers.get('Content-Encoding', ''), res.headers.get('Content-Length', '')) s = socket.create_connection(('localhost', 9801)) text = r"""SEND SSTP/1.1 Sender: SendSSTPProxy Script: \v\_q\h\s0%s\u%s\e Option: nodescript,notranslate Charset: UTF-8 """ % (reqmsg, resmsg) s.sendall(text.replace("\n", "\r\n")) s.close() if __name__ == '__main__': test(HandlerClass=SendSSTPProxyHandler)
#!/usr/bin/env python from SimpleHTTPProxy import SimpleHTTPProxyHandler, test class HideRefererProxyHandler(SimpleHTTPProxyHandler): def request_handler(self, req, reqbody): req.headers['Referer'] = req.path # del req.headers['Referer'] if __name__ == '__main__': test(HandlerClass=HideRefererProxyHandler)
certfile=certpath, server_side=True) else: self.connection = ssl.wrap_socket(self.connection, keyfile=self.keyfile, certfile=self.certfile, server_side=True) self.rfile = self.connection.makefile("rb", self.rbufsize) self.wfile = self.connection.makefile("wb", self.wbufsize) self.https_origin = req.path.rstrip('/') return True elif req.command == 'GET' and req.path == 'http://proxy.test/': with open(self.ca_certfile, 'rb') as f: data = f.read() self.send_response(200) self.send_header('Content-Type', 'application/x-x509-ca-cert') self.send_header('Content-Length', len(data)) self.send_header('Connection', 'close') self.end_headers() self.wfile.write(data) return True else: if hasattr(self, 'https_origin'): req.path = self.https_origin + req.path if __name__ == '__main__': test(HandlerClass=SSLBumpProxyHandler)