aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/policies
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2022-09-09 11:44:59 +0200
committerWojtek Kosior <koszko@koszko.org>2022-09-28 14:03:18 +0200
commitbeb163cfd15b93bf664b6a0590e114d4432e3ef9 (patch)
tree4a98def490dabb5f4df927a7a79504eb2ab66c9c /src/hydrilla/proxy/policies
parent45e5cf8dc3ca936e2db8e7e45689d0a3331aad43 (diff)
downloadhaketilo-hydrilla-beb163cfd15b93bf664b6a0590e114d4432e3ef9.tar.gz
haketilo-hydrilla-beb163cfd15b93bf664b6a0590e114d4432e3ef9.zip
[proxy] allow prompting the user when a package suitable for current site is found; add proxy's options page
This commit also causes uncached responses to be forced on all HTTP request to external servers. This is needed to make sure that changes Haketilo makes to HTTP response headers are always picked up by the browser. The drawback is that this increases network traffic causing a performance hit. We might optimize this in the future.
Diffstat (limited to 'src/hydrilla/proxy/policies')
-rw-r--r--src/hydrilla/proxy/policies/base.py15
-rw-r--r--src/hydrilla/proxy/policies/payload.py52
2 files changed, 45 insertions, 22 deletions
diff --git a/src/hydrilla/proxy/policies/base.py b/src/hydrilla/proxy/policies/base.py
index bb95d29..8ffc45e 100644
--- a/src/hydrilla/proxy/policies/base.py
+++ b/src/hydrilla/proxy/policies/base.py
@@ -58,22 +58,21 @@ class Policy(ABC):
"""...."""
process_request: t.ClassVar[bool] = False
process_response: t.ClassVar[bool] = False
+ anticache: t.ClassVar[bool] = True
priority: t.ClassVar[PolicyPriority]
- @property
- def anticache(self) -> bool:
- return self.process_request or self.process_response
-
def consume_request(self, request_info: http_messages.RequestInfo) \
-> t.Optional[ProducedMessage]:
- """...."""
- return None
+ raise NotImplementedError(
+ 'This kind of policy does not consume requests.'
+ )
def consume_response(self, response_info: http_messages.ResponseInfo) \
-> t.Optional[http_messages.ProducedResponse]:
- """...."""
- return None
+ raise NotImplementedError(
+ 'This kind of policy does not consume responses.'
+ )
# mypy needs to be corrected:
diff --git a/src/hydrilla/proxy/policies/payload.py b/src/hydrilla/proxy/policies/payload.py
index a063c7c..1069c41 100644
--- a/src/hydrilla/proxy/policies/payload.py
+++ b/src/hydrilla/proxy/policies/payload.py
@@ -60,6 +60,20 @@ class PayloadAwarePolicy(base.Policy):
return f'{request_url.url_without_path}/{"/".join(base_path_segments)}/'
+ def _payload_details_to_signed_query_string(
+ self,
+ _salt: str,
+ **extra_keys: str
+ ) -> str:
+ params: t.Mapping[str, str] = {
+ 'payload_id': self.payload_data.ref.id,
+ **extra_keys
+ }
+
+ serializer = URLSafeSerializer(self.payload_data.global_secret, _salt)
+
+ return urlencode({'details': serializer.dumps(params)})
+
@dc.dataclass(frozen=True) # type: ignore[misc]
class PayloadAwarePolicyFactory(base.PolicyFactory):
@@ -283,20 +297,18 @@ class AutoPayloadInjectPolicy(PayloadInjectPolicy):
return super().consume_response(response_info)
except (state.RepoCommunicationError, state.FileInstallationError,
_PayloadHasProblemsError) as ex:
- params = {
- 'next_url': response_info.url.orig_url,
- 'payload_id': self.payload_data.ref.id
+ extra_params: dict[str, str] = {
+ 'next_url': response_info.url.orig_url
}
-
if isinstance(ex, state.FileInstallationError):
- params['repo_id'] = ex.repo_id
- params['file_sha256'] = ex.sha256
+ extra_params['repo_id'] = ex.repo_id
+ extra_params['file_sha256'] = ex.sha256
- serializer = URLSafeSerializer(
- self.payload_data.global_secret,
- salt = 'auto_install_error'
+ query = self._payload_details_to_signed_query_string(
+ _salt = 'auto_install_error',
+ **extra_params
)
- query = urlencode({'details': serializer.dumps(params)})
+
redirect_url = 'https://hkt.mitm.it/auto_install_error?' + query
msg = 'Error occured when installing payload. Redirecting.'
@@ -310,13 +322,25 @@ class AutoPayloadInjectPolicy(PayloadInjectPolicy):
@dc.dataclass(frozen=True)
class PayloadSuggestPolicy(PayloadAwarePolicy):
"""...."""
+ process_request: t.ClassVar[bool] = True
+
priority: t.ClassVar[base.PolicyPriority] = base.PolicyPriority._ONE
- def make_response(self, request_info: http_messages.RequestInfo) \
+ def consume_request(self, request_info: http_messages.RequestInfo) \
-> http_messages.ProducedResponse:
- """...."""
- # TODO: implement
- return http_messages.ProducedResponse(200, ((b'a', b'b'),), b'')
+ query = self._payload_details_to_signed_query_string(
+ _salt = 'package_suggestion',
+ next_url = request_info.url.orig_url
+ )
+
+ redirect_url = 'https://hkt.mitm.it/package_suggestion?' + query
+ msg = 'A package was found that could be used on this site. Redirecting.'
+
+ return http_messages.ProducedResponse(
+ status_code = 303,
+ headers = [(b'Location', redirect_url.encode())],
+ body = msg.encode()
+ )
@dc.dataclass(frozen=True, unsafe_hash=True)