Skip to content

Commit c437d6a

Browse files
authored
Merge pull request #1 from stypr/12.1.1
Upgrade scripts to be compatible for mitmproxy 12.x
2 parents 29f4f93 + f4b205c commit c437d6a

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

caddy/Caddyfile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ example.tld {
3636

3737
handle_path /* {
3838
# use `caddy hash-password` to generate credentials
39-
basicauth {
40-
admin $2a$......
41-
}
39+
# or use the new authentication feature shipped on mitmproxy 12.x and higher.
40+
# In case you want to use the built-in authentication feature, please change run.sh accordingly
41+
# and remove `header_up` values from Caddyfile.
42+
# basicauth {
43+
# admin $2a$......
44+
# }
4245
reverse_proxy * http://localhost:8000 {
4346
header_up Host localhost:8000
47+
header_up Authorization "Bearer changeme"
4448
header_up Origin http://localhost:8000
4549
}
4650
}

main.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
Main script for mitmproxy
55
"""
66

7-
import os
8-
import pkgutil
7+
import sys
98
import asyncio
10-
import importlib
9+
from pathlib import Path
1110

1211
from mitmproxy import ctx, contentviews
1312
from reloader import watch_changes, reload_modules
@@ -18,12 +17,19 @@
1817
from addons.no_cache import NoCache
1918
from addons.debug import Debug
2019

21-
# Hot Reloading
20+
# Redirect all stdout to stderr
21+
sys.stdout = sys.stderr
22+
23+
# HotReloading
2224
for task in asyncio.all_tasks():
2325
if task.get_name() == 'watch_changes':
2426
ctx.log.info("Canceling previous autoreload task")
2527
task.cancel()
26-
asyncio.create_task(watch_changes(), name="watch_changes")
28+
29+
asyncio.create_task(
30+
watch_changes(trigger_file=Path(__file__)),
31+
name="watch_changes"
32+
)
2733

2834
reload_modules([
2935
"addons",
@@ -43,7 +49,10 @@
4349
ViewSekai(),
4450
]
4551

52+
####
4653
# This code is required for adding/removing views
54+
####
55+
4756
def load(loader):
4857
for view in views:
4958
contentviews.add(view)

reloader.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
11
import os
22
import pkgutil
3-
import asyncio
43
import importlib
54

65
from pathlib import Path
6+
from watchfiles import awatch, PythonFilter
77
from mitmproxy import ctx
8-
from watchfiles import awatch, Change, PythonFilter
98

109
# By default, Hot reloading is only done for Python scripts only
11-
# Use https://watchfiles.helpmanual.io/api/filters/ as a reference
10+
# Read https://watchfiles.helpmanual.io/api/filters/ for more information
1211

1312
class ReloadFilter(PythonFilter):
1413
ignore_paths = [os.path.realpath(__file__)]
1514

16-
async def watch_changes():
15+
async def watch_changes(trigger_file: Path):
1716
"""
1817
Watch for all changes in the subdirectory
1918
the main script needs to be filtered as mitmproxy autoreloads main script changes
2019
"""
2120
ctx.log.info("Autoreload task started")
2221
async for changes in awatch(
23-
os.path.realpath(__file__),
22+
os.path.dirname(os.path.realpath(__file__)),
2423
watch_filter=ReloadFilter()
2524
):
2625
try:
2726
ctx.log.info(f"Changes detected: {changes}")
28-
Path(__file__).touch()
27+
trigger_file.touch()
2928
except Exception as e:
3029
print("Exception in autoreload:", e)
3130

32-
def reload_modules(module_dirs):
31+
def reload_modules(module_dirs: list):
3332
"""
3433
Reload all modules in subdirectories
3534
"""
@@ -44,5 +43,4 @@ def reload_modules(module_dirs):
4443
ctx.log.info(f"Reloading module: {module_name}")
4544
importlib.reload(importlib.import_module(module_name))
4645
except ModuleNotFoundError as e:
47-
ctx.log.error("Reloading Module Error: {%s}")
48-
46+
ctx.log.error(f"Reloading Module Error: {e}")

run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
warp-cli disconnect
44
warp-cli connect
55
mitmweb \
6+
--set web_password='changeme' \
67
--ignore-hosts dns.google.com \
78
--web-port 8000 \
89
--web-host localhost \
@@ -15,8 +16,7 @@ mitmweb \
1516
--set confdir=$PWD/config \
1617
--mode wireguard:$PWD/config/wireguard_server1.conf@303 \
1718
--script main.py \
18-
-r config/flow \
19-
-w config/flow
19+
-r config/flow
2020

2121
# For mulitple clients, simple add more configs.
2222
# Wireguard client configs are available in the web frontend.

views/sekai.py

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
"""
88

99
from typing import Optional
10-
from urllib.parse import urlparse
10+
import json
1111
import msgpack
1212
from Crypto.Cipher import AES
13-
from mitmproxy import contentviews
14-
from mitmproxy import flow
15-
from mitmproxy import http
16-
# from mitmproxy import ctx
13+
from mitmproxy.contentviews import Contentview
1714

1815
class SekaiCore:
19-
# To use this you need a valid key and IV.
16+
# To use this class, you need a valid key and IV.
2017
aes_key = b""
2118
aes_iv = b""
2219

@@ -48,39 +45,31 @@ def decrypt_data(cls, crypttext):
4845
return {}
4946

5047

51-
class ViewSekai(contentviews.View):
52-
name = "sekai"
53-
54-
def __call__(
55-
self,
56-
data: bytes,
57-
*,
58-
content_type: Optional[str] = None,
59-
flow: Optional[flow.Flow] = None,
60-
http_message: Optional[http.Message] = None,
61-
**unknown_metadata,
62-
) -> contentviews.TViewResult:
63-
""" Decrypts data upon response. """
64-
try:
65-
plaintext: Optional[dict] = SekaiCore.decrypt_data(data)
66-
return "sekai", contentviews.json.format_json(plaintext)
67-
except Exception as e:
68-
return "sekai", contentviews.format_text(e)
48+
class ViewProjectSekai(Contentview):
49+
name = "pjsekai"
6950

7051
def render_priority(
7152
self,
7253
data: bytes,
73-
*,
74-
content_type: Optional[str] = None,
75-
flow: Optional[flow.Flow] = None,
76-
http_message: Optional[http.Message] = None,
77-
**unknown_metadata,
54+
metadata
7855
) -> float:
7956
""" prioritize using this when priority is set """
8057
try:
81-
if not data:
58+
if not data or not metadata:
8259
return 0
83-
_ = SekaiCore.decrypt_data(data)
84-
return 1
60+
61+
decrypt_result = SekaiCore.decrypt_data(data)
62+
return 1 if decrypt_result else 0
8563
except Exception as e:
8664
return 0
65+
66+
def prettify(
67+
self,
68+
data: bytes,
69+
_
70+
) -> str:
71+
try:
72+
plaintext: Optional[dict] = SekaiCore.decrypt_data(data)
73+
return json.dumps(plaintext, indent=4, ensure_ascii=False)
74+
except Exception as e:
75+
return e

0 commit comments

Comments
 (0)