all: rework fetching/generating config in cd mode

Config fetching/generating in cd mode is currently weird, error prone,
and easy for user to break ctrld when using custom config.

This commit reworks the flow:

 - Fetching config from Control D API.
 - No custom config, use the current default config.
 - If custom config presents, but there's no listener, use 0.0.0.0:53.
 - Try listening on current ip+port config, if ok, ctrld could be a
   direct listener with current setup, moving on.
 - If failed, trying 127.0.0.1:53.
 - If failed, trying current ip + port 5354
 - If still failed, pick a random ip:port pair, retry until listening ok.

With this flow, thing is more predictable/stable, and help removing the
Config interface for router.
This commit is contained in:
Cuong Manh Le
2023-08-09 23:54:23 +07:00
committed by Cuong Manh Le
parent 3f3c1d6d78
commit 7af59ee589
23 changed files with 442 additions and 344 deletions
+13 -10
View File
@@ -54,6 +54,14 @@ func (p *Pfsense) ConfigureService(svc *service.Config) error {
}
func (p *Pfsense) Install(config *service.Config) error {
// pfsense need ".sh" extension for script to be run at boot.
// See: https://docs.netgate.com/pfsense/en/latest/development/boot-commands.html#shell-script-option
oldname := filepath.Join(rcPath, p.svcName)
newname := filepath.Join(rcPath, p.svcName+".sh")
_ = os.Remove(newname)
if err := os.Symlink(oldname, newname); err != nil {
return fmt.Errorf("os.Symlink: %w", err)
}
return nil
}
@@ -62,16 +70,7 @@ func (p *Pfsense) Uninstall(config *service.Config) error {
}
func (p *Pfsense) PreRun() error {
return nil
}
func (p *Pfsense) Configure() error {
p.cfg.Listener["0"].IP = "127.0.0.1"
p.cfg.Listener["0"].Port = 53
return nil
}
func (p *Pfsense) Setup() error {
// TODO: remove this hacky solution.
// If Pfsense is in DNS Resolver mode, ensure no unbound processes running.
_ = exec.Command("killall", "unbound").Run()
@@ -80,6 +79,10 @@ func (p *Pfsense) Setup() error {
return nil
}
func (p *Pfsense) Setup() error {
return nil
}
func (p *Pfsense) Cleanup() error {
if err := os.Remove(filepath.Join(rcPath, p.svcName+".sh")); err != nil {
return fmt.Errorf("os.Remove: %w", err)