Body Editing Fixes (#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 #4798
This commit is contained in:
Maximilian Hils 2021-10-12 00:23:35 +02:00 committed by GitHub
commit aa2ec224c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 8 deletions

View File

@ -444,7 +444,10 @@ class Screen(BaseScreen, RealTerminal):
self._input_thread = None
for handle in self._current_event_loop_handles:
event_loop.remove_watch_file(handle)
try:
event_loop.remove_watch_file(handle)
except KeyError:
pass
if self._input_timeout:
event_loop.remove_alarm(self._input_timeout)

View File

@ -14,7 +14,7 @@ from mitmproxy.utils import human, emoji
from mitmproxy.tcp import TCPFlow
# Detect Windows Subsystem for Linux and Windows
IS_WINDOWS = "Microsoft" in platform.platform() or "Windows" in platform.platform()
IS_WINDOWS_OR_WSL = "Microsoft" in platform.platform() or "Windows" in platform.platform()
def is_keypress(k):

View File

@ -4,6 +4,7 @@ import mimetypes
import os
import os.path
import shlex
import shutil
import signal
import stat
import subprocess
@ -24,7 +25,6 @@ from mitmproxy.addons import intercept
from mitmproxy.addons import eventstore
from mitmproxy.addons import readfile
from mitmproxy.addons import view
from mitmproxy.tools.console import common
from mitmproxy.tools.console import consoleaddons
from mitmproxy.tools.console import defaultkeys
from mitmproxy.tools.console import keymap
@ -116,13 +116,26 @@ class ConsoleMaster(master.Master):
self.loop.screen_size = None
self.loop.draw_screen()
def get_editor(self) -> str:
# based upon https://github.com/pallets/click/blob/main/src/click/_termui_impl.py
if m := os.environ.get("MITMPROXY_EDITOR"):
return m
if m := os.environ.get("EDITOR"):
return m
for editor in "sensible-editor", "nano", "vim":
if shutil.which(editor):
return editor
if os.name == "nt":
return "notepad"
else:
return "vi"
def spawn_editor(self, data):
text = not isinstance(data, bytes)
fd, name = tempfile.mkstemp('', "mproxy", text=text)
fd, name = tempfile.mkstemp('', "mitmproxy", text=text)
with open(fd, "w" if text else "wb") as f:
f.write(data)
# if no EDITOR is set, assume 'vi'
c = os.environ.get("MITMPROXY_EDITOR") or os.environ.get("EDITOR") or "vi"
c = self.get_editor()
cmd = shlex.split(c)
cmd.append(name)
with self.uistopped():
@ -203,7 +216,7 @@ class ConsoleMaster(master.Master):
["console_palette", "console_palette_transparent"]
)
loop = asyncio.get_event_loop()
if common.IS_WINDOWS:
if isinstance(loop, getattr(asyncio, "ProactorEventLoop", tuple())):
# fix for https://bugs.python.org/issue37373
loop = AddThreadSelectorEventLoop(loop)
self.loop = urwid.MainLoop(

View File

@ -324,7 +324,7 @@ class Window(urwid.Frame):
class Screen(raw_display.Screen):
def write(self, data):
if common.IS_WINDOWS:
if common.IS_WINDOWS_OR_WSL:
# replace urwid's SI/SO, which produce artifacts under WSL.
# at some point we may figure out what they actually do.
data = re.sub("[\x0e\x0f]", "", data)