diff --git a/mitmproxy/addons/__init__.py b/mitmproxy/addons/__init__.py index 165106403..68030cb95 100644 --- a/mitmproxy/addons/__init__.py +++ b/mitmproxy/addons/__init__.py @@ -30,7 +30,6 @@ def default_addons(): onboarding.Onboarding(), proxyauth.ProxyAuth(), replace.Replace(), - replace.ReplaceFile(), script.ScriptLoader(), serverplayback.ServerPlayback(), setheaders.SetHeaders(), diff --git a/mitmproxy/addons/replace.py b/mitmproxy/addons/replace.py index 0d0c3aa5b..a653bd701 100644 --- a/mitmproxy/addons/replace.py +++ b/mitmproxy/addons/replace.py @@ -1,3 +1,4 @@ +import os import re from mitmproxy import exceptions @@ -58,7 +59,6 @@ class _ReplaceBase: lst = [] for rep in getattr(options, self.optionName): fpatt, rex, s = parse_hook(rep) - flt = flowfilter.parse(fpatt) if not flt: raise exceptions.OptionsError( @@ -94,16 +94,11 @@ class Replace(_ReplaceBase): optionName = "replacements" def replace(self, obj, rex, s): + if s.startswith("@"): + s = s.replace("@", "") + try: + s = open(os.path.expanduser(s), "rb").read() + except IOError as e: + ctx.log.warn("Could not read replacement file: %s" % s) + return obj.replace(rex, s, flags=re.DOTALL) - - -class ReplaceFile(_ReplaceBase): - optionName = "replacement_files" - - def replace(self, obj, rex, s): - try: - v = open(s, "rb").read() - except IOError as e: - ctx.log.warn("Could not read replacement file: %s" % s) - return - obj.replace(rex, v, flags=re.DOTALL) diff --git a/mitmproxy/options.py b/mitmproxy/options.py index 6dd8616be..462752c43 100644 --- a/mitmproxy/options.py +++ b/mitmproxy/options.py @@ -120,13 +120,7 @@ class Options(optmanager.OptManager): the separator can be any character. """ ) - self.add_option( - "replacement_files", Sequence[str], [], - """ - Replacement pattern, where the replacement clause is a path to a - file. - """ - ) + self.add_option( "server_replay_use_headers", Sequence[str], [], "Request headers to be considered during replay." diff --git a/mitmproxy/tools/cmdline.py b/mitmproxy/tools/cmdline.py index aaefd10a1..797485693 100644 --- a/mitmproxy/tools/cmdline.py +++ b/mitmproxy/tools/cmdline.py @@ -93,7 +93,6 @@ def common_options(parser, opts): # Replacements group = parser.add_argument_group("Replacements") opts.make_parser(group, "replacements", metavar="PATTERN", short="R") - opts.make_parser(group, "replacement_files", metavar="PATTERN") # Set headers group = parser.add_argument_group("Set Headers") diff --git a/mitmproxy/tools/console/grideditor/editors.py b/mitmproxy/tools/console/grideditor/editors.py index 2d24cf862..503e9768d 100644 --- a/mitmproxy/tools/console/grideditor/editors.py +++ b/mitmproxy/tools/console/grideditor/editors.py @@ -1,5 +1,8 @@ +import os import re + import urwid + from mitmproxy import exceptions from mitmproxy import flowfilter from mitmproxy.addons import script @@ -87,6 +90,9 @@ class ReplaceEditor(base.GridEditor): re.compile(val) except re.error: return "Invalid regular expression." + elif col == 2: + if val.startswith("@") and not os.path.isfile(os.path.expanduser(val.replace("@", ""))): + return "Invalid file path" return False