mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
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.
90 lines
1.8 KiB
Go
90 lines
1.8 KiB
Go
package router
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
func setupMerlin() error {
|
|
buf, err := os.ReadFile(merlinDNSMasqPostConfPath)
|
|
// Already setup.
|
|
if bytes.Contains(buf, []byte(merlinDNSMasqPostConfMarker)) {
|
|
return nil
|
|
}
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
|
|
merlinDNSMasqPostConf, err := dnsMasqConf()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data := strings.Join([]string{
|
|
merlinDNSMasqPostConf,
|
|
"\n",
|
|
merlinDNSMasqPostConfMarker,
|
|
"\n",
|
|
string(buf),
|
|
}, "\n")
|
|
// Write dnsmasq post conf file.
|
|
if err := os.WriteFile(merlinDNSMasqPostConfPath, []byte(data), 0750); err != nil {
|
|
return err
|
|
}
|
|
// Restart dnsmasq service.
|
|
if err := restartDNSMasq(); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := nvramSetKV(nvramSetupKV(), nvramCtrldSetupKey); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func cleanupMerlin() error {
|
|
// Restore old configs.
|
|
if err := nvramRestore(nvramSetupKV(), nvramCtrldSetupKey); err != nil {
|
|
return err
|
|
}
|
|
buf, err := os.ReadFile(merlinDNSMasqPostConfPath)
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
// Restore dnsmasq post conf file.
|
|
if err := os.WriteFile(merlinDNSMasqPostConfPath, merlinParsePostConf(buf), 0750); err != nil {
|
|
return err
|
|
}
|
|
// Restart dnsmasq service.
|
|
if err := restartDNSMasq(); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func postInstallMerlin() error {
|
|
return nil
|
|
}
|
|
|
|
func merlinRestartDNSMasq() error {
|
|
if out, err := exec.Command("service", "restart_dnsmasq").CombinedOutput(); err != nil {
|
|
return fmt.Errorf("restart_dnsmasq: %s, %w", string(out), err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func merlinParsePostConf(buf []byte) []byte {
|
|
if len(buf) == 0 {
|
|
return nil
|
|
}
|
|
parts := bytes.Split(buf, []byte(merlinDNSMasqPostConfMarker))
|
|
if len(parts) != 1 {
|
|
return bytes.TrimLeftFunc(parts[1], unicode.IsSpace)
|
|
}
|
|
return buf
|
|
}
|