diff options
-rw-r--r-- | libmproxy/cmdline.py | 27 | ||||
-rw-r--r-- | libmproxy/console/__init__.py | 2 | ||||
-rw-r--r-- | libmproxy/dump.py | 5 | ||||
-rw-r--r-- | libmproxy/flow.py | 26 | ||||
-rw-r--r-- | libmproxy/proxy.py | 5 | ||||
-rw-r--r-- | libmproxy/script.py | 6 | ||||
-rw-r--r-- | test/test_console.py | 4 | ||||
-rw-r--r-- | test/test_console_common.py | 5 | ||||
-rw-r--r-- | test/test_console_contentview.py | 5 | ||||
-rw-r--r-- | test/test_console_help.py | 5 | ||||
-rw-r--r-- | test/test_dump.py | 7 | ||||
-rw-r--r-- | test/test_flow.py | 16 | ||||
-rw-r--r-- | test/test_proxy.py | 1 | ||||
-rw-r--r-- | test/test_server.py | 3 | ||||
-rw-r--r-- | test/tservers.py | 6 | ||||
-rw-r--r-- | test/tutils.py | 10 |
16 files changed, 86 insertions, 47 deletions
diff --git a/libmproxy/cmdline.py b/libmproxy/cmdline.py index fc054b5e..4b5b0f5f 100644 --- a/libmproxy/cmdline.py +++ b/libmproxy/cmdline.py @@ -4,8 +4,8 @@ import argparse import shlex import os -APP_DOMAIN = "mitm" -APP_IP = "1.1.1.1" +APP_HOST = "mitm" +APP_PORT = 80 class ParseException(Exception): pass class OptionException(Exception): pass @@ -130,8 +130,9 @@ def get_common_options(options): return dict( app = options.app, - app_ip = options.app_ip, - app_domain = options.app_domain, + app_host = options.app_host, + app_port = options.app_port, + app_external = options.app_external, anticache = options.anticache, anticomp = options.anticomp, @@ -268,15 +269,19 @@ def common_options(parser): 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." + "--app-host", + action="store", dest="app_host", default=APP_HOST, metavar="host", + help="Domain to serve the app from. For transparent mode, use an IP when a DNS entry for the app domain is not present." ) 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.""" + "--app-port", + action="store", dest="app_port", default=APP_PORT, type=int, metavar="80", + help="Port to serve the app from." + ) + group.add_argument( + "--app-external", + action="store_true", dest="app_external", + help="Serve the app outside of the proxy." ) group = parser.add_argument_group("Client Replay") diff --git a/libmproxy/console/__init__.py b/libmproxy/console/__init__.py index 04e240d3..b2c1deef 100644 --- a/libmproxy/console/__init__.py +++ b/libmproxy/console/__init__.py @@ -423,7 +423,7 @@ class ConsoleMaster(flow.FlowMaster): sys.exit(1) if options.app: - self.start_app(options.app_domain, options.app_ip) + self.start_app(self.o.app_host, self.o.app_port, self.o.app_external) def start_stream(self, path): path = os.path.expanduser(path) diff --git a/libmproxy/dump.py b/libmproxy/dump.py index 12488937..55fd94b3 100644 --- a/libmproxy/dump.py +++ b/libmproxy/dump.py @@ -109,7 +109,8 @@ class DumpMaster(flow.FlowMaster): not options.keepserving ) - for script_argv in options.scripts: + scripts = options.scripts or [] + for script_argv in scripts: err = self.load_script(script_argv) if err: raise DumpError(err) @@ -127,7 +128,7 @@ class DumpMaster(flow.FlowMaster): self.add_event("Flow file corrupted. Stopped loading.") if self.o.app: - self.start_app(self.o.app_domain, self.o.app_ip) + self.start_app(self.o.app_host, self.o.app_port, self.o.app_external) def _readflow(self, path): path = os.path.expanduser(path) diff --git a/libmproxy/flow.py b/libmproxy/flow.py index d79f2862..96103ddb 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -2,7 +2,7 @@ This module provides more sophisticated flow tracking. These match requests with their responses, and provide filtering and interception facilities. """ -import hashlib, Cookie, cookielib, copy, re, urlparse, os +import hashlib, Cookie, cookielib, copy, re, urlparse, os, threading import time, urllib import tnetstring, filt, script, utils, encoding, proxy from email.utils import parsedate_tz, formatdate, mktime_tz @@ -1367,17 +1367,19 @@ 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 start_app(self, host, port, external): + if not external: + self.server.apps.add( + app.mapp, + host, + port + ) + else: + print host + threading.Thread(target=app.mapp.run,kwargs={ + "use_reloader": False, + "host": host, + "port": port}).start() def add_event(self, e, level="info"): """ diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 1fc289ed..ab2cdb7c 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -474,8 +474,9 @@ class ProxyHandler(tcp.BaseHandler): self.wfile.write("Server: %s\r\n"%self.server_version) self.wfile.write("Content-type: text/html\r\n") self.wfile.write("Content-Length: %d\r\n"%len(html_content)) - for key, value in headers.items(): - self.wfile.write("%s: %s\r\n"%(key, value)) + if headers: + for key, value in headers.items(): + self.wfile.write("%s: %s\r\n"%(key, value)) self.wfile.write("Connection: close\r\n") self.wfile.write("\r\n") self.wfile.write(html_content) diff --git a/libmproxy/script.py b/libmproxy/script.py index c32628b0..623f2b92 100644 --- a/libmproxy/script.py +++ b/libmproxy/script.py @@ -31,10 +31,12 @@ class Script: ns = {} try: execfile(path, ns, ns) - self.ns = ns - self.run("start", self.argv) except Exception, v: raise ScriptError(traceback.format_exc(v)) + self.ns = ns + r = self.run("start", self.argv) + if not r[0] and r[1]: + raise ScriptError(r[1][1]) def unload(self): return self.run("done") diff --git a/test/test_console.py b/test/test_console.py index 498a93bd..4fd9bb9f 100644 --- a/test/test_console.py +++ b/test/test_console.py @@ -1,4 +1,8 @@ import os +from nose.plugins.skip import SkipTest +if os.name == "nt": + raise SkipTest("Skipped on Windows.") + from libmproxy import console from libmproxy.console import common import tutils diff --git a/test/test_console_common.py b/test/test_console_common.py index 29bf7b84..d798e4dc 100644 --- a/test/test_console_common.py +++ b/test/test_console_common.py @@ -1,3 +1,8 @@ +import os +from nose.plugins.skip import SkipTest +if os.name == "nt": + raise SkipTest("Skipped on Windows.") + import libmproxy.console.common as common from libmproxy import utils, flow, encoding import tutils diff --git a/test/test_console_contentview.py b/test/test_console_contentview.py index f6b45f67..ef44f834 100644 --- a/test/test_console_contentview.py +++ b/test/test_console_contentview.py @@ -1,3 +1,8 @@ +import os +from nose.plugins.skip import SkipTest +if os.name == "nt": + raise SkipTest("Skipped on Windows.") + import sys import libmproxy.console.contentview as cv from libmproxy import utils, flow, encoding diff --git a/test/test_console_help.py b/test/test_console_help.py index 98e202ad..6e1f9fad 100644 --- a/test/test_console_help.py +++ b/test/test_console_help.py @@ -1,3 +1,8 @@ +import os +from nose.plugins.skip import SkipTest +if os.name == "nt": + raise SkipTest("Skipped on Windows.") + import libmproxy.console.help as help class DummyMaster: diff --git a/test/test_dump.py b/test/test_dump.py index b195d5f0..255651a9 100644 --- a/test/test_dump.py +++ b/test/test_dump.py @@ -43,6 +43,7 @@ class TestDumpMaster: m = dump.DumpMaster(None, o, filt, outfile=cs) for i in range(n): self._cycle(m, content) + m.shutdown() return cs.getvalue() def _flowfile(self, path): @@ -149,7 +150,7 @@ class TestDumpMaster: def test_script(self): ret = self._dummy_cycle( 1, None, "", - script=tutils.test_data.path("scripts/all.py"), verbosity=0, eventlog=True + scripts=[[tutils.test_data.path("scripts/all.py")]], verbosity=0, eventlog=True ) assert "XCLIENTCONNECT" in ret assert "XREQUEST" in ret @@ -157,11 +158,11 @@ class TestDumpMaster: assert "XCLIENTDISCONNECT" in ret tutils.raises( dump.DumpError, - self._dummy_cycle, 1, None, "", script="nonexistent" + self._dummy_cycle, 1, None, "", scripts=[["nonexistent"]] ) tutils.raises( dump.DumpError, - self._dummy_cycle, 1, None, "", script="starterr.py" + self._dummy_cycle, 1, None, "", scripts=[["starterr.py"]] ) def test_stickycookie(self): diff --git a/test/test_flow.py b/test/test_flow.py index 68aa14cd..9844e0fd 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -542,12 +542,12 @@ class TestFlowMaster: def test_load_script(self): s = flow.State() fm = flow.FlowMaster(None, s) - assert not fm.load_script(tutils.test_data.path("scripts/a.py")) - assert not fm.load_script(tutils.test_data.path("scripts/a.py")) + assert not fm.load_script([tutils.test_data.path("scripts/a.py")]) + assert not fm.load_script([tutils.test_data.path("scripts/a.py")]) assert not fm.unload_script(fm.scripts[0]) assert not fm.unload_script(fm.scripts[0]) - assert fm.load_script("nonexistent") - assert "ValueError" in fm.load_script(tutils.test_data.path("scripts/starterr.py")) + assert fm.load_script(["nonexistent"]) + assert "ValueError" in fm.load_script([tutils.test_data.path("scripts/starterr.py")]) assert len(fm.scripts) == 0 def test_replay(self): @@ -563,7 +563,7 @@ class TestFlowMaster: def test_script_reqerr(self): s = flow.State() fm = flow.FlowMaster(None, s) - assert not fm.load_script(tutils.test_data.path("scripts/reqerr.py")) + assert not fm.load_script([tutils.test_data.path("scripts/reqerr.py")]) req = tutils.treq() fm.handle_clientconnect(req.client_conn) assert fm.handle_request(req) @@ -571,7 +571,7 @@ class TestFlowMaster: def test_script(self): s = flow.State() fm = flow.FlowMaster(None, s) - assert not fm.load_script(tutils.test_data.path("scripts/all.py")) + assert not fm.load_script([tutils.test_data.path("scripts/all.py")]) req = tutils.treq() fm.handle_clientconnect(req.client_conn) assert fm.scripts[0].ns["log"][-1] == "clientconnect" @@ -581,7 +581,7 @@ class TestFlowMaster: fm.handle_response(resp) assert fm.scripts[0].ns["log"][-1] == "response" #load second script - assert not fm.load_script(tutils.test_data.path("scripts/all.py")) + assert not fm.load_script([tutils.test_data.path("scripts/all.py")]) assert len(fm.scripts) == 2 dc = flow.ClientDisconnect(req.client_conn) dc.reply = controller.DummyReply() @@ -634,7 +634,7 @@ class TestFlowMaster: err.reply = controller.DummyReply() fm.handle_error(err) - fm.load_script(tutils.test_data.path("scripts/a.py")) + fm.load_script([tutils.test_data.path("scripts/a.py")]) fm.shutdown() def test_client_playback(self): diff --git a/test/test_proxy.py b/test/test_proxy.py index 85be82cb..dfccaaf7 100644 --- a/test/test_proxy.py +++ b/test/test_proxy.py @@ -130,6 +130,7 @@ class TestProcessProxyOptions: class TestProxyServer: + @tutils.SkipWindows # binding to 0.0.0.0:1 works without special permissions on Windows def test_err(self): parser = argparse.ArgumentParser() cmdline.common_options(parser) diff --git a/test/test_server.py b/test/test_server.py index e9a6b727..d832ff18 100644 --- a/test/test_server.py +++ b/test/test_server.py @@ -246,13 +246,14 @@ class TestProxy(tservers.HTTPProxTest): # call pathod server, wait a second to complete the request connection.send("GET http://localhost:%d/p/304:b@1k HTTP/1.1\r\n"%self.server.port) + time.sleep(1) connection.send("\r\n"); connection.recv(50000) connection.close() request, response = self.master.state.view[0].request, self.master.state.view[0].response assert response.code == 304 # sanity test for our low level request - assert request.timestamp_end - request.timestamp_start > 0 + assert 0.95 < (request.timestamp_end - request.timestamp_start) < 1.2 #time.sleep might be a little bit shorter than a second def test_request_timestamps_not_affected_by_client_time(self): # test that don't include user wait time in request's timestamps diff --git a/test/tservers.py b/test/tservers.py index 0606bd9c..eb3b91a4 100644 --- a/test/tservers.py +++ b/test/tservers.py @@ -2,11 +2,9 @@ import threading, Queue import flask import libpathod.test, libpathod.pathoc from libmproxy import proxy, flow, controller +from libmproxy.cmdline import APP_HOST, APP_PORT import tutils -APP_DOMAIN = "mitm" -APP_IP = "1.1.1.1" - testapp = flask.Flask(__name__) @testapp.route("/") @@ -31,7 +29,7 @@ class TestMaster(flow.FlowMaster): flow.FlowMaster.__init__(self, s, state) self.testq = testq self.clear_log() - self.start_app(APP_DOMAIN, APP_IP) + self.start_app(APP_HOST, APP_PORT, False) def handle_request(self, m): flow.FlowMaster.handle_request(self, m) diff --git a/test/tutils.py b/test/tutils.py index fbce615a..e42256ed 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -2,7 +2,15 @@ import os, shutil, tempfile from contextlib import contextmanager from libmproxy import flow, utils, controller from netlib import certutils -import mock +from nose.plugins.skip import SkipTest + +def _SkipWindows(): + raise SkipTest("Skipped on Windows.") +def SkipWindows(fn): + if os.name == "nt": + return _SkipWindows + else: + return fn def treq(conn=None): if not conn: |