diff options
Diffstat (limited to 'src/hydrilla/proxy/policies')
-rw-r--r-- | src/hydrilla/proxy/policies/base.py | 25 | ||||
-rw-r--r-- | src/hydrilla/proxy/policies/misc.py | 10 | ||||
-rw-r--r-- | src/hydrilla/proxy/policies/payload.py | 2 | ||||
-rw-r--r-- | src/hydrilla/proxy/policies/rule.py | 6 |
4 files changed, 30 insertions, 13 deletions
diff --git a/src/hydrilla/proxy/policies/base.py b/src/hydrilla/proxy/policies/base.py index 95021cd..967e2c4 100644 --- a/src/hydrilla/proxy/policies/base.py +++ b/src/hydrilla/proxy/policies/base.py @@ -43,7 +43,7 @@ import jinja2 from immutables import Map -from ...translations import translation as make_translation +from ... import translations from ... import url_patterns from ... import common_jinja_templates from .. import state @@ -62,14 +62,9 @@ _jinja_info_env = jinja2.Environment( lstrip_blocks = True, extensions = ['jinja2.ext.i18n', 'jinja2.ext.do'] ) -_jinja_info_env.install_gettext_translations(make_translation()) # type: ignore _jinja_info_env.globals['url_patterns'] = url_patterns _jinja_info_lock = threading.Lock() -def get_info_template(template_file_name: str) -> jinja2.Template: - with _jinja_info_lock: - return _jinja_info_env.get_template(template_file_name) - _jinja_script_loader = jinja2.PackageLoader( __package__, @@ -164,6 +159,20 @@ class Policy(ABC): return (self.current_popup_settings.popup_enabled and http_messages.is_likely_a_page(request_info, response_info)) + def _get_info_template(self, template_file_name: str) -> jinja2.Template: + with _jinja_info_lock: + chosen_locale = self.haketilo_settings.locale + if chosen_locale not in translations.supported_locales: + chosen_locale = None + + if chosen_locale is None: + chosen_locale = translations.default_locale + + trans = translations.translation(chosen_locale) + _jinja_info_env.install_gettext_translations(trans) # type: ignore + return _jinja_info_env.get_template(template_file_name) + + def _csp_to_clear(self, http_info: http_messages.FullHTTPInfo) \ -> t.Union[t.Sequence[str], t.Literal['all']]: return () @@ -215,7 +224,9 @@ class Policy(ABC): popup_page = self.make_info_page(http_info) if popup_page is None: - template = get_info_template('special_page_info.html.jinja') + template = self._get_info_template( + 'special_page_info.html.jinja' + ) popup_page = template.render( url = http_info.request_info.url.orig_url ) diff --git a/src/hydrilla/proxy/policies/misc.py b/src/hydrilla/proxy/policies/misc.py index 0ff4596..e789b29 100644 --- a/src/hydrilla/proxy/policies/misc.py +++ b/src/hydrilla/proxy/policies/misc.py @@ -47,7 +47,9 @@ class FallbackAllowPolicy(AllowPolicy): def make_info_page(self, http_info: http_messages.FullHTTPInfo) \ -> t.Optional[str]: - template = base.get_info_template('js_fallback_allowed_info.html.jinja') + template = self._get_info_template( + 'js_fallback_allowed_info.html.jinja' + ) return template.render(url=http_info.request_info.url.orig_url) @@ -56,7 +58,9 @@ class FallbackBlockPolicy(BlockPolicy): def make_info_page(self, http_info: http_messages.FullHTTPInfo) \ -> t.Optional[str]: - template = base.get_info_template('js_fallback_blocked_info.html.jinja') + template = self._get_info_template( + 'js_fallback_blocked_info.html.jinja' + ) return template.render(url=http_info.request_info.url.orig_url) @@ -71,7 +75,7 @@ class ErrorBlockPolicy(BlockPolicy): def make_info_page(self, http_info: http_messages.FullHTTPInfo) \ -> t.Optional[str]: - template = base.get_info_template('js_error_blocked_info.html.jinja') + template = self._get_info_template('js_error_blocked_info.html.jinja') return template.render( url = http_info.request_info.url.orig_url, settings = self.haketilo_settings, diff --git a/src/hydrilla/proxy/policies/payload.py b/src/hydrilla/proxy/policies/payload.py index 55851cc..3660eac 100644 --- a/src/hydrilla/proxy/policies/payload.py +++ b/src/hydrilla/proxy/policies/payload.py @@ -175,7 +175,7 @@ class PayloadInjectPolicy(PayloadAwarePolicy): def make_info_page(self, http_info: http_messages.FullHTTPInfo) \ -> t.Optional[str]: - return base.get_info_template('payload_info.html.jinja').render( + return self._get_info_template('payload_info.html.jinja').render( url = http_info.request_info.url.orig_url, payload_data = self.payload_data ) diff --git a/src/hydrilla/proxy/policies/rule.py b/src/hydrilla/proxy/policies/rule.py index 1f39295..e318a7f 100644 --- a/src/hydrilla/proxy/policies/rule.py +++ b/src/hydrilla/proxy/policies/rule.py @@ -69,7 +69,8 @@ class RuleAllowPolicy(AllowPolicy): def make_info_page(self, http_info: http_messages.FullHTTPInfo) \ -> t.Optional[str]: - return base.get_info_template('js_rule_allowed_info.html.jinja').render( + template = self._get_info_template('js_rule_allowed_info.html.jinja') + return template.render( url = http_info.request_info.url.orig_url, pattern = self.pattern.orig_url ) @@ -81,7 +82,8 @@ class RuleBlockPolicy(BlockPolicy): def make_info_page(self, http_info: http_messages.FullHTTPInfo) \ -> t.Optional[str]: - return base.get_info_template('js_rule_blocked_info.html.jinja').render( + template = self._get_info_template('js_rule_blocked_info.html.jinja') + return template.render( url = http_info.request_info.url.orig_url, pattern = self.pattern.orig_url ) |