From 5830244af020139000c6225858297ee74f5b50cc Mon Sep 17 00:00:00 2001 From: "Arthur K." Date: Fri, 30 May 2025 13:33:32 +0300 Subject: [PATCH] chore: always assume `TRAEFIK_INSTANCE` points to host.docker.internal --- app.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index b1da383..9dc458c 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import base64 import os import re import requests +import urllib3 from concurrent.futures import Future, ThreadPoolExecutor, wait as gather_futures import fabric @@ -14,6 +15,8 @@ TRAEFIK_HOST = os.getenv('TRAEFIK_HOST') or '' INTERNAL_DESTS = (os.getenv('INTERNAL_DESTS') or '').split(',') or [] EXTERNAL_DESTS = (os.getenv('EXTERNAL_DESTS') or '').split(',') or [] PRIVATE_KEY = os.getenv('PRIVATE_KEY') or '' +HOST = os.getenv('HOST') or '0.0.0.0' +PORT = os.getenv('PORT') or '80' assert NODE_NAME != '_traefik' assert TRAEFIK_INSTANCE @@ -49,10 +52,35 @@ class Observer: @classmethod def _init(cls): try: - data = requests.get(TRAEFIK_INSTANCE + '/rawdata').json() - except Exception: ... - else: - cls.update(data) + with urllib3.HTTPSConnectionPool( + 'host.docker.internal', + server_hostname=TRAEFIK_INSTANCE + ) as pool: + runtime_conf = pool.request( + 'GET', + '/api/rawdata', + headers={'Host': TRAEFIK_INSTANCE}, + assert_same_host=False + ).json() + except Exception: + return + dynamic_conf = { + 'http': { + 'middlewares': runtime_conf.get('middlewares') or {}, + 'routers': runtime_conf.get('routers') or {}, + 'services': runtime_conf.get('services') or {}, + }, + 'tcp': { + 'middlewares': runtime_conf.get('tcpMiddlewares') or {}, + 'routers': runtime_conf.get('tcpRouters') or {}, + 'services': runtime_conf.get('tcpServices') or {}, + }, + 'udp': { + 'routers': runtime_conf.get('udpRouters') or {}, + 'services': runtime_conf.get('udpServices') or {}, + } + } + cls.update(dynamic_conf) print("Initialized!") @@ -97,7 +125,7 @@ class Observer: @classmethod def parse_raw_data(cls, data: dict): - http = (data.get('http') or {}) | (data.get('tcp') or {}) | (data.get('udp') or {}) or data + http = data.get('http') or {} flt = lambda ep: set([x.group(1) for x in [re.match(PATTERN, x['rule']) for x in http['routers'].values() if ep in x['entryPoints']] if x]) internal = flt(INTERNAL_ENTRYPOINT) @@ -120,6 +148,6 @@ def callback(): if __name__ == "__main__": try: - app.run(host='0.0.0.0', port=80) + app.run(host=HOST, port=int(PORT)) finally: Observer._close()