add sans-io switch

This commit is contained in:
Maximilian Hils 2020-11-17 15:04:40 +01:00
parent 3fd89e1bb2
commit 98f060d019
3 changed files with 28 additions and 15 deletions

View File

@ -1,10 +1,10 @@
import html import html
import time import time
from typing import Optional, Tuple from typing import Optional, Tuple
from mitmproxy import connections
from mitmproxy import flow from mitmproxy import flow
from mitmproxy import version from mitmproxy import version
from mitmproxy.net import http from mitmproxy.net import http
from mitmproxy.utils import compat
HTTPRequest = http.Request HTTPRequest = http.Request
HTTPResponse = http.Response HTTPResponse = http.Response
@ -15,16 +15,16 @@ class HTTPFlow(flow.Flow):
An HTTPFlow is a collection of objects representing a single HTTP An HTTPFlow is a collection of objects representing a single HTTP
transaction. transaction.
""" """
request: HTTPRequest request: http.Request
response: Optional[HTTPResponse] = None response: Optional[http.Response] = None
error: Optional[flow.Error] = None error: Optional[flow.Error] = None
""" """
Note that it's possible for a Flow to have both a response and an error Note that it's possible for a Flow to have both a response and an error
object. This might happen, for instance, when a response was received object. This might happen, for instance, when a response was received
from the server, but there was an error sending it back to the client. from the server, but there was an error sending it back to the client.
""" """
server_conn: connections.ServerConnection server_conn: compat.Server
client_conn: connections.ClientConnection client_conn: compat.Client
intercepted: bool = False intercepted: bool = False
""" Is this flow currently being intercepted? """ """ Is this flow currently being intercepted? """
mode: str mode: str
@ -37,8 +37,8 @@ class HTTPFlow(flow.Flow):
_stateobject_attributes = flow.Flow._stateobject_attributes.copy() _stateobject_attributes = flow.Flow._stateobject_attributes.copy()
# mypy doesn't support update with kwargs # mypy doesn't support update with kwargs
_stateobject_attributes.update(dict( _stateobject_attributes.update(dict(
request=HTTPRequest, request=http.Request,
response=HTTPResponse, response=http.Response,
mode=str mode=str
)) ))
@ -67,7 +67,7 @@ def make_error_response(
status_code: int, status_code: int,
message: str = "", message: str = "",
headers: Optional[http.Headers] = None, headers: Optional[http.Headers] = None,
) -> HTTPResponse: ) -> http.Response:
body: bytes = """ body: bytes = """
<html> <html>
<head> <head>
@ -92,11 +92,11 @@ def make_error_response(
Content_Type="text/html" Content_Type="text/html"
) )
return HTTPResponse.make(status_code, body, headers) return http.Response.make(status_code, body, headers)
def make_connect_request(address: Tuple[str, int]) -> HTTPRequest: def make_connect_request(address: Tuple[str, int]) -> http.Request:
return HTTPRequest( return http.Request(
host=address[0], host=address[0],
port=address[1], port=address[1],
method=b"CONNECT", method=b"CONNECT",
@ -115,7 +115,7 @@ def make_connect_request(address: Tuple[str, int]) -> HTTPRequest:
def make_connect_response(http_version): def make_connect_response(http_version):
# Do not send any response headers as it breaks proxying non-80 ports on # Do not send any response headers as it breaks proxying non-80 ports on
# Android emulators using the -http-proxy option. # Android emulators using the -http-proxy option.
return HTTPResponse( return http.Response(
http_version, http_version,
200, 200,
b"Connection established", b"Connection established",
@ -128,4 +128,4 @@ def make_connect_response(http_version):
def make_expect_continue_response(): def make_expect_continue_response():
return HTTPResponse.make(100) return http.Response.make(100)

View File

@ -15,7 +15,7 @@ from mitmproxy import exceptions, master
from mitmproxy import options from mitmproxy import options
from mitmproxy import optmanager from mitmproxy import optmanager
from mitmproxy import proxy from mitmproxy import proxy
from mitmproxy.utils import debug, arg_check from mitmproxy.utils import compat, debug, arg_check
def assert_utf8_env(): def assert_utf8_env():
@ -92,7 +92,7 @@ def run(
) )
pconf = process_options(parser, opts, args) pconf = process_options(parser, opts, args)
server: typing.Any = None server: typing.Any = None
if pconf.options.server: if pconf.options.server and not compat.new_proxy_core: # new core initializes itself as an addon
try: try:
server = proxy.server.ProxyServer(pconf) server = proxy.server.ProxyServer(pconf)
except exceptions.ServerException as v: except exceptions.ServerException as v:

13
mitmproxy/utils/compat.py Normal file
View File

@ -0,0 +1,13 @@
new_proxy_core = False
"""If true, use mitmproxy's new sans-io proxy core."""
if new_proxy_core: # pragma: no cover
from mitmproxy.proxy2 import context
Client = context.Client
Server = context.Server
else: # pragma: no cover
from mitmproxy import connections
Client = connections.ClientConnection
Server = connections.ServerConnection