API to extract current served hostnames from traefik for split horizon dns

Hi!

I have multiple hosts with docker compose stacks. Serving each of the web entry points via a host dedicated traefik. All stand alone, no swarm.

I also have a central adguardhome instance on my openwrt router where I provision dns rewrites for split horizon dns.

I would like to extract host names from each traefik instance via api and add those to my agh instance with the ip that listens for them.

I currently poll the /api/certificates on each traefik instance and parse cn and sans. But this gives me also old certs for apps I have not running anymore.
Is there any better way to get such a list?

The goal is to have a single instance running on either machine of my homelab doing the fetch and provisioning.

Thx!

I once used this to get the domains (forum post):

  DOMAINS=$( \
    curl --silent --max-time 5 http://user:pass@traefik:8080/api/http/routers | \
    pcregrep -o '(?<=Host\(`).*?(?=`\))' | sort | uniq \
  )

Thanks for this pointer. I think I'll use this.

Since the API returns JSON, it made more sense to me to parse the JSON directly vs. trying to grep it. I came up with a jq filter, if anyone wants to try:

curl -s http://user:pass@traefik:8080/api/http/routers |
jq -r 'map(select(.rule|test("^Host"))
  | .rule | split("||")[]
  | match("`([^`]+)").captures[0].string)
  | unique[]'