cmd/cli,internal/router: detect unbound/dnsmasq status correctly on *BSD

Also detect cd mode for stop/uninstall command correctly, too.
This commit is contained in:
Cuong Manh Le
2024-03-14 23:53:34 +07:00
committed by Cuong Manh Le
parent ab88174091
commit 43d82cf1a7
4 changed files with 77 additions and 4 deletions
+19 -2
View File
@@ -437,7 +437,7 @@ func initCLI() {
Run: func(cmd *cobra.Command, args []string) {
readConfig(false)
v.Unmarshal(&cfg)
p := &prog{router: router.New(&cfg, cdUID != "")}
p := &prog{router: router.New(&cfg, runInCdMode())}
s, err := newService(p, svcConfig)
if err != nil {
mainLog.Load().Error().Msg(err.Error())
@@ -594,7 +594,7 @@ NOTE: Uninstalling will set DNS to values provided by DHCP.`,
Run: func(cmd *cobra.Command, args []string) {
readConfig(false)
v.Unmarshal(&cfg)
p := &prog{router: router.New(&cfg, cdUID != "")}
p := &prog{router: router.New(&cfg, runInCdMode())}
s, err := newService(p, svcConfig)
if err != nil {
mainLog.Load().Error().Msg(err.Error())
@@ -2440,3 +2440,20 @@ func absHomeDir(filename string) string {
}
return filepath.Join(dir, filename)
}
// runInCdMode reports whether ctrld service is running in cd mode.
func runInCdMode() bool {
if s, _ := newService(&prog{}, svcConfig); s != nil {
if dir, _ := socketDir(); dir != "" {
cc := newSocketControlClient(s, dir)
if cc != nil {
resp, _ := cc.post(cdPath, nil)
if resp != nil {
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
}
}
}
return false
}
+8
View File
@@ -21,6 +21,7 @@ const (
startedPath = "/started"
reloadPath = "/reload"
deactivationPath = "/deactivation"
cdPath = "/cd"
)
type controlServer struct {
@@ -171,6 +172,13 @@ func (p *prog) registerControlServerHandler() {
}
w.WriteHeader(code)
}))
p.cs.register(cdPath, http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
if cdUID != "" {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusBadRequest)
}))
}
func jsonResponse(next http.Handler) http.Handler {