Repository: mitmproxy/mitmproxy

Issue:
libGL error when starting latest version of mitmweb 8.1.1 on Debian 
#### Problem Description
I was using old version of mitmproxy 6.0.2 that I got installed from the debian unstable repository and it works just fine. then today I decided to download the latest version of mitmproxy 8.1.1 and I got the below errors immediately after I type in `./mitmweb`

```
Web server listening at http://127.0.0.1:8081/
Opening in existing browser session.
Proxy server listening at *:8080
libGL error: MESA-LOADER: failed to open crocus: /usr/lib/dri/crocus_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
libGL error: failed to load driver: crocus
libGL error: MESA-LOADER: failed to open crocus: /usr/lib/dri/crocus_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
libGL error: failed to load driver: crocus
libGL error: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
libGL error: failed to load driver: swrast
[5508:5508:0100/000000.622195:ERROR:angle_platform_impl.cc(43)] Display.cpp:992 (initialize): ANGLE Display::initialize error 12289: Could not create a backing OpenGL context.
[5508:5508:0100/000000.622454:ERROR:gl_surface_egl.cc(831)] EGL Driver message (Critical) eglInitialize: Could not create a backing OpenGL context.
[5508:5508:0100/000000.622599:ERROR:gl_surface_egl.cc(1353)] eglInitialize OpenGL failed with error EGL_NOT_INITIALIZED, trying next display type
[5508:5508:0100/000000.625277:ERROR:angle_platform_impl.cc(43)] Display.cpp:992 (initialize): ANGLE Display::initialize error 12289: Could not create a backing OpenGL context.
[5508:5508:0100/000000.625508:ERROR:gl_surface_egl.cc(831)] EGL Driver message (Critical) eglInitialize: Could not create a backing OpenGL context.
[5508:5508:0100/000000.625555:ERROR:gl_surface_egl.cc(1353)] eglInitialize OpenGLES failed with error EGL_NOT_INITIALIZED
[5508:5508:0100/000000.625654:ERROR:gl_ozone_egl.cc(23)] GLSurfaceEGL::InitializeOneOff failed.
```
And the URL at http://127.0.0.1:8081 loads just a blank page.

Note that I checked, and I have `libgl1-mesa-dri` package already installed.

#### Steps to reproduce the behavior:
1. download latest version of mitmproxy 8.1.1
2. open the terminal and type in `./mitmweb`

#### System Information
Paste the output of "./mitmproxy --version" 
```
Mitmproxy: 8.1.1 binary
Python:    3.10.5
OpenSSL:   OpenSSL 3.0.3 3 May 2022
Platform:  Linux-5.18.0-3-amd64-x86_64-with-glibc2.34
```

I will include the output of mitmproxy of version 6.0.2 that I have installed on the same system as I noticed that Python and OpenSSL versions are different:
```
Mitmproxy: 6.0.2
Python:    3.10.6
OpenSSL:   OpenSSL 3.0.5 5 Jul 2022
Platform:  Linux-5.18.0-3-amd64-x86_64-with-glibc2.34
```



Discussion:
These messages come from Chromium (or whatever browser mitmweb launched) and should be irrelevant. Try `mitmweb --no-web-open-browser` and then manually open http://127.0.0.1:8081/. Does it still show a blank page?

For me what's always happening is that `mitmweb` will launch a browser and the page will never load. If I open the page in a second browser it also doesn't load unless I close the original one. Then it loads. I've always just lived with it, but if you're seeing the same then we should improve that.
`mitmweb` loads correctly when I do this.
Thanks for confirming!

I see two things to do here:

1. Fix whatever is causing the initial page to not load (maybe we shouldn't open the link before tornado is ready to accept connections)
2. Silence the stdout of whatever browser we're launching
The relevant code is here: https://github.com/mitmproxy/mitmproxy/blob/6494e97e781a03bc4df99fdde5ab8d6f3c5e7e9c/mitmproxy/tools/web/webaddons.py#L30

Seems like the Linux install does not ship with xdg-open or something like that?
It seems like `b.open(url)` is blocking? Tornado will only respond to request when I exit the browser process.

https://stackoverflow.com/questions/1149233/how-to-resume-program-or-exit-after-opening-webbrowser

But this seems unrelated to the libGL logs, which I'm only seeing when the browser is launched via mitmweb. This looks funny

```
(search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
```

Or is `\$${ORIGIN}` correct?

Anyway, the browser still works despite these warning. But it blocks mitmweb.
I've managed to make the latest version work, I'm not sure why, but it wasn't working with Debian Sid, I reinstalled Debian Stable and now it's working as expected.
I thought I would add that _if_ my browser is already open, and I run `mitmweb`, it will open in a new tab as expected. This error only occurs for me when a browser is not running.
This sounds like a bug in Python's webbrowser module to me when there's no `x-www-browser`, `gnome-open`, or `xdg-open` binary on the system and we fall back to Chrome. See the code snippet I linked above.
I have both `x-www-browser` and `xdg-open`.

Check out what I get in the REPL

```
>>> webbrowser.get('x-www-browser %s')
<webbrowser.GenericBrowser object at 0x7fcce4da0160>
>>> webbrowser.get('x-www-browser')
<webbrowser.BackgroundBrowser object at 0x7fcce4ed3fa0>
```

I assume anything other than `BackgroundBrowser` is blocking? Why the `%s`? Why even have the array when

> If using is None, return a controller for a default browser appropriate to the caller’s environment.
> Why even have the array when

The default list includes stuff like `lynx`, which makes for a usability nightmare on server OSes. `x-www-browser` should probably not have the `%s`. If you can test this on your system, would you mind sending a PR?

(`%s` is needed for executables not natively supported by the webbrowser module)

Resolving patch:
diff --git a/mitmproxy/tools/web/webaddons.py b/mitmproxy/tools/web/webaddons.py
--- a/mitmproxy/tools/web/webaddons.py
+++ b/mitmproxy/tools/web/webaddons.py
@@ -44,7 +44,7 @@ def open_browser(url: str) -> bool:
         "macosx",
         "wslview %s",
         "gio",
-        "x-www-browser %s",
+        "x-www-browser",
         "gnome-open %s",
         "xdg-open",
         "google-chrome",


Test patch: