first commit
This commit is contained in:
commit
ad31bba666
8
example.config.py
Normal file
8
example.config.py
Normal file
|
@ -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"
|
36
main.py
Normal file
36
main.py
Normal file
|
@ -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)
|
Loading…
Reference in a new issue