-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaddon.py
55 lines (43 loc) · 1.96 KB
/
addon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import Iterable
from mitmproxy import tls, http, connection, addonmanager
from mitmproxy.addons.tlsconfig import TlsConfig
from mitmproxy.http import HTTPFlow
import yaml
def matches_any(domains: Iterable[str], domain: str) -> bool:
return any(to_unblock in domain for to_unblock in domains)
class SNIProxy:
config = TlsConfig()
sni: str
domains: Iterable[str]
delicate_domains: Iterable[str]
def load(self, _loader: addonmanager.Loader):
with open("./domains.yaml") as f:
out = yaml.safe_load(f)
self.domains = out.get("domains", [])
self.delicate_domains = out.get("delicate_domains", [])
self.sni = out.get("default_sni", "google.com")
def request(self, flow: HTTPFlow):
if flow.request.scheme == "http":
# Firefox displays an annoying message if it thinks its
# captive portal check is being intercepted by a mitm redirect
# we also need mitm.it to be accessible in http so mitmproxy can
# change the response to be a cert download page
if flow.request.host in ("detectportal.firefox.com", "mitm.it"):
# TBH I don't think this is working for firefox at all lol
return
# Always redirect other non-https requests to https
# since they will be blocked without TLS
flow.response = http.Response.make(
307,
b"",
{"Location": flow.request.url.replace("http://", "https://")},
)
def tls_start_server(self, data: tls.TlsData):
if isinstance(data.conn, connection.Server):
(domain, _) = data.conn.address
if matches_any(self.domains, domain):
data.context.client.sni = self.sni
elif matches_any(self.delicate_domains, domain):
data.context.client.sni = rf"{domain}."
self.config.tls_start_server(data)
addons = [SNIProxy()]