diff options
author | Wojtek Kosior <koszko@koszko.org> | 2022-10-22 11:19:14 +0200 |
---|---|---|
committer | Wojtek Kosior <koszko@koszko.org> | 2022-10-22 13:22:40 +0200 |
commit | 1fc2dbe5a3e867eb5e6c2f759409f1c94146a794 (patch) | |
tree | 73e9b90b26fb30e115ae111bb7592fdb92615cd3 /src/hydrilla/proxy/state_impl | |
parent | fe43bd552aaacd649b0e00afada01d07ad8dae9a (diff) | |
download | haketilo-hydrilla-1fc2dbe5a3e867eb5e6c2f759409f1c94146a794.tar.gz haketilo-hydrilla-1fc2dbe5a3e867eb5e6c2f759409f1c94146a794.zip |
[proxy] make popup script injection configurable throught the web UI
Diffstat (limited to 'src/hydrilla/proxy/state_impl')
-rw-r--r-- | src/hydrilla/proxy/state_impl/concrete_state.py | 114 |
1 files changed, 90 insertions, 24 deletions
diff --git a/src/hydrilla/proxy/state_impl/concrete_state.py b/src/hydrilla/proxy/state_impl/concrete_state.py index 2dd8810..5df7c34 100644 --- a/src/hydrilla/proxy/state_impl/concrete_state.py +++ b/src/hydrilla/proxy/state_impl/concrete_state.py @@ -55,6 +55,22 @@ from . import _operations here = Path(__file__).resolve().parent +def _add_popup_settings_columns(cursor: sqlite3.Cursor) -> None: + for page_type in ('jsallowed', 'jsblocked', 'payloadon'): + cursor.execute( + f''' + ALTER TABLE general ADD COLUMN + default_popup_{page_type}_onkeyboard BOOLEAN NOT NULL DEFAULT TRUE; + ''' + ) + cursor.execute( + f''' + ALTER TABLE general ADD COLUMN + default_popup_{page_type}_style CHAR(1) NOT NULL DEFAULT 'T' + CHECK (default_popup_{page_type}_style IN ('D', 'T')); + ''' + ) + def _prepare_database(connection: sqlite3.Connection) -> None: cursor = connection.cursor() @@ -74,19 +90,43 @@ def _prepare_database(connection: sqlite3.Connection) -> None: if not db_initialized: cursor.executescript((here / 'tables.sql').read_text()) - else: - cursor.execute( - ''' - SELECT - haketilo_version - FROM - general; - ''' - ) - (db_haketilo_version,) = cursor.fetchone() - if db_haketilo_version != '3.0b1': - raise HaketiloException(_('err.proxy.unknown_db_schema')) + cursor.execute('BEGIN TRANSACTION;') + + try: + if db_initialized: + # If db was initialized before we connected to it, we must check + # what its schema version is. + cursor.execute( + ''' + SELECT + haketilo_version + FROM + general; + ''' + ) + + (db_haketilo_version,) = cursor.fetchone() + if db_haketilo_version != '3.0b1': + raise HaketiloException(_('err.proxy.unknown_db_schema')) + + popup_settings_columns_present = False + + cursor.execute("PRAGMA TABLE_INFO('general')") + for __cid, name, __type, __notnull, __dflt_value, __pk \ + in cursor.fetchall(): + if name == 'default_popup_jsallowed_onkeyboard': + popup_settings_columns_present = True + + if not popup_settings_columns_present: + _add_popup_settings_columns(cursor) + + cursor.execute('COMMIT TRANSACTION;') + except: + cursor.execute('ROLLBACK TRANSACTION;') + raise + + cursor.execute('PRAGMA FOREIGN_KEYS;') if cursor.fetchall() == []: @@ -106,17 +146,35 @@ def load_settings(cursor: sqlite3.Cursor) -> st.HaketiloGlobalSettings: repo_refresh_seconds, mapping_use_mode FROM - general + general; ''' ) (default_allow_scripts, advanced_user, repo_refresh_seconds, mapping_use_mode), = cursor.fetchall() - default_popup_settings = st.PopupSettings( - keyboard_trigger = True, - style = st.PopupStyle.TAB - ) + popup_settings_dict = {} + + for page_type in ('jsallowed', 'jsblocked', 'payloadon'): + try: + cursor.execute( + f''' + SELECT + default_popup_{page_type}_onkeyboard, + default_popup_{page_type}_style + FROM + general; + ''' + ) + + (onkeyboard, style), = cursor.fetchall() + except: + onkeyboard, style = True, 'T' + + popup_settings_dict[f'default_popup_{page_type}'] = st.PopupSettings( + keyboard_trigger = onkeyboard, + style = st.PopupStyle(style) + ) return st.HaketiloGlobalSettings( default_allow_scripts = default_allow_scripts, @@ -124,9 +182,7 @@ def load_settings(cursor: sqlite3.Cursor) -> st.HaketiloGlobalSettings: repo_refresh_seconds = repo_refresh_seconds, mapping_use_mode = st.MappingUseMode(mapping_use_mode), - default_popup_jsallowed = default_popup_settings, - default_popup_jsblocked = default_popup_settings, - default_popup_payloadon = default_popup_settings + **popup_settings_dict ) @dc.dataclass @@ -360,10 +416,11 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): def update_settings( self, *, - mapping_use_mode: t.Optional[st.MappingUseMode] = None, - default_allow_scripts: t.Optional[bool] = None, - advanced_user: t.Optional[bool] = None, - repo_refresh_seconds: t.Optional[int] = None + mapping_use_mode: t.Optional[st.MappingUseMode] = None, + default_allow_scripts: t.Optional[bool] = None, + advanced_user: t.Optional[bool] = None, + repo_refresh_seconds: t.Optional[int] = None, + default_popup_settings: t.Mapping[str, st.PopupSettings] = {} ) -> None: with self.cursor(transaction=True) as cursor: def set_opt(col_name: str, val: t.Union[bool, int, str]) -> None: @@ -378,6 +435,15 @@ class ConcreteHaketiloState(base.HaketiloStateWithFields): if repo_refresh_seconds is not None: set_opt('repo_refresh_seconds', repo_refresh_seconds) + for page_type in ('jsallowed', 'jsblocked', 'payloadon'): + popup_settings = default_popup_settings.get(page_type) + if popup_settings is not None: + trigger_col_name = f'default_popup_{page_type}_onkeyboard' + set_opt(trigger_col_name, popup_settings.keyboard_trigger) + + style_col_name = f'default_popup_{page_type}_style' + set_opt(style_col_name, popup_settings.style.value) + self.settings = load_settings(cursor) @staticmethod |