diff --git a/app/http/controllers/info_controller.go b/app/http/controllers/info_controller.go index 06b73c36..bcc0e003 100644 --- a/app/http/controllers/info_controller.go +++ b/app/http/controllers/info_controller.go @@ -108,8 +108,8 @@ func (r *InfoController) CountInfo(ctx http.Context) http.Response { } var databaseCount int64 if mysqlInstalled { - status, err := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") - if status == "active" && err == nil { + status, err := tools.ServiceStatus("mysqld") + if status && err == nil { rootPassword := r.setting.Get(models.SettingKeyMysqlRootPassword) type database struct { Name string `json:"name"` @@ -150,8 +150,8 @@ func (r *InfoController) CountInfo(ctx http.Context) http.Response { } } if postgresqlInstalled { - status, err := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") - if status == "active" && err == nil { + status, err := tools.ServiceStatus("postgresql") + if status && err == nil { raw, err := tools.Exec(`echo "\l" | su - postgres -c "psql"`) if err == nil { databases := strings.Split(raw, "\n") diff --git a/app/http/controllers/plugins/fail2ban_controller.go b/app/http/controllers/plugins/fail2ban_controller.go index 8122ffc4..67652249 100644 --- a/app/http/controllers/plugins/fail2ban_controller.go +++ b/app/http/controllers/plugins/fail2ban_controller.go @@ -38,7 +38,7 @@ func (r *Fail2banController) Status(ctx http.Context) http.Response { // Reload 重载配置 func (r *Fail2banController) Reload(ctx http.Context) http.Response { - if _, err := tools.Exec("systemctl reload fail2ban"); err != nil { + if err := tools.ServiceReload("fail2ban"); err != nil { return controllers.Error(ctx, http.StatusInternalServerError, "重载配置失败") } diff --git a/app/http/controllers/safe_controller.go b/app/http/controllers/safe_controller.go index d11c4d21..0a611dff 100644 --- a/app/http/controllers/safe_controller.go +++ b/app/http/controllers/safe_controller.go @@ -28,24 +28,23 @@ func (r *SafeController) GetFirewallStatus(ctx http.Context) http.Response { // SetFirewallStatus 设置防火墙状态 func (r *SafeController) SetFirewallStatus(ctx http.Context) http.Response { - var out string var err error if ctx.Request().InputBool("status") { if tools.IsRHEL() { - out, err = tools.Exec("systemctl start firewalld") + err = tools.ServiceStart("firewalld") } else { - out, err = tools.Exec("echo y | ufw enable") + _, err = tools.Exec("echo y | ufw enable") } } else { if tools.IsRHEL() { - out, err = tools.Exec("systemctl stop firewalld") + err = tools.ServiceStop("firewalld") } else { - out, err = tools.Exec("ufw disable") + _, err = tools.Exec("ufw disable") } } if err != nil { - return Error(ctx, http.StatusInternalServerError, out) + return Error(ctx, http.StatusInternalServerError, err.Error()) } return Success(ctx, nil) @@ -213,27 +212,11 @@ func (r *SafeController) DeleteFirewallRule(ctx http.Context) http.Response { // firewallStatus 获取防火墙状态 func (r *SafeController) firewallStatus() bool { - var out string - var err error var running bool if tools.IsRHEL() { - out, err = tools.Exec("systemctl status firewalld | grep Active | awk '{print $3}'") - if out == "(running)" { - running = true - } else { - running = false - } + running, _ = tools.ServiceStatus("firewalld") } else { - out, err = tools.Exec("ufw status | grep Status | awk '{print $2}'") - if out == "active" { - running = true - } else { - running = false - } - } - - if err != nil { - return false + running, _ = tools.ServiceStatus("ufw") } return running @@ -241,53 +224,53 @@ func (r *SafeController) firewallStatus() bool { // GetSshStatus 获取 SSH 状态 func (r *SafeController) GetSshStatus(ctx http.Context) http.Response { - var out string + var running bool var err error if tools.IsRHEL() { - out, err = tools.Exec("systemctl status sshd | grep Active | awk '{print $3}'") + running, err = tools.ServiceStatus("sshd") } else { - out, err = tools.Exec("systemctl status ssh | grep Active | awk '{print $3}'") + running, err = tools.ServiceStatus("ssh") } if err != nil { - return Error(ctx, http.StatusInternalServerError, out) + return Error(ctx, http.StatusInternalServerError, err.Error()) } - return Success(ctx, out == "(running)") + return Success(ctx, running) } // SetSshStatus 设置 SSH 状态 func (r *SafeController) SetSshStatus(ctx http.Context) http.Response { if ctx.Request().InputBool("status") { if tools.IsRHEL() { - if out, err := tools.Exec("systemctl enable sshd"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceEnable("sshd"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } - if out, err := tools.Exec("systemctl start sshd"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceStart("sshd"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } } else { - if out, err := tools.Exec("systemctl enable ssh"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceEnable("ssh"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } - if out, err := tools.Exec("systemctl start ssh"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceStart("ssh"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } } } else { if tools.IsRHEL() { - if out, err := tools.Exec("systemctl stop sshd"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceStop("sshd"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } - if out, err := tools.Exec("systemctl disable sshd"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceDisable("sshd"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } } else { - if out, err := tools.Exec("systemctl stop ssh"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceStop("ssh"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } - if out, err := tools.Exec("systemctl disable ssh"); err != nil { - return Error(ctx, http.StatusInternalServerError, out) + if err := tools.ServiceDisable("ssh"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } } } @@ -319,12 +302,19 @@ func (r *SafeController) SetSshPort(ctx http.Context) http.Response { _, _ = tools.Exec("sed -i 's/#Port " + oldPort + "/Port " + cast.ToString(port) + "/g' /etc/ssh/sshd_config") _, _ = tools.Exec("sed -i 's/Port " + oldPort + "/Port " + cast.ToString(port) + "/g' /etc/ssh/sshd_config") - out, err := tools.Exec("systemctl status sshd | grep Active | awk '{print $3}'") - if err != nil || out != "(running)" { - Error(ctx, http.StatusInternalServerError, out) + var sshName string + if tools.IsRHEL() { + sshName = "sshd" + } else { + sshName = "ssh" } - _, _ = tools.Exec("systemctl restart sshd") + status, _ := tools.ServiceStatus(sshName) + if !status { + Error(ctx, http.StatusInternalServerError, "SSH 服务未运行") + } + + _ = tools.ServiceRestart(sshName) return Success(ctx, nil) } diff --git a/app/http/controllers/website_controller.go b/app/http/controllers/website_controller.go index a43b331f..4b6d6146 100644 --- a/app/http/controllers/website_controller.go +++ b/app/http/controllers/website_controller.go @@ -606,8 +606,8 @@ server if err := tools.Write("/www/server/vhost/rewrite"+website.Name+".conf", "", 0644); err != nil { return nil } - if exec, err := tools.Exec("systemctl reload openresty"); err != nil { - return Error(ctx, http.StatusInternalServerError, exec) + if err := tools.ServiceReload("openresty"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } return Success(ctx, nil) @@ -670,11 +670,11 @@ func (r *WebsiteController) Status(ctx http.Context) http.Response { } } - if err := tools.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644); err != nil { + if err = tools.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644); err != nil { return ErrorSystem(ctx) } - if exec, err := tools.Exec("systemctl reload openresty"); err != nil { - return Error(ctx, http.StatusInternalServerError, exec) + if err = tools.ServiceReload("openresty"); err != nil { + return Error(ctx, http.StatusInternalServerError, err.Error()) } return Success(ctx, nil) diff --git a/internal/services/website.go b/internal/services/website.go index 081c8fd5..645cf862 100644 --- a/internal/services/website.go +++ b/internal/services/website.go @@ -224,7 +224,7 @@ server return models.Website{}, err } - if _, err := tools.Exec("systemctl reload openresty"); err != nil { + if err := tools.ServiceReload("openresty"); err != nil { return models.Website{}, err } @@ -265,10 +265,10 @@ func (r *WebsiteImpl) SaveConfig(config requests.SaveConfig) error { return err } if strings.TrimSpace(raw) != strings.TrimSpace(config.Raw) { - if err := tools.Write("/www/server/vhost/"+website.Name+".conf", config.Raw, 0644); err != nil { + if err = tools.Write("/www/server/vhost/"+website.Name+".conf", config.Raw, 0644); err != nil { return err } - if _, err := tools.Exec("systemctl reload openresty"); err != nil { + if err = tools.ServiceReload("openresty"); err != nil { return err }