diff --git a/CHANGELOG.md b/CHANGELOG.md index 051202a58b..f049a80a9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ ([#6659](https://github.com/mitmproxy/mitmproxy/pull/6659), @basedBaba) * Fix a regression when leaf cert creation would fail with intermediate CAs in `ca_file`. ([#6666](https://github.com/mitmproxy/mitmproxy/pull/6666), @manselmi) +* Adding support to edit binary files in a hex editor if one is installed ## 21 January 2024: mitmproxy 10.2.2 diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py index ee303ff5d2..b1203a8289 100644 --- a/mitmproxy/tools/console/master.py +++ b/mitmproxy/tools/console/master.py @@ -9,6 +9,7 @@ import sys import tempfile import threading +from typing import cast from typing import TypeVar import urwid @@ -29,6 +30,7 @@ from mitmproxy.tools.console import palettes from mitmproxy.tools.console import signals from mitmproxy.tools.console import window +from mitmproxy.utils import strutils T = TypeVar("T", str, bytes) @@ -120,12 +122,28 @@ def get_editor(self) -> str: else: return "vi" + @staticmethod + def get_binary_editor() -> str: + if m := os.environ.get("MITMPROXY_BINARY_EDITOR"): + return m + for editor in "hexedit", "hexer": + if shutil.which(editor): + return editor + if os.name == "nt": + return "notepad" + return "vi" + def spawn_editor(self, data: T) -> T: text = not isinstance(data, bytes) fd, name = tempfile.mkstemp("", "mitmproxy", text=text) with open(fd, "w" if text else "wb") as f: f.write(data) - c = self.get_editor() + + if not text and strutils.is_mostly_bin(cast(bytes, data)): + c = self.get_binary_editor() + else: + c = self.get_editor() + cmd = shlex.split(c) cmd.append(name) with self.uistopped():