mholt / caddy-dynamicdns

Caddy app that keeps your DNS records (A/AAAA) pointed at itself.
Apache License 2.0
251 stars 25 forks source link

Support dynamically generated domains #10

Closed caiych closed 2 years ago

caiych commented 3 years ago

Imagine a Caddyfile like this:

{
    dynamic_dns {
        provider cloudflare {env.CLOUDFLARE_API_TOKEN}
        domains {
            example.com on_demand
        }
        check_interval 5m
    }
}

yo.example.com {
  dynamic_dns
  reverse_proxy 127.0.0.1:10000
}

hi.example.com {
  dynamic_dns
  file_server
}

And this middleware could pick up the domains has dynamic_dns in its config and start to config dns for them. This removes the need of updating the top level block whenever a new domain is added.

I can also look into the implementation later once we are agree on the direction.

Thanks.

mholt commented 3 years ago

Can you clarify this:

And this middleware could pick up the domains has dynamic_dns in its config and start to config dns for them

What do you mean by this?

Your config is currently set to manage DNS for on_demand.example.com (that's not even a valid DNS name).

caiych commented 3 years ago

Sorry I wasn't clear(and the on_demand part is probably a bad idea).

The above Caddyfile is meant to be equivalent of:

{
    dynamic_dns {
        provider cloudflare {env.CLOUDFLARE_API_TOKEN}
        domains {
            example.com yo hi
        }
        check_interval 5m
    }
}

yo.example.com {
  reverse_proxy 127.0.0.1:10000
}

hi.example.com {
  file_server
}
francislavoie commented 3 years ago

@mholt I think on_demand was meant as a constant here to indicate "read a list of subdomains dynamically from the ones Caddy is managing".

It's an interesting idea, but I'm not sure what the best approach to extracting that information would be. I don't think Caddy has a good API for fetching a list of domains that are currently being managed by it.

That might be something that can be done by reading from the configured storage and listing the domains that Caddy has in storage, and filter that list by only including those matching the base domain that dynamic_dns has configured.

Note that care needs to be taken for this to be efficient though, because List() calls can be expensive on certain storage drivers (e.g. DynamoDB) but I think it's unlikely that this would be paired with any storage driver other than filesystem since dynamic DNS is most likely used by "self hosted" type environments which generally strive for simplicity.

mholt commented 3 years ago

Oh, I see. That'd be kind of cool, you just provide your DNS credentials, and Caddy will set the A/AAAA records automatically and then try to get a certificate. Hmmmm.

caiych commented 3 years ago

Noticed this https://github.com/tiredofit/docker-traefik-cloudflare-companion which shares the same idea but doing it from the outside -- makes sense since it's traefik and there's no centralized list of domain.

francislavoie commented 2 years ago

@mholt this can be closed, the PR was merged!