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

Added Command IPSource #39

Closed mietzen closed 1 year ago

mietzen commented 1 year ago

Generalist approach to cover all niche use cases, discussion: https://github.com/mholt/caddy-dynamicdns/issues/37#issuecomment-1510223729

Implements:

francislavoie commented 1 year ago

This doesn't solve #31. I opened #38 for that instead.

mietzen commented 1 year ago

I'm running into some troubles when testing the new ipsource:

panic: interface conversion: interface {} is nil, not *caddy.Replacer

@francislavoie I've seen your post about debugging with xcaddy and vscode: https://caddy.community/t/debugging-modules/10266/3

Could you post a example launch.json?

Edit: I just wrote a main.go

mietzen commented 1 year ago

caddy complains about the formatting of my Caddyfile otherwise this works:

{
    dynamic_dns {
        provider cloudflare {env.CLOUDFLARE_API_TOKEN}
        domains {
            my-domain.com a1234
        }
        ip_source command echo 1.2.3.4,2606:4700::1234
        check_interval 5m
        ttl 1h
    }
}
$ CLOUDFLARE_API_TOKEN="xxxxxxxx-xxxxxxxx" ./caddy run                                     
2023/04/17 09:00:12.400 INFO    using adjacent Caddyfile
2023/04/17 09:00:12.400 WARN    Caddyfile input is not formatted; run the 'caddy fmt' command to fix inconsistencies    {"adapter": "caddyfile", "file": "Caddyfile", "line": 7}
2023/04/17 09:00:12.402 INFO    admin   admin endpoint started  {"address": "localhost:2019", "enforce_origin": false, "origins": ["//localhost:2019", "//[::1]:2019", "//127.0.0.1:2019"]}
2023/04/17 09:00:12.402 INFO    autosaved config (load with --resume flag)      {"file": "/Users/nils/Library/Application Support/Caddy/autosave.json"}
2023/04/17 09:00:12.402 INFO    serving initial configuration
2023/04/17 09:00:14.301 INFO    dynamic_dns     domain not found in DNS {"domain": "a1234"}
2023/04/17 09:00:14.302 INFO    dynamic_dns     domain not found in DNS {"domain": "a1234"}
2023/04/17 09:00:14.311 INFO    dynamic_dns     different IP address    {"new_ip": "1.2.3.4", "old_ips": ["<nil>"]}
2023/04/17 09:00:14.311 INFO    dynamic_dns     different IP address    {"new_ip": "2606:4700::1234", "old_ips": ["<nil>"]}
2023/04/17 09:00:14.311 INFO    dynamic_dns     updating DNS record     {"zone": "my-domain.com", "type": "A", "name": "a1234", "value": "1.2.3.4", "ttl": 3600}
2023/04/17 09:00:14.311 INFO    dynamic_dns     updating DNS record     {"zone": "my-domain.com", "type": "AAAA", "name": "a1234", "value": "2606:4700::1234", "ttl": 3600}
2023/04/17 09:00:17.592 INFO    dynamic_dns     finished updating DNS   {"current_ips": ["1.2.3.4", "2606:4700::1234"]}

image

mholt commented 1 year ago

@mietzen

caddy complains about the formatting of my Caddyfile

It's because the indentation isn't consistent:

{
    dynamic_dns {
        provider cloudflare {env.CLOUDFLARE_API_TOKEN}
        domains {
            my-domain.com a1234
        }
            ip_source command echo 1.2.3.4,2606:4700::1234
            check_interval 5m
        ttl 1h
    }
}

Anyway, thanks for the contribution!

As stated in the issue, I'm not sure I'm ready to make this built-into the dynamic DNS app, as I'm not sure of the security implications. In theory it should be fine unless people are running untrusted code on their servers. But I'd like to play it safe for now, and maybe leave this to be a third-party plugin. Would that be alright?

francislavoie commented 1 year ago

I agree, doesn't need to be in here. We could add a note in the README that an additional plugin exists if the users want to opt-in to building with that plugin as well.

@mietzen you can make your own repo for this plugin. Rough instructions:

mietzen commented 1 year ago

As stated in the issue, I'm not sure I'm ready to make this built-into the dynamic DNS app, as I'm not sure of the security implications. In theory it should be fine unless people are running untrusted code on their servers. But I'd like to play it safe for now, and maybe leave this to be a third-party plugin. Would that be alright?

I get your point, most of the users won't need it and this option opens a potential security risk.

I copied my code to a new repo: https://github.com/mietzen/caddy-dynamicdns-cmd-source

When testing I get:

Error: adapting config using caddyfile: parsing caddyfile tokens for 'dynamic_dns': Caddyfile:7 - Error during parsing: getting module named 'dynamic_dns.ip_sources.command': module not registered: dynamic_dns.ip_sources.command

I guess I've to change something in caddy.ModuleInfo or caddy.RegisterModule, but I don't no what.

Btw.: Big a thank you to @francislavoie for tutoring me on my golang adventure and @mholt for creating caddy 😉 👍

mholt commented 1 year ago

Thank you so much for contributing :D