all: support GL.iNET router

This commit is contained in:
Cuong Manh Le
2023-05-15 23:54:00 +07:00
committed by Cuong Manh Le
parent d9dfc584e7
commit 3b6c12abd4
3 changed files with 47 additions and 0 deletions

View File

@@ -137,6 +137,7 @@ func initCLI() {
if err != nil {
mainLog.Fatal().Err(err).Msg("failed create new service")
}
s = newService(s)
serviceLogger, err := s.Logger(nil)
if err != nil {
mainLog.Error().Err(err).Msg("failed to get service logger")
@@ -310,6 +311,7 @@ func initCLI() {
mainLog.Error().Msg(err.Error())
return
}
s = newService(s)
tasks := []task{
{s.Stop, false},
{s.Uninstall, false},
@@ -371,6 +373,7 @@ func initCLI() {
mainLog.Error().Msg(err.Error())
return
}
s = newService(s)
initLogging()
if doTasks([]task{{s.Stop, true}}) {
prog.resetDNS()
@@ -394,6 +397,7 @@ func initCLI() {
mainLog.Error().Msg(err.Error())
return
}
s = newService(s)
initLogging()
if doTasks([]task{{s.Restart, true}}) {
mainLog.Notice().Msg("Service restarted")
@@ -414,6 +418,7 @@ func initCLI() {
mainLog.Error().Msg(err.Error())
return
}
s = newService(s)
status, err := serviceStatus(s)
if err != nil {
mainLog.Error().Msg(err.Error())

View File

@@ -7,8 +7,40 @@ import (
"os/exec"
"github.com/kardianos/service"
"github.com/Control-D-Inc/ctrld/internal/router"
)
func newService(s service.Service) service.Service {
// TODO: unify for other SysV system.
if router.IsGLiNet() {
return &sysV{s}
}
return s
}
// sysV wraps a service.Service, and provide start/stop/status command
// base on "/etc/init.d/<service_name>".
//
// Use this on system wherer "service" command is not available, like GL.iNET router.
type sysV struct {
service.Service
}
func (s *sysV) Start() error {
_, err := exec.Command("/etc/init.d/ctrld", "start").CombinedOutput()
return err
}
func (s *sysV) Stop() error {
_, err := exec.Command("/etc/init.d/ctrld", "stop").CombinedOutput()
return err
}
func (s *sysV) Status() (service.Status, error) {
return unixSystemVServiceStatus()
}
type task struct {
f func() error
abortOnError bool

View File

@@ -13,6 +13,16 @@ var errUCIEntryNotFound = errors.New("uci: Entry not found")
const openwrtDNSMasqConfigPath = "/tmp/dnsmasq.d/ctrld.conf"
// IsGLiNet reports whether the router is an GL.iNet router.
func IsGLiNet() bool {
if Name() != OpenWrt {
return false
}
buf, _ := os.ReadFile("/proc/version")
// The output of /proc/version contains "(glinet@glinet)".
return bytes.Contains(buf, []byte(" (glinet"))
}
func setupOpenWrt() error {
// Delete dnsmasq port if set.
if _, err := uci("delete", "dhcp.@dnsmasq[0].port"); err != nil && !errors.Is(err, errUCIEntryNotFound) {