mholt / caddy-dynamicdns

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

Only use public global unicast addresses for NetInterface source #61

Closed jm355 closed 8 months ago

jm355 commented 8 months ago

59

Monviech commented 8 months ago

I have compiled and tested this. For me it correctly filters out the unwanted IPv4 and IPv6 addresses.

Example interface:

vtnet0: flags=8863<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        description: WAN (wan)
        options=800a8<VLAN_MTU,JUMBO_MTU,VLAN_HWCSUM,LINKSTATE>
        ether 52:54:00:f8:72:36
        inet 172.16.0.162 netmask 0xffffff00 broadcast 172.16.0.255
        inet 203.0.113.1 netmask 0xffffffff broadcast 203.0.113.1
        inet6 fe80::5054:ff:fef8:7236%vtnet0 prefixlen 64 scopeid 0x1
        inet6 2003:a:1704:63aa:5054:ff:fef8:7236 prefixlen 64 autoconf
        media: Ethernet autoselect (10Gbase-T <full-duplex>)
        status: active
        nd6 options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>

Log emission:

2024-03-08T07:57:20 Informational   caddy   "info","ts":"2024-03-08T07:57:20Z","logger":"dynamic_dns","msg":"finished updating DNS","current_ips":["2003:a:1704:63aa:5054:ff:fef8:7236","203.0.113.1"]}
jm355 commented 8 months ago

Thanks for testing it! I was going to try it out today. Do you use ipv6 privacy address? I'm guessing not, since your link local address and global address have the same postfix, which is usually based on the mac address. I'd like to also add a change where it only uses the first address it finds, depending on what it does when I test it

jm355 commented 8 months ago

Since the way privacy addresses work, you still have the mac-based global address, but then you also have an address with a randomly generated postfix which is used and gets cycled periodically. So you can have up to three global addresses at a time - the mac one, the latest privacy one, and sometimes the old privacy one, which stays around briefly with a "deprecated" label after the new one is generated. So if the address array is sorted such that the correct one is the first in the list, then I think it should be easy

jm355 commented 8 months ago

Yep, just tested and it submits all my addresses. I'm gonna to add a change to only return one address for ipv4 and one for ipv6

Monviech commented 8 months ago

Shouldn't the IPv6 privacy extension only be used on clients? I think on most Linux Distributions and in Freebsd its off by default. On Windows Clients I've seen it enabled by default.

jm355 commented 8 months ago

I've never heard that, but I also don't know. But why not use it for a server? Isn't the whole point of dynamicdns being able to gracefully handle a dynamic target ip address?

jm355 commented 8 months ago

if it shouldn't be used for servers, then we'd need to find a way to only use the non-priv-extensions address

jm355 commented 8 months ago

Ok, latest changes seem to work for me though. It only returns one address

francislavoie commented 8 months ago

Could you also update the readme & godoc comment to explain the behaviour?

Monviech commented 8 months ago

I've never heard that, but I also don't know. But why not use it for a server? Isn't the whole point of dynamicdns being able to gracefully handle a dynamic target ip address?

Yeah you are right. There's two types of dynamic IPv6 happening. One is the privacy extension. The other is if your ISP changes your Prefix from time to time.

If this can handle both scenarios, all the better really. :)

jm355 commented 8 months ago

Is the godoc comment just the // comment in front of the GetIPs function?

jm355 commented 8 months ago

It would be better if it was more robust and picked the "correct" option, but I don't know what the "correct" option is and unless I missed a function in the net package to help, the logic to find it would be a lot more convoluted. But just taking the first one it finds seems to work for both of us, so I think it's fine

jm355 commented 8 months ago

I've updated the readme and comment, not sure if it's the godoc comment though

Monviech commented 8 months ago

I've tested the build and function after these latest changes, still same result as above.

jm355 commented 8 months ago

Yep, I've been running my server with these since I made the changes and everything seems to work as expected