Move app instantiation out of proxy.py.

This commit is contained in:
Aldo Cortesi 2013-07-24 10:32:56 +12:00
parent 64ce3b358f
commit 5c1157ddaf
8 changed files with 57 additions and 24 deletions

View File

@ -17,6 +17,9 @@ import proxy
import re, filt
import argparse
APP_DOMAIN = "mitm"
APP_IP = "1.1.1.1"
class ParseException(Exception): pass
class OptionException(Exception): pass
@ -139,6 +142,10 @@ def get_common_options(options):
setheaders.append(p)
return dict(
app = options.app,
app_ip = options.app_ip,
app_domain = options.app_domain,
anticache = options.anticache,
anticomp = options.anticomp,
client_replay = options.client_replay,
@ -267,6 +274,17 @@ def common_options(parser):
action="store_true", dest="app", default=False,
help="Enable the mitmproxy web app."
)
group.add_argument(
"--appdomain",
action="store", dest="app_domain", default=APP_DOMAIN, metavar="domain",
help="Domain to serve the app from."
)
group.add_argument(
"--appip",
action="store", dest="app_ip", default=APP_IP, metavar="ip",
help="""IP to serve the app from. Useful for transparent mode, when a DNS
entry for the app domain is not present."""
)
group = parser.add_argument_group("Client Replay")
group.add_argument(

View File

@ -328,6 +328,9 @@ class ConsoleState(flow.State):
class Options(object):
attributes = [
"app",
"app_domain",
"app_ip",
"anticache",
"anticomp",
"client_replay",
@ -426,6 +429,9 @@ class ConsoleMaster(flow.FlowMaster):
print >> sys.stderr, "Script load error:", err
sys.exit(1)
if options.app:
self.start_app(options.app_domain, options.app_ip)
def start_stream(self, path):
path = os.path.expanduser(path)
try:

View File

@ -22,6 +22,9 @@ class DumpError(Exception): pass
class Options(object):
attributes = [
"app",
"app_domain",
"app_ip",
"anticache",
"anticomp",
"client_replay",
@ -138,6 +141,9 @@ class DumpMaster(flow.FlowMaster):
except flow.FlowReadError, v:
self.add_event("Flow file corrupted. Stopped loading.")
if self.o.app:
self.start_app(self.o.app_domain, self.o.app_ip)
def _readflow(self, path):
path = os.path.expanduser(path)
try:

View File

@ -1376,6 +1376,18 @@ class FlowMaster(controller.Master):
self.stream = None
app.mapp.config["PMASTER"] = self
def start_app(self, domain, ip):
self.server.apps.add(
app.mapp,
domain,
80
)
self.server.apps.add(
app.mapp,
ip,
80
)
def add_event(self, e, level="info"):
"""
level: info, error
@ -1655,7 +1667,7 @@ class FlowReader:
try:
while 1:
data = tnetstring.load(self.fo)
if tuple(data["version"][:1]) != version.IVERSION[:1]:
if tuple(data["version"][:2]) != version.IVERSION[:2]:
v = ".".join(str(i) for i in data["version"])
raise FlowReadError("Incompatible serialized data version: %s"%v)
off = self.fo.tell()
@ -1678,4 +1690,3 @@ class FilteredFlowWriter:
d = f._get_state()
tnetstring.dump(d, self.fo)

View File

@ -17,11 +17,9 @@ import shutil, tempfile, threading
import SocketServer
from OpenSSL import SSL
from netlib import odict, tcp, http, wsgi, certutils, http_status, http_auth
import utils, flow, version, platform, controller, app
import utils, flow, version, platform, controller
APP_DOMAIN = "mitm"
APP_IP = "1.1.1.1"
KILL = 0
@ -39,8 +37,7 @@ class Log:
class ProxyConfig:
def __init__(self, app=False, certfile = None, cacert = None, clientcerts = None, no_upstream_cert=False, body_size_limit = None, reverse_proxy=None, transparent_proxy=None, certdir = None, authenticator=None):
self.app = app
def __init__(self, certfile = None, cacert = None, clientcerts = None, no_upstream_cert=False, body_size_limit = None, reverse_proxy=None, transparent_proxy=None, certdir = None, authenticator=None):
self.certfile = certfile
self.cacert = cacert
self.clientcerts = clientcerts
@ -512,17 +509,6 @@ class ProxyServer(tcp.TCPServer):
raise ProxyServerError('Error starting proxy server: ' + v.strerror)
self.channel = None
self.apps = AppRegistry()
if config.app:
self.apps.add(
app.mapp,
APP_DOMAIN,
80
)
self.apps.add(
app.mapp,
APP_IP,
80
)
def start_slave(self, klass, channel):
slave = klass(channel, self)
@ -655,7 +641,6 @@ def process_proxy_options(parser, options):
authenticator = http_auth.NullProxyAuth(None)
return ProxyConfig(
app = options.app,
certfile = options.cert,
cacert = cacert,
clientcerts = options.clientcerts,

View File

@ -70,5 +70,3 @@ if __name__ == '__main__':
except KeyboardInterrupt:
pass

View File

@ -106,6 +106,12 @@ class TestDumpMaster:
def test_filter(self):
assert not "GET" in self._dummy_cycle(1, "~u foo", "", verbosity=1)
def test_app(self):
o = dump.Options(app=True)
s = mock.MagicMock()
m = dump.DumpMaster(s, o, None)
assert s.apps.add.call_count == 2
def test_replacements(self):
o = dump.Options(replacements=[(".*", "content", "foo")])
m = dump.DumpMaster(None, o, None)

View File

@ -4,6 +4,9 @@ import libpathod.test, libpathod.pathoc
from libmproxy import proxy, flow, controller
import tutils
APP_DOMAIN = "mitm"
APP_IP = "1.1.1.1"
testapp = flask.Flask(__name__)
@testapp.route("/")
@ -28,6 +31,7 @@ class TestMaster(flow.FlowMaster):
flow.FlowMaster.__init__(self, s, state)
self.testq = testq
self.clear_log()
self.start_app(APP_DOMAIN, APP_IP)
def handle_request(self, m):
flow.FlowMaster.handle_request(self, m)
@ -85,7 +89,6 @@ class ProxTestBase:
no_upstream_cert = cls.no_upstream_cert,
cacert = tutils.test_data.path("data/serverkey.pem"),
authenticator = cls.authenticator,
app = True,
**pconf
)
tmaster = cls.masterclass(cls.tqueue, config)
@ -162,12 +165,12 @@ class HTTPProxTest(ProxTestBase):
if self.ssl:
p = libpathod.pathoc.Pathoc("127.0.0.1", self.proxy.port, True)
print "PRE"
p.connect((proxy.APP_IP, 80))
p.connect((APP_IP, 80))
print "POST"
return p.request("get:'/%s'"%page)
else:
p = self.pathoc()
return p.request("get:'http://%s/%s'"%(proxy.APP_DOMAIN, page))
return p.request("get:'http://%s/%s'"%(APP_DOMAIN, page))
class TResolver: