mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-26 02:10:59 +00:00
move script context to mitmproxy.ctx
This commit is contained in:
parent
7c67faa8da
commit
5d2b7c52f9
@ -219,17 +219,17 @@ def done():
|
|||||||
compressed_json_dump = context.HARLog.compress()
|
compressed_json_dump = context.HARLog.compress()
|
||||||
|
|
||||||
if context.dump_file == '-':
|
if context.dump_file == '-':
|
||||||
mitmproxy.log(pprint.pformat(json.loads(json_dump)))
|
mitmproxy.ctx.log(pprint.pformat(json.loads(json_dump)))
|
||||||
elif context.dump_file.endswith('.zhar'):
|
elif context.dump_file.endswith('.zhar'):
|
||||||
file(context.dump_file, "w").write(compressed_json_dump)
|
file(context.dump_file, "w").write(compressed_json_dump)
|
||||||
else:
|
else:
|
||||||
file(context.dump_file, "w").write(json_dump)
|
file(context.dump_file, "w").write(json_dump)
|
||||||
mitmproxy.log(
|
mitmproxy.ctx.log(
|
||||||
"HAR log finished with %s bytes (%s bytes compressed)" % (
|
"HAR log finished with %s bytes (%s bytes compressed)" % (
|
||||||
len(json_dump), len(compressed_json_dump)
|
len(json_dump), len(compressed_json_dump)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
mitmproxy.log(
|
mitmproxy.ctx.log(
|
||||||
"Compression rate is %s%%" % str(
|
"Compression rate is %s%%" % str(
|
||||||
100. * len(compressed_json_dump) / len(json_dump)
|
100. * len(compressed_json_dump) / len(json_dump)
|
||||||
)
|
)
|
||||||
|
@ -5,6 +5,6 @@ from mitmproxy.script import concurrent
|
|||||||
|
|
||||||
@concurrent # Remove this and see what happens
|
@concurrent # Remove this and see what happens
|
||||||
def request(flow):
|
def request(flow):
|
||||||
mitmproxy.log("handle request: %s%s" % (flow.request.host, flow.request.path))
|
mitmproxy.ctx.log("handle request: %s%s" % (flow.request.host, flow.request.path))
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
mitmproxy.log("start request: %s%s" % (flow.request.host, flow.request.path))
|
mitmproxy.ctx.log("start request: %s%s" % (flow.request.host, flow.request.path))
|
||||||
|
@ -17,9 +17,9 @@ def hello_world():
|
|||||||
# Register the app using the magic domain "proxapp" on port 80. Requests to
|
# Register the app using the magic domain "proxapp" on port 80. Requests to
|
||||||
# this domain and port combination will now be routed to the WSGI app instance.
|
# this domain and port combination will now be routed to the WSGI app instance.
|
||||||
def start():
|
def start():
|
||||||
mitmproxy.master.apps.add(app, "proxapp", 80)
|
mitmproxy.ctx.master.apps.add(app, "proxapp", 80)
|
||||||
|
|
||||||
# SSL works too, but the magic domain needs to be resolvable from the mitmproxy machine due to mitmproxy's design.
|
# SSL works too, but the magic domain needs to be resolvable from the mitmproxy machine due to mitmproxy's design.
|
||||||
# mitmproxy will connect to said domain and use serve its certificate (unless --no-upstream-cert is set)
|
# mitmproxy will connect to said domain and use serve its certificate (unless --no-upstream-cert is set)
|
||||||
# but won't send any data.
|
# but won't send any data.
|
||||||
mitmproxy.master.apps.add(app, "example.com", 443)
|
mitmproxy.ctx.master.apps.add(app, "example.com", 443)
|
||||||
|
@ -8,7 +8,7 @@ def start():
|
|||||||
"""
|
"""
|
||||||
Called once on script startup, before any other events.
|
Called once on script startup, before any other events.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("start")
|
mitmproxy.ctx.log("start")
|
||||||
|
|
||||||
|
|
||||||
def clientconnect(root_layer):
|
def clientconnect(root_layer):
|
||||||
@ -16,14 +16,14 @@ def clientconnect(root_layer):
|
|||||||
Called when a client initiates a connection to the proxy. Note that a
|
Called when a client initiates a connection to the proxy. Note that a
|
||||||
connection can correspond to multiple HTTP requests
|
connection can correspond to multiple HTTP requests
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("clientconnect")
|
mitmproxy.ctx.log("clientconnect")
|
||||||
|
|
||||||
|
|
||||||
def request(flow):
|
def request(flow):
|
||||||
"""
|
"""
|
||||||
Called when a client request has been received.
|
Called when a client request has been received.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("request")
|
mitmproxy.ctx.log("request")
|
||||||
|
|
||||||
|
|
||||||
def serverconnect(server_conn):
|
def serverconnect(server_conn):
|
||||||
@ -31,7 +31,7 @@ def serverconnect(server_conn):
|
|||||||
Called when the proxy initiates a connection to the target server. Note that a
|
Called when the proxy initiates a connection to the target server. Note that a
|
||||||
connection can correspond to multiple HTTP requests
|
connection can correspond to multiple HTTP requests
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("serverconnect")
|
mitmproxy.ctx.log("serverconnect")
|
||||||
|
|
||||||
|
|
||||||
def responseheaders(flow):
|
def responseheaders(flow):
|
||||||
@ -40,14 +40,14 @@ def responseheaders(flow):
|
|||||||
but the response body has not been processed yet. Can be used to tell mitmproxy
|
but the response body has not been processed yet. Can be used to tell mitmproxy
|
||||||
to stream the response.
|
to stream the response.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("responseheaders")
|
mitmproxy.ctx.log("responseheaders")
|
||||||
|
|
||||||
|
|
||||||
def response(flow):
|
def response(flow):
|
||||||
"""
|
"""
|
||||||
Called when a server response has been received.
|
Called when a server response has been received.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("response")
|
mitmproxy.ctx.log("response")
|
||||||
|
|
||||||
|
|
||||||
def error(flow):
|
def error(flow):
|
||||||
@ -56,25 +56,25 @@ def error(flow):
|
|||||||
interrupted connections. This is distinct from a valid server HTTP error
|
interrupted connections. This is distinct from a valid server HTTP error
|
||||||
response, which is simply a response with an HTTP error code.
|
response, which is simply a response with an HTTP error code.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("error")
|
mitmproxy.ctx.log("error")
|
||||||
|
|
||||||
|
|
||||||
def serverdisconnect(server_conn):
|
def serverdisconnect(server_conn):
|
||||||
"""
|
"""
|
||||||
Called when the proxy closes the connection to the target server.
|
Called when the proxy closes the connection to the target server.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("serverdisconnect")
|
mitmproxy.ctx.log("serverdisconnect")
|
||||||
|
|
||||||
|
|
||||||
def clientdisconnect(root_layer):
|
def clientdisconnect(root_layer):
|
||||||
"""
|
"""
|
||||||
Called when a client disconnects from the proxy.
|
Called when a client disconnects from the proxy.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("clientdisconnect")
|
mitmproxy.ctx.log("clientdisconnect")
|
||||||
|
|
||||||
|
|
||||||
def done():
|
def done():
|
||||||
"""
|
"""
|
||||||
Called once on script shutdown, after any other events.
|
Called once on script shutdown, after any other events.
|
||||||
"""
|
"""
|
||||||
mitmproxy.log("done")
|
mitmproxy.ctx.log("done")
|
||||||
|
@ -135,7 +135,7 @@ def next_layer(next_layer):
|
|||||||
next_layer.__class__ = TlsFeedback
|
next_layer.__class__ = TlsFeedback
|
||||||
else:
|
else:
|
||||||
# We don't intercept - reply with a pass-through layer and add a "skipped" entry.
|
# We don't intercept - reply with a pass-through layer and add a "skipped" entry.
|
||||||
mitmproxy.log("TLS passthrough for %s" % repr(next_layer.server_conn.address), "info")
|
mitmproxy.ctx.log("TLS passthrough for %s" % repr(next_layer.server_conn.address), "info")
|
||||||
next_layer_replacement = RawTCPLayer(next_layer.ctx, ignore=True)
|
next_layer_replacement = RawTCPLayer(next_layer.ctx, ignore=True)
|
||||||
next_layer.reply.send(next_layer_replacement)
|
next_layer.reply.send(next_layer_replacement)
|
||||||
tls_strategy.record_skipped(server_address)
|
tls_strategy.record_skipped(server_address)
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
from typing import Callable # noqa
|
|
||||||
from mitmproxy import flow # noqa
|
|
||||||
|
|
||||||
master = None # type: flow.FlowMaster
|
|
||||||
log = None # type: Callable[[str], None]
|
|
@ -6,7 +6,7 @@ import contextlib
|
|||||||
|
|
||||||
from six.moves import queue
|
from six.moves import queue
|
||||||
|
|
||||||
import mitmproxy
|
from . import ctx as mitmproxy_ctx
|
||||||
from netlib import basethread
|
from netlib import basethread
|
||||||
from . import exceptions
|
from . import exceptions
|
||||||
|
|
||||||
@ -59,16 +59,16 @@ class Master(object):
|
|||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def handlecontext(self):
|
def handlecontext(self):
|
||||||
# Handlecontexts also have to nest - leave cleanup to the outermost
|
# Handlecontexts also have to nest - leave cleanup to the outermost
|
||||||
if mitmproxy.master:
|
if mitmproxy_ctx.master:
|
||||||
yield
|
yield
|
||||||
return
|
return
|
||||||
mitmproxy.master = self
|
mitmproxy_ctx.master = self
|
||||||
mitmproxy.log = Log(self)
|
mitmproxy_ctx.log = Log(self)
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
mitmproxy.master = None
|
mitmproxy_ctx.master = None
|
||||||
mitmproxy.log = None
|
mitmproxy_ctx.log = None
|
||||||
|
|
||||||
def add_server(self, server):
|
def add_server(self, server):
|
||||||
# We give a Channel to the server which can be used to communicate with the master
|
# We give a Channel to the server which can be used to communicate with the master
|
||||||
|
4
mitmproxy/ctx.py
Normal file
4
mitmproxy/ctx.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from typing import Callable # noqa
|
||||||
|
|
||||||
|
master = None # type: "mitmproxy.flow.FlowMaster"
|
||||||
|
log = None # type: Callable[[str], None]
|
@ -3,35 +3,35 @@ log = []
|
|||||||
|
|
||||||
|
|
||||||
def clientconnect(cc):
|
def clientconnect(cc):
|
||||||
mitmproxy.log("XCLIENTCONNECT")
|
mitmproxy.ctx.log("XCLIENTCONNECT")
|
||||||
log.append("clientconnect")
|
log.append("clientconnect")
|
||||||
|
|
||||||
|
|
||||||
def serverconnect(cc):
|
def serverconnect(cc):
|
||||||
mitmproxy.log("XSERVERCONNECT")
|
mitmproxy.ctx.log("XSERVERCONNECT")
|
||||||
log.append("serverconnect")
|
log.append("serverconnect")
|
||||||
|
|
||||||
|
|
||||||
def request(f):
|
def request(f):
|
||||||
mitmproxy.log("XREQUEST")
|
mitmproxy.ctx.log("XREQUEST")
|
||||||
log.append("request")
|
log.append("request")
|
||||||
|
|
||||||
|
|
||||||
def response(f):
|
def response(f):
|
||||||
mitmproxy.log("XRESPONSE")
|
mitmproxy.ctx.log("XRESPONSE")
|
||||||
log.append("response")
|
log.append("response")
|
||||||
|
|
||||||
|
|
||||||
def responseheaders(f):
|
def responseheaders(f):
|
||||||
mitmproxy.log("XRESPONSEHEADERS")
|
mitmproxy.ctx.log("XRESPONSEHEADERS")
|
||||||
log.append("responseheaders")
|
log.append("responseheaders")
|
||||||
|
|
||||||
|
|
||||||
def clientdisconnect(cc):
|
def clientdisconnect(cc):
|
||||||
mitmproxy.log("XCLIENTDISCONNECT")
|
mitmproxy.ctx.log("XCLIENTDISCONNECT")
|
||||||
log.append("clientdisconnect")
|
log.append("clientdisconnect")
|
||||||
|
|
||||||
|
|
||||||
def error(cc):
|
def error(cc):
|
||||||
mitmproxy.log("XERROR")
|
mitmproxy.ctx.log("XERROR")
|
||||||
log.append("error")
|
log.append("error")
|
||||||
|
@ -2,5 +2,5 @@ import mitmproxy
|
|||||||
|
|
||||||
|
|
||||||
def request(f):
|
def request(f):
|
||||||
f = mitmproxy.master.duplicate_flow(f)
|
f = mitmproxy.ctx.master.duplicate_flow(f)
|
||||||
mitmproxy.master.replay_request(f, block=True, run_scripthooks=False)
|
mitmproxy.ctx.master.replay_request(f, block=True, run_scripthooks=False)
|
||||||
|
@ -21,8 +21,8 @@ def example(command):
|
|||||||
yield s
|
yield s
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("mitmproxy.master")
|
@mock.patch("mitmproxy.ctx.master")
|
||||||
@mock.patch("mitmproxy.log")
|
@mock.patch("mitmproxy.ctx.log")
|
||||||
def test_load_scripts(log, master):
|
def test_load_scripts(log, master):
|
||||||
scripts = glob.glob("%s/*.py" % example_dir)
|
scripts = glob.glob("%s/*.py" % example_dir)
|
||||||
|
|
||||||
@ -121,7 +121,7 @@ def test_redirect_requests():
|
|||||||
assert flow.request.host == "mitmproxy.org"
|
assert flow.request.host == "mitmproxy.org"
|
||||||
|
|
||||||
|
|
||||||
@mock.patch("mitmproxy.log")
|
@mock.patch("mitmproxy.ctx.log")
|
||||||
def test_har_extractor(log):
|
def test_har_extractor(log):
|
||||||
if sys.version_info >= (3, 0):
|
if sys.version_info >= (3, 0):
|
||||||
with tutils.raises("does not work on Python 3"):
|
with tutils.raises("does not work on Python 3"):
|
||||||
|
Loading…
Reference in New Issue
Block a user