From 9688618bbda97415e65b43bb9fdcd87851a7456a Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 11 Oct 2021 18:26:58 +0200 Subject: [PATCH 1/3] clarify that `IS_WINDOWS` includes WSL --- mitmproxy/tools/console/common.py | 2 +- mitmproxy/tools/console/window.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mitmproxy/tools/console/common.py b/mitmproxy/tools/console/common.py index f807ae17c..fe3f6469d 100644 --- a/mitmproxy/tools/console/common.py +++ b/mitmproxy/tools/console/common.py @@ -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): diff --git a/mitmproxy/tools/console/window.py b/mitmproxy/tools/console/window.py index cae290092..2912abeea 100644 --- a/mitmproxy/tools/console/window.py +++ b/mitmproxy/tools/console/window.py @@ -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) From 82529d88f022dd2b1073c4ab4a6b4a9d3894c66e Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 11 Oct 2021 18:27:39 +0200 Subject: [PATCH 2/3] windows: fix file editing tornado's asnycio patch does not take nonexisting file descriptors very well, so we need to catch errors here. --- mitmproxy/contrib/urwid/raw_display.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mitmproxy/contrib/urwid/raw_display.py b/mitmproxy/contrib/urwid/raw_display.py index b2a56b4a1..ee6d30bdf 100644 --- a/mitmproxy/contrib/urwid/raw_display.py +++ b/mitmproxy/contrib/urwid/raw_display.py @@ -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) From 0d466f2a2edbea8439d0475bc7c2afd1c51877f9 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Mon, 11 Oct 2021 18:29:09 +0200 Subject: [PATCH 3/3] body editing: better editor guessing, fix #4798 --- mitmproxy/tools/console/master.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index 02cd9e9d7..ea3668f46 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -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(