Add a new dynamic IP function#6
Conversation
|
|
||
| func UpdateIPAndRestart() error { | ||
| // Step 1: Query current IP address | ||
| resp, err := http.Get(ipQueryURL) |
There was a problem hiding this comment.
rather than relying on a 3rd party API, I think this could be simplified to do a DNS lookup, something like:
ips, err := net.LookupIP("doubleunion.tplinkdns.com")
...(error handling)...
fmt.Printf("IP address: %s\n", ips[0].String())There should always just be a single IPv4 address in the result ([135.180.39.34]), based on how the dyndns is setup, but we could also update localInternetAddress to be an array that we check against in requireLocalNetworkMiddleware to future proof in case at some point in the future we start getting IPv6 or multiple addresses assigned
| } | ||
|
|
||
| // Step 4: Update the file if necessary | ||
| if ipUpdated { |
There was a problem hiding this comment.
Instead of editing the accesscontrol.service and rebooting the pi, it might be cleaner to just update the localInternetAddress variable and stop caching the IP in a file / reading it from ENV altogether
If there's still a compelling reason for a cache, it still might be cleaner to make a new file that we write just the IP to, and that we read it from at each app startup (instead of from ENV). Then instead of rebooting, you can just exit and wait for the service to restart the process.
| defer rpio.Close() | ||
|
|
||
| // Run updateIPAndRestart every minute in a separate thread | ||
| go func() { |
There was a problem hiding this comment.
Small suggestion – it might be cleaner to move this to the router module, and put all the logic around it in a new file
e.g. dynamic_ip.go might have:
// starts the go routine that calls lookup every second, could optionally implement a `stop` but probably never need it. started from within `RunRouter`
func startPolling()
// Looks up the latest IP for `doubleunion.tplinkdns.com` and assigns it to `localInternetAddress`
func lookupIP()
|
Thank you for finally making this happen! Left some small suggestions on how it could be cleaned up, but totally looks like it should work as is :) |
No description provided.