mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-09-29 13:51:51 +02:00
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:
1 parent
ab88174091
commit
43d82cf1a7
4 files changed
+77
-4
No files matched your search
+19
-2
@@ -437,7 +437,7 @@ func initCLI() {
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
readConfig(false)
|
readConfig(false)
|
||||||
v.Unmarshal(&cfg)
|
v.Unmarshal(&cfg)
|
||||||
p := &prog{router: router.New(&cfg, cdUID != "")}
|
p := &prog{router: router.New(&cfg, runInCdMode())}
|
||||||
s, err := newService(p, svcConfig)
|
s, err := newService(p, svcConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Msg(err.Error())
|
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) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
readConfig(false)
|
readConfig(false)
|
||||||
v.Unmarshal(&cfg)
|
v.Unmarshal(&cfg)
|
||||||
p := &prog{router: router.New(&cfg, cdUID != "")}
|
p := &prog{router: router.New(&cfg, runInCdMode())}
|
||||||
s, err := newService(p, svcConfig)
|
s, err := newService(p, svcConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mainLog.Load().Error().Msg(err.Error())
|
mainLog.Load().Error().Msg(err.Error())
|
||||||
@@ -2440,3 +2440,20 @@ func absHomeDir(filename string) string {
|
|||||||
}
|
}
|
||||||
return filepath.Join(dir, filename)
|
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
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ const (
|
|||||||
startedPath = "/started"
|
startedPath = "/started"
|
||||||
reloadPath = "/reload"
|
reloadPath = "/reload"
|
||||||
deactivationPath = "/deactivation"
|
deactivationPath = "/deactivation"
|
||||||
|
cdPath = "/cd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type controlServer struct {
|
type controlServer struct {
|
||||||
@@ -171,6 +172,13 @@ func (p *prog) registerControlServerHandler() {
|
|||||||
}
|
}
|
||||||
w.WriteHeader(code)
|
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 {
|
func jsonResponse(next http.Handler) http.Handler {
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package router
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/xml"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config represents /conf/config.xml file found on pfsense/opnsense.
|
||||||
|
type Config struct {
|
||||||
|
PfsenseUnbound *string `xml:"unbound>enable,omitempty"`
|
||||||
|
OPNsenseUnbound *string `xml:"OPNsense>unboundplus>general>enabled,omitempty"`
|
||||||
|
Dnsmasq *string `xml:"dnsmasq>enable,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DnsmasqEnabled reports whether dnsmasq is enabled.
|
||||||
|
func (c *Config) DnsmasqEnabled() bool {
|
||||||
|
if isPfsense() { // pfsense only set the attribute if dnsmasq is enabled.
|
||||||
|
return c.Dnsmasq != nil
|
||||||
|
}
|
||||||
|
return c.Dnsmasq != nil && *c.Dnsmasq == "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnboundEnabled reports whether unbound is enabled.
|
||||||
|
func (c *Config) UnboundEnabled() bool {
|
||||||
|
if isPfsense() { // pfsense only set the attribute if unbound is enabled.
|
||||||
|
return c.PfsenseUnbound != nil
|
||||||
|
}
|
||||||
|
return c.OPNsenseUnbound != nil && *c.OPNsenseUnbound == "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
// currentConfig does unmarshalling /conf/config.xml file,
|
||||||
|
// return the corresponding *Config represent it.
|
||||||
|
func currentConfig() (*Config, error) {
|
||||||
|
buf, _ := os.ReadFile("/conf/config.xml")
|
||||||
|
c := Config{}
|
||||||
|
if err := xml.Unmarshal(buf, &c); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &c, nil
|
||||||
|
}
|
||||||
@@ -111,8 +111,16 @@ func (or *osRouter) Setup() error {
|
|||||||
|
|
||||||
func (or *osRouter) Cleanup() error {
|
func (or *osRouter) Cleanup() error {
|
||||||
if or.cdMode {
|
if or.cdMode {
|
||||||
_ = exec.Command(unboundRcPath, "onerestart").Run()
|
c, err := currentConfig()
|
||||||
_ = exec.Command(dnsmasqRcPath, "onerestart").Run()
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if c.UnboundEnabled() {
|
||||||
|
_ = exec.Command(unboundRcPath, "onerestart").Run()
|
||||||
|
}
|
||||||
|
if c.DnsmasqEnabled() {
|
||||||
|
_ = exec.Command(dnsmasqRcPath, "onerestart").Run()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user