Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 8, 2024
1 parent b51a8e1 commit 3101486
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 62 deletions.
8 changes: 4 additions & 4 deletions app/http/controllers/info_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion app/http/controllers/plugins/fail2ban_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "重载配置失败")
}

Expand Down
88 changes: 39 additions & 49 deletions app/http/controllers/safe_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -213,81 +212,65 @@ 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
}

// 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())
}
}
}
Expand Down Expand Up @@ -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"
}

status, _ := tools.ServiceStatus(sshName)
if !status {
Error(ctx, http.StatusInternalServerError, "SSH 服务未运行")
}

_, _ = tools.Exec("systemctl restart sshd")
_ = tools.ServiceRestart(sshName)
return Success(ctx, nil)
}

Expand Down
10 changes: 5 additions & 5 deletions app/http/controllers/website_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/services/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3101486

Please sign in to comment.