initial commit

This commit is contained in:
Alireza Ahmadi
2024-02-13 01:17:03 +01:00
commit f40b27fd8b
136 changed files with 16023 additions and 0 deletions
+54
View File
@@ -0,0 +1,54 @@
package singbox
import (
"errors"
"io/fs"
"os"
"os/exec"
"s-ui/config"
"strings"
)
var serviceName = "sing-box"
type Controller struct {
}
func (s *Controller) GetBinaryName() string {
return "sing-box"
}
func (s *Controller) GetBinaryPath() string {
return config.GetBinFolderPath() + "/" + s.GetBinaryName()
}
func (s *Controller) GetConfigPath() string {
return config.GetBinFolderPath() + "/config.json"
}
func (s *Controller) IsRunning() bool {
cmd := exec.Command("pgrep", "sing-box")
output, err := cmd.Output()
if err != nil {
return false
}
// If pgrep found the Controller, its output will not be empty
return strings.TrimSpace(string(output)) != ""
}
func (s *Controller) signalSingbox(signal string) error {
return os.WriteFile(config.GetBinFolderPath()+"/signal", []byte(signal), fs.ModePerm)
}
func (s *Controller) Restart() error {
return s.signalSingbox("restart")
}
func (s *Controller) Stop() error {
if !s.IsRunning() {
return errors.New("Sing-Box is not running")
}
return s.signalSingbox("stop")
}