Skip to content

Commit

Permalink
finish FIXME: move open_browser() to addon/browser (mitmproxy#3832)
Browse files Browse the repository at this point in the history
  • Loading branch information
naivekun committed Mar 8, 2020
1 parent 6d3b8c9 commit e01f044
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion mitmproxy/addons/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ def done(self):
self.browser.kill()
self.tdir.cleanup()
self.browser = None
self.tdir = None
self.tdir = None
36 changes: 0 additions & 36 deletions mitmproxy/tools/web/master.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import webbrowser

import tornado.httpserver
import tornado.ioloop
from tornado.platform.asyncio import AsyncIOMainLoop
Expand Down Expand Up @@ -112,38 +110,4 @@ def run(self): # pragma: no cover
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
41 changes: 41 additions & 0 deletions mitmproxy/tools/web/webaddons.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import webbrowser

from mitmproxy import ctx


class WebAddon:
def load(self, loader):
loader.add_option(
Expand All @@ -16,3 +21,39 @@ def load(self, loader):
"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

0 comments on commit e01f044

Please sign in to comment.