diff --git a/cmd/root.go b/cmd/root.go index c947c6ec..bdfcb72a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -33,7 +33,7 @@ func check() { core.WritePassword(nil) trojan.InstallTls() trojan.InstallMysql() - util.ExecCommand("systemctl restart trojan-web") + util.SystemctlRestart("trojan-web") } } diff --git a/cmd/web.go b/cmd/web.go index 1e9ef393..fb02f79d 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -23,30 +23,22 @@ var webCmd = &cobra.Command{ }, } -func controllWeb(key, keyCN string) { - if err := util.ExecCommand(fmt.Sprintf("systemctl %s trojan-web", key)); err != nil { - fmt.Println(util.Red(fmt.Sprintf("%strojan-web失败!", keyCN))) - } else { - fmt.Println(util.Green(fmt.Sprintf("%strojan-web成功!", keyCN))) - } -} - func init() { webCmd.Flags().StringVarP(&host, "host", "", "0.0.0.0", "web服务监听地址") webCmd.Flags().IntVarP(&port, "port", "p", 80, "web服务启动端口") webCmd.Flags().BoolVarP(&ssl, "ssl", "", false, "web服务是否以https方式运行") webCmd.Flags().IntVarP(&timeout, "timeout", "t", 120, "登录超时时间(min)") webCmd.AddCommand(&cobra.Command{Use: "stop", Short: "停止trojan-web", Run: func(cmd *cobra.Command, args []string) { - controllWeb("stop", "停止") + util.SystemctlStop("trojan-web") }}) webCmd.AddCommand(&cobra.Command{Use: "start", Short: "启动trojan-web", Run: func(cmd *cobra.Command, args []string) { - controllWeb("start", "启动") + util.SystemctlStart("trojan-web") }}) webCmd.AddCommand(&cobra.Command{Use: "restart", Short: "重启trojan-web", Run: func(cmd *cobra.Command, args []string) { - controllWeb("restart", "重启") + util.SystemctlRestart("trojan-web") }}) webCmd.AddCommand(&cobra.Command{Use: "status", Short: "查看trojan-web状态", Run: func(cmd *cobra.Command, args []string) { - util.ExecCommand("systemctl status trojan-web -l") + fmt.Println(util.SystemctlStatus("trojan-web")) }}) webCmd.AddCommand(&cobra.Command{Use: "log", Short: "查看trojan-web日志", Run: func(cmd *cobra.Command, args []string) { util.Log("trojan-web", 300) diff --git a/trojan/install.go b/trojan/install.go index 05d37afc..57fa8cdf 100644 --- a/trojan/install.go +++ b/trojan/install.go @@ -54,8 +54,8 @@ func InstallTrojan(version string) { } util.ExecCommand(data) util.OpenPort(443) - util.ExecCommand("systemctl restart trojan") - util.ExecCommand("systemctl enable trojan") + util.SystemctlRestart("trojan") + util.SystemctlEnable("trojan") } // InstallTls 安装证书 @@ -112,7 +112,7 @@ func InstallTls() { if !util.IsExists("/root/.acme.sh/acme.sh") { util.RunWebShell("https://get.acme.sh") } - util.ExecCommand("systemctl stop trojan-web") + util.SystemctlStop("trojan-web") util.OpenPort(80) checkResult := util.ExecCommandWithResult("/root/.acme.sh/acme.sh -v|tr -cd '[0-9]'") acmeVersion, _ := strconv.Atoi(checkResult) @@ -144,7 +144,7 @@ func InstallTls() { core.WriteTls(crtFile, keyFile, domain) } Restart() - util.ExecCommand("systemctl restart trojan-web") + util.SystemctlRestart("trojan-web") fmt.Println() } diff --git a/trojan/trojan.go b/trojan/trojan.go index 8635f596..acf76c91 100644 --- a/trojan/trojan.go +++ b/trojan/trojan.go @@ -50,35 +50,23 @@ func ControllMenu() { // Restart 重启trojan func Restart() { util.OpenPort(core.GetConfig().LocalPort) - if err := util.ExecCommand("systemctl restart trojan"); err != nil { - fmt.Println(util.Red("重启trojan失败!")) - } else { - fmt.Println(util.Green("重启trojan成功!")) - } + util.SystemctlRestart("trojan") } // Start 启动trojan func Start() { util.OpenPort(core.GetConfig().LocalPort) - if err := util.ExecCommand("systemctl start trojan"); err != nil { - fmt.Println(util.Red("启动trojan失败!")) - } else { - fmt.Println(util.Green("启动trojan成功!")) - } + util.SystemctlStart("trojan") } // Stop 停止trojan func Stop() { - if err := util.ExecCommand("systemctl stop trojan"); err != nil { - fmt.Println(util.Red("停止trojan失败!")) - } else { - fmt.Println(util.Green("停止trojan成功!")) - } + util.SystemctlStop("trojan") } // Status 获取trojan状态 func Status(isPrint bool) string { - result := util.ExecCommandWithResult("systemctl status trojan") + result := util.SystemctlStatus("trojan") if isPrint { fmt.Println(result) } diff --git a/util/command.go b/util/command.go index d2a0d8ba..40e32e3c 100644 --- a/util/command.go +++ b/util/command.go @@ -9,6 +9,74 @@ import ( "strings" ) +func systemctlReplace(err error) (bool, error) { + var isReplace bool + if err != nil && IsExists("/.dockerenv") && strings.Contains(err.Error(), "Failed to get D-Bus") { + isReplace = true + fmt.Println(Yellow("正在下载并替换适配的systemctl。。")) + if err = ExecCommand("curl -L https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -o /usr/bin/systemctl && chmod +x /usr/bin/systemctl"); err != nil { + return isReplace, err + } + } + return isReplace, err +} + +func systemctlBase(name, operate string) error { + err := ExecCommand(fmt.Sprintf("systemctl %s %s", operate, name)) + if v, err := systemctlReplace(err); v { + if err = ExecCommand(fmt.Sprintf("systemctl %s %s", operate, name)); err != nil { + return err + } + } + return err +} + +// SystemctlStart 服务启动 +func SystemctlStart(name string) { + if err := systemctlBase(name, "start"); err != nil { + fmt.Println(Red(fmt.Sprintf("启动%s失败!", name))) + } else { + fmt.Println(Green(fmt.Sprintf("启动%s成功!", name))) + } +} + +// SystemctlStop 服务停止 +func SystemctlStop(name string) { + if err := systemctlBase(name, "stop"); err != nil { + fmt.Println(Red(fmt.Sprintf("停止%s失败!", name))) + } else { + fmt.Println(Green(fmt.Sprintf("停止%s成功!", name))) + } +} + +// SystemctlRestart 服务重启 +func SystemctlRestart(name string) { + if err := systemctlBase(name, "restart"); err != nil { + fmt.Println(Red(fmt.Sprintf("重启%s失败!", name))) + } else { + fmt.Println(Green(fmt.Sprintf("重启%s成功!", name))) + } +} + +// SystemctlEnable 服务设置开机自启 +func SystemctlEnable(name string) { + if err := systemctlBase(name, "enable"); err != nil { + fmt.Println(Red(fmt.Sprintf("设置%s开机自启失败!", name))) + } +} + +// SystemctlStatus 服务状态查看 +func SystemctlStatus(name string) string { + out, err := exec.Command("bash", "-c", fmt.Sprintf("systemctl status %s", name)).CombinedOutput() + if v, _ := systemctlReplace(err); v { + out, err = exec.Command("bash", "-c", fmt.Sprintf("systemctl status %s", name)).CombinedOutput() + } + if err != nil { + fmt.Println(Red(fmt.Sprintf("查看%s状态失败!", name))) + } + return string(out) +} + // CheckCommandExists 检查命令是否存在 func CheckCommandExists(command string) bool { if _, err := exec.LookPath(command); err != nil { @@ -78,6 +146,11 @@ func ExecCommand(command string) error { // ExecCommandWithResult 运行命令并获取结果 func ExecCommandWithResult(command string) string { out, err := exec.Command("bash", "-c", command).CombinedOutput() + if strings.Contains(command, "systemctl") { + if v, _ := systemctlReplace(err); v { + out, err = exec.Command("bash", "-c", command).CombinedOutput() + } + } if err != nil && !strings.Contains(err.Error(), "exit status") { fmt.Println("err: " + err.Error()) return ""