[Issue #1] - Added configurable NTPServer option in config.go; also added DNS name resolution so you don't have to use IPs
This commit is contained in:
@@ -10,5 +10,6 @@ const (
|
|||||||
EnableICMP = true
|
EnableICMP = true
|
||||||
|
|
||||||
// Time settings
|
// Time settings
|
||||||
|
NTPServer = "pool.ntp.org"
|
||||||
ActiveTimezone = "UTC"
|
ActiveTimezone = "UTC"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -109,16 +109,27 @@ func main() {
|
|||||||
// Extract IP string from the returned NetworkStack
|
// Extract IP string from the returned NetworkStack
|
||||||
ipStr := netStack.localIP.String()
|
ipStr := netStack.localIP.String()
|
||||||
|
|
||||||
// Sync wall time using public NTP (e.g. Cloudflare: 162.159.200.1 / time.cloudflare.com)
|
println("Resolving NTP server:", NTPServer)
|
||||||
println("Synchronizing clock via NTP...")
|
oled.ClearBuffer()
|
||||||
oled.ClearBuffer()
|
tinyfont.WriteLine(oled, defaultFont, 0, 32, "Resolving NTP...", white)
|
||||||
tinyfont.WriteLine(oled, defaultFont, 0, 32, "Syncing NTP...", white)
|
oled.Display()
|
||||||
oled.Display()
|
|
||||||
ntpServer := netip.MustParseAddr("162.159.200.1")
|
ntpServer, err := resolveHostToIP(netStack, NTPServer, 5*time.Second)
|
||||||
timeOffset, err := syncNTP(netStack, ntpServer, 5*time.Second)
|
if err != nil {
|
||||||
if err != nil {
|
panicErr("NTP DNS lookup failed", err)
|
||||||
panicErr("NTP sync failed", err)
|
}
|
||||||
}
|
println("NTP server IP: ", ntpServer.String())
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
|
println("Synchronizing clock via NTP...")
|
||||||
|
oled.ClearBuffer()
|
||||||
|
tinyfont.WriteLine(oled, defaultFont, 0, 32, "Syncing NTP...", white)
|
||||||
|
oled.Display()
|
||||||
|
|
||||||
|
timeOffset, err := syncNTP(netStack, ntpServer, 5*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
panicErr("NTP sync failed", err)
|
||||||
|
}
|
||||||
|
|
||||||
println("NTP synced successfully!")
|
println("NTP synced successfully!")
|
||||||
oled.ClearBuffer()
|
oled.ClearBuffer()
|
||||||
|
|||||||
+31
@@ -225,3 +225,34 @@ func tcpBackoff(consecutiveBackoffs uint) time.Duration {
|
|||||||
wait := min(shifted, maxWait)
|
wait := min(shifted, maxWait)
|
||||||
return time.Duration(wait)
|
return time.Duration(wait)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resolveHostToIP checks if host is already an IP literal, or queries the DNS server.
|
||||||
|
func resolveHostToIP(stack *NetworkStack, host string, timeout time.Duration) (netip.Addr, error) {
|
||||||
|
// 1. If it's already an IP string (e.g. "162.159.200.1"), parse and return directly
|
||||||
|
if ip, err := netip.ParseAddr(host); err == nil {
|
||||||
|
return ip, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Start asynchronous DNS A-record query
|
||||||
|
if err := stack.stack.StartLookupIP(host); err != nil {
|
||||||
|
return netip.Addr{}, fmt.Errorf("start DNS lookup: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Poll ResultLookupIP until done or timed out
|
||||||
|
deadline := time.Now().Add(timeout)
|
||||||
|
for time.Now().Before(deadline) {
|
||||||
|
addrs, done, err := stack.stack.ResultLookupIP(host)
|
||||||
|
if done {
|
||||||
|
if err != nil {
|
||||||
|
return netip.Addr{}, fmt.Errorf("DNS lookup error: %w", err)
|
||||||
|
}
|
||||||
|
if len(addrs) == 0 {
|
||||||
|
return netip.Addr{}, fmt.Errorf("no addresses returned for %s", host)
|
||||||
|
}
|
||||||
|
return addrs[0], nil
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
return netip.Addr{}, fmt.Errorf("DNS lookup timed out for %s", host)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user