diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-10-18 17:18:32 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-10-18 20:13:45 +0200 |
commit | 14eeee3fbc0a839d918149765d2134d05cd14601 (patch) | |
tree | dec1ca7dbc8668120e53e8ea45328246313199d1 /src/hydrilla/proxy/state_impl | |
parent | 76f4b6769d0acaeffbd8e8a003fcbb3e2fbea107 (diff) | |
download | haketilo-hydrilla-14eeee3fbc0a839d918149765d2134d05cd14601.tar.gz haketilo-hydrilla-14eeee3fbc0a839d918149765d2134d05cd14601.zip |
[proxy] upon Haketilo launch automatically open Haketilo landing page in user's default web browser
* The landing page instructs user to configure browser's proxy settings.
* It is now possible to choose the IP address to listen on via command line parameter.
* The browser launching behavior can be switched off via command line parameter.
Diffstat (limited to 'src/hydrilla/proxy/state_impl')
-rw-r--r-- | src/hydrilla/proxy/state_impl/base.py | 30 | ||||
-rw-r--r-- | src/hydrilla/proxy/state_impl/concrete_state.py | 43 |
2 files changed, 64 insertions, 9 deletions
diff --git a/src/hydrilla/proxy/state_impl/base.py b/src/hydrilla/proxy/state_impl/base.py index 357ae88..df3287b 100644 --- a/src/hydrilla/proxy/state_impl/base.py +++ b/src/hydrilla/proxy/state_impl/base.py @@ -34,6 +34,7 @@ subtype. import sqlite3 import threading import secrets +import webbrowser import dataclasses as dc import typing as t @@ -137,6 +138,9 @@ PayloadsData = t.Mapping[st.PayloadRef, st.PayloadData] class HaketiloStateWithFields(st.HaketiloState): """....""" store_dir: Path + _listen_host: str + _listen_port: int + _logger: st.Logger connection: sqlite3.Connection settings: st.HaketiloGlobalSettings current_cursor: t.Optional[sqlite3.Cursor] = None @@ -251,3 +255,29 @@ class HaketiloStateWithFields(st.HaketiloState): dependencies as well as at startup. """ ... + + @property + def listen_host(self) -> str: + if self._listen_host != '0.0.0.0': + return '127.0.0.1' + + return self._listen_host + + @property + def listen_port(self) -> int: + return self._listen_port + + @property + def efective_listen_addr(self) -> str: + effective_host = self._listen_host + if self._listen_host == '0.0.0.0': + effective_host = '127.0.0.1' + + return f'http://{effective_host}:{self._listen_port}' + + def launch_browser(self) -> bool: + return webbrowser.open(self.efective_listen_addr) + + @property + def logger(self) -> st.Logger: + return self._logger diff --git a/src/hydrilla/proxy/state_impl/concrete_state.py b/src/hydrilla/proxy/state_impl/concrete_state.py index d2d6e56..83522cf 100644 --- a/src/hydrilla/proxy/state_impl/concrete_state.py +++ b/src/hydrilla/proxy/state_impl/concrete_state.py @@ -198,12 +198,29 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): def _rebuild_structures(self, cursor: sqlite3.Cursor) -> None: new_policy_tree = base.PolicyTree() - ui_factory = policies.WebUIPolicyFactory(builtin=True) - web_ui_pattern = 'http*://hkt.mitm.it/***' - for parsed_pattern in url_patterns.parse_pattern(web_ui_pattern): + web_ui_main_pattern = 'http*://hkt.mitm.it/***' + web_ui_main_factory = policies.WebUIMainPolicyFactory(builtin=True) + + for parsed_pattern in url_patterns.parse_pattern(web_ui_main_pattern): + new_policy_tree = new_policy_tree.register( + parsed_pattern = parsed_pattern, + item = web_ui_main_factory + ) + + web_ui_landing_pattern = f'{self.efective_listen_addr}/***' + web_ui_landing_factory = policies.WebUILandingPolicyFactory( + builtin = True + ) + + try: + parsed_pattern, = url_patterns.parse_pattern(web_ui_landing_pattern) + except url_patterns.HaketiloURLException: + fmt = _('warn.proxy.failed_to_register_landing_page_at_{}') + self.logger.warn(fmt.format(web_ui_landing_pattern)) + else: new_policy_tree = new_policy_tree.register( - parsed_pattern, - ui_factory + parsed_pattern = parsed_pattern, + item = web_ui_landing_factory ) # Put script blocking/allowing rules in policy tree. @@ -346,7 +363,12 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): self.settings = load_settings(cursor) @staticmethod - def make(store_dir: Path) -> 'ConcreteHaketiloState': + def make( + store_dir: Path, + listen_host: str, + listen_port: int, + logger: st.Logger + ) -> 'ConcreteHaketiloState': store_dir.mkdir(parents=True, exist_ok=True) connection = sqlite3.connect( @@ -360,7 +382,10 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): global_settings = load_settings(connection.cursor()) return ConcreteHaketiloState( - store_dir = store_dir, - connection = connection, - settings = global_settings + store_dir = store_dir, + _logger = logger, + _listen_host = listen_host, + _listen_port = listen_port, + connection = connection, + settings = global_settings ) |