Merge pull request #4426 from mhils/mypy

update mypy
This commit is contained in:
Maximilian Hils 2021-02-04 03:36:25 +01:00 committed by GitHub
commit c5bd7b1ae1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 52 additions and 49 deletions

View File

@ -4,12 +4,14 @@ import typing
from mitmproxy import command from mitmproxy import command
from mitmproxy import ctx from mitmproxy import ctx
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import http
class MyAddon: class MyAddon:
@command.command("myaddon.addheader") @command.command("myaddon.addheader")
def addheader(self, flows: typing.Sequence[flow.Flow]) -> None: def addheader(self, flows: typing.Sequence[flow.Flow]) -> None:
for f in flows: for f in flows:
if isinstance(f, http.HTTPFlow):
f.request.headers["myheader"] = "value" f.request.headers["myheader"] = "value"
ctx.log.alert("done") ctx.log.alert("done")

View File

@ -4,6 +4,7 @@ import typing
from mitmproxy import command from mitmproxy import command
from mitmproxy import ctx from mitmproxy import ctx
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import http
from mitmproxy import types from mitmproxy import types
@ -14,8 +15,9 @@ class MyAddon:
flows: typing.Sequence[flow.Flow], flows: typing.Sequence[flow.Flow],
path: types.Path, path: types.Path,
) -> None: ) -> None:
totals = {} totals: typing.Dict[str, int] = {}
for f in flows: for f in flows:
if isinstance(f, http.HTTPFlow):
totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1 totals[f.request.host] = totals.setdefault(f.request.host, 0) + 1
with open(path, "w+") as fp: with open(path, "w+") as fp:

View File

@ -36,6 +36,7 @@ def request(flow: http.HTTPFlow):
def response(flow: http.HTTPFlow): def response(flow: http.HTTPFlow):
assert flow.response # make type checker happy
if flow.response.trailers: if flow.response.trailers:
print("HTTP Trailers detected! Response contains:", flow.response.trailers) print("HTTP Trailers detected! Response contains:", flow.response.trailers)

View File

@ -7,5 +7,6 @@ from mitmproxy import http
def response(flow: http.HTTPFlow) -> None: def response(flow: http.HTTPFlow) -> None:
assert flow.response # make type checker happy
reflector = b"<style>body {transform: scaleX(-1);}</style></head>" reflector = b"<style>body {transform: scaleX(-1);}</style></head>"
flow.response.content = flow.response.content.replace(b"</head>", reflector) flow.response.content = flow.response.content.replace(b"</head>", reflector)

View File

@ -2,7 +2,7 @@
""" """
Read a mitmproxy dump file. Read a mitmproxy dump file.
""" """
from mitmproxy import io from mitmproxy import io, http
from mitmproxy.exceptions import FlowReadException from mitmproxy.exceptions import FlowReadException
import pprint import pprint
import sys import sys
@ -13,6 +13,7 @@ with open(sys.argv[1], "rb") as logfile:
try: try:
for f in freader.stream(): for f in freader.stream():
print(f) print(f)
if isinstance(f, http.HTTPFlow):
print(f.request.host) print(f.request.host)
pp.pprint(f.get_state()) pp.pprint(f.get_state())
print("") print("")

View File

@ -393,9 +393,9 @@ class CertStore:
# Dump the certificate in PKCS12 format for Windows devices # Dump the certificate in PKCS12 format for Windows devices
(path / f"{basename}-ca-cert.p12").write_bytes( (path / f"{basename}-ca-cert.p12").write_bytes(
pkcs12.serialize_key_and_certificates( # type: ignore pkcs12.serialize_key_and_certificates(
name=basename.encode(), name=basename.encode(),
key=None, key=None, # type: ignore
cert=ca, cert=ca,
cas=None, cas=None,
encryption_algorithm=serialization.NoEncryption(), encryption_algorithm=serialization.NoEncryption(),

View File

View File

@ -1,15 +1,13 @@
import io
import uuid import uuid
from mitmproxy.test import tutils from mitmproxy import controller
from mitmproxy import flow
from mitmproxy import http
from mitmproxy import tcp from mitmproxy import tcp
from mitmproxy import websocket from mitmproxy import websocket
from mitmproxy import controller
from mitmproxy import http
from mitmproxy import flow
from mitmproxy.net import http as net_http from mitmproxy.net import http as net_http
from mitmproxy.proxy import context from mitmproxy.proxy import context
from mitmproxy.test import tutils
from wsproto.frame_protocol import Opcode from wsproto.frame_protocol import Opcode
@ -34,7 +32,6 @@ def ttcpflow(client_conn=True, server_conn=True, messages=True, err=None):
def twebsocketflow(client_conn=True, server_conn=True, messages=True, err=None, handshake_flow=True): def twebsocketflow(client_conn=True, server_conn=True, messages=True, err=None, handshake_flow=True):
if client_conn is True: if client_conn is True:
client_conn = tclient_conn() client_conn = tclient_conn()
if server_conn is True: if server_conn is True:
@ -169,7 +166,7 @@ def tclient_conn() -> context.Client:
alpn_offers=[], alpn_offers=[],
cipher_list=[], cipher_list=[],
)) ))
c.reply = controller.DummyReply() c.reply = controller.DummyReply() # type: ignore
return c return c
@ -197,9 +194,7 @@ def tserver_conn() -> context.Server:
cipher_list=[], cipher_list=[],
via2=None, via2=None,
)) ))
c.reply = controller.DummyReply() c.reply = controller.DummyReply() # type: ignore
c.rfile = io.BytesIO()
c.wfile = io.BytesIO()
return c return c

View File

@ -21,7 +21,7 @@ def treq(**kwargs) -> http.Request:
timestamp_end=946681201, timestamp_end=946681201,
) )
default.update(kwargs) default.update(kwargs)
return http.Request(**default) return http.Request(**default) # type: ignore
def tresp(**kwargs) -> http.Response: def tresp(**kwargs) -> http.Response:
@ -40,4 +40,4 @@ def tresp(**kwargs) -> http.Response:
timestamp_end=946681203, timestamp_end=946681203,
) )
default.update(kwargs) default.update(kwargs)
return http.Response(**default) return http.Response(**default) # type: ignore

View File

@ -18,7 +18,7 @@ from mitmproxy.tools.console import statusbar
if os.name == "nt": if os.name == "nt":
from mitmproxy.contrib.urwid import raw_display from mitmproxy.contrib.urwid import raw_display
else: else:
from urwid import raw_display from urwid import raw_display # type: ignore
class StackWidget(urwid.Frame): class StackWidget(urwid.Frame):

View File

@ -25,6 +25,7 @@ exclude_lines =
[mypy] [mypy]
ignore_missing_imports = True ignore_missing_imports = True
files = mitmproxy,examples/addons,release
[mypy-mitmproxy.contrib.*] [mypy-mitmproxy.contrib.*]
ignore_errors = True ignore_errors = True

View File

@ -28,9 +28,9 @@ commands =
python ./test/filename_matching.py python ./test/filename_matching.py
[testenv:mypy] [testenv:mypy]
deps = mypy==0.790 deps = mypy==0.800
commands = commands =
mypy . {posargs} mypy {posargs}
[testenv:individual_coverage] [testenv:individual_coverage]
commands = commands =