Repository: mitmproxy/mitmproxy

Issue:
Mitmdump option error
#### Problem Description
When passing random arguments to mitmdump it doesn't show any error.

Steps to reproduce the behavior:
1. Type mitmdump abc 

It doesn't show any flow after that just clientconnect and clientdisconnect on requests but no error about wrong argument. Mitmproxy show this kind of error. I am not sure this is supposed to behave like this or this is a bug.

#### System Information
Paste the output of "mitmproxy --version" here.
Mitmproxy: 6.0.0.dev (+116, commit 7791410)
Python:    3.7.5
OpenSSL:   OpenSSL 1.1.0j  20 Nov 2018
Platform:  Linux-5.3.0-kali2-amd64-x86_64-with-debian-kali-rolling



Discussion:
They don't have the same signature. `mitmdump` has `[filter]`. I assume that's what confuses you (I didn't know either, just noticed it). Request sth. like `www.example.com/abc` and you'll see it because of the /abc

```
$ mitmproxy --help
usage: mitmproxy [options]
```

```
$ mitmdump --help
usage: mitmdump [options] [filter]

positional arguments:
  filter_args           Filter expression, equivalent to setting both the
                        view_filter and save_stream_filter options.
```
>  I assume that's what confuses you (I didn't know either, just noticed it). Request sth. like `www.example.com/abc` and you'll see it because of the /abc
> 
Well you are right, this confuses me and thanks for clearing this up,I think for spaces filter is apply to url as request to www.example.com/abc is shown on
`mitmdump abc`
And for more spaces i guess it check for content after slashes as in 
`mitmdump example abc mod`
request to www.example.com/abc/mod is shown.
I think a example of this should be added to docs for better understanding.
It is a filter expression, see https://docs.mitmproxy.org/stable/concepts-filters/

> Strings with no operators are matched against the request URL.

What I don't understand is why `mitmdump "foo bar"` will match "/foo/bar". Maybe that's something I don't know about Python-style regex @mhils ? Shouldn't the space be taken literal?

I think what can be done better is that if `[filter]` is provided then mitmdump could show the user what `view_filter` this results in. At least in `verbose` mode.

>  I think a example of this should be added to docs for better understanding.

I think `--help` should contain some examples in general

Do you have any suggestions what exactly you would change to make things more clear? What what you add to the docs?
> Strings with no operators are matched against the request URL.
>
   I think i miss that line.
> I think --help should contain some examples in general
>
Yeah it can be applied instead of docs.
For the docs i think we can add some example in https://docs.mitmproxy.org/stable/tools-mitmdump/ about url filtering with or without "~u" and if multiple spaces is legit then also add some more info in https://docs.mitmproxy.org/stable/concepts-filters/


Related: https://github.com/mitmproxy/mitmproxy/issues/3142

I think there's room for improvement in multiple places. Thank you for bringing it up.
Yeah, let's see @mhils  thought about that.
> What I don't understand is why mitmdump "foo bar" will match "/foo/bar". Maybe that's something I don't know about Python-style regex @mhils ? Shouldn't the space be taken literal?

mitmweb actually has the best interface here which explains it:
![image](https://user-images.githubusercontent.com/1019198/78750493-a339e580-7970-11ea-934e-454c36760b54.png)

The explanation is generated by the JS reimplementation in [web/src/js/filt/filt.peg](https://github.com/mitmproxy/mitmproxy/blob/master/web/src/js/filt/filt.peg)). Would be nice if we had the same in the console somewhere.

FWIW, the behavior is also mentioned in the docs at https://docs.mitmproxy.org/stable/concepts-filters/:

-  Strings with no operators are matched against the request URL.
-  The default binary operator is &.

That explanation is obviously super cryptic for everyone who doesn't know filters. 😄 

> For the docs i think we can add some example in https://docs.mitmproxy.org/stable/tools-mitmdump/ about url filtering with or without "~u" and if multiple spaces is legit then also add some more info in https://docs.mitmproxy.org/stable/concepts-filters/

I feel that it might be more intuitive to just make it a regular option (like --filter) instead of parsing argv. What do you two think?
>I feel that it might be more intuitive to just make it a regular option (like --filter) instead of parsing argv. What do you two think?
>
You mean for url filter add an extra option like
`mitmdump [options] --urlfilter [expression]`
Yeah it world be great and as @Prinzhorn suggested we can add some more info in `--help` 
about this.
@mhils @Prinzhorn any update on this.
Looking at the implementation the argv handling does a bit more than what I initially thought:

https://github.com/mitmproxy/mitmproxy/blob/bfb8da4b1c5f1422b4a9031d1e3e77d40552208c/mitmproxy/tools/_main.py#L154-L162

Maybe we just add a `f"Only processing flows that match {v!r}"` message to mitmdump and be done with it. If someone feels like it we can move the mitmweb filter explainer to Python.

Resolving patch:
diff --git a/mitmproxy/tools/_main.py b/mitmproxy/tools/_main.py
--- a/mitmproxy/tools/_main.py
+++ b/mitmproxy/tools/_main.py
@@ -110,6 +110,8 @@ def run(
             master.commands.dump()
             sys.exit(0)
         if extra:
+            if(args.filter_args):
+                master.log.info(f"Only processing flows that match \"{' & '.join(args.filter_args)}\"")
             opts.update(**extra(args))
 
         loop = asyncio.get_event_loop()


Test patch: