From bb56690144f7fe7a1aaede92323780eea2747e62 Mon Sep 17 00:00:00 2001 From: Herklos Date: Wed, 11 Oct 2023 15:01:09 +0200 Subject: [PATCH] [Webhook] Add custom domain support --- Services/Services_bases/webhook_service/webhook.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Services/Services_bases/webhook_service/webhook.py b/Services/Services_bases/webhook_service/webhook.py index 5cc50193e..6b7b11af2 100644 --- a/Services/Services_bases/webhook_service/webhook.py +++ b/Services/Services_bases/webhook_service/webhook.py @@ -42,6 +42,7 @@ def get_fields_description(self): return { services_constants.CONFIG_ENABLE_NGROK: "Use Ngrok", services_constants.CONFIG_NGROK_TOKEN: "The ngrok token used to expose the webhook to the internet.", + services_constants.CONFIG_NGROK_DOMAIN: "[Optional] The ngrok subdomain.", services_constants.CONFIG_WEBHOOK_SERVER_IP: "WebHook bind IP: used for webhook when ngrok is not enabled.", services_constants.CONFIG_WEBHOOK_SERVER_PORT: "WebHook port: used for webhook when ngrok is not enabled." } @@ -52,6 +53,7 @@ def get_default_value(self): return { services_constants.CONFIG_ENABLE_NGROK: True, services_constants.CONFIG_NGROK_TOKEN: "", + services_constants.CONFIG_NGROK_DOMAIN: "", services_constants.CONFIG_WEBHOOK_SERVER_IP: services_constants.DEFAULT_WEBHOOK_SERVER_IP, services_constants.CONFIG_WEBHOOK_SERVER_PORT: services_constants.DEFAULT_WEBHOOK_SERVER_PORT } @@ -62,6 +64,7 @@ def __init__(self): self.ngrok_tunnel = None self.webhook_public_url = "" self.ngrok_enabled = True + self.ngrok_domain = None self.service_feed_webhooks = {} self.service_feed_auth_callbacks = {} @@ -128,14 +131,14 @@ def is_subscribed(self, feed_name): return feed_name in self.service_feed_webhooks @staticmethod - def connect(port, protocol="http") -> ngrok.NgrokTunnel: + def connect(port, protocol="http", domain=None)-> ngrok.NgrokTunnel: """ Create a new ngrok tunnel :param port: the tunnel local port :param protocol: the protocol to use :return: the ngrok url """ - return ngrok.connect(port, protocol) + return ngrok.connect(port, protocol, domain=domain) def subscribe_feed(self, service_feed_name, service_feed_callback, auth_callback) -> None: """ @@ -203,6 +206,8 @@ async def prepare(self) -> None: ngrok.set_auth_token( self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_WEBHOOK][ services_constants.CONFIG_NGROK_TOKEN]) + self.ngrok_domain = self.config[services_constants.CONFIG_CATEGORY_SERVICES][services_constants.CONFIG_WEBHOOK].get( + services_constants.CONFIG_NGROK_DOMAIN, None) try: self.webhook_host = os.getenv(services_constants.ENV_WEBHOOK_ADDRESS, self.config[services_constants.CONFIG_CATEGORY_SERVICES] @@ -224,7 +229,7 @@ def _start_server(self): self._register_webhook_routes(self.webhook_app) self.webhook_public_url = f"http://{self.webhook_host}:{self.webhook_port}/webhook" if self.ngrok_enabled: - self.ngrok_tunnel = self.connect(self.webhook_port, protocol="http") + self.ngrok_tunnel = self.connect(self.webhook_port, protocol="http", domain=self.ngrok_domain) self.webhook_public_url = f"{self.ngrok_tunnel.public_url}/webhook" if self.webhook_server: self.connected = True