commit ad31bba666a5cb867ffc0c599d7fd459c3167acf Author: Michael Rodin Date: Sun Nov 12 20:26:38 2023 +0100 first commit diff --git a/example.config.py b/example.config.py new file mode 100644 index 0000000..db12371 --- /dev/null +++ b/example.config.py @@ -0,0 +1,8 @@ +TRAEFIK_HOST="traefik.example.com" +TRAEFIK_USER = "administrator" +TRAEFIK_PASS = 'passwd_123' +TRAEFIK_PROXY_TARGET="http://10.0.0.1:80" # IP/Domain with http(s):// and port of the other Traefik instance! + +TRAEFIK_EXCLUDE=("api@internal","dashboard@internal","noop@internal") # List of which routers to ignore +TRAEFIK_PROXY_SERVICE_NAME="private-server-proxy" # Name of the created service (make sure it doesn't overlap with existing services!) +TRAEFIK_CONF_OUT="proxyConfig.yml" \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..c3c5d57 --- /dev/null +++ b/main.py @@ -0,0 +1,36 @@ +import json,yaml +from http.client import HTTPSConnection +from base64 import b64encode +from config import * + +#----------------------------------------------------------------------FUNCTIONS +def basic_auth(username, password): + token = b64encode(f"{username}:{password}".encode('utf-8')).decode("ascii") + return f'Basic {token}' + + + +#----------------------------------------------------------------------GET ALL ROUTERS FROM LOCAL TRAEFIK INSTANCE +# Connect to HTTPS Traefik and get all routers +c = HTTPSConnection(TRAEFIK_HOST) +headers = { 'Authorization' : basic_auth(TRAEFIK_USER, TRAEFIK_PASS) } +c.request('GET', '/api/http/routers', headers=headers) +res = c.getresponse() +# Get the data and convert to dict +jsondata = res.read().decode() +dictdata = json.loads(jsondata) + +#----------------------------------------------------------------------CREATE DICT FOR CONFIG AND POPULATE IT +dictconf = {"http": {"routers": {}}} # create blank config +# Loop through all routers and filter them by exclude list +for i in dictdata: + if i["service"] in TRAEFIK_EXCLUDE: + continue + # Add router to dict + dictconf["http"]["routers"].update({i["service"]: {"rule":i["rule"],"entryPoints":"https","service":TRAEFIK_PROXY_SERVICE_NAME}}) + print(i["service"],i["rule"]) + +# Add the targeted service and write to out file +dictconf["http"].update({"services": {TRAEFIK_PROXY_SERVICE_NAME: {"loadBalancer": {"servers": [{"url":TRAEFIK_PROXY_TARGET}]}}}}) +with open(TRAEFIK_CONF_OUT,"w") as f: + yaml.dump(dictconf, f) \ No newline at end of file