From 1960eedc813b878ab193ff84ae10054ca4e0eccd Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Thu, 11 Aug 2022 21:55:46 +0200 Subject: serve a stub of meta-site for proxy configuration --- src/hydrilla/proxy/addon.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) (limited to 'src/hydrilla/proxy/addon.py') diff --git a/src/hydrilla/proxy/addon.py b/src/hydrilla/proxy/addon.py index 06bce86..4058421 100644 --- a/src/hydrilla/proxy/addon.py +++ b/src/hydrilla/proxy/addon.py @@ -41,15 +41,7 @@ from threading import Lock from pathlib import Path from contextlib import contextmanager -# for mitmproxy 6.* -from mitmproxy.net import http -if not hasattr(http, 'Headers'): - # for mitmproxy 8.* - from mitmproxy import http # type: ignore - -from mitmproxy.http import HTTPFlow - -from mitmproxy import addonmanager, ctx +from mitmproxy import tls, http, addonmanager, ctx from mitmproxy.script import concurrent from ..exceptions import HaketiloException @@ -127,7 +119,7 @@ class HaketiloAddon: self.configured = True - def try_get_policy(self, flow: HTTPFlow, fail_ok: bool = True) -> \ + def try_get_policy(self, flow: http.HTTPFlow, fail_ok: bool = True) -> \ t.Optional[policies.Policy]: """....""" with self.policies_lock: @@ -151,16 +143,16 @@ class HaketiloAddon: return policy - def get_policy(self, flow: HTTPFlow) -> policies.Policy: + def get_policy(self, flow: http.HTTPFlow) -> policies.Policy: return t.cast(policies.Policy, self.try_get_policy(flow, fail_ok=False)) - def forget_policy(self, flow: HTTPFlow) -> None: + def forget_policy(self, flow: http.HTTPFlow) -> None: """....""" with self.policies_lock: self.flow_policies.pop(id(flow), None) @contextmanager - def http_safe_event_handling(self, flow: HTTPFlow) -> t.Iterator: + def http_safe_event_handling(self, flow: http.HTTPFlow) -> t.Iterator: """....""" with self.configured_lock: assert self.configured @@ -181,7 +173,7 @@ class HaketiloAddon: self.forget_policy(flow) @concurrent - def requestheaders(self, flow: HTTPFlow) -> None: + def requestheaders(self, flow: http.HTTPFlow) -> None: # TODO: don't account for mitmproxy 6 in the code # Mitmproxy 6 causes even more strange behavior than described below. # This cannot be easily worked around. Let's just use version 8 and @@ -201,7 +193,7 @@ class HaketiloAddon: flow.request.anticache() @concurrent - def request(self, flow: HTTPFlow) -> None: + def request(self, flow: http.HTTPFlow) -> None: """ .... """ @@ -236,7 +228,7 @@ class HaketiloAddon: content = result.body ) - def responseheaders(self, flow: HTTPFlow) -> None: + def responseheaders(self, flow: http.HTTPFlow) -> None: """ ...... """ @@ -249,7 +241,7 @@ class HaketiloAddon: flow.response.stream = True @concurrent - def response(self, flow: HTTPFlow) -> None: + def response(self, flow: http.HTTPFlow) -> None: """ ...... """ @@ -276,6 +268,17 @@ class HaketiloAddon: self.forget_policy(flow) - def error(self, flow: HTTPFlow) -> None: + def tls_clienthello(self, data: tls.ClientHelloData): + if data.context.server.address is None: + return + + host, port = data.context.server.address + if (host == 'hkt.mitm.it' or host.endswith('.hkt.mitm.it')) and \ + port == 443: + return + + data.establish_server_tls_first = True + + def error(self, flow: http.HTTPFlow) -> None: """....""" self.forget_policy(flow) -- cgit v1.2.3