Skip to content

Add a new dynamic IP function#6

Merged
Imperiopolis merged 1 commit intodoubleunion:mainfrom
jellysquider:dynamic-ip
Jul 10, 2025
Merged

Add a new dynamic IP function#6
Imperiopolis merged 1 commit intodoubleunion:mainfrom
jellysquider:dynamic-ip

Conversation

@jellysquider
Copy link
Copy Markdown

No description provided.

Comment thread router/router.go

func UpdateIPAndRestart() error {
// Step 1: Query current IP address
resp, err := http.Get(ipQueryURL)
Copy link
Copy Markdown
Member

@Imperiopolis Imperiopolis Jul 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread router/router.go
}

// Step 4: Update the file if necessary
if ipUpdated {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cmd/main.go
defer rpio.Close()

// Run updateIPAndRestart every minute in a separate thread
go func() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

@Imperiopolis
Copy link
Copy Markdown
Member

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants