Last active
August 29, 2015 14:19
-
-
Save jpic/79aff38abb36d35034de to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import time | |
import sys | |
import os | |
import re | |
from static.apps import Cling | |
from libmproxy.flow import AppRegistry, version | |
from netlib import wsgi | |
import requests | |
import sys | |
from libmproxy import controller, protocol, tnetstring, filt, script, version | |
import mock | |
import logging | |
import os | |
from libmproxy import controller, proxy | |
from libmproxy.proxy.server import ProxyServer, ConnectionHandler | |
from libmproxy.proxy.primitives import TransparentProxyMode | |
from libmproxy.platform import resolver | |
from libmproxy.proxy.connection import ServerConnection | |
logger = logging.getLogger('devoproxy') | |
ch = logging.StreamHandler(sys.stdout) | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
ch.setFormatter(formatter) | |
logger.setLevel(logging.DEBUG) | |
logger.addHandler(ch) | |
from myscript import MyAppRegistry | |
def caching_proxy(environ, start_response): | |
"""Simplest possible application object""" | |
url = '%(wsgi.url_scheme)s://%(HTTP_HOST)s%(PATH_INFO)s' % environ | |
download_path = 'cache/%(HTTP_HOST)s%(PATH_INFO)s' % environ | |
if url.endswith('/'): | |
download_path += 'index.html' | |
elif re.search('pypi.python\.org/simple/\w+$', url): | |
download_path += '/index.html' | |
url += '/' | |
elif re.search('registry.npmjs.org/[^/]+$', url): | |
download_path += '/index.html' | |
url += '/' | |
if not os.path.exists(download_path): | |
download_dir = u'/'.join(download_path.split('/')[:-1]) | |
if not os.path.exists(download_dir): | |
os.makedirs(download_dir) | |
headers = {} | |
if 'HTTP_AUTHORIZATION' in environ: | |
headers['Authorization'] = environ['HTTP_AUTHORIZATION'] | |
r = requests.get(url, stream=True, headers=headers) | |
if r.status_code >= 400: | |
os.symlink(str(r.status_code), download_path) | |
else: | |
with open(download_path, 'wb+') as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: # filter out keep-alive new chunks | |
f.write(chunk) | |
f.flush() | |
if os.path.islink(download_path): | |
start_response(os.path.readlink(download_path)) | |
return [] | |
else: | |
return Cling('cache/%(HTTP_HOST)s' % environ)(environ, start_response) | |
class DevopMaster(controller.Master): | |
def handle_log(self, log): | |
logger.log(getattr(logging, log.level.upper()), log.msg) | |
def handle_clientconnect(self, flow): | |
logger.debug('handle_clientconnect') | |
flow.reply() | |
def handle_serverconnect(self, flow): | |
logger.debug('handle_serverconnect') | |
if not getattr(flow, 'hasreplied', False): | |
flow.reply() | |
flow.hasreplied = True | |
else: | |
logger.critical('not replying again') | |
def handle_response(self, flow): | |
logger.debug('handle_response') | |
flow.reply() | |
def handle_request(self, flow): | |
logger.debug('handle_request') | |
flow.reply() | |
def handle_request(self, f): | |
logger.debug('handle_request') | |
if f.live: | |
request = f.request | |
host = request.headers.get('Host')[0] | |
app = wsgi.WSGIAdaptor(caching_proxy, host, request.port, | |
version.NAMEVERSION) | |
err = app.serve( | |
f, | |
f.client_conn.wfile, | |
**{"mitmproxy.master": self} | |
) | |
if err: | |
self.add_event("Error in wsgi app. %s" % err, "error") | |
f.reply(protocol.KILL) | |
return | |
if f not in self.state.flows: # don't add again on replay | |
self.state.add_flow(f) | |
self.run_script_hook("request", f) | |
self.process_new_request(f) | |
return f | |
def run(self): | |
try: | |
return controller.Master.run(self) | |
except KeyboardInterrupt: | |
self.shutdown() | |
config = proxy.ProxyConfig(port=7000, mode='transparent', cadir='/etc/devoproxy') | |
config.mode = TransparentProxyMode(resolver(), [443, 8443]) | |
server = ProxyServer(config) | |
m = DevopMaster(server) | |
m.apps = MyAppRegistry() | |
m.run() |
This file contains 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
2015-04-22 14:33:40,759 - devoproxy - INFO - 10.0.30.20:60908: clientconnect | |
2015-04-22 14:33:40,759 - devoproxy - DEBUG - 10.0.30.20:60908: Set new server address: 176.9.184.93:80 | |
2015-04-22 14:33:40,760 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:40,760 - devoproxy - INFO - 10.0.30.20:38943: clientconnect | |
2015-04-22 14:33:40,760 - devoproxy - DEBUG - 10.0.30.20:38943: Set new server address: 195.20.242.89:80 | |
2015-04-22 14:33:40,760 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:40,792 - devoproxy - DEBUG - 10.0.30.20:60908: serverconnect | |
-> 176.9.184.93:80 | |
2015-04-22 14:33:40,792 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:40,793 - devoproxy - DEBUG - 10.0.30.20:38943: serverconnect | |
-> 195.20.242.89:80 | |
2015-04-22 14:33:40,793 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:40,893 - devoproxy - DEBUG - 10.0.30.20:38943: request | |
-> GET /dists/jessie/updates/InRelease HTTP/1.1 | |
2015-04-22 14:33:40,893 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:40,905 - devoproxy - DEBUG - 10.0.30.20:60908: request | |
-> GET /debian/dists/jessie/InRelease HTTP/1.1 | |
2015-04-22 14:33:40,906 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:40,906 - devoproxy - INFO - 10.0.30.20:38943: Connection killed | |
2015-04-22 14:33:40,906 - devoproxy - INFO - 10.0.30.20:38943: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:40,906 - devoproxy - DEBUG - 10.0.30.20:38943: serverdisconnect | |
-> 195.20.242.89:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:40,914 - devoproxy - INFO - 10.0.30.20:60908: Connection killed | |
2015-04-22 14:33:40,914 - devoproxy - INFO - 10.0.30.20:60908: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:40,914 - devoproxy - DEBUG - 10.0.30.20:60908: serverdisconnect | |
-> 176.9.184.93:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:40,914 - devoproxy - INFO - 10.0.30.20:52367: clientconnect | |
2015-04-22 14:33:40,914 - devoproxy - DEBUG - 10.0.30.20:52367: Set new server address: 212.211.132.32:80 | |
2015-04-22 14:33:40,914 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:40,917 - devoproxy - DEBUG - 10.0.30.20:52367: serverconnect | |
-> 212.211.132.32:80 | |
2015-04-22 14:33:40,917 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:40,933 - devoproxy - INFO - 10.0.30.20:51493: clientconnect | |
2015-04-22 14:33:40,933 - devoproxy - DEBUG - 10.0.30.20:51493: Set new server address: 5.153.231.35:80 | |
2015-04-22 14:33:40,933 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:40,937 - devoproxy - DEBUG - 10.0.30.20:51493: serverconnect | |
-> 5.153.231.35:80 | |
2015-04-22 14:33:40,937 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,037 - devoproxy - DEBUG - 10.0.30.20:52367: request | |
-> GET /dists/jessie/updates/main/binary-amd64/Packages.bz2 HTTP/1.1 | |
2015-04-22 14:33:41,037 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,038 - devoproxy - DEBUG - 10.0.30.20:51493: request | |
-> GET /debian/dists/jessie/main/binary-amd64/Packages.diff/Index HTTP/1.1 | |
2015-04-22 14:33:41,038 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,039 - devoproxy - INFO - 10.0.30.20:51493: Connection killed | |
2015-04-22 14:33:41,039 - devoproxy - INFO - 10.0.30.20:51493: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,040 - devoproxy - DEBUG - 10.0.30.20:51493: serverdisconnect | |
-> 5.153.231.35:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,055 - devoproxy - INFO - 10.0.30.20:52367: Connection killed | |
2015-04-22 14:33:41,055 - devoproxy - INFO - 10.0.30.20:52367: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,055 - devoproxy - DEBUG - 10.0.30.20:52367: serverdisconnect | |
-> 212.211.132.32:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,319 - devoproxy - INFO - 10.0.30.20:51495: clientconnect | |
2015-04-22 14:33:41,319 - devoproxy - DEBUG - 10.0.30.20:51495: Set new server address: 5.153.231.35:80 | |
2015-04-22 14:33:41,319 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,320 - devoproxy - INFO - 10.0.30.20:52372: clientconnect | |
2015-04-22 14:33:41,320 - devoproxy - DEBUG - 10.0.30.20:52372: Set new server address: 212.211.132.32:80 | |
2015-04-22 14:33:41,320 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,323 - devoproxy - DEBUG - 10.0.30.20:52372: serverconnect | |
-> 212.211.132.32:80 | |
2015-04-22 14:33:41,323 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,327 - devoproxy - DEBUG - 10.0.30.20:51495: serverconnect | |
-> 5.153.231.35:80 | |
2015-04-22 14:33:41,327 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,427 - devoproxy - DEBUG - 10.0.30.20:51495: request | |
-> GET /debian/dists/jessie/contrib/binary-amd64/Packages.diff/Index HTTP/1.1 | |
2015-04-22 14:33:41,427 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,428 - devoproxy - DEBUG - 10.0.30.20:52372: request | |
-> GET /dists/jessie/updates/contrib/binary-amd64/Packages.bz2 HTTP/1.1 | |
2015-04-22 14:33:41,428 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,429 - devoproxy - INFO - 10.0.30.20:52372: Connection killed | |
2015-04-22 14:33:41,430 - devoproxy - INFO - 10.0.30.20:52372: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,430 - devoproxy - DEBUG - 10.0.30.20:52372: serverdisconnect | |
-> 212.211.132.32:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,430 - devoproxy - INFO - 10.0.30.20:38954: clientconnect | |
2015-04-22 14:33:41,430 - devoproxy - DEBUG - 10.0.30.20:38954: Set new server address: 195.20.242.89:80 | |
2015-04-22 14:33:41,430 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,431 - devoproxy - DEBUG - 10.0.30.20:38954: serverconnect | |
-> 195.20.242.89:80 | |
2015-04-22 14:33:41,431 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,432 - devoproxy - INFO - 10.0.30.20:51495: Connection killed | |
2015-04-22 14:33:41,432 - devoproxy - INFO - 10.0.30.20:51495: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,433 - devoproxy - DEBUG - 10.0.30.20:51495: serverdisconnect | |
-> 5.153.231.35:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,434 - devoproxy - INFO - 10.0.30.20:51501: clientconnect | |
2015-04-22 14:33:41,434 - devoproxy - DEBUG - 10.0.30.20:51501: Set new server address: 5.153.231.35:80 | |
2015-04-22 14:33:41,434 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,435 - devoproxy - DEBUG - 10.0.30.20:51501: serverconnect | |
-> 5.153.231.35:80 | |
2015-04-22 14:33:41,435 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,535 - devoproxy - DEBUG - 10.0.30.20:38954: request | |
-> GET /dists/jessie/updates/non-free/binary-amd64/Packages.bz2 HTTP/1.1 | |
2015-04-22 14:33:41,536 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,536 - devoproxy - DEBUG - 10.0.30.20:51501: request | |
-> GET /debian/dists/jessie/non-free/binary-amd64/Packages.diff/Index HTTP/1.1 | |
2015-04-22 14:33:41,536 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,544 - devoproxy - INFO - 10.0.30.20:38954: Connection killed | |
2015-04-22 14:33:41,544 - devoproxy - INFO - 10.0.30.20:38954: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,545 - devoproxy - DEBUG - 10.0.30.20:38954: serverdisconnect | |
-> 195.20.242.89:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,545 - devoproxy - INFO - 10.0.30.20:52379: clientconnect | |
2015-04-22 14:33:41,545 - devoproxy - DEBUG - 10.0.30.20:52379: Set new server address: 212.211.132.32:80 | |
2015-04-22 14:33:41,545 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,545 - devoproxy - INFO - 10.0.30.20:51501: Connection killed | |
2015-04-22 14:33:41,545 - devoproxy - INFO - 10.0.30.20:51501: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,545 - devoproxy - DEBUG - 10.0.30.20:51501: serverdisconnect | |
-> 5.153.231.35:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,546 - devoproxy - INFO - 10.0.30.20:51504: clientconnect | |
2015-04-22 14:33:41,546 - devoproxy - DEBUG - 10.0.30.20:51504: Set new server address: 5.153.231.35:80 | |
2015-04-22 14:33:41,546 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,547 - devoproxy - DEBUG - 10.0.30.20:51504: serverconnect | |
-> 5.153.231.35:80 | |
2015-04-22 14:33:41,547 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,550 - devoproxy - DEBUG - 10.0.30.20:52379: serverconnect | |
-> 212.211.132.32:80 | |
2015-04-22 14:33:41,550 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,651 - devoproxy - DEBUG - 10.0.30.20:51504: request | |
-> GET /debian/dists/jessie/contrib/i18n/Translation-en.diff/Index HTTP/1.1 | |
2015-04-22 14:33:41,651 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,655 - devoproxy - INFO - 10.0.30.20:51504: Connection killed | |
2015-04-22 14:33:41,655 - devoproxy - INFO - 10.0.30.20:51504: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,655 - devoproxy - DEBUG - 10.0.30.20:51504: serverdisconnect | |
-> 5.153.231.35:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,657 - devoproxy - INFO - 10.0.30.20:51507: clientconnect | |
2015-04-22 14:33:41,657 - devoproxy - DEBUG - 10.0.30.20:51507: Set new server address: 5.153.231.35:80 | |
2015-04-22 14:33:41,657 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,660 - devoproxy - DEBUG - 10.0.30.20:51507: serverconnect | |
-> 5.153.231.35:80 | |
2015-04-22 14:33:41,660 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,676 - devoproxy - DEBUG - 10.0.30.20:52379: request | |
-> GET /dists/jessie/updates/contrib/i18n/Translation-en.bz2 HTTP/1.1 | |
2015-04-22 14:33:41,676 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,678 - devoproxy - INFO - 10.0.30.20:52379: Connection killed | |
2015-04-22 14:33:41,678 - devoproxy - INFO - 10.0.30.20:52379: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,678 - devoproxy - DEBUG - 10.0.30.20:52379: serverdisconnect | |
-> 212.211.132.32:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,679 - devoproxy - INFO - 10.0.30.20:38964: clientconnect | |
2015-04-22 14:33:41,679 - devoproxy - DEBUG - 10.0.30.20:38964: Set new server address: 195.20.242.89:80 | |
2015-04-22 14:33:41,679 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,680 - devoproxy - DEBUG - 10.0.30.20:38964: serverconnect | |
-> 195.20.242.89:80 | |
2015-04-22 14:33:41,680 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,780 - devoproxy - DEBUG - 10.0.30.20:51507: request | |
-> GET /debian/dists/jessie/main/i18n/Translation-en.diff/Index HTTP/1.1 | |
2015-04-22 14:33:41,780 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,781 - devoproxy - DEBUG - 10.0.30.20:38964: request | |
-> GET /dists/jessie/updates/main/i18n/Translation-en.bz2 HTTP/1.1 | |
2015-04-22 14:33:41,781 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,785 - devoproxy - INFO - 10.0.30.20:51507: Connection killed | |
2015-04-22 14:33:41,785 - devoproxy - INFO - 10.0.30.20:51507: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,785 - devoproxy - DEBUG - 10.0.30.20:51507: serverdisconnect | |
-> 5.153.231.35:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,786 - devoproxy - INFO - 10.0.30.20:51511: clientconnect | |
2015-04-22 14:33:41,786 - devoproxy - DEBUG - 10.0.30.20:51511: Set new server address: 5.153.231.35:80 | |
2015-04-22 14:33:41,786 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,787 - devoproxy - DEBUG - 10.0.30.20:51511: serverconnect | |
-> 5.153.231.35:80 | |
2015-04-22 14:33:41,787 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,791 - devoproxy - INFO - 10.0.30.20:38964: Connection killed | |
2015-04-22 14:33:41,791 - devoproxy - INFO - 10.0.30.20:38964: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,791 - devoproxy - DEBUG - 10.0.30.20:38964: serverdisconnect | |
-> 195.20.242.89:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,791 - devoproxy - INFO - 10.0.30.20:52389: clientconnect | |
2015-04-22 14:33:41,791 - devoproxy - DEBUG - 10.0.30.20:52389: Set new server address: 212.211.132.32:80 | |
2015-04-22 14:33:41,791 - devoproxy - DEBUG - handle_clientconnect | |
2015-04-22 14:33:41,792 - devoproxy - DEBUG - 10.0.30.20:52389: serverconnect | |
-> 212.211.132.32:80 | |
2015-04-22 14:33:41,792 - devoproxy - DEBUG - handle_serverconnect | |
2015-04-22 14:33:41,892 - devoproxy - DEBUG - 10.0.30.20:51511: request | |
-> GET /debian/dists/jessie/non-free/i18n/Translation-en.diff/Index HTTP/1.1 | |
2015-04-22 14:33:41,892 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,893 - devoproxy - DEBUG - 10.0.30.20:52389: request | |
-> GET /dists/jessie/updates/non-free/i18n/Translation-en.bz2 HTTP/1.1 | |
2015-04-22 14:33:41,893 - devoproxy - DEBUG - handle_request | |
2015-04-22 14:33:41,894 - devoproxy - INFO - 10.0.30.20:51511: Connection killed | |
2015-04-22 14:33:41,895 - devoproxy - INFO - 10.0.30.20:51511: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,895 - devoproxy - DEBUG - 10.0.30.20:51511: serverdisconnect | |
-> 5.153.231.35:80 | |
handle_serverdisconnect | |
2015-04-22 14:33:41,896 - devoproxy - INFO - 10.0.30.20:52389: Connection killed | |
2015-04-22 14:33:41,896 - devoproxy - INFO - 10.0.30.20:52389: clientdisconnect | |
handle_clientdisconnect | |
2015-04-22 14:33:41,896 - devoproxy - DEBUG - 10.0.30.20:52389: serverdisconnect | |
-> 212.211.132.32:80 | |
handle_serverdisconnect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment