extend mypy example/simple
This commit is contained in:
parent
b537997f4f
commit
36118973d9
|
@ -1,2 +1,5 @@
|
||||||
def response(flow):
|
from mitmproxy import http
|
||||||
|
|
||||||
|
|
||||||
|
def response(flow: http.HTTPFlow) -> None:
|
||||||
flow.response.headers["newheader"] = "foo"
|
flow.response.headers["newheader"] = "foo"
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
from mitmproxy import http
|
||||||
|
|
||||||
|
|
||||||
class AddHeader:
|
class AddHeader:
|
||||||
def response(self, flow):
|
def response(self, flow: http.HTTPFlow) -> None:
|
||||||
flow.response.headers["newheader"] = "foo"
|
flow.response.headers["newheader"] = "foo"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ This example shows how one can add a custom contentview to mitmproxy.
|
||||||
The content view API is explained in the mitmproxy.contentviews module.
|
The content view API is explained in the mitmproxy.contentviews module.
|
||||||
"""
|
"""
|
||||||
from mitmproxy import contentviews
|
from mitmproxy import contentviews
|
||||||
|
from typing import Tuple, Iterable, AnyStr, List
|
||||||
|
|
||||||
|
|
||||||
class ViewSwapCase(contentviews.View):
|
class ViewSwapCase(contentviews.View):
|
||||||
|
@ -13,7 +14,7 @@ class ViewSwapCase(contentviews.View):
|
||||||
prompt = ("swap case text", "z")
|
prompt = ("swap case text", "z")
|
||||||
content_types = ["text/plain"]
|
content_types = ["text/plain"]
|
||||||
|
|
||||||
def __call__(self, data: bytes, **metadata):
|
def __call__(self, data: bytes, **metadata) -> Tuple[str,Iterable[List[Tuple[str, AnyStr]]]]:
|
||||||
return "case-swapped text", contentviews.format_text(data.swapcase())
|
return "case-swapped text", contentviews.format_text(data.swapcase())
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
This scripts demonstrates how to use mitmproxy's filter pattern in scripts.
|
This scripts demonstrates how to use mitmproxy's filter pattern in scripts.
|
||||||
"""
|
"""
|
||||||
from mitmproxy import flowfilter
|
from mitmproxy import flowfilter
|
||||||
from mitmproxy import ctx
|
from mitmproxy import ctx, http
|
||||||
|
|
||||||
|
|
||||||
class Filter:
|
class Filter:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.filter = None
|
self.filter = None # type: flowfilter.TFilter
|
||||||
|
|
||||||
def configure(self, updated):
|
def configure(self, updated):
|
||||||
self.filter = flowfilter.parse(ctx.options.flowfilter)
|
self.filter = flowfilter.parse(ctx.options.flowfilter)
|
||||||
|
@ -17,7 +17,7 @@ class Filter:
|
||||||
"flowfilter", str, "", "Check that flow matches filter."
|
"flowfilter", str, "", "Check that flow matches filter."
|
||||||
)
|
)
|
||||||
|
|
||||||
def response(self, flow):
|
def response(self, flow: http.HTTPFlow) -> None:
|
||||||
if flowfilter.match(self.filter, flow):
|
if flowfilter.match(self.filter, flow):
|
||||||
print("Flow matches filter:")
|
print("Flow matches filter:")
|
||||||
print(flow)
|
print(flow)
|
||||||
|
|
|
@ -8,6 +8,7 @@ from mitmproxy.exceptions import FlowReadException
|
||||||
import pprint
|
import pprint
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
with open(sys.argv[1], "rb") as logfile:
|
with open(sys.argv[1], "rb") as logfile:
|
||||||
freader = io.FlowReader(logfile)
|
freader = io.FlowReader(logfile)
|
||||||
pp = pprint.PrettyPrinter(indent=4)
|
pp = pprint.PrettyPrinter(indent=4)
|
||||||
|
|
|
@ -7,18 +7,18 @@ to multiple files in parallel.
|
||||||
"""
|
"""
|
||||||
import random
|
import random
|
||||||
import sys
|
import sys
|
||||||
from mitmproxy import io
|
from mitmproxy import io, http
|
||||||
|
|
||||||
|
|
||||||
class Writer:
|
class Writer:
|
||||||
def __init__(self, path):
|
def __init__(self, path: str) -> None:
|
||||||
if path == "-":
|
if path == "-":
|
||||||
f = sys.stdout
|
f = sys.stdout # type: io.TextIO
|
||||||
else:
|
else:
|
||||||
f = open(path, "wb")
|
f = open(path, "wb")
|
||||||
self.w = io.FlowWriter(f)
|
self.w = io.FlowWriter(f)
|
||||||
|
|
||||||
def response(self, flow):
|
def response(self, flow: http.HTTPFlow) -> None:
|
||||||
if random.choice([True, False]):
|
if random.choice([True, False]):
|
||||||
self.w.add(flow)
|
self.w.add(flow)
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# (this script works best with --anticache)
|
# (this script works best with --anticache)
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from mitmproxy import ctx
|
from mitmproxy import ctx, http
|
||||||
|
|
||||||
|
|
||||||
class Injector:
|
class Injector:
|
||||||
|
@ -9,7 +9,7 @@ class Injector:
|
||||||
"iframe", str, "", "IFrame to inject"
|
"iframe", str, "", "IFrame to inject"
|
||||||
)
|
)
|
||||||
|
|
||||||
def response(self, flow):
|
def response(self, flow: http.HTTPFlow) -> None:
|
||||||
if ctx.options.iframe:
|
if ctx.options.iframe:
|
||||||
html = BeautifulSoup(flow.response.content, "html.parser")
|
html = BeautifulSoup(flow.response.content, "html.parser")
|
||||||
if html.body:
|
if html.body:
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
def request(flow):
|
from mitmproxy import http
|
||||||
|
|
||||||
|
|
||||||
|
def request(flow: http.HTTPFlow) -> None:
|
||||||
if flow.request.urlencoded_form:
|
if flow.request.urlencoded_form:
|
||||||
# If there's already a form, one can just add items to the dict:
|
# If there's already a form, one can just add items to the dict:
|
||||||
flow.request.urlencoded_form["mitmproxy"] = "rocks"
|
flow.request.urlencoded_form["mitmproxy"] = "rocks"
|
||||||
|
|
|
@ -1,2 +1,5 @@
|
||||||
def request(flow):
|
from mitmproxy import http
|
||||||
|
|
||||||
|
|
||||||
|
def request(flow: http.HTTPFlow) -> None:
|
||||||
flow.request.query["mitmproxy"] = "rocks"
|
flow.request.query["mitmproxy"] = "rocks"
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
"""
|
"""
|
||||||
This example shows two ways to redirect flows to another server.
|
This example shows two ways to redirect flows to another server.
|
||||||
"""
|
"""
|
||||||
|
from mitmproxy import http
|
||||||
|
|
||||||
|
|
||||||
def request(flow):
|
def request(flow: http.HTTPFlow) -> None:
|
||||||
# pretty_host takes the "Host" header of the request into account,
|
# pretty_host takes the "Host" header of the request into account,
|
||||||
# which is useful in transparent mode where we usually only have the IP
|
# which is useful in transparent mode where we usually only have the IP
|
||||||
# otherwise.
|
# otherwise.
|
||||||
|
|
|
@ -5,7 +5,7 @@ without sending any data to the remote server.
|
||||||
from mitmproxy import http
|
from mitmproxy import http
|
||||||
|
|
||||||
|
|
||||||
def request(flow):
|
def request(flow: http.HTTPFlow) -> None:
|
||||||
# pretty_url takes the "Host" header of the request into account, which
|
# pretty_url takes the "Host" header of the request into account, which
|
||||||
# is useful in transparent mode where we usually only have the IP otherwise.
|
# is useful in transparent mode where we usually only have the IP otherwise.
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
This script rotates all images passing through the proxy by 180 degrees.
|
This script rotates all images passing through the proxy by 180 degrees.
|
||||||
"""
|
"""
|
||||||
import io
|
import io
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from mitmproxy import http
|
||||||
|
|
||||||
|
|
||||||
def response(flow):
|
def response(flow: http.HTTPFlow) -> None:
|
||||||
if flow.response.headers.get("content-type", "").startswith("image"):
|
if flow.response.headers.get("content-type", "").startswith("image"):
|
||||||
s = io.BytesIO(flow.response.content)
|
s = io.BytesIO(flow.response.content)
|
||||||
img = Image.open(s).rotate(180)
|
img = Image.open(s).rotate(180)
|
||||||
|
|
|
@ -10,7 +10,7 @@ app = Flask("proxapp")
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def hello_world():
|
def hello_world() -> str:
|
||||||
return 'Hello World!'
|
return 'Hello World!'
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue