-
-
Save miletskiy/9dd838e032b3cd92c45606c0f6d97010 to your computer and use it in GitHub Desktop.
simple http proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import argparse | |
| import re | |
| import requests | |
| from bs4 import BeautifulSoup, Comment | |
| from flask import Flask | |
| from flask import Response | |
| app = Flask(__name__) | |
| @app.route('/') | |
| @app.route('/<path:url>') | |
| def proxy(url=''): | |
| url = '{}/{}'.format(args.target, url) | |
| req = requests.get(url) | |
| content_type = req.headers['content-type'] | |
| content = req.content | |
| if 'text/html' in content_type: | |
| soup = BeautifulSoup(content, 'html5lib') | |
| for line in soup.find_all(string=re_marker): | |
| if line.parent.name in ('script', 'style', 'code', '[document]'): | |
| continue | |
| if line.parent(text=lambda x: isinstance(x, Comment)): | |
| continue | |
| text = re.sub(u'\\b(\\w{{{}}})\\b'.format(args.symbols), u'\\1™', line, flags=re.U) | |
| line.replace_with(text) | |
| for link in soup.find_all('a', href=re_source): | |
| link['href'] = link['href'].replace(args.target, 'http://{}:{}'.format(args.hostname, args.port)) | |
| content = str(soup) | |
| return Response(content, content_type=req.headers['content-type']) | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser(description='TM HTTP Proxy.') | |
| parser.add_argument('--hostname', default='127.0.0.1', help='Default: 127.0.0.1') | |
| parser.add_argument('--port', default='8232', help='Default: 8232') | |
| parser.add_argument('--target', default='https://habrahabr.ru', help='Default: https://habrahabr.ru') | |
| parser.add_argument('--symbols', default='6', help='Default: 6') | |
| args = parser.parse_args() | |
| re_source = re.compile(args.target, re.U) | |
| re_marker = re.compile(u'.*', re.U) | |
| print('Proxy for {}'.format(args.target)) | |
| app.run(host=args.hostname, port=int(args.port)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment