all: rework routers ntp waiting mechanism

Currently, on routers that require NTP waiting, ctrld makes the cleanup
process, and restart dnsmasq for restoring default DNS config, so ntpd
can query the NTP servers. It did work, but the code will depends on
router platforms.

Instead, we can spawn a plain DNS listener before PreRun on routers,
this listener will serve NTP dns queries and once ntp is configured, the
listener is terminated and ctrld will start serving using its configured
upstreams.

While at it, also fix the userHomeDir function on freshtomato, which
must return the binary directory for routers that requires JFFS.
This commit is contained in:
Cuong Manh Le
2023-05-30 18:17:48 +07:00
committed by Cuong Manh Le
parent 2d950eecdf
commit 1cd54a48e9
6 changed files with 81 additions and 79 deletions
+6 -2
View File
@@ -165,9 +165,13 @@ func initCLI() {
initLogging()
if setupRouter {
if err := router.PreStart(); err != nil {
s, _ := runDNSServerForNTPD()
if err := router.PreRun(); err != nil {
mainLog.Fatal().Err(err).Msg("failed to perform router pre-start check")
}
if err := s.Shutdown(); err != nil {
mainLog.Fatal().Err(err).Msg("failed to shutdown dns server for ntpd")
}
}
processCDFlags()
@@ -909,7 +913,7 @@ func unsupportedPlatformHelp(cmd *cobra.Command) {
func userHomeDir() (string, error) {
switch router.Name() {
case router.DDWrt, router.Merlin:
case router.DDWrt, router.Merlin, router.Tomato:
exe, err := os.Executable()
if err != nil {
return "", err
+42
View File
@@ -459,3 +459,45 @@ func runDNSServer(addr, network string, handler dns.Handler) (*dns.Server, <-cha
waitLock.Lock()
return s, errCh
}
func runDNSServerForNTPD() (*dns.Server, <-chan error) {
dnsResolver := ctrld.NewBootstrapResolver()
s := &dns.Server{
Addr: router.ListenAddress(),
Net: "udp",
Handler: dns.HandlerFunc(func(w dns.ResponseWriter, m *dns.Msg) {
mainLog.Debug().Msg("Serving query for ntpd")
resolveCtx, cancel := context.WithCancel(context.Background())
defer cancel()
if osUpstreamConfig.Timeout > 0 {
timeoutCtx, cancel := context.WithTimeout(resolveCtx, time.Millisecond*time.Duration(osUpstreamConfig.Timeout))
defer cancel()
resolveCtx = timeoutCtx
}
answer, err := dnsResolver.Resolve(resolveCtx, m)
if err != nil {
mainLog.Error().Err(err).Msgf("could not resolve: %v", m)
return
}
if err := w.WriteMsg(answer); err != nil {
mainLog.Error().Err(err).Msg("runDNSServerForNTPD: failed to send DNS response")
}
}),
}
waitLock := sync.Mutex{}
waitLock.Lock()
s.NotifyStartedFunc = waitLock.Unlock
errCh := make(chan error)
go func() {
defer close(errCh)
if err := s.ListenAndServe(); err != nil {
waitLock.Unlock()
mainLog.Error().Err(err).Msgf("could not listen and serve on: %s", s.Addr)
errCh <- err
}
}()
waitLock.Lock()
return s, errCh
}