aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy
diff options
context:
space:
mode:
authornaivekun <qq2694985673@outlook.com>2020-03-09 04:58:04 +0800
committerGitHub <noreply@github.com>2020-03-08 21:58:04 +0100
commite01f044c33aba51d9e5b6053e759b47594ad0855 (patch)
tree4101d60e115676d4c1a50bc888d91a99547b4764 /mitmproxy
parent6d3b8c9716a274fdcc72c4dc9a841f2e8fa4d1e9 (diff)
downloadmitmproxy-e01f044c33aba51d9e5b6053e759b47594ad0855.tar.gz
mitmproxy-e01f044c33aba51d9e5b6053e759b47594ad0855.tar.bz2
mitmproxy-e01f044c33aba51d9e5b6053e759b47594ad0855.zip
finish FIXME: move open_browser() to addon/browser (#3832)
Diffstat (limited to 'mitmproxy')
-rw-r--r--mitmproxy/addons/browser.py2
-rw-r--r--mitmproxy/tools/web/master.py36
-rw-r--r--mitmproxy/tools/web/webaddons.py41
3 files changed, 42 insertions, 37 deletions
diff --git a/mitmproxy/addons/browser.py b/mitmproxy/addons/browser.py
index e74b50db..5735698b 100644
--- a/mitmproxy/addons/browser.py
+++ b/mitmproxy/addons/browser.py
@@ -73,4 +73,4 @@ class Browser:
self.browser.kill()
self.tdir.cleanup()
self.browser = None
- self.tdir = None
+ self.tdir = None \ No newline at end of file
diff --git a/mitmproxy/tools/web/master.py b/mitmproxy/tools/web/master.py
index e2c60d94..17845106 100644
--- a/mitmproxy/tools/web/master.py
+++ b/mitmproxy/tools/web/master.py
@@ -1,5 +1,3 @@
-import webbrowser
-
import tornado.httpserver
import tornado.ioloop
from tornado.platform.asyncio import AsyncIOMainLoop
@@ -112,38 +110,4 @@ class WebMaster(master.Master):
self.log.info(
"Web server listening at {}".format(web_url),
)
- # FIXME: This should be in an addon hooked to the "running" event, not in master
- if self.options.web_open_browser:
- success = open_browser(web_url)
- if not success:
- self.log.info(
- "No web browser found. Please open a browser and point it to {}".format(web_url),
- )
self.run_loop(iol.start)
-
-
-def open_browser(url: str) -> bool:
- """
- Open a URL in a browser window.
- In contrast to webbrowser.open, we limit the list of suitable browsers.
- This gracefully degrades to a no-op on headless servers, where webbrowser.open
- would otherwise open lynx.
-
- Returns:
- True, if a browser has been opened
- False, if no suitable browser has been found.
- """
- browsers = (
- "windows-default", "macosx",
- "google-chrome", "chrome", "chromium", "chromium-browser",
- "firefox", "opera", "safari",
- )
- for browser in browsers:
- try:
- b = webbrowser.get(browser)
- except webbrowser.Error:
- pass
- else:
- b.open(url)
- return True
- return False
diff --git a/mitmproxy/tools/web/webaddons.py b/mitmproxy/tools/web/webaddons.py
index 6b52188c..ba3bdead 100644
--- a/mitmproxy/tools/web/webaddons.py
+++ b/mitmproxy/tools/web/webaddons.py
@@ -1,3 +1,8 @@
+import webbrowser
+
+from mitmproxy import ctx
+
+
class WebAddon:
def load(self, loader):
loader.add_option(
@@ -16,3 +21,39 @@ class WebAddon:
"web_iface", str, "127.0.0.1",
"Web UI interface."
)
+
+ def running(self):
+ if hasattr(ctx.options, "web_open_browser") and ctx.options.web_open_browser:
+ web_url = "http://{}:{}/".format(ctx.options.web_iface, ctx.options.web_port)
+ success = open_browser(web_url)
+ if not success:
+ ctx.log.info(
+ "No web browser found. Please open a browser and point it to {}".format(web_url),
+ )
+
+
+def open_browser(url: str) -> bool:
+ """
+ Open a URL in a browser window.
+ In contrast to webbrowser.open, we limit the list of suitable browsers.
+ This gracefully degrades to a no-op on headless servers, where webbrowser.open
+ would otherwise open lynx.
+
+ Returns:
+ True, if a browser has been opened
+ False, if no suitable browser has been found.
+ """
+ browsers = (
+ "windows-default", "macosx",
+ "google-chrome", "chrome", "chromium", "chromium-browser",
+ "firefox", "opera", "safari",
+ )
+ for browser in browsers:
+ try:
+ b = webbrowser.get(browser)
+ except webbrowser.Error:
+ pass
+ else:
+ b.open(url)
+ return True
+ return False