Skip to content

Commit aa2ec22

Browse files
authored
Body Editing Fixes (mitmproxy#4853)
* clarify that `IS_WINDOWS` includes WSL * windows: fix file editing tornado's asnycio patch does not take nonexisting file descriptors very well, so we need to catch errors here. * body editing: better editor guessing, fix mitmproxy#4798
2 parents aad92c9 + 0d466f2 commit aa2ec22

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

mitmproxy/contrib/urwid/raw_display.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ def unhook_event_loop(self, event_loop):
444444
self._input_thread = None
445445

446446
for handle in self._current_event_loop_handles:
447-
event_loop.remove_watch_file(handle)
447+
try:
448+
event_loop.remove_watch_file(handle)
449+
except KeyError:
450+
pass
448451

449452
if self._input_timeout:
450453
event_loop.remove_alarm(self._input_timeout)

mitmproxy/tools/console/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from mitmproxy.tcp import TCPFlow
1515

1616
# Detect Windows Subsystem for Linux and Windows
17-
IS_WINDOWS = "Microsoft" in platform.platform() or "Windows" in platform.platform()
17+
IS_WINDOWS_OR_WSL = "Microsoft" in platform.platform() or "Windows" in platform.platform()
1818

1919

2020
def is_keypress(k):

mitmproxy/tools/console/master.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import os.path
66
import shlex
7+
import shutil
78
import signal
89
import stat
910
import subprocess
@@ -24,7 +25,6 @@
2425
from mitmproxy.addons import eventstore
2526
from mitmproxy.addons import readfile
2627
from mitmproxy.addons import view
27-
from mitmproxy.tools.console import common
2828
from mitmproxy.tools.console import consoleaddons
2929
from mitmproxy.tools.console import defaultkeys
3030
from mitmproxy.tools.console import keymap
@@ -116,13 +116,26 @@ def uistopped(self):
116116
self.loop.screen_size = None
117117
self.loop.draw_screen()
118118

119+
def get_editor(self) -> str:
120+
# based upon https://github.com/pallets/click/blob/main/src/click/_termui_impl.py
121+
if m := os.environ.get("MITMPROXY_EDITOR"):
122+
return m
123+
if m := os.environ.get("EDITOR"):
124+
return m
125+
for editor in "sensible-editor", "nano", "vim":
126+
if shutil.which(editor):
127+
return editor
128+
if os.name == "nt":
129+
return "notepad"
130+
else:
131+
return "vi"
132+
119133
def spawn_editor(self, data):
120134
text = not isinstance(data, bytes)
121-
fd, name = tempfile.mkstemp('', "mproxy", text=text)
135+
fd, name = tempfile.mkstemp('', "mitmproxy", text=text)
122136
with open(fd, "w" if text else "wb") as f:
123137
f.write(data)
124-
# if no EDITOR is set, assume 'vi'
125-
c = os.environ.get("MITMPROXY_EDITOR") or os.environ.get("EDITOR") or "vi"
138+
c = self.get_editor()
126139
cmd = shlex.split(c)
127140
cmd.append(name)
128141
with self.uistopped():
@@ -203,7 +216,7 @@ def run(self):
203216
["console_palette", "console_palette_transparent"]
204217
)
205218
loop = asyncio.get_event_loop()
206-
if common.IS_WINDOWS:
219+
if isinstance(loop, getattr(asyncio, "ProactorEventLoop", tuple())):
207220
# fix for https://bugs.python.org/issue37373
208221
loop = AddThreadSelectorEventLoop(loop)
209222
self.loop = urwid.MainLoop(

mitmproxy/tools/console/window.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def keypress(self, size, k):
324324
class Screen(raw_display.Screen):
325325

326326
def write(self, data):
327-
if common.IS_WINDOWS:
327+
if common.IS_WINDOWS_OR_WSL:
328328
# replace urwid's SI/SO, which produce artifacts under WSL.
329329
# at some point we may figure out what they actually do.
330330
data = re.sub("[\x0e\x0f]", "", data)

0 commit comments

Comments
 (0)