flow.state -> addons.state

This commit is contained in:
Aldo Cortesi 2016-10-19 14:48:42 +13:00
parent 22eebfd574
commit 7c32d4ea2a
9 changed files with 26 additions and 118 deletions

View File

@ -37,7 +37,7 @@ class MyMaster(master.Master):
opts = options.Options(cadir="~/.mitmproxy/")
config = ProxyConfig(opts)
state = flow.State()
state = state.State()
server = ProxyServer(config)
m = MyMaster(opts, server, state)
m.run()

View File

@ -1,93 +0,0 @@
import os
import tornado.template
import tornado.web
import tornado.wsgi
from mitmproxy import utils
from mitmproxy.proxy import config
loader = tornado.template.Loader(utils.pkg_data.path("addons/onboardingapp/templates"))
class Adapter(tornado.wsgi.WSGIAdapter):
# Tornado doesn't make the WSGI environment available to pages, so this
# hideous monkey patch is the easiest way to get to the mitmproxy.master
# variable.
def __init__(self, application):
self._application = application
def application(self, request):
request.master = self.environ["mitmproxy.master"]
return self._application(request)
def __call__(self, environ, start_response):
self.environ = environ
return tornado.wsgi.WSGIAdapter.__call__(
self,
environ,
start_response
)
class Index(tornado.web.RequestHandler):
def get(self):
t = loader.load("index.html")
self.write(t.generate())
class PEM(tornado.web.RequestHandler):
@property
def filename(self):
return config.CONF_BASENAME + "-ca-cert.pem"
def get(self):
p = os.path.join(self.request.master.options.cadir, self.filename)
p = os.path.expanduser(p)
self.set_header("Content-Type", "application/x-x509-ca-cert")
self.set_header(
"Content-Disposition",
"inline; filename={}".format(
self.filename))
with open(p, "rb") as f:
self.write(f.read())
class P12(tornado.web.RequestHandler):
@property
def filename(self):
return config.CONF_BASENAME + "-ca-cert.p12"
def get(self):
p = os.path.join(self.request.master.options.cadir, self.filename)
p = os.path.expanduser(p)
self.set_header("Content-Type", "application/x-pkcs12")
self.set_header(
"Content-Disposition",
"inline; filename={}".format(
self.filename))
with open(p, "rb") as f:
self.write(f.read())
application = tornado.web.Application(
[
(r"/", Index),
(r"/cert/pem", PEM),
(r"/cert/p12", P12),
(
r"/static/(.*)",
tornado.web.StaticFileHandler,
{
"path": utils.pkg_data.path("onboarding/static")
}
),
],
# debug=True
)

View File

@ -22,6 +22,7 @@ from mitmproxy import master
from mitmproxy import flow
from mitmproxy import flowfilter
from mitmproxy import utils
from mitmproxy.addons import state
import mitmproxy.options
from mitmproxy.console import flowlist
from mitmproxy.console import flowview
@ -39,10 +40,10 @@ from netlib import tcp, strutils
EVENTLOG_SIZE = 500
class ConsoleState(flow.State):
class ConsoleState(state.State):
def __init__(self):
flow.State.__init__(self)
state.State.__init__(self)
self.focus = None
self.follow_focus = None
self.default_body_view = contentviews.get("Auto")

View File

@ -1,9 +1,7 @@
from mitmproxy.flow import export
from mitmproxy.flow.io import FlowWriter, FilteredFlowWriter, FlowReader, read_flows_from_paths
from mitmproxy.flow.state import State, FlowView
__all__ = [
"export",
"FlowWriter", "FilteredFlowWriter", "FlowReader", "read_flows_from_paths",
"State", "FlowView",
]

View File

@ -9,7 +9,7 @@ from typing import Optional
from mitmproxy import addons
from mitmproxy import controller
from mitmproxy import exceptions
from mitmproxy import flow
from mitmproxy.addons import state
from mitmproxy import options
from mitmproxy import master
from mitmproxy.web import app
@ -20,7 +20,7 @@ class Stop(Exception):
pass
class WebFlowView(flow.FlowView):
class WebFlowView(state.FlowView):
def __init__(self, store):
super().__init__(store, None)
@ -57,7 +57,7 @@ class WebFlowView(flow.FlowView):
)
class WebState(flow.State):
class WebState(state.State):
def __init__(self):
super().__init__()

View File

@ -1,12 +1,13 @@
from mitmproxy import flow
from mitmproxy import proxy
from mitmproxy import master
from . import tutils
from mitmproxy.addons import state
from .. import tutils
class TestState:
def test_duplicate_flow(self):
s = flow.State()
s = state.State()
fm = master.Master(None, proxy.DummyServer())
fm.addons.add(s)
f = tutils.tflow(resp=True)

View File

@ -4,6 +4,7 @@ import io
import netlib.utils
from netlib.http import Headers
from mitmproxy import flowfilter, flow, options
from mitmproxy.addons import state
from mitmproxy.contrib import tnetstring
from mitmproxy.exceptions import FlowReadException, Kill
from mitmproxy.models import Error
@ -110,7 +111,7 @@ class TestHTTPFlow:
def test_killall(self):
srv = DummyServer(None)
s = flow.State()
s = state.State()
fm = master.Master(None, srv)
fm.addons.add(s)
@ -190,7 +191,7 @@ class TestTCPFlow:
class TestState:
def test_backup(self):
c = flow.State()
c = state.State()
f = tutils.tflow()
c.add_flow(f)
f.backup()
@ -202,7 +203,7 @@ class TestState:
connect -> request -> response
"""
c = flow.State()
c = state.State()
f = tutils.tflow()
c.add_flow(f)
assert f
@ -226,13 +227,13 @@ class TestState:
assert c.active_flow_count() == 0
def test_err(self):
c = flow.State()
c = state.State()
f = tutils.tflow()
c.add_flow(f)
f.error = Error("message")
assert c.update_flow(f)
c = flow.State()
c = state.State()
f = tutils.tflow()
c.add_flow(f)
c.set_view_filter("~e")
@ -242,7 +243,7 @@ class TestState:
assert c.view
def test_set_view_filter(self):
c = flow.State()
c = state.State()
f = tutils.tflow()
assert len(c.view) == 0
@ -270,7 +271,7 @@ class TestState:
assert "Invalid" in c.set_view_filter("~")
def test_set_intercept(self):
c = flow.State()
c = state.State()
assert not c.set_intercept("~q")
assert c.intercept_txt == "~q"
assert "Invalid" in c.set_intercept("~")
@ -293,7 +294,7 @@ class TestState:
state.add_flow(f)
def test_clear(self):
c = flow.State()
c = state.State()
f = self._add_request(c)
f.intercepted = True
@ -301,7 +302,7 @@ class TestState:
assert c.flow_count() == 0
def test_dump_flows(self):
c = flow.State()
c = state.State()
self._add_request(c)
self._add_response(c)
self._add_request(c)
@ -317,7 +318,7 @@ class TestState:
assert isinstance(c.flows[0], Flow)
def test_accept_all(self):
c = flow.State()
c = state.State()
self._add_request(c)
self._add_response(c)
self._add_request(c)
@ -363,7 +364,7 @@ class TestSerialize:
def test_load_flows(self):
r = self._treader()
s = flow.State()
s = state.State()
fm = master.Master(None, DummyServer())
fm.addons.add(s)
fm.load_flows(r)
@ -371,7 +372,7 @@ class TestSerialize:
def test_load_flows_reverse(self):
r = self._treader()
s = flow.State()
s = state.State()
opts = options.Options(
mode="reverse",
upstream_server="https://use-this-domain"
@ -440,7 +441,7 @@ class TestFlowMaster:
assert fm.create_request("GET", "http", "example.com", 80, "/")
def test_all(self):
s = flow.State()
s = state.State()
fm = master.Master(None, DummyServer())
fm.addons.add(s)
f = tutils.tflow(req=None)

View File

@ -7,7 +7,7 @@ import sys
from mitmproxy.proxy.config import ProxyConfig
from mitmproxy.proxy.server import ProxyServer
from mitmproxy import master
from mitmproxy.flow import state
from mitmproxy.addons import state
import pathod.test
import pathod.pathoc
from mitmproxy import controller, options