From de1c269a4a191097dbcfea2de0da40afba36b409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Mon, 14 Aug 2023 22:26:42 +0800 Subject: [PATCH] refactor: tools --- app/console/commands/panel.go | 38 ++++---- app/http/controllers/cron_controller.go | 14 +-- app/http/controllers/info_controller.go | 2 +- .../plugins/fail2ban/fail2ban_controller.go | 58 +++++------ .../plugins/mysql57/mysql57_controller.go | 74 +++++++------- .../plugins/mysql80/mysql80_controller.go | 74 +++++++------- .../plugins/openresty/openresty_controller.go | 30 +++--- .../plugins/php74/php74_controller.go | 32 +++---- .../plugins/php80/php80_controller.go | 32 +++---- .../plugins/php81/php81_controller.go | 32 +++---- .../plugins/php82/php82_controller.go | 32 +++---- .../phpmyadmin/phpmyadmin_controller.go | 16 ++-- .../postgresql15/postgresql15_controller.go | 72 +++++++------- .../plugins/pureftpd/pureftpd_controller.go | 46 ++++----- .../plugins/redis/redis_controller.go | 26 ++--- .../plugins/s3fs/s3fs_controller.go | 22 ++--- .../supervisor/supervisor_controller.go | 96 +++++++++---------- app/http/controllers/safe_controller.go | 84 ++++++++-------- app/http/controllers/setting_controller.go | 6 +- app/http/controllers/task_controller.go | 2 +- app/http/controllers/website_controller.go | 44 ++++----- app/services/backup.go | 46 ++++----- app/services/cron.go | 16 ++-- app/services/plugin.go | 12 +++ app/services/website.go | 44 ++++----- pkg/ssh/ssh.go | 2 +- pkg/tools/system.go | 47 +++++---- pkg/tools/system_test.go | 52 +++++++--- pkg/tools/tools.go | 22 ++--- 29 files changed, 556 insertions(+), 517 deletions(-) diff --git a/app/console/commands/panel.go b/app/console/commands/panel.go index 95b12e35..532404f7 100644 --- a/app/console/commands/panel.go +++ b/app/console/commands/panel.go @@ -123,7 +123,7 @@ func (receiver *Panel) Handle(ctx console.Context) error { return nil } - port := tools.ExecShell(`cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}' | tr -d '\n'`) + port := tools.Exec(`cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}' | tr -d '\n'`) color.Greenln("用户名: " + user.Username) color.Greenln("密码: " + password) @@ -131,7 +131,7 @@ func (receiver *Panel) Handle(ctx console.Context) error { color.Greenln("面板入口: " + services.NewSettingImpl().Get(models.SettingKeyEntrance, "/")) case "getPort": - port := tools.ExecShell("cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}'") + port := tools.Exec("cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}'") color.Greenln("面板端口: " + port) case "getEntrance": @@ -243,7 +243,7 @@ func (receiver *Panel) Handle(ctx console.Context) error { } backupFile := path + "/" + website.Name + "_" + carbon.Now().ToShortDateTimeString() + ".zip" - tools.ExecShell(`cd '` + website.Path + `' && zip -r '` + backupFile + `' .`) + tools.Exec(`cd '` + website.Path + `' && zip -r '` + backupFile + `' .`) color.Greenln("|-备份成功") case "mysql": @@ -259,21 +259,24 @@ func (receiver *Panel) Handle(ctx console.Context) error { color.Greenln("|-目标MySQL数据库: " + name) color.Greenln("|-开始导出") - tools.ExecShell(`mysqldump -uroot ` + name + ` > /tmp/` + backupFile + ` 2>&1`) + tools.Exec(`mysqldump -uroot ` + name + ` > /tmp/` + backupFile + ` 2>&1`) color.Greenln("|-导出成功") color.Greenln("|-开始压缩") - tools.ExecShell("cd /tmp && zip -r " + backupFile + ".zip " + backupFile) - tools.RemoveFile("/tmp/" + backupFile) + tools.Exec("cd /tmp && zip -r " + backupFile + ".zip " + backupFile) + tools.Remove("/tmp/" + backupFile) color.Greenln("|-压缩成功") color.Greenln("|-开始移动") - tools.Mv("/tmp/"+backupFile+".zip", path+"/"+backupFile+".zip") + if _, err := tools.Mv("/tmp/"+backupFile+".zip", path+"/"+backupFile+".zip"); err != nil { + color.Redln("|-移动失败: " + err.Error()) + return nil + } color.Greenln("|-移动成功") _ = os.Unsetenv("MYSQL_PWD") color.Greenln("|-备份成功") case "postgresql": backupFile := name + "_" + carbon.Now().ToShortDateTimeString() + ".sql" - check := tools.ExecShell(`su - postgres -c "psql -l" 2>&1`) + check := tools.Exec(`su - postgres -c "psql -l" 2>&1`) if strings.Contains(check, name) { color.Redln("|-数据库不存在") color.Greenln(hr) @@ -282,14 +285,17 @@ func (receiver *Panel) Handle(ctx console.Context) error { color.Greenln("|-目标PostgreSQL数据库: " + name) color.Greenln("|-开始导出") - tools.ExecShell(`su - postgres -c "pg_dump '` + name + `'" > /tmp/` + backupFile + ` 2>&1`) + tools.Exec(`su - postgres -c "pg_dump '` + name + `'" > /tmp/` + backupFile + ` 2>&1`) color.Greenln("|-导出成功") color.Greenln("|-开始压缩") - tools.ExecShell("cd /tmp && zip -r " + backupFile + ".zip " + backupFile) - tools.RemoveFile("/tmp/" + backupFile) + tools.Exec("cd /tmp && zip -r " + backupFile + ".zip " + backupFile) + tools.Remove("/tmp/" + backupFile) color.Greenln("|-压缩成功") color.Greenln("|-开始移动") - tools.Mv("/tmp/"+backupFile+".zip", path+"/"+backupFile+".zip") + if _, err := tools.Mv("/tmp/"+backupFile+".zip", path+"/"+backupFile+".zip"); err != nil { + color.Redln("|-移动失败: " + err.Error()) + return nil + } color.Greenln("|-移动成功") color.Greenln("|-备份成功") } @@ -316,7 +322,7 @@ func (receiver *Panel) Handle(ctx console.Context) error { for i := cast.ToInt(save); i < len(filteredFiles); i++ { fileToDelete := filepath.Join(path, filteredFiles[i].Name()) color.Yellowln("|-清理备份: " + fileToDelete) - tools.RemoveFile(fileToDelete) + tools.Remove(fileToDelete) } color.Greenln("|-清理完成") color.Greenln(hr) @@ -352,8 +358,8 @@ func (receiver *Panel) Handle(ctx console.Context) error { } backupPath := "/www/wwwlogs/" + website.Name + "_" + carbon.Now().ToShortDateTimeString() + ".log.zip" - tools.ExecShell(`cd /www/wwwlogs && zip -r ` + backupPath + ` ` + website.Name + ".log") - tools.ExecShell(`echo "" > ` + logPath) + tools.Exec(`cd /www/wwwlogs && zip -r ` + backupPath + ` ` + website.Name + ".log") + tools.Exec(`echo "" > ` + logPath) color.Greenln("|-切割成功") color.Greenln(hr) @@ -378,7 +384,7 @@ func (receiver *Panel) Handle(ctx console.Context) error { for i := cast.ToInt(save); i < len(filteredFiles); i++ { fileToDelete := filepath.Join("/www/wwwlogs", filteredFiles[i].Name()) color.Yellowln("|-清理日志: " + fileToDelete) - tools.RemoveFile(fileToDelete) + tools.Remove(fileToDelete) } color.Greenln("|-清理完成") color.Greenln(hr) diff --git a/app/http/controllers/cron_controller.go b/app/http/controllers/cron_controller.go index baf28bb1..50548704 100644 --- a/app/http/controllers/cron_controller.go +++ b/app/http/controllers/cron_controller.go @@ -123,12 +123,12 @@ panel cutoff ${name} ${save} 2>&1 return } shellFile := strconv.Itoa(int(carbon.Now().Timestamp())) + tools.RandomString(16) - if !tools.WriteFile(shellDir+shellFile+".sh", shell, 0700) { + if !tools.Write(shellDir+shellFile+".sh", shell, 0700) { facades.Log().Error("[面板][CronController] 创建计划任务脚本失败 ", err) Error(ctx, http.StatusInternalServerError, "系统内部错误") return } - tools.ExecShell("dos2unix " + shellDir + shellFile + ".sh") + tools.Exec("dos2unix " + shellDir + shellFile + ".sh") var cron models.Cron cron.Name = ctx.Request().Input("name") @@ -161,7 +161,7 @@ func (c *CronController) Script(ctx http.Context) { return } - Success(ctx, tools.ReadFile(cron.Shell)) + Success(ctx, tools.Read(cron.Shell)) } func (c *CronController) Update(ctx http.Context) { @@ -206,12 +206,12 @@ func (c *CronController) Update(ctx http.Context) { return } - if !tools.WriteFile(cron.Shell, ctx.Request().Input("script"), 0644) { + if !tools.Write(cron.Shell, ctx.Request().Input("script"), 0644) { facades.Log().Error("[面板][CronController] 更新计划任务脚本失败 ", err) Error(ctx, http.StatusInternalServerError, "系统内部错误") return } - tools.ExecShell("dos2unix " + cron.Shell) + tools.Exec("dos2unix " + cron.Shell) c.cron.DeleteFromSystem(cron) if cron.Status { @@ -230,7 +230,7 @@ func (c *CronController) Delete(ctx http.Context) { } c.cron.DeleteFromSystem(cron) - tools.RemoveFile(cron.Shell) + tools.Remove(cron.Shell) _, err = facades.Orm().Query().Delete(&cron) if err != nil { @@ -291,5 +291,5 @@ func (c *CronController) Log(ctx http.Context) { return } - Success(ctx, tools.ReadFile(cron.Log)) + Success(ctx, tools.Read(cron.Log)) } diff --git a/app/http/controllers/info_controller.go b/app/http/controllers/info_controller.go index 627a0b8f..49e05087 100644 --- a/app/http/controllers/info_controller.go +++ b/app/http/controllers/info_controller.go @@ -196,6 +196,6 @@ func (c *InfoController) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart panel") + tools.Exec("systemctl restart panel") Success(ctx, nil) } diff --git a/app/http/controllers/plugins/fail2ban/fail2ban_controller.go b/app/http/controllers/plugins/fail2ban/fail2ban_controller.go index 080c08b5..e78e9e1b 100644 --- a/app/http/controllers/plugins/fail2ban/fail2ban_controller.go +++ b/app/http/controllers/plugins/fail2ban/fail2ban_controller.go @@ -39,7 +39,7 @@ func (c *Fail2banController) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败") return @@ -58,8 +58,8 @@ func (c *Fail2banController) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload fail2ban") - status := tools.ExecShell("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload fail2ban") + status := tools.Exec("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败") return @@ -78,8 +78,8 @@ func (c *Fail2banController) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart fail2ban") - status := tools.ExecShell("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart fail2ban") + status := tools.Exec("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败") return @@ -98,8 +98,8 @@ func (c *Fail2banController) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start fail2ban") - status := tools.ExecShell("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start fail2ban") + status := tools.Exec("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败") return @@ -118,8 +118,8 @@ func (c *Fail2banController) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop fail2ban") - status := tools.ExecShell("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop fail2ban") + status := tools.Exec("systemctl status fail2ban | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取服务运行状态失败") return @@ -140,7 +140,7 @@ func (c *Fail2banController) List(ctx http.Context) { page := ctx.Request().QueryInt("page", 1) limit := ctx.Request().QueryInt("limit", 10) - raw := tools.ReadFile("/etc/fail2ban/jail.local") + raw := tools.Read("/etc/fail2ban/jail.local") if len(raw) == 0 { controllers.Error(ctx, http.StatusBadRequest, "Fail2ban 规则为空") return @@ -231,7 +231,7 @@ func (c *Fail2banController) Add(ctx http.Context) { jailWebsiteMode := ctx.Request().Input("website_mode") jailWebsitePath := ctx.Request().Input("website_path") - raw := tools.ReadFile("/etc/fail2ban/jail.local") + raw := tools.Read("/etc/fail2ban/jail.local") if strings.Contains(raw, "["+jailName+"]") || (strings.Contains(raw, "["+jailName+"]"+"-cc") && jailWebsiteMode == "cc") || (strings.Contains(raw, "["+jailName+"]"+"-path") && jailWebsiteMode == "path") { controllers.Error(ctx, http.StatusUnprocessableEntity, "规则已存在") return @@ -273,7 +273,7 @@ logpath = /www/wwwlogs/` + website.Name + `.log # ` + jailName + `-` + jailWebsiteMode + `-END ` raw += rule - tools.WriteFile("/etc/fail2ban/jail.local", raw, 0644) + tools.Write("/etc/fail2ban/jail.local", raw, 0644) var filter string if jailWebsiteMode == "cc" { @@ -289,7 +289,7 @@ failregex = ^\s-.*\s` + jailWebsitePath + `.*HTTP/.*$ ignoreregex = ` } - tools.WriteFile("/etc/fail2ban/filter.d/haozi-"+jailName+"-"+jailWebsiteMode+".conf", filter, 0644) + tools.Write("/etc/fail2ban/filter.d/haozi-"+jailName+"-"+jailWebsiteMode+".conf", filter, 0644) case "service": var logPath string @@ -303,15 +303,15 @@ ignoreregex = logPath = "/var/log/secure" } filter = "sshd" - port = tools.ExecShell("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'") + port = tools.Exec("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'") case "mysql": logPath = "/www/server/mysql/mysql-error.log" filter = "mysqld-auth" - port = tools.ExecShell("cat /www/server/mysql/conf/my.cnf | grep 'port' | head -n 1 | awk '{print $3}'") + port = tools.Exec("cat /www/server/mysql/conf/my.cnf | grep 'port' | head -n 1 | awk '{print $3}'") case "pure-ftpd": logPath = "/var/log/messages" filter = "pure-ftpd" - port = tools.ExecShell(`cat /www/server/pure-ftpd/etc/pure-ftpd.conf | grep "Bind" | awk '{print $2}' | awk -F "," '{print $2}'`) + port = tools.Exec(`cat /www/server/pure-ftpd/etc/pure-ftpd.conf | grep "Bind" | awk '{print $2}' | awk -F "," '{print $2}'`) default: controllers.Error(ctx, http.StatusUnprocessableEntity, "未知服务") return @@ -335,10 +335,10 @@ logpath = ` + logPath + ` # ` + jailName + `-END ` raw += rule - tools.WriteFile("/etc/fail2ban/jail.local", raw, 0644) + tools.Write("/etc/fail2ban/jail.local", raw, 0644) } - tools.ExecShell("fail2ban-client reload") + tools.Exec("fail2ban-client reload") controllers.Success(ctx, nil) } @@ -349,7 +349,7 @@ func (c *Fail2banController) Delete(ctx http.Context) { } jailName := ctx.Request().Input("name") - raw := tools.ReadFile("/etc/fail2ban/jail.local") + raw := tools.Read("/etc/fail2ban/jail.local") if !strings.Contains(raw, "["+jailName+"]") { controllers.Error(ctx, http.StatusUnprocessableEntity, "规则不存在") return @@ -358,9 +358,9 @@ func (c *Fail2banController) Delete(ctx http.Context) { rule := tools.Cut(raw, "# "+jailName+"-START", "# "+jailName+"-END") raw = strings.Replace(raw, "\n# "+jailName+"-START"+rule+"# "+jailName+"-END", "", -1) raw = strings.TrimSpace(raw) - tools.WriteFile("/etc/fail2ban/jail.local", raw, 0644) + tools.Write("/etc/fail2ban/jail.local", raw, 0644) - tools.ExecShell("fail2ban-client reload") + tools.Exec("fail2ban-client reload") controllers.Success(ctx, nil) } @@ -376,9 +376,9 @@ func (c *Fail2banController) BanList(ctx http.Context) { return } - currentlyBan := tools.ExecShell(`fail2ban-client status ` + name + ` | grep "Currently banned" | awk '{print $4}'`) - totalBan := tools.ExecShell(`fail2ban-client status ` + name + ` | grep "Total banned" | awk '{print $4}'`) - bannedIp := tools.ExecShell(`fail2ban-client status ` + name + ` | grep "Banned IP list" | awk -F ":" '{print $2}'`) + currentlyBan := tools.Exec(`fail2ban-client status ` + name + ` | grep "Currently banned" | awk '{print $4}'`) + totalBan := tools.Exec(`fail2ban-client status ` + name + ` | grep "Total banned" | awk '{print $4}'`) + bannedIp := tools.Exec(`fail2ban-client status ` + name + ` | grep "Banned IP list" | awk -F ":" '{print $2}'`) bannedIpList := strings.Split(bannedIp, " ") var list []map[string]string @@ -411,7 +411,7 @@ func (c *Fail2banController) Unban(ctx http.Context) { return } - tools.ExecShell("fail2ban-client set " + name + " unbanip " + ip) + tools.Exec("fail2ban-client set " + name + " unbanip " + ip) controllers.Success(ctx, nil) } @@ -427,7 +427,7 @@ func (c *Fail2banController) SetWhiteList(ctx http.Context) { return } - raw := tools.ReadFile("/etc/fail2ban/jail.local") + raw := tools.Read("/etc/fail2ban/jail.local") // 正则替换 reg := regexp.MustCompile(`ignoreip\s*=\s*.*\n`) if reg.MatchString(raw) { @@ -437,8 +437,8 @@ func (c *Fail2banController) SetWhiteList(ctx http.Context) { return } - tools.WriteFile("/etc/fail2ban/jail.local", raw, 0644) - tools.ExecShell("fail2ban-client reload") + tools.Write("/etc/fail2ban/jail.local", raw, 0644) + tools.Exec("fail2ban-client reload") controllers.Success(ctx, nil) } @@ -448,7 +448,7 @@ func (c *Fail2banController) GetWhiteList(ctx http.Context) { return } - raw := tools.ReadFile("/etc/fail2ban/jail.local") + raw := tools.Read("/etc/fail2ban/jail.local") reg := regexp.MustCompile(`ignoreip\s*=\s*(.*)\n`) if reg.MatchString(raw) { ignoreIp := reg.FindStringSubmatch(raw)[1] diff --git a/app/http/controllers/plugins/mysql57/mysql57_controller.go b/app/http/controllers/plugins/mysql57/mysql57_controller.go index 5c56c82e..2a080dfc 100644 --- a/app/http/controllers/plugins/mysql57/mysql57_controller.go +++ b/app/http/controllers/plugins/mysql57/mysql57_controller.go @@ -34,7 +34,7 @@ func (c *Mysql57Controller) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -53,8 +53,8 @@ func (c *Mysql57Controller) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -73,8 +73,8 @@ func (c *Mysql57Controller) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -93,8 +93,8 @@ func (c *Mysql57Controller) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -113,8 +113,8 @@ func (c *Mysql57Controller) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -134,7 +134,7 @@ func (c *Mysql57Controller) GetConfig(ctx http.Context) { } // 获取配置 - config := tools.ReadFile("/www/server/mysql/conf/my.cnf") + config := tools.Read("/www/server/mysql/conf/my.cnf") if len(config) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL配置失败") return @@ -155,7 +155,7 @@ func (c *Mysql57Controller) SaveConfig(ctx http.Context) { return } - if !tools.WriteFile("/www/server/mysql/conf/my.cnf", config, 0644) { + if !tools.Write("/www/server/mysql/conf/my.cnf", config, 0644) { controllers.Error(ctx, http.StatusInternalServerError, "写入MySQL配置失败") return } @@ -175,13 +175,13 @@ func (c *Mysql57Controller) Load(ctx http.Context) { return } - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if status != "active" { controllers.Error(ctx, http.StatusInternalServerError, "MySQL 已停止运行") return } - raw := tools.ExecShell("/www/server/mysql/bin/mysqladmin -uroot -p" + rootPassword + " extended-status 2>&1") + raw := tools.Exec("/www/server/mysql/bin/mysqladmin -uroot -p" + rootPassword + " extended-status 2>&1") if strings.Contains(raw, "Access denied for user") { controllers.Error(ctx, http.StatusBadRequest, "MySQL root密码错误") return @@ -247,7 +247,7 @@ func (c *Mysql57Controller) ErrorLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/mysql/mysql-error.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/mysql/mysql-error.log")) controllers.Success(ctx, log) } @@ -257,7 +257,7 @@ func (c *Mysql57Controller) ClearErrorLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/mysql/mysql-error.log") + tools.Exec("echo '' > /www/server/mysql/mysql-error.log") controllers.Success(ctx, "清空错误日志成功") } @@ -267,7 +267,7 @@ func (c *Mysql57Controller) SlowLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/mysql/mysql-slow.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/mysql/mysql-slow.log")) controllers.Success(ctx, log) } @@ -277,7 +277,7 @@ func (c *Mysql57Controller) ClearSlowLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/mysql/mysql-slow.log") + tools.Exec("echo '' > /www/server/mysql/mysql-slow.log") controllers.Success(ctx, "清空慢查询日志成功") } @@ -302,7 +302,7 @@ func (c *Mysql57Controller) SetRootPassword(ctx http.Context) { return } - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -320,12 +320,12 @@ func (c *Mysql57Controller) SetRootPassword(ctx http.Context) { oldRootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) if oldRootPassword != rootPassword { - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + rootPassword + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + rootPassword + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"FLUSH PRIVILEGES;\"") err := c.setting.Set(models.SettingKeyMysqlRootPassword, rootPassword) if err != nil { - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + oldRootPassword + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + oldRootPassword + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Error(ctx, http.StatusInternalServerError, "设置root密码失败") return } @@ -425,10 +425,10 @@ func (c *Mysql57Controller) AddDatabase(ctx http.Context) { user := ctx.Request().Input("user") password := ctx.Request().Input("password") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE DATABASE IF NOT EXISTS " + database + " DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE DATABASE IF NOT EXISTS " + database + " DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "添加数据库成功") } @@ -453,7 +453,7 @@ func (c *Mysql57Controller) DeleteDatabase(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) database := ctx.Request().Input("database") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP DATABASE IF EXISTS " + database + ";\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP DATABASE IF EXISTS " + database + ";\"") controllers.Success(ctx, "删除数据库成功") } @@ -550,7 +550,7 @@ func (c *Mysql57Controller) DeleteBackup(ctx http.Context) { backupPath := c.setting.Get(models.SettingKeyBackupPath) + "/mysql" fileName := ctx.Request().Input("name") - tools.RemoveFile(backupPath + "/" + fileName) + tools.Remove(backupPath + "/" + fileName) controllers.Success(ctx, "删除备份成功") } @@ -695,9 +695,9 @@ func (c *Mysql57Controller) AddUser(ctx http.Context) { user := ctx.Request().Input("user") password := ctx.Request().Input("password") database := ctx.Request().Input("database") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + ";'\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + ";'\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "添加成功") } @@ -722,7 +722,7 @@ func (c *Mysql57Controller) DeleteUser(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) user := ctx.Request().Input("user") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP USER '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP USER '" + user + "'@'localhost';\"") controllers.Success(ctx, "删除成功") } @@ -749,8 +749,8 @@ func (c *Mysql57Controller) SetUserPassword(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) user := ctx.Request().Input("user") password := ctx.Request().Input("password") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "修改成功") } @@ -777,9 +777,9 @@ func (c *Mysql57Controller) SetUserPrivileges(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) user := ctx.Request().Input("user") database := ctx.Request().Input("database") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"REVOKE ALL PRIVILEGES ON *.* FROM '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"REVOKE ALL PRIVILEGES ON *.* FROM '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "修改成功") } diff --git a/app/http/controllers/plugins/mysql80/mysql80_controller.go b/app/http/controllers/plugins/mysql80/mysql80_controller.go index 30cc79cd..54feb9c2 100644 --- a/app/http/controllers/plugins/mysql80/mysql80_controller.go +++ b/app/http/controllers/plugins/mysql80/mysql80_controller.go @@ -34,7 +34,7 @@ func (c *Mysql80Controller) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -53,8 +53,8 @@ func (c *Mysql80Controller) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -73,8 +73,8 @@ func (c *Mysql80Controller) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -93,8 +93,8 @@ func (c *Mysql80Controller) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -113,8 +113,8 @@ func (c *Mysql80Controller) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop mysqld") - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop mysqld") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -134,7 +134,7 @@ func (c *Mysql80Controller) GetConfig(ctx http.Context) { } // 获取配置 - config := tools.ReadFile("/www/server/mysql/conf/my.cnf") + config := tools.Read("/www/server/mysql/conf/my.cnf") if len(config) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL配置失败") return @@ -155,7 +155,7 @@ func (c *Mysql80Controller) SaveConfig(ctx http.Context) { return } - if !tools.WriteFile("/www/server/mysql/conf/my.cnf", config, 0644) { + if !tools.Write("/www/server/mysql/conf/my.cnf", config, 0644) { controllers.Error(ctx, http.StatusInternalServerError, "写入MySQL配置失败") return } @@ -175,13 +175,13 @@ func (c *Mysql80Controller) Load(ctx http.Context) { return } - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if status != "active" { controllers.Error(ctx, http.StatusInternalServerError, "MySQL 已停止运行") return } - raw := tools.ExecShell("/www/server/mysql/bin/mysqladmin -uroot -p" + rootPassword + " extended-status 2>&1") + raw := tools.Exec("/www/server/mysql/bin/mysqladmin -uroot -p" + rootPassword + " extended-status 2>&1") if strings.Contains(raw, "Access denied for user") { controllers.Error(ctx, http.StatusBadRequest, "MySQL root密码错误") return @@ -247,7 +247,7 @@ func (c *Mysql80Controller) ErrorLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/mysql/mysql-error.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/mysql/mysql-error.log")) controllers.Success(ctx, log) } @@ -257,7 +257,7 @@ func (c *Mysql80Controller) ClearErrorLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/mysql/mysql-error.log") + tools.Exec("echo '' > /www/server/mysql/mysql-error.log") controllers.Success(ctx, "清空错误日志成功") } @@ -267,7 +267,7 @@ func (c *Mysql80Controller) SlowLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/mysql/mysql-slow.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/mysql/mysql-slow.log")) controllers.Success(ctx, log) } @@ -277,7 +277,7 @@ func (c *Mysql80Controller) ClearSlowLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/mysql/mysql-slow.log") + tools.Exec("echo '' > /www/server/mysql/mysql-slow.log") controllers.Success(ctx, "清空慢查询日志成功") } @@ -302,7 +302,7 @@ func (c *Mysql80Controller) SetRootPassword(ctx http.Context) { return } - status := tools.ExecShell("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status mysqld | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL状态失败") return @@ -320,12 +320,12 @@ func (c *Mysql80Controller) SetRootPassword(ctx http.Context) { oldRootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) if oldRootPassword != rootPassword { - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + rootPassword + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + rootPassword + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + oldRootPassword + " -e \"FLUSH PRIVILEGES;\"") err := c.setting.Set(models.SettingKeyMysqlRootPassword, rootPassword) if err != nil { - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + oldRootPassword + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER 'root'@'localhost' IDENTIFIED BY '" + oldRootPassword + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Error(ctx, http.StatusInternalServerError, "设置root密码失败") return } @@ -425,10 +425,10 @@ func (c *Mysql80Controller) AddDatabase(ctx http.Context) { user := ctx.Request().Input("user") password := ctx.Request().Input("password") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE DATABASE IF NOT EXISTS " + database + " DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE DATABASE IF NOT EXISTS " + database + " DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "添加数据库成功") } @@ -453,7 +453,7 @@ func (c *Mysql80Controller) DeleteDatabase(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) database := ctx.Request().Input("database") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP DATABASE IF EXISTS " + database + ";\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP DATABASE IF EXISTS " + database + ";\"") controllers.Success(ctx, "删除数据库成功") } @@ -550,7 +550,7 @@ func (c *Mysql80Controller) DeleteBackup(ctx http.Context) { backupPath := c.setting.Get(models.SettingKeyBackupPath) + "/mysql" fileName := ctx.Request().Input("name") - tools.RemoveFile(backupPath + "/" + fileName) + tools.Remove(backupPath + "/" + fileName) controllers.Success(ctx, "删除备份成功") } @@ -695,9 +695,9 @@ func (c *Mysql80Controller) AddUser(ctx http.Context) { user := ctx.Request().Input("user") password := ctx.Request().Input("password") database := ctx.Request().Input("database") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + ";'\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"CREATE USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + ";'\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "添加成功") } @@ -722,7 +722,7 @@ func (c *Mysql80Controller) DeleteUser(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) user := ctx.Request().Input("user") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP USER '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"DROP USER '" + user + "'@'localhost';\"") controllers.Success(ctx, "删除成功") } @@ -749,8 +749,8 @@ func (c *Mysql80Controller) SetUserPassword(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) user := ctx.Request().Input("user") password := ctx.Request().Input("password") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"ALTER USER '" + user + "'@'localhost' IDENTIFIED BY '" + password + "';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "修改成功") } @@ -777,9 +777,9 @@ func (c *Mysql80Controller) SetUserPrivileges(ctx http.Context) { rootPassword := c.setting.Get(models.SettingKeyMysqlRootPassword) user := ctx.Request().Input("user") database := ctx.Request().Input("database") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"REVOKE ALL PRIVILEGES ON *.* FROM '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") - tools.ExecShell("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"REVOKE ALL PRIVILEGES ON *.* FROM '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"GRANT ALL PRIVILEGES ON " + database + ".* TO '" + user + "'@'localhost';\"") + tools.Exec("/www/server/mysql/bin/mysql -uroot -p" + rootPassword + " -e \"FLUSH PRIVILEGES;\"") controllers.Success(ctx, "修改成功") } diff --git a/app/http/controllers/plugins/openresty/openresty_controller.go b/app/http/controllers/plugins/openresty/openresty_controller.go index 802ee711..3e5e1042 100644 --- a/app/http/controllers/plugins/openresty/openresty_controller.go +++ b/app/http/controllers/plugins/openresty/openresty_controller.go @@ -29,7 +29,7 @@ func (c *OpenRestyController) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty状态失败") return @@ -48,8 +48,8 @@ func (c *OpenRestyController) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload openresty") - status := tools.ExecShell("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload openresty") + status := tools.Exec("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty状态失败") return @@ -68,8 +68,8 @@ func (c *OpenRestyController) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start openresty") - status := tools.ExecShell("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start openresty") + status := tools.Exec("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty状态失败") return @@ -88,8 +88,8 @@ func (c *OpenRestyController) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop openresty") - status := tools.ExecShell("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop openresty") + status := tools.Exec("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty状态失败") return @@ -108,8 +108,8 @@ func (c *OpenRestyController) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart openresty") - status := tools.ExecShell("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart openresty") + status := tools.Exec("systemctl status openresty | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty状态失败") return @@ -128,7 +128,7 @@ func (c *OpenRestyController) GetConfig(ctx http.Context) { return } - config := tools.ReadFile("/www/server/openresty/conf/nginx.conf") + config := tools.Read("/www/server/openresty/conf/nginx.conf") if len(config) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty配置失败") return @@ -149,7 +149,7 @@ func (c *OpenRestyController) SaveConfig(ctx http.Context) { return } - if !tools.WriteFile("/www/server/openresty/conf/nginx.conf", config, 0644) { + if !tools.Write("/www/server/openresty/conf/nginx.conf", config, 0644) { controllers.Error(ctx, http.StatusInternalServerError, "保存OpenResty配置失败") return } @@ -168,7 +168,7 @@ func (c *OpenRestyController) ErrorLog(ctx http.Context) { return } - out := tools.ExecShell("tail -n 100 /www/wwwlogs/nginx_error.log") + out := tools.Exec("tail -n 100 /www/wwwlogs/nginx_error.log") controllers.Success(ctx, out) } @@ -178,7 +178,7 @@ func (c *OpenRestyController) ClearErrorLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/wwwlogs/nginx_error.log") + tools.Exec("echo '' > /www/wwwlogs/nginx_error.log") controllers.Success(ctx, "清空OpenResty错误日志成功") } @@ -203,13 +203,13 @@ func (c *OpenRestyController) Load(ctx http.Context) { } var data []nginxStatus - workers := tools.ExecShell("ps aux | grep nginx | grep 'worker process' | wc -l") + workers := tools.Exec("ps aux | grep nginx | grep 'worker process' | wc -l") data = append(data, nginxStatus{ Name: "工作进程", Value: workers, }) - out := tools.ExecShell("ps aux | grep nginx | grep 'worker process' | awk '{memsum+=$6};END {print memsum}'") + out := tools.Exec("ps aux | grep nginx | grep 'worker process' | awk '{memsum+=$6};END {print memsum}'") mem := tools.FormatBytes(cast.ToFloat64(out)) data = append(data, nginxStatus{ Name: "内存占用", diff --git a/app/http/controllers/plugins/php74/php74_controller.go b/app/http/controllers/plugins/php74/php74_controller.go index 1fe1396e..657ce731 100644 --- a/app/http/controllers/plugins/php74/php74_controller.go +++ b/app/http/controllers/plugins/php74/php74_controller.go @@ -35,7 +35,7 @@ func (c *Php74Controller) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") return @@ -53,8 +53,8 @@ func (c *Php74Controller) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -73,8 +73,8 @@ func (c *Php74Controller) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -93,8 +93,8 @@ func (c *Php74Controller) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -113,8 +113,8 @@ func (c *Php74Controller) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -133,7 +133,7 @@ func (c *Php74Controller) GetConfig(ctx http.Context) { return } - config := tools.ReadFile("/www/server/php/" + c.version + "/etc/php.ini") + config := tools.Read("/www/server/php/" + c.version + "/etc/php.ini") controllers.Success(ctx, config) } @@ -143,7 +143,7 @@ func (c *Php74Controller) SaveConfig(ctx http.Context) { } config := ctx.Request().Input("config") - tools.WriteFile("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) + tools.Write("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) c.Reload(ctx) } @@ -188,7 +188,7 @@ func (c *Php74Controller) ErrorLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) controllers.Success(ctx, log) } @@ -197,7 +197,7 @@ func (c *Php74Controller) SlowLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) controllers.Success(ctx, log) } @@ -206,7 +206,7 @@ func (c *Php74Controller) ClearErrorLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") controllers.Success(ctx, true) } @@ -215,7 +215,7 @@ func (c *Php74Controller) ClearSlowLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") controllers.Success(ctx, true) } @@ -354,7 +354,7 @@ func (c *Php74Controller) GetExtensions() []Extension { Installed: false, }) - raw := tools.ExecShell("/www/server/php/" + c.version + "/bin/php -m") + raw := tools.Exec("/www/server/php/" + c.version + "/bin/php -m") rawExtensionList := strings.Split(raw, "\n") for _, item := range rawExtensionList { diff --git a/app/http/controllers/plugins/php80/php80_controller.go b/app/http/controllers/plugins/php80/php80_controller.go index 1f394fbd..afc2e745 100644 --- a/app/http/controllers/plugins/php80/php80_controller.go +++ b/app/http/controllers/plugins/php80/php80_controller.go @@ -35,7 +35,7 @@ func (c *Php80Controller) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") return @@ -53,8 +53,8 @@ func (c *Php80Controller) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -73,8 +73,8 @@ func (c *Php80Controller) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -93,8 +93,8 @@ func (c *Php80Controller) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -113,8 +113,8 @@ func (c *Php80Controller) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -133,7 +133,7 @@ func (c *Php80Controller) GetConfig(ctx http.Context) { return } - config := tools.ReadFile("/www/server/php/" + c.version + "/etc/php.ini") + config := tools.Read("/www/server/php/" + c.version + "/etc/php.ini") controllers.Success(ctx, config) } @@ -143,7 +143,7 @@ func (c *Php80Controller) SaveConfig(ctx http.Context) { } config := ctx.Request().Input("config") - tools.WriteFile("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) + tools.Write("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) c.Reload(ctx) } @@ -188,7 +188,7 @@ func (c *Php80Controller) ErrorLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) controllers.Success(ctx, log) } @@ -197,7 +197,7 @@ func (c *Php80Controller) SlowLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) controllers.Success(ctx, log) } @@ -206,7 +206,7 @@ func (c *Php80Controller) ClearErrorLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") controllers.Success(ctx, true) } @@ -215,7 +215,7 @@ func (c *Php80Controller) ClearSlowLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") controllers.Success(ctx, true) } @@ -354,7 +354,7 @@ func (c *Php80Controller) GetExtensions() []Extension { Installed: false, }) - raw := tools.ExecShell("/www/server/php/" + c.version + "/bin/php -m") + raw := tools.Exec("/www/server/php/" + c.version + "/bin/php -m") rawExtensionList := strings.Split(raw, "\n") for _, item := range rawExtensionList { diff --git a/app/http/controllers/plugins/php81/php81_controller.go b/app/http/controllers/plugins/php81/php81_controller.go index d78e9ffa..d7f8d5d9 100644 --- a/app/http/controllers/plugins/php81/php81_controller.go +++ b/app/http/controllers/plugins/php81/php81_controller.go @@ -35,7 +35,7 @@ func (c *Php81Controller) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") return @@ -53,8 +53,8 @@ func (c *Php81Controller) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -73,8 +73,8 @@ func (c *Php81Controller) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -93,8 +93,8 @@ func (c *Php81Controller) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -113,8 +113,8 @@ func (c *Php81Controller) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -133,7 +133,7 @@ func (c *Php81Controller) GetConfig(ctx http.Context) { return } - config := tools.ReadFile("/www/server/php/" + c.version + "/etc/php.ini") + config := tools.Read("/www/server/php/" + c.version + "/etc/php.ini") controllers.Success(ctx, config) } @@ -143,7 +143,7 @@ func (c *Php81Controller) SaveConfig(ctx http.Context) { } config := ctx.Request().Input("config") - tools.WriteFile("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) + tools.Write("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) c.Reload(ctx) } @@ -188,7 +188,7 @@ func (c *Php81Controller) ErrorLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) controllers.Success(ctx, log) } @@ -197,7 +197,7 @@ func (c *Php81Controller) SlowLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) controllers.Success(ctx, log) } @@ -206,7 +206,7 @@ func (c *Php81Controller) ClearErrorLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") controllers.Success(ctx, true) } @@ -215,7 +215,7 @@ func (c *Php81Controller) ClearSlowLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") controllers.Success(ctx, true) } @@ -354,7 +354,7 @@ func (c *Php81Controller) GetExtensions() []Extension { Installed: false, }) - raw := tools.ExecShell("/www/server/php/" + c.version + "/bin/php -m") + raw := tools.Exec("/www/server/php/" + c.version + "/bin/php -m") rawExtensionList := strings.Split(raw, "\n") for _, item := range rawExtensionList { diff --git a/app/http/controllers/plugins/php82/php82_controller.go b/app/http/controllers/plugins/php82/php82_controller.go index d5f5783f..7bdbb3c2 100644 --- a/app/http/controllers/plugins/php82/php82_controller.go +++ b/app/http/controllers/plugins/php82/php82_controller.go @@ -35,7 +35,7 @@ func (c *Php82Controller) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") return @@ -53,8 +53,8 @@ func (c *Php82Controller) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -73,8 +73,8 @@ func (c *Php82Controller) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -93,8 +93,8 @@ func (c *Php82Controller) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -113,8 +113,8 @@ func (c *Php82Controller) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart php-fpm-" + c.version) - out := tools.ExecShell("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart php-fpm-" + c.version) + out := tools.Exec("systemctl status php-fpm-" + c.version + " | grep Active | grep -v grep | awk '{print $2}'") status := strings.TrimSpace(out) if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+c.version+"运行状态失败") @@ -133,7 +133,7 @@ func (c *Php82Controller) GetConfig(ctx http.Context) { return } - config := tools.ReadFile("/www/server/php/" + c.version + "/etc/php.ini") + config := tools.Read("/www/server/php/" + c.version + "/etc/php.ini") controllers.Success(ctx, config) } @@ -143,7 +143,7 @@ func (c *Php82Controller) SaveConfig(ctx http.Context) { } config := ctx.Request().Input("config") - tools.WriteFile("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) + tools.Write("/www/server/php/"+c.version+"/etc/php.ini", config, 0644) c.Reload(ctx) } @@ -188,7 +188,7 @@ func (c *Php82Controller) ErrorLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/php-fpm.log")) controllers.Success(ctx, log) } @@ -197,7 +197,7 @@ func (c *Php82Controller) SlowLog(ctx http.Context) { return } - log := tools.Escape(tools.ExecShell("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) + log := tools.Escape(tools.Exec("tail -n 100 /www/server/php/" + c.version + "/var/log/slow.log")) controllers.Success(ctx, log) } @@ -206,7 +206,7 @@ func (c *Php82Controller) ClearErrorLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/php-fpm.log") controllers.Success(ctx, true) } @@ -215,7 +215,7 @@ func (c *Php82Controller) ClearSlowLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") + tools.Exec("echo '' > /www/server/php/" + c.version + "/var/log/slow.log") controllers.Success(ctx, true) } @@ -348,7 +348,7 @@ func (c *Php82Controller) GetExtensions() []Extension { Installed: false, }) - raw := tools.ExecShell("/www/server/php/" + c.version + "/bin/php -m") + raw := tools.Exec("/www/server/php/" + c.version + "/bin/php -m") rawExtensionList := strings.Split(raw, "\n") for _, item := range rawExtensionList { diff --git a/app/http/controllers/plugins/phpmyadmin/phpmyadmin_controller.go b/app/http/controllers/plugins/phpmyadmin/phpmyadmin_controller.go index 9bed043b..743ee40a 100644 --- a/app/http/controllers/plugins/phpmyadmin/phpmyadmin_controller.go +++ b/app/http/controllers/plugins/phpmyadmin/phpmyadmin_controller.go @@ -40,7 +40,7 @@ func (c *PhpMyAdminController) Info(ctx http.Context) { return } - conf := tools.ReadFile("/www/server/vhost/phpmyadmin.conf") + conf := tools.Read("/www/server/vhost/phpmyadmin.conf") match := regexp.MustCompile(`listen\s+(\d+);`).FindStringSubmatch(conf) if len(match) == 0 { controllers.Error(ctx, http.StatusBadRequest, "找不到 phpMyAdmin 端口") @@ -64,18 +64,18 @@ func (c *PhpMyAdminController) SetPort(ctx http.Context) { return } - conf := tools.ReadFile("/www/server/vhost/phpmyadmin.conf") + conf := tools.Read("/www/server/vhost/phpmyadmin.conf") conf = regexp.MustCompile(`listen\s+(\d+);`).ReplaceAllString(conf, "listen "+port+";") - tools.WriteFile("/www/server/vhost/phpmyadmin.conf", conf, 0644) + tools.Write("/www/server/vhost/phpmyadmin.conf", conf, 0644) if tools.IsRHEL() { - tools.ExecShell("firewall-cmd --zone=public --add-port=" + port + "/tcp --permanent") - tools.ExecShell("firewall-cmd --reload") + tools.Exec("firewall-cmd --zone=public --add-port=" + port + "/tcp --permanent") + tools.Exec("firewall-cmd --reload") } else { - tools.ExecShell("ufw allow " + port + "/tcp") - tools.ExecShell("ufw reload") + tools.Exec("ufw allow " + port + "/tcp") + tools.Exec("ufw reload") } - tools.ExecShell("systemctl reload openresty") + tools.Exec("systemctl reload openresty") controllers.Success(ctx, nil) } diff --git a/app/http/controllers/plugins/postgresql15/postgresql15_controller.go b/app/http/controllers/plugins/postgresql15/postgresql15_controller.go index 80be6779..48e6a9dc 100644 --- a/app/http/controllers/plugins/postgresql15/postgresql15_controller.go +++ b/app/http/controllers/plugins/postgresql15/postgresql15_controller.go @@ -36,7 +36,7 @@ func (c *Postgresql15Controller) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL状态失败") return @@ -55,8 +55,8 @@ func (c *Postgresql15Controller) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload postgresql") - status := tools.ExecShell("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload postgresql") + status := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL状态失败") return @@ -75,8 +75,8 @@ func (c *Postgresql15Controller) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart postgresql") - status := tools.ExecShell("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart postgresql") + status := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL状态失败") return @@ -95,8 +95,8 @@ func (c *Postgresql15Controller) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start postgresql") - status := tools.ExecShell("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start postgresql") + status := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL状态失败") return @@ -115,8 +115,8 @@ func (c *Postgresql15Controller) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop postgresql") - status := tools.ExecShell("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop postgresql") + status := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL状态失败") return @@ -136,7 +136,7 @@ func (c *Postgresql15Controller) GetConfig(ctx http.Context) { } // 获取配置 - config := tools.ReadFile("/www/server/postgresql/data/postgresql.conf") + config := tools.Read("/www/server/postgresql/data/postgresql.conf") if len(config) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败") return @@ -152,7 +152,7 @@ func (c *Postgresql15Controller) GetUserConfig(ctx http.Context) { } // 获取配置 - config := tools.ReadFile("/www/server/postgresql/data/pg_hba.conf") + config := tools.Read("/www/server/postgresql/data/pg_hba.conf") if len(config) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败") return @@ -173,7 +173,7 @@ func (c *Postgresql15Controller) SaveConfig(ctx http.Context) { return } - if !tools.WriteFile("/www/server/postgresql/data/postgresql.conf", config, 0644) { + if !tools.Write("/www/server/postgresql/data/postgresql.conf", config, 0644) { controllers.Error(ctx, http.StatusInternalServerError, "写入PostgreSQL配置失败") return } @@ -193,7 +193,7 @@ func (c *Postgresql15Controller) SaveUserConfig(ctx http.Context) { return } - if !tools.WriteFile("/www/server/postgresql/data/pg_hba.conf", config, 0644) { + if !tools.Write("/www/server/postgresql/data/pg_hba.conf", config, 0644) { controllers.Error(ctx, http.StatusInternalServerError, "写入PostgreSQL配置失败") return } @@ -207,18 +207,18 @@ func (c *Postgresql15Controller) Load(ctx http.Context) { return } - status := tools.ExecShell("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") if status != "active" { controllers.Error(ctx, http.StatusInternalServerError, "PostgreSQL 已停止运行") return } data := []Info{ - {"启动时间", carbon.Parse(tools.ExecShell(`echo "select pg_postmaster_start_time();" | su - postgres -c "psql" | sed -n 3p | cut -d'.' -f1`)).ToDateTimeString()}, - {"进程 PID", tools.ExecShell(`echo "select pg_backend_pid();" | su - postgres -c "psql" | sed -n 3p`)}, - {"进程数", tools.ExecShell(`ps aux | grep postgres | grep -v grep | wc -l`)}, - {"总连接数", tools.ExecShell(`echo "SELECT count(*) FROM pg_stat_activity WHERE NOT pid=pg_backend_pid();" | su - postgres -c "psql" | sed -n 3p`)}, - {"空间占用", tools.ExecShell(`echo "select pg_size_pretty(pg_database_size('postgres'));" | su - postgres -c "psql" | sed -n 3p`)}, + {"启动时间", carbon.Parse(tools.Exec(`echo "select pg_postmaster_start_time();" | su - postgres -c "psql" | sed -n 3p | cut -d'.' -f1`)).ToDateTimeString()}, + {"进程 PID", tools.Exec(`echo "select pg_backend_pid();" | su - postgres -c "psql" | sed -n 3p`)}, + {"进程数", tools.Exec(`ps aux | grep postgres | grep -v grep | wc -l`)}, + {"总连接数", tools.Exec(`echo "SELECT count(*) FROM pg_stat_activity WHERE NOT pid=pg_backend_pid();" | su - postgres -c "psql" | sed -n 3p`)}, + {"空间占用", tools.Exec(`echo "select pg_size_pretty(pg_database_size('postgres'));" | su - postgres -c "psql" | sed -n 3p`)}, } controllers.Success(ctx, data) @@ -230,7 +230,7 @@ func (c *Postgresql15Controller) Log(ctx http.Context) { return } - log := tools.ExecShell("tail -n 100 /www/server/postgresql/logs/postgresql-" + carbon.Now().ToDateString() + ".log") + log := tools.Exec("tail -n 100 /www/server/postgresql/logs/postgresql-" + carbon.Now().ToDateString() + ".log") controllers.Success(ctx, log) } @@ -240,7 +240,7 @@ func (c *Postgresql15Controller) ClearLog(ctx http.Context) { return } - tools.ExecShell("echo '' > /www/server/postgresql/logs/postgresql-" + carbon.Now().ToDateString() + ".log") + tools.Exec("echo '' > /www/server/postgresql/logs/postgresql-" + carbon.Now().ToDateString() + ".log") controllers.Success(ctx, nil) } @@ -250,13 +250,13 @@ func (c *Postgresql15Controller) DatabaseList(ctx http.Context) { return } - status := tools.ExecShell("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status postgresql | grep Active | grep -v grep | awk '{print $2}'") if status != "active" { controllers.Error(ctx, http.StatusInternalServerError, "PostgreSQL 已停止运行") return } - raw := tools.ExecShell(`echo "\l" | su - postgres -c "psql"`) + raw := tools.Exec(`echo "\l" | su - postgres -c "psql"`) databases := strings.Split(raw, "\n") databases = databases[3 : len(databases)-1] @@ -326,12 +326,12 @@ func (c *Postgresql15Controller) AddDatabase(ctx http.Context) { user := ctx.Request().Input("user") password := ctx.Request().Input("password") - tools.ExecShell(`echo "CREATE DATABASE ` + database + `;" | su - postgres -c "psql"`) - tools.ExecShell(`echo "CREATE USER ` + user + ` WITH PASSWORD '` + password + `';" | su - postgres -c "psql"`) - tools.ExecShell(`echo "GRANT ALL PRIVILEGES ON DATABASE ` + database + ` TO ` + user + `;" | su - postgres -c "psql"`) + tools.Exec(`echo "CREATE DATABASE ` + database + `;" | su - postgres -c "psql"`) + tools.Exec(`echo "CREATE USER ` + user + ` WITH PASSWORD '` + password + `';" | su - postgres -c "psql"`) + tools.Exec(`echo "GRANT ALL PRIVILEGES ON DATABASE ` + database + ` TO ` + user + `;" | su - postgres -c "psql"`) userConfig := "host " + database + " " + user + " 127.0.0.1/32 scram-sha-256" - tools.ExecShell(`echo "` + userConfig + `" >> /www/server/postgresql/data/pg_hba.conf`) + tools.Exec(`echo "` + userConfig + `" >> /www/server/postgresql/data/pg_hba.conf`) c.Reload(ctx) } @@ -355,7 +355,7 @@ func (c *Postgresql15Controller) DeleteDatabase(ctx http.Context) { } database := ctx.Request().Input("database") - tools.ExecShell(`echo "DROP DATABASE ` + database + `;" | su - postgres -c "psql"`) + tools.Exec(`echo "DROP DATABASE ` + database + `;" | su - postgres -c "psql"`) controllers.Success(ctx, nil) } @@ -452,7 +452,7 @@ func (c *Postgresql15Controller) DeleteBackup(ctx http.Context) { backupPath := c.setting.Get(models.SettingKeyBackupPath) + "/postgresql" fileName := ctx.Request().Input("name") - tools.RemoveFile(backupPath + "/" + fileName) + tools.Remove(backupPath + "/" + fileName) controllers.Success(ctx, nil) } @@ -497,7 +497,7 @@ func (c *Postgresql15Controller) UserList(ctx http.Context) { Role string `json:"role"` } - raw := tools.ExecShell(`echo "\du" | su - postgres -c "psql"`) + raw := tools.Exec(`echo "\du" | su - postgres -c "psql"`) users := strings.Split(raw, "\n") if len(users) < 4 { controllers.Error(ctx, http.StatusInternalServerError, "用户列表为空") @@ -563,11 +563,11 @@ func (c *Postgresql15Controller) AddUser(ctx http.Context) { user := ctx.Request().Input("user") password := ctx.Request().Input("password") database := ctx.Request().Input("database") - tools.ExecShell(`echo "CREATE USER ` + user + ` WITH PASSWORD '` + password + `';" | su - postgres -c "psql"`) - tools.ExecShell(`echo "GRANT ALL PRIVILEGES ON DATABASE ` + database + ` TO ` + user + `;" | su - postgres -c "psql"`) + tools.Exec(`echo "CREATE USER ` + user + ` WITH PASSWORD '` + password + `';" | su - postgres -c "psql"`) + tools.Exec(`echo "GRANT ALL PRIVILEGES ON DATABASE ` + database + ` TO ` + user + `;" | su - postgres -c "psql"`) userConfig := "host " + database + " " + user + " 127.0.0.1/32 scram-sha-256" - tools.ExecShell(`echo "` + userConfig + `" >> /www/server/postgresql/data/pg_hba.conf`) + tools.Exec(`echo "` + userConfig + `" >> /www/server/postgresql/data/pg_hba.conf`) c.Reload(ctx) } @@ -591,8 +591,8 @@ func (c *Postgresql15Controller) DeleteUser(ctx http.Context) { } user := ctx.Request().Input("user") - tools.ExecShell(`echo "DROP USER ` + user + `;" | su - postgres -c "psql"`) - tools.ExecShell(`sed -i '/` + user + `/d' /www/server/postgresql/data/pg_hba.conf`) + tools.Exec(`echo "DROP USER ` + user + `;" | su - postgres -c "psql"`) + tools.Exec(`sed -i '/` + user + `/d' /www/server/postgresql/data/pg_hba.conf`) c.Reload(ctx) } @@ -618,7 +618,7 @@ func (c *Postgresql15Controller) SetUserPassword(ctx http.Context) { user := ctx.Request().Input("user") password := ctx.Request().Input("password") - tools.ExecShell(`echo "ALTER USER ` + user + ` WITH PASSWORD '` + password + `';" | su - postgres -c "psql"`) + tools.Exec(`echo "ALTER USER ` + user + ` WITH PASSWORD '` + password + `';" | su - postgres -c "psql"`) controllers.Success(ctx, nil) } diff --git a/app/http/controllers/plugins/pureftpd/pureftpd_controller.go b/app/http/controllers/plugins/pureftpd/pureftpd_controller.go index ea2f86b6..2ffa3b3b 100644 --- a/app/http/controllers/plugins/pureftpd/pureftpd_controller.go +++ b/app/http/controllers/plugins/pureftpd/pureftpd_controller.go @@ -28,7 +28,7 @@ func (c *PureFtpdController) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PureFtpd状态失败") return @@ -47,8 +47,8 @@ func (c *PureFtpdController) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload pure-ftpd") - status := tools.ExecShell("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload pure-ftpd") + status := tools.Exec("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PureFtpd状态失败") return @@ -67,8 +67,8 @@ func (c *PureFtpdController) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart pure-ftpd") - status := tools.ExecShell("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart pure-ftpd") + status := tools.Exec("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PureFtpd状态失败") return @@ -87,8 +87,8 @@ func (c *PureFtpdController) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start pure-ftpd") - status := tools.ExecShell("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start pure-ftpd") + status := tools.Exec("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PureFtpd状态失败") return @@ -107,8 +107,8 @@ func (c *PureFtpdController) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop pure-ftpd") - status := tools.ExecShell("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop pure-ftpd") + status := tools.Exec("systemctl status pure-ftpd | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PureFtpd状态失败") return @@ -127,7 +127,7 @@ func (c *PureFtpdController) List(ctx http.Context) { return } - listRaw := tools.ExecShell("pure-pw list") + listRaw := tools.Exec("pure-pw list") if len(listRaw) == 0 { controllers.Success(ctx, http.Json{ "total": 0, @@ -206,8 +206,8 @@ func (c *PureFtpdController) Add(ctx http.Context) { tools.Chmod(path, 0755) tools.Chown(path, "www", "www") - tools.ExecShell(`yes '` + password + `' | pure-pw useradd ` + username + ` -u www -g www -d ` + path) - tools.ExecShell("pure-pw mkdb") + tools.Exec(`yes '` + password + `' | pure-pw useradd ` + username + ` -u www -g www -d ` + path) + tools.Exec("pure-pw mkdb") controllers.Success(ctx, nil) } @@ -232,8 +232,8 @@ func (c *PureFtpdController) Delete(ctx http.Context) { username := ctx.Request().Input("username") - tools.ExecShell("pure-pw userdel " + username + " -m") - tools.ExecShell("pure-pw mkdb") + tools.Exec("pure-pw userdel " + username + " -m") + tools.Exec("pure-pw mkdb") controllers.Success(ctx, nil) } @@ -260,8 +260,8 @@ func (c *PureFtpdController) ChangePassword(ctx http.Context) { username := ctx.Request().Input("username") password := ctx.Request().Input("password") - tools.ExecShell(`yes '` + password + `' | pure-pw passwd ` + username + ` -m`) - tools.ExecShell("pure-pw mkdb") + tools.Exec(`yes '` + password + `' | pure-pw passwd ` + username + ` -m`) + tools.Exec("pure-pw mkdb") controllers.Success(ctx, nil) } @@ -272,7 +272,7 @@ func (c *PureFtpdController) GetPort(ctx http.Context) { return } - port := tools.ExecShell(`cat /www/server/pure-ftpd/etc/pure-ftpd.conf | grep "Bind" | awk '{print $2}' | awk -F "," '{print $2}'`) + port := tools.Exec(`cat /www/server/pure-ftpd/etc/pure-ftpd.conf | grep "Bind" | awk '{print $2}' | awk -F "," '{print $2}'`) if len(port) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取PureFtpd端口失败") return @@ -300,15 +300,15 @@ func (c *PureFtpdController) SetPort(ctx http.Context) { } port := ctx.Request().Input("port") - tools.ExecShell(`sed -i "s/Bind.*/Bind 0.0.0.0,` + port + `/g" /www/server/pure-ftpd/etc/pure-ftpd.conf`) + tools.Exec(`sed -i "s/Bind.*/Bind 0.0.0.0,` + port + `/g" /www/server/pure-ftpd/etc/pure-ftpd.conf`) if tools.IsRHEL() { - tools.ExecShell("firewall-cmd --zone=public --add-port=" + port + "/tcp --permanent") - tools.ExecShell("firewall-cmd --reload") + tools.Exec("firewall-cmd --zone=public --add-port=" + port + "/tcp --permanent") + tools.Exec("firewall-cmd --reload") } else { - tools.ExecShell("ufw allow " + port + "/tcp") - tools.ExecShell("ufw reload") + tools.Exec("ufw allow " + port + "/tcp") + tools.Exec("ufw reload") } - tools.ExecShell("systemctl restart pure-ftpd") + tools.Exec("systemctl restart pure-ftpd") controllers.Success(ctx, nil) } diff --git a/app/http/controllers/plugins/redis/redis_controller.go b/app/http/controllers/plugins/redis/redis_controller.go index a542f4c4..18c706cb 100644 --- a/app/http/controllers/plugins/redis/redis_controller.go +++ b/app/http/controllers/plugins/redis/redis_controller.go @@ -27,7 +27,7 @@ func (c *RedisController) Status(ctx http.Context) { return } - status := tools.ExecShell("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取Redis状态失败") return @@ -46,8 +46,8 @@ func (c *RedisController) Reload(ctx http.Context) { return } - tools.ExecShell("systemctl reload redis") - status := tools.ExecShell("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl reload redis") + status := tools.Exec("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取Redis状态失败") return @@ -66,8 +66,8 @@ func (c *RedisController) Restart(ctx http.Context) { return } - tools.ExecShell("systemctl restart redis") - status := tools.ExecShell("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl restart redis") + status := tools.Exec("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取Redis状态失败") return @@ -86,8 +86,8 @@ func (c *RedisController) Start(ctx http.Context) { return } - tools.ExecShell("systemctl start redis") - status := tools.ExecShell("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl start redis") + status := tools.Exec("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取Redis状态失败") return @@ -106,8 +106,8 @@ func (c *RedisController) Stop(ctx http.Context) { return } - tools.ExecShell("systemctl stop redis") - status := tools.ExecShell("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") + tools.Exec("systemctl stop redis") + status := tools.Exec("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") if len(status) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取Redis状态失败") return @@ -127,7 +127,7 @@ func (c *RedisController) GetConfig(ctx http.Context) { } // 获取配置 - config := tools.ReadFile("/www/server/redis/redis.conf") + config := tools.Read("/www/server/redis/redis.conf") if len(config) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取Redis配置失败") return @@ -148,7 +148,7 @@ func (c *RedisController) SaveConfig(ctx http.Context) { return } - if !tools.WriteFile("/www/server/redis/redis.conf", config, 0644) { + if !tools.Write("/www/server/redis/redis.conf", config, 0644) { controllers.Error(ctx, http.StatusInternalServerError, "写入Redis配置失败") return } @@ -162,13 +162,13 @@ func (c *RedisController) Load(ctx http.Context) { return } - status := tools.ExecShell("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") + status := tools.Exec("systemctl status redis | grep Active | grep -v grep | awk '{print $2}'") if status != "active" { controllers.Error(ctx, http.StatusInternalServerError, "Redis 已停止运行") return } - raw := tools.ExecShell("redis-cli info") + raw := tools.Exec("redis-cli info") if len(raw) == 0 { controllers.Error(ctx, http.StatusInternalServerError, "获取Redis负载失败") return diff --git a/app/http/controllers/plugins/s3fs/s3fs_controller.go b/app/http/controllers/plugins/s3fs/s3fs_controller.go index 0751c182..7570856f 100644 --- a/app/http/controllers/plugins/s3fs/s3fs_controller.go +++ b/app/http/controllers/plugins/s3fs/s3fs_controller.go @@ -125,17 +125,17 @@ func (c *S3fsController) Add(ctx http.Context) { id := carbon.Now().TimestampMilli() password := ak + ":" + sk - tools.WriteFile("/etc/passwd-s3fs-"+cast.ToString(id), password, 0600) - tools.ExecShell(`echo 's3fs#` + bucket + ` ` + path + ` fuse _netdev,allow_other,nonempty,url=` + url + `,passwd_file=/etc/passwd-s3fs-` + cast.ToString(id) + ` 0 0' >> /etc/fstab`) - check := tools.ExecShell("mount -a 2>&1") + tools.Write("/etc/passwd-s3fs-"+cast.ToString(id), password, 0600) + tools.Exec(`echo 's3fs#` + bucket + ` ` + path + ` fuse _netdev,allow_other,nonempty,url=` + url + `,passwd_file=/etc/passwd-s3fs-` + cast.ToString(id) + ` 0 0' >> /etc/fstab`) + check := tools.Exec("mount -a 2>&1") if len(check) != 0 { - tools.ExecShell(`sed -i 's@^s3fs#` + bucket + `\s` + path + `.*$@@g' /etc/fstab`) + tools.Exec(`sed -i 's@^s3fs#` + bucket + `\s` + path + `.*$@@g' /etc/fstab`) controllers.Error(ctx, http.StatusInternalServerError, "检测到/etc/fstab有误: "+check) return } - check2 := tools.ExecShell("df -h | grep " + path + " 2>&1") + check2 := tools.Exec("df -h | grep " + path + " 2>&1") if len(check2) == 0 { - tools.ExecShell(`sed -i 's@^s3fs#` + bucket + `\s` + path + `.*$@@g' /etc/fstab`) + tools.Exec(`sed -i 's@^s3fs#` + bucket + `\s` + path + `.*$@@g' /etc/fstab`) controllers.Error(ctx, http.StatusInternalServerError, "挂载失败,请检查配置是否正确") return } @@ -191,15 +191,15 @@ func (c *S3fsController) Delete(ctx http.Context) { return } - tools.ExecShell(`fusermount -u '` + mount.Path + `' 2>&1`) - tools.ExecShell(`umount '` + mount.Path + `' 2>&1`) - tools.ExecShell(`sed -i 's@^s3fs#` + mount.Bucket + `\s` + mount.Path + `.*$@@g' /etc/fstab`) - check := tools.ExecShell("mount -a 2>&1") + tools.Exec(`fusermount -u '` + mount.Path + `' 2>&1`) + tools.Exec(`umount '` + mount.Path + `' 2>&1`) + tools.Exec(`sed -i 's@^s3fs#` + mount.Bucket + `\s` + mount.Path + `.*$@@g' /etc/fstab`) + check := tools.Exec("mount -a 2>&1") if len(check) != 0 { controllers.Error(ctx, http.StatusInternalServerError, "检测到/etc/fstab有误: "+check) return } - tools.RemoveFile("/etc/passwd-s3fs-" + cast.ToString(mount.ID)) + tools.Remove("/etc/passwd-s3fs-" + cast.ToString(mount.ID)) var newS3fsList []s3fs for _, s := range s3fsList { diff --git a/app/http/controllers/plugins/supervisor/supervisor_controller.go b/app/http/controllers/plugins/supervisor/supervisor_controller.go index a71ac4ef..c3b16b93 100644 --- a/app/http/controllers/plugins/supervisor/supervisor_controller.go +++ b/app/http/controllers/plugins/supervisor/supervisor_controller.go @@ -33,7 +33,7 @@ func (c *SupervisorController) Status(ctx http.Context) { return } - status := tools.ExecShell(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) + status := tools.Exec(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) if status == "active" { controllers.Success(ctx, true) } else { @@ -47,8 +47,8 @@ func (c *SupervisorController) Start(ctx http.Context) { return } - tools.ExecShell(`systemctl start ` + c.ServiceName) - status := tools.ExecShell(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) + tools.Exec(`systemctl start ` + c.ServiceName) + status := tools.Exec(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) if status == "active" { controllers.Success(ctx, true) } else { @@ -62,8 +62,8 @@ func (c *SupervisorController) Stop(ctx http.Context) { return } - tools.ExecShell(`systemctl stop ` + c.ServiceName) - status := tools.ExecShell(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) + tools.Exec(`systemctl stop ` + c.ServiceName) + status := tools.Exec(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) if status != "active" { controllers.Success(ctx, true) } else { @@ -77,8 +77,8 @@ func (c *SupervisorController) Restart(ctx http.Context) { return } - tools.ExecShell(`systemctl restart ` + c.ServiceName) - status := tools.ExecShell(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) + tools.Exec(`systemctl restart ` + c.ServiceName) + status := tools.Exec(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) if status == "active" { controllers.Success(ctx, true) } else { @@ -92,8 +92,8 @@ func (c *SupervisorController) Reload(ctx http.Context) { return } - tools.ExecShell(`systemctl reload ` + c.ServiceName) - status := tools.ExecShell(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) + tools.Exec(`systemctl reload ` + c.ServiceName) + status := tools.Exec(`systemctl status ` + c.ServiceName + ` | grep Active | grep -v grep | awk '{print $2}'`) if status == "active" { controllers.Success(ctx, true) } else { @@ -107,7 +107,7 @@ func (c *SupervisorController) Log(ctx http.Context) { return } - log := tools.ExecShell(`tail -n 200 /var/log/supervisor/supervisord.log`) + log := tools.Exec(`tail -n 200 /var/log/supervisor/supervisord.log`) controllers.Success(ctx, log) } @@ -117,7 +117,7 @@ func (c *SupervisorController) ClearLog(ctx http.Context) { return } - tools.ExecShell(`echo "" > /var/log/supervisor/supervisord.log`) + tools.Exec(`echo "" > /var/log/supervisor/supervisord.log`) controllers.Success(ctx, nil) } @@ -129,9 +129,9 @@ func (c *SupervisorController) Config(ctx http.Context) { var config string if tools.IsRHEL() { - config = tools.ReadFile(`/etc/supervisord.conf`) + config = tools.Read(`/etc/supervisord.conf`) } else { - config = tools.ReadFile(`/etc/supervisor/supervisord.conf`) + config = tools.Read(`/etc/supervisor/supervisord.conf`) } controllers.Success(ctx, config) } @@ -144,9 +144,9 @@ func (c *SupervisorController) SaveConfig(ctx http.Context) { config := ctx.Request().Input("config") if tools.IsRHEL() { - tools.WriteFile(`/etc/supervisord.conf`, config, 0644) + tools.Write(`/etc/supervisord.conf`, config, 0644) } else { - tools.WriteFile(`/etc/supervisor/supervisord.conf`, config, 0644) + tools.Write(`/etc/supervisor/supervisord.conf`, config, 0644) } c.Restart(ctx) @@ -168,7 +168,7 @@ func (c *SupervisorController) Processes(ctx http.Context) { Uptime string `json:"uptime"` } - out := tools.ExecShell(`supervisorctl status | awk '{print $1}'`) + out := tools.Exec(`supervisorctl status | awk '{print $1}'`) var processList []process for _, line := range strings.Split(out, "\n") { if len(line) == 0 { @@ -177,10 +177,10 @@ func (c *SupervisorController) Processes(ctx http.Context) { var p process p.Name = line - p.Status = tools.ExecShell(`supervisorctl status ` + line + ` | awk '{print $2}'`) + p.Status = tools.Exec(`supervisorctl status ` + line + ` | awk '{print $2}'`) if p.Status == "RUNNING" { - p.Pid = strings.ReplaceAll(tools.ExecShell(`supervisorctl status `+line+` | awk '{print $4}'`), ",", "") - p.Uptime = tools.ExecShell(`supervisorctl status ` + line + ` | awk '{print $6}'`) + p.Pid = strings.ReplaceAll(tools.Exec(`supervisorctl status `+line+` | awk '{print $4}'`), ",", "") + p.Uptime = tools.Exec(`supervisorctl status ` + line + ` | awk '{print $6}'`) } else { p.Pid = "-" p.Uptime = "-" @@ -215,7 +215,7 @@ func (c *SupervisorController) StartProcess(ctx http.Context) { } process := ctx.Request().Input("process") - tools.ExecShell(`supervisorctl start ` + process) + tools.Exec(`supervisorctl start ` + process) controllers.Success(ctx, nil) } @@ -226,7 +226,7 @@ func (c *SupervisorController) StopProcess(ctx http.Context) { } process := ctx.Request().Input("process") - tools.ExecShell(`supervisorctl stop ` + process) + tools.Exec(`supervisorctl stop ` + process) controllers.Success(ctx, nil) } @@ -237,7 +237,7 @@ func (c *SupervisorController) RestartProcess(ctx http.Context) { } process := ctx.Request().Input("process") - tools.ExecShell(`supervisorctl restart ` + process) + tools.Exec(`supervisorctl restart ` + process) controllers.Success(ctx, nil) } @@ -250,12 +250,12 @@ func (c *SupervisorController) ProcessLog(ctx http.Context) { process := ctx.Request().Input("process") var logPath string if tools.IsRHEL() { - logPath = tools.ExecShell(`cat '/etc/supervisord.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) + logPath = tools.Exec(`cat '/etc/supervisord.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) } else { - logPath = tools.ExecShell(`cat '/etc/supervisor/conf.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) + logPath = tools.Exec(`cat '/etc/supervisor/conf.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) } - log := tools.ExecShell(`tail -n 200 ` + logPath) + log := tools.Exec(`tail -n 200 ` + logPath) controllers.Success(ctx, log) } @@ -268,12 +268,12 @@ func (c *SupervisorController) ClearProcessLog(ctx http.Context) { process := ctx.Request().Input("process") var logPath string if tools.IsRHEL() { - logPath = tools.ExecShell(`cat '/etc/supervisord.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) + logPath = tools.Exec(`cat '/etc/supervisord.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) } else { - logPath = tools.ExecShell(`cat '/etc/supervisor/conf.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) + logPath = tools.Exec(`cat '/etc/supervisor/conf.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) } - tools.ExecShell(`echo "" > ` + logPath) + tools.Exec(`echo "" > ` + logPath) controllers.Success(ctx, nil) } @@ -286,9 +286,9 @@ func (c *SupervisorController) ProcessConfig(ctx http.Context) { process := ctx.Request().Query("process") var config string if tools.IsRHEL() { - config = tools.ReadFile(`/etc/supervisord.d/` + process + `.conf`) + config = tools.Read(`/etc/supervisord.d/` + process + `.conf`) } else { - config = tools.ReadFile(`/etc/supervisor/conf.d/` + process + `.conf`) + config = tools.Read(`/etc/supervisor/conf.d/` + process + `.conf`) } controllers.Success(ctx, config) @@ -303,13 +303,13 @@ func (c *SupervisorController) SaveProcessConfig(ctx http.Context) { process := ctx.Request().Input("process") config := ctx.Request().Input("config") if tools.IsRHEL() { - tools.WriteFile(`/etc/supervisord.d/`+process+`.conf`, config, 0644) + tools.Write(`/etc/supervisord.d/`+process+`.conf`, config, 0644) } else { - tools.WriteFile(`/etc/supervisor/conf.d/`+process+`.conf`, config, 0644) + tools.Write(`/etc/supervisor/conf.d/`+process+`.conf`, config, 0644) } - tools.ExecShell(`supervisorctl reread`) - tools.ExecShell(`supervisorctl update`) - tools.ExecShell(`supervisorctl start ` + process) + tools.Exec(`supervisorctl reread`) + tools.Exec(`supervisorctl update`) + tools.Exec(`supervisorctl start ` + process) controllers.Success(ctx, nil) } @@ -354,13 +354,13 @@ stdout_logfile=/var/log/supervisor/` + name + `.log stdout_logfile_maxbytes=2MB ` if tools.IsRHEL() { - tools.WriteFile(`/etc/supervisord.d/`+name+`.conf`, config, 0644) + tools.Write(`/etc/supervisord.d/`+name+`.conf`, config, 0644) } else { - tools.WriteFile(`/etc/supervisor/conf.d/`+name+`.conf`, config, 0644) + tools.Write(`/etc/supervisor/conf.d/`+name+`.conf`, config, 0644) } - tools.ExecShell(`supervisorctl reread`) - tools.ExecShell(`supervisorctl update`) - tools.ExecShell(`supervisorctl start ` + name) + tools.Exec(`supervisorctl reread`) + tools.Exec(`supervisorctl update`) + tools.Exec(`supervisorctl start ` + name) controllers.Success(ctx, nil) } @@ -372,18 +372,18 @@ func (c *SupervisorController) DeleteProcess(ctx http.Context) { } process := ctx.Request().Input("process") - tools.ExecShell(`supervisorctl stop ` + process) + tools.Exec(`supervisorctl stop ` + process) var logPath string if tools.IsRHEL() { - logPath = tools.ExecShell(`cat '/etc/supervisord.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) - tools.RemoveFile(`/etc/supervisord.d/` + process + `.conf`) + logPath = tools.Exec(`cat '/etc/supervisord.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) + tools.Remove(`/etc/supervisord.d/` + process + `.conf`) } else { - logPath = tools.ExecShell(`cat '/etc/supervisor/conf.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) - tools.RemoveFile(`/etc/supervisor/conf.d/` + process + `.conf`) + logPath = tools.Exec(`cat '/etc/supervisor/conf.d/` + process + `.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`) + tools.Remove(`/etc/supervisor/conf.d/` + process + `.conf`) } - tools.RemoveFile(logPath) - tools.ExecShell(`supervisorctl reread`) - tools.ExecShell(`supervisorctl update`) + tools.Remove(logPath) + tools.Exec(`supervisorctl reread`) + tools.Exec(`supervisorctl update`) controllers.Success(ctx, nil) } diff --git a/app/http/controllers/safe_controller.go b/app/http/controllers/safe_controller.go index 53c8a4a8..521872e7 100644 --- a/app/http/controllers/safe_controller.go +++ b/app/http/controllers/safe_controller.go @@ -28,15 +28,15 @@ func (r *SafeController) SetFirewallStatus(ctx http.Context) { var out string if ctx.Request().InputBool("status") { if tools.IsRHEL() { - out = tools.ExecShell("systemctl start firewalld") + out = tools.Exec("systemctl start firewalld") } else { - out = tools.ExecShell("echo y | ufw enable") + out = tools.Exec("echo y | ufw enable") } } else { if tools.IsRHEL() { - out = tools.ExecShell("systemctl stop firewalld") + out = tools.Exec("systemctl stop firewalld") } else { - out = tools.ExecShell("ufw disable") + out = tools.Exec("ufw disable") } } @@ -52,7 +52,7 @@ func (r *SafeController) GetFirewallRules(ctx http.Context) { limit := ctx.Request().QueryInt("limit", 10) if tools.IsRHEL() { - out := tools.ExecShell("firewall-cmd --list-all 2>&1") + out := tools.Exec("firewall-cmd --list-all 2>&1") match := regexp.MustCompile(`ports: (.*)`).FindStringSubmatch(out) if len(match) == 0 { Success(ctx, http.Json{ @@ -90,7 +90,7 @@ func (r *SafeController) GetFirewallRules(ctx http.Context) { "items": pagedRules, }) } else { - out := tools.ExecShell("ufw status | grep -v '(v6)' | grep ALLOW | awk '{print $1}'") + out := tools.Exec("ufw status | grep -v '(v6)' | grep ALLOW | awk '{print $1}'") if len(out) == 0 { Success(ctx, http.Json{ "total": 0, @@ -142,13 +142,13 @@ func (r *SafeController) AddFirewallRule(ctx http.Context) { } if tools.IsRHEL() { - tools.ExecShell("firewall-cmd --remove-port=" + cast.ToString(port) + "/" + protocol + " --permanent 2>&1") - tools.ExecShell("firewall-cmd --add-port=" + cast.ToString(port) + "/" + protocol + " --permanent 2>&1") - tools.ExecShell("firewall-cmd --reload") + tools.Exec("firewall-cmd --remove-port=" + cast.ToString(port) + "/" + protocol + " --permanent 2>&1") + tools.Exec("firewall-cmd --add-port=" + cast.ToString(port) + "/" + protocol + " --permanent 2>&1") + tools.Exec("firewall-cmd --reload") } else { - tools.ExecShell("ufw delete allow " + cast.ToString(port) + "/" + protocol) - tools.ExecShell("ufw allow " + cast.ToString(port) + "/" + protocol) - tools.ExecShell("ufw reload") + tools.Exec("ufw delete allow " + cast.ToString(port) + "/" + protocol) + tools.Exec("ufw allow " + cast.ToString(port) + "/" + protocol) + tools.Exec("ufw reload") } Success(ctx, nil) @@ -168,11 +168,11 @@ func (r *SafeController) DeleteFirewallRule(ctx http.Context) { } if tools.IsRHEL() { - tools.ExecShell("firewall-cmd --remove-port=" + cast.ToString(port) + "/" + protocol + " --permanent 2>&1") - tools.ExecShell("firewall-cmd --reload") + tools.Exec("firewall-cmd --remove-port=" + cast.ToString(port) + "/" + protocol + " --permanent 2>&1") + tools.Exec("firewall-cmd --reload") } else { - tools.ExecShell("ufw delete allow " + cast.ToString(port) + "/" + protocol) - tools.ExecShell("ufw reload") + tools.Exec("ufw delete allow " + cast.ToString(port) + "/" + protocol) + tools.Exec("ufw reload") } Success(ctx, nil) @@ -182,14 +182,14 @@ func (r *SafeController) firewallStatus() bool { var out string var running bool if tools.IsRHEL() { - out = tools.ExecShell("systemctl status firewalld | grep Active | awk '{print $3}'") + out = tools.Exec("systemctl status firewalld | grep Active | awk '{print $3}'") if out == "(running)" { running = true } else { running = false } } else { - out = tools.ExecShell("ufw status | grep Status | awk '{print $2}'") + out = tools.Exec("ufw status | grep Status | awk '{print $2}'") if out == "active" { running = true } else { @@ -203,9 +203,9 @@ func (r *SafeController) firewallStatus() bool { func (r *SafeController) GetSshStatus(ctx http.Context) { var out string if tools.IsRHEL() { - out = tools.ExecShell("systemctl status sshd | grep Active | awk '{print $3}'") + out = tools.Exec("systemctl status sshd | grep Active | awk '{print $3}'") } else { - out = tools.ExecShell("systemctl status ssh | grep Active | awk '{print $3}'") + out = tools.Exec("systemctl status ssh | grep Active | awk '{print $3}'") } running := false @@ -219,19 +219,19 @@ func (r *SafeController) GetSshStatus(ctx http.Context) { func (r *SafeController) SetSshStatus(ctx http.Context) { if ctx.Request().InputBool("status") { if tools.IsRHEL() { - tools.ExecShell("systemctl enable sshd") - tools.ExecShell("systemctl start sshd") + tools.Exec("systemctl enable sshd") + tools.Exec("systemctl start sshd") } else { - tools.ExecShell("systemctl enable ssh") - tools.ExecShell("systemctl start ssh") + tools.Exec("systemctl enable ssh") + tools.Exec("systemctl start ssh") } } else { if tools.IsRHEL() { - tools.ExecShell("systemctl stop sshd") - tools.ExecShell("systemctl disable sshd") + tools.Exec("systemctl stop sshd") + tools.Exec("systemctl disable sshd") } else { - tools.ExecShell("systemctl stop ssh") - tools.ExecShell("systemctl disable ssh") + tools.Exec("systemctl stop ssh") + tools.Exec("systemctl disable ssh") } } @@ -239,7 +239,7 @@ func (r *SafeController) SetSshStatus(ctx http.Context) { } func (r *SafeController) GetSshPort(ctx http.Context) { - out := tools.ExecShell("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'") + out := tools.Exec("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'") Success(ctx, out) } @@ -250,12 +250,12 @@ func (r *SafeController) SetSshPort(ctx http.Context) { return } - oldPort := tools.ExecShell("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'") - tools.ExecShell("sed -i 's/#Port " + oldPort + "/Port " + cast.ToString(port) + "/g' /etc/ssh/sshd_config") - tools.ExecShell("sed -i 's/Port " + oldPort + "/Port " + cast.ToString(port) + "/g' /etc/ssh/sshd_config") + oldPort := tools.Exec("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'") + 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") - if status := tools.ExecShell("systemctl status sshd | grep Active | awk '{print $3}'"); status == "(running)" { - tools.ExecShell("systemctl restart sshd") + if status := tools.Exec("systemctl status sshd | grep Active | awk '{print $3}'"); status == "(running)" { + tools.Exec("systemctl restart sshd") } Success(ctx, nil) @@ -263,14 +263,14 @@ func (r *SafeController) SetSshPort(ctx http.Context) { func (r *SafeController) GetPingStatus(ctx http.Context) { if tools.IsRHEL() { - out := tools.ExecShell(`firewall-cmd --list-all 2>&1`) + out := tools.Exec(`firewall-cmd --list-all 2>&1`) if !strings.Contains(out, `rule protocol value="icmp" drop`) { Success(ctx, true) } else { Success(ctx, false) } } else { - config := tools.ReadFile("/etc/ufw/before.rules") + config := tools.Read("/etc/ufw/before.rules") if strings.Contains(config, "-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT") { Success(ctx, true) } else { @@ -282,18 +282,18 @@ func (r *SafeController) GetPingStatus(ctx http.Context) { func (r *SafeController) SetPingStatus(ctx http.Context) { if tools.IsRHEL() { if ctx.Request().InputBool("status") { - tools.ExecShell(`firewall-cmd --permanent --remove-rich-rule='rule protocol value=icmp drop'`) + tools.Exec(`firewall-cmd --permanent --remove-rich-rule='rule protocol value=icmp drop'`) } else { - tools.ExecShell(`firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop'`) + tools.Exec(`firewall-cmd --permanent --add-rich-rule='rule protocol value=icmp drop'`) } - tools.ExecShell(`firewall-cmd --reload`) + tools.Exec(`firewall-cmd --reload`) } else { if ctx.Request().InputBool("status") { - tools.ExecShell(`sed -i 's/-A ufw-before-input -p icmp --icmp-type echo-request -j DROP/-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT/g' /etc/ufw/before.rules`) + tools.Exec(`sed -i 's/-A ufw-before-input -p icmp --icmp-type echo-request -j DROP/-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT/g' /etc/ufw/before.rules`) } else { - tools.ExecShell(`sed -i 's/-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT/-A ufw-before-input -p icmp --icmp-type echo-request -j DROP/g' /etc/ufw/before.rules`) + tools.Exec(`sed -i 's/-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT/-A ufw-before-input -p icmp --icmp-type echo-request -j DROP/g' /etc/ufw/before.rules`) } - tools.ExecShell(`ufw reload`) + tools.Exec(`ufw reload`) } Success(ctx, nil) diff --git a/app/http/controllers/setting_controller.go b/app/http/controllers/setting_controller.go index f4be66f6..0c51249c 100644 --- a/app/http/controllers/setting_controller.go +++ b/app/http/controllers/setting_controller.go @@ -49,7 +49,7 @@ func (r *SettingController) List(ctx http.Context) { result["username"] = user.Username result["email"] = user.Email - result["port"] = tools.ExecShell(`cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}' | tr -d '\n'`) + result["port"] = tools.Exec(`cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}' | tr -d '\n'`) Success(ctx, result) } @@ -71,9 +71,9 @@ func (r *SettingController) Save(ctx http.Context) { return } - oldPort := tools.ExecShell(`cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}' | tr -d '\n'`) + oldPort := tools.Exec(`cat /www/panel/panel.conf | grep APP_PORT | awk -F '=' '{print $2}' | tr -d '\n'`) if oldPort != port { - tools.ExecShell("sed -i 's/APP_PORT=" + oldPort + "/APP_PORT=" + port + "/g' /www/panel/panel.conf") + tools.Exec("sed -i 's/APP_PORT=" + oldPort + "/APP_PORT=" + port + "/g' /www/panel/panel.conf") } if !tools.Exists(backupPath) { tools.Mkdir(backupPath, 0644) diff --git a/app/http/controllers/task_controller.go b/app/http/controllers/task_controller.go index 275ec52f..1741ae00 100644 --- a/app/http/controllers/task_controller.go +++ b/app/http/controllers/task_controller.go @@ -62,7 +62,7 @@ func (r *TaskController) Log(ctx http.Context) { return } - log := tools.ExecShell("tail -n 30 " + task.Log) + log := tools.Exec("tail -n 30 " + task.Log) Success(ctx, log) } diff --git a/app/http/controllers/website_controller.go b/app/http/controllers/website_controller.go index 9ec136a1..3c5fa02c 100644 --- a/app/http/controllers/website_controller.go +++ b/app/http/controllers/website_controller.go @@ -111,8 +111,8 @@ func (c *WebsiteController) GetDefaultConfig(ctx http.Context) { if !Check(ctx, "openresty") { return } - index := tools.ReadFile("/www/server/openresty/html/index.html") - stop := tools.ReadFile("/www/server/openresty/html/stop.html") + index := tools.Read("/www/server/openresty/html/index.html") + stop := tools.Read("/www/server/openresty/html/stop.html") Success(ctx, http.Json{ "index": index, @@ -128,13 +128,13 @@ func (c *WebsiteController) SaveDefaultConfig(ctx http.Context) { index := ctx.Request().Input("index") stop := ctx.Request().Input("stop") - if !tools.WriteFile("/www/server/openresty/html/index.html", index, 0644) { + if !tools.Write("/www/server/openresty/html/index.html", index, 0644) { facades.Log().Error("[面板][WebsiteController] 保存默认配置失败") Error(ctx, http.StatusInternalServerError, "系统内部错误") return } - if !tools.WriteFile("/www/server/openresty/html/stop.html", stop, 0644) { + if !tools.Write("/www/server/openresty/html/stop.html", stop, 0644) { facades.Log().Error("[面板][WebsiteController] 保存默认配置失败") Error(ctx, http.StatusInternalServerError, "系统内部错误") return @@ -210,10 +210,10 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) { } // 原文 - raw := tools.ReadFile("/www/server/vhost/" + website.Name + ".conf") + raw := tools.Read("/www/server/vhost/" + website.Name + ".conf") if strings.TrimSpace(raw) != strings.TrimSpace(ctx.Request().Input("raw")) { - tools.WriteFile("/www/server/vhost/"+website.Name+".conf", ctx.Request().Input("raw"), 0644) - tools.ExecShell("systemctl reload openresty") + tools.Write("/www/server/vhost/"+website.Name+".conf", ctx.Request().Input("raw"), 0644) + tools.Exec("systemctl reload openresty") Success(ctx, nil) return } @@ -309,10 +309,10 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) { root += "/" } if ctx.Request().InputBool("open_basedir") { - tools.WriteFile(root+".user.ini", "open_basedir="+path+":/tmp/", 0644) + tools.Write(root+".user.ini", "open_basedir="+path+":/tmp/", 0644) } else { if tools.Exists(root + ".user.ini") { - tools.RemoveFile(root + ".user.ini") + tools.Remove(root + ".user.ini") } } @@ -337,8 +337,8 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) { // SSL ssl := ctx.Request().InputBool("ssl") website.Ssl = ssl - tools.WriteFile("/www/server/vhost/ssl/"+website.Name+".pem", ctx.Request().Input("ssl_certificate"), 0644) - tools.WriteFile("/www/server/vhost/ssl/"+website.Name+".key", ctx.Request().Input("ssl_certificate_key"), 0644) + tools.Write("/www/server/vhost/ssl/"+website.Name+".pem", ctx.Request().Input("ssl_certificate"), 0644) + tools.Write("/www/server/vhost/ssl/"+website.Name+".key", ctx.Request().Input("ssl_certificate_key"), 0644) if ssl { sslConfig := `# ssl标记位开始 ssl_certificate /www/server/vhost/ssl/` + website.Name + `.pem; @@ -395,9 +395,9 @@ func (c *WebsiteController) SaveConfig(ctx http.Context) { return } - tools.WriteFile("/www/server/vhost/"+website.Name+".conf", raw, 0644) - tools.WriteFile("/www/server/vhost/rewrite/"+website.Name+".conf", ctx.Request().Input("rewrite"), 0644) - tools.ExecShell("systemctl reload openresty") + tools.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644) + tools.Write("/www/server/vhost/rewrite/"+website.Name+".conf", ctx.Request().Input("rewrite"), 0644) + tools.Exec("systemctl reload openresty") Success(ctx, nil) } @@ -421,7 +421,7 @@ func (c *WebsiteController) ClearLog(ctx http.Context) { return } - tools.RemoveFile("/www/wwwlogs/" + website.Name + ".log") + tools.Remove("/www/wwwlogs/" + website.Name + ".log") Success(ctx, nil) } @@ -558,7 +558,7 @@ func (c *WebsiteController) DeleteBackup(ctx http.Context) { tools.Mkdir(backupPath, 0644) } - if !tools.RemoveFile(backupPath + "/" + fileName) { + if !tools.Remove(backupPath + "/" + fileName) { Error(ctx, http.StatusInternalServerError, "删除备份失败") return } @@ -651,9 +651,9 @@ server `, website.Path, website.Php, website.Name, website.Name, website.Name) - tools.WriteFile("/www/server/vhost/"+website.Name+".conf", raw, 0644) - tools.WriteFile("/www/server/vhost/rewrite"+website.Name+".conf", "", 0644) - tools.ExecShell("systemctl reload openresty") + tools.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644) + tools.Write("/www/server/vhost/rewrite"+website.Name+".conf", "", 0644) + tools.Exec("systemctl reload openresty") Success(ctx, nil) } @@ -683,7 +683,7 @@ func (c *WebsiteController) Status(ctx http.Context) { return } - raw := tools.ReadFile("/www/server/vhost/" + website.Name + ".conf") + raw := tools.Read("/www/server/vhost/" + website.Name + ".conf") // 运行目录 rootConfig := tools.Cut(raw, "# root标记位开始\n", "# root标记位结束") @@ -709,8 +709,8 @@ func (c *WebsiteController) Status(ctx http.Context) { } } - tools.WriteFile("/www/server/vhost/"+website.Name+".conf", raw, 0644) - tools.ExecShell("systemctl reload openresty") + tools.Write("/www/server/vhost/"+website.Name+".conf", raw, 0644) + tools.Exec("systemctl reload openresty") Success(ctx, nil) } diff --git a/app/services/backup.go b/app/services/backup.go index 05936fc1..1b6dc141 100644 --- a/app/services/backup.go +++ b/app/services/backup.go @@ -84,7 +84,7 @@ func (s *BackupImpl) WebSiteBackup(website models.Website) error { } backupFile := backupPath + "/" + website.Name + "_" + carbon.Now().ToShortDateTimeString() + ".zip" - tools.ExecShell(`cd '` + website.Path + `' && zip -r '` + backupFile + `' .`) + tools.Exec(`cd '` + website.Path + `' && zip -r '` + backupFile + `' .`) return nil } @@ -106,8 +106,8 @@ func (s *BackupImpl) WebsiteRestore(website models.Website, backupFile string) e return errors.New("备份文件不存在") } - tools.ExecShell(`rm -rf '` + website.Path + `/*'`) - tools.ExecShell(`unzip -o '` + backupFile + `' -d '` + website.Path + `' 2>&1`) + tools.Exec(`rm -rf '` + website.Path + `/*'`) + tools.Exec(`unzip -o '` + backupFile + `' -d '` + website.Path + `' 2>&1`) tools.Chmod(website.Path, 0755) tools.Chown(website.Path, "www", "www") @@ -158,9 +158,9 @@ func (s *BackupImpl) MysqlBackup(database string) error { return err } - tools.ExecShell("/www/server/mysql/bin/mysqldump -uroot " + database + " > " + backupPath + "/" + backupFile) - tools.ExecShell("cd " + backupPath + " && zip -r " + backupPath + "/" + backupFile + ".zip " + backupFile) - tools.RemoveFile(backupPath + "/" + backupFile) + tools.Exec("/www/server/mysql/bin/mysqldump -uroot " + database + " > " + backupPath + "/" + backupFile) + tools.Exec("cd " + backupPath + " && zip -r " + backupPath + "/" + backupFile + ".zip " + backupFile) + tools.Remove(backupPath + "/" + backupFile) _ = os.Unsetenv("MYSQL_PWD") return nil @@ -183,26 +183,26 @@ func (s *BackupImpl) MysqlRestore(database string, backupFile string) error { switch ext { case ".zip": - tools.ExecShell("unzip -o " + backupFile + " -d " + backupPath) + tools.Exec("unzip -o " + backupFile + " -d " + backupPath) backupFile = strings.TrimSuffix(backupFile, ext) case ".gz": if strings.HasSuffix(backupFile, ".tar.gz") { // 解压.tar.gz文件 - tools.ExecShell("tar -zxvf " + backupFile + " -C " + backupPath) + tools.Exec("tar -zxvf " + backupFile + " -C " + backupPath) backupFile = strings.TrimSuffix(backupFile, ".tar.gz") } else { // 解压.gz文件 - tools.ExecShell("gzip -d " + backupFile) + tools.Exec("gzip -d " + backupFile) backupFile = strings.TrimSuffix(backupFile, ext) } case ".bz2": - tools.ExecShell("bzip2 -d " + backupFile) + tools.Exec("bzip2 -d " + backupFile) backupFile = strings.TrimSuffix(backupFile, ext) case ".tar": - tools.ExecShell("tar -xvf " + backupFile + " -C " + backupPath) + tools.Exec("tar -xvf " + backupFile + " -C " + backupPath) backupFile = strings.TrimSuffix(backupFile, ext) case ".rar": - tools.ExecShell("unrar x " + backupFile + " " + backupPath) + tools.Exec("unrar x " + backupFile + " " + backupPath) backupFile = strings.TrimSuffix(backupFile, ext) } @@ -210,7 +210,7 @@ func (s *BackupImpl) MysqlRestore(database string, backupFile string) error { return errors.New("自动解压失败,请手动解压") } - tools.ExecShell("/www/server/mysql/bin/mysql -uroot " + database + " < " + backupFile) + tools.Exec("/www/server/mysql/bin/mysql -uroot " + database + " < " + backupFile) _ = os.Unsetenv("MYSQL_PWD") return nil @@ -255,9 +255,9 @@ func (s *BackupImpl) PostgresqlBackup(database string) error { tools.Mkdir(backupPath, 0644) } - tools.ExecShell(`su - postgres -c "pg_dump ` + database + `" > ` + backupPath + "/" + backupFile) - tools.ExecShell("cd " + backupPath + " && zip -r " + backupPath + "/" + backupFile + ".zip " + backupFile) - tools.RemoveFile(backupPath + "/" + backupFile) + tools.Exec(`su - postgres -c "pg_dump ` + database + `" > ` + backupPath + "/" + backupFile) + tools.Exec("cd " + backupPath + " && zip -r " + backupPath + "/" + backupFile + ".zip " + backupFile) + tools.Remove(backupPath + "/" + backupFile) return nil } @@ -273,26 +273,26 @@ func (s *BackupImpl) PostgresqlRestore(database string, backupFile string) error switch ext { case ".zip": - tools.ExecShell("unzip -o " + backupFile + " -d " + backupPath) + tools.Exec("unzip -o " + backupFile + " -d " + backupPath) backupFile = strings.TrimSuffix(backupFile, ext) case ".gz": if strings.HasSuffix(backupFile, ".tar.gz") { // 解压.tar.gz文件 - tools.ExecShell("tar -zxvf " + backupFile + " -C " + backupPath) + tools.Exec("tar -zxvf " + backupFile + " -C " + backupPath) backupFile = strings.TrimSuffix(backupFile, ".tar.gz") } else { // 解压.gz文件 - tools.ExecShell("gzip -d " + backupFile) + tools.Exec("gzip -d " + backupFile) backupFile = strings.TrimSuffix(backupFile, ext) } case ".bz2": - tools.ExecShell("bzip2 -d " + backupFile) + tools.Exec("bzip2 -d " + backupFile) backupFile = strings.TrimSuffix(backupFile, ext) case ".tar": - tools.ExecShell("tar -xvf " + backupFile + " -C " + backupPath) + tools.Exec("tar -xvf " + backupFile + " -C " + backupPath) backupFile = strings.TrimSuffix(backupFile, ext) case ".rar": - tools.ExecShell("unrar x " + backupFile + " " + backupPath) + tools.Exec("unrar x " + backupFile + " " + backupPath) backupFile = strings.TrimSuffix(backupFile, ext) } @@ -300,7 +300,7 @@ func (s *BackupImpl) PostgresqlRestore(database string, backupFile string) error return errors.New("自动解压失败,请手动解压") } - tools.ExecShell(`su - postgres -c "psql ` + database + `" < ` + backupFile) + tools.Exec(`su - postgres -c "psql ` + database + `" < ` + backupFile) return nil } diff --git a/app/services/cron.go b/app/services/cron.go index c7f40d29..49bf5095 100644 --- a/app/services/cron.go +++ b/app/services/cron.go @@ -22,11 +22,11 @@ func NewCronImpl() *CronImpl { // AddToSystem 添加到系统 func (r *CronImpl) AddToSystem(cron models.Cron) { if tools.IsRHEL() { - tools.ExecShell("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/root") - tools.ExecShell("systemctl restart crond") + tools.Exec("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/root") + tools.Exec("systemctl restart crond") } else { - tools.ExecShell("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/crontabs/root") - tools.ExecShell("systemctl restart cron") + tools.Exec("echo \"" + cron.Time + " " + cron.Shell + " >> " + cron.Log + " 2>&1\" >> /var/spool/cron/crontabs/root") + tools.Exec("systemctl restart cron") } } @@ -35,10 +35,10 @@ func (r *CronImpl) DeleteFromSystem(cron models.Cron) { // 需要转义Shell路径的/为\/ cron.Shell = strings.ReplaceAll(cron.Shell, "/", "\\/") if tools.IsRHEL() { - tools.ExecShell("sed -i '/" + cron.Shell + "/d' /var/spool/cron/root") - tools.ExecShell("systemctl restart crond") + tools.Exec("sed -i '/" + cron.Shell + "/d' /var/spool/cron/root") + tools.Exec("systemctl restart crond") } else { - tools.ExecShell("sed -i '/" + cron.Shell + "/d' /var/spool/cron/crontabs/root") - tools.ExecShell("systemctl restart cron") + tools.Exec("sed -i '/" + cron.Shell + "/d' /var/spool/cron/crontabs/root") + tools.Exec("systemctl restart cron") } } diff --git a/app/services/plugin.go b/app/services/plugin.go index 9ecca2da..93a22df5 100644 --- a/app/services/plugin.go +++ b/app/services/plugin.go @@ -19,6 +19,7 @@ import ( "panel/app/plugins/redis" "panel/app/plugins/s3fs" "panel/app/plugins/supervisor" + "panel/app/plugins/toolbox" ) // PanelPlugin 插件元数据结构 @@ -216,6 +217,17 @@ func (r *PluginImpl) All() []PanelPlugin { Uninstall: fail2ban.Uninstall, Update: fail2ban.Update, }) + p = append(p, PanelPlugin{ + Name: toolbox.Name, + Description: toolbox.Description, + Slug: toolbox.Slug, + Version: toolbox.Version, + Requires: toolbox.Requires, + Excludes: toolbox.Excludes, + Install: toolbox.Install, + Uninstall: toolbox.Uninstall, + Update: toolbox.Update, + }) return p } diff --git a/app/services/website.go b/app/services/website.go index a8f66f2a..a24deaf2 100644 --- a/app/services/website.go +++ b/app/services/website.go @@ -129,7 +129,7 @@ func (r *WebsiteImpl) Add(website PanelWebsite) (models.Website, error) { ` - tools.WriteFile(website.Path+"/index.html", index, 0644) + tools.Write(website.Path+"/index.html", index, 0644) domainArr := strings.Split(website.Domain, "\n") portList := "" @@ -217,24 +217,24 @@ server } `, portList, domainList, website.Path, website.Php, website.Name, website.Name, website.Name) - tools.WriteFile("/www/server/vhost/"+website.Name+".conf", nginxConf, 0644) - tools.WriteFile("/www/server/vhost/rewrite/"+website.Name+".conf", "", 0644) - tools.WriteFile("/www/server/vhost/ssl/"+website.Name+".pem", "", 0644) - tools.WriteFile("/www/server/vhost/ssl/"+website.Name+".key", "", 0644) + tools.Write("/www/server/vhost/"+website.Name+".conf", nginxConf, 0644) + tools.Write("/www/server/vhost/rewrite/"+website.Name+".conf", "", 0644) + tools.Write("/www/server/vhost/ssl/"+website.Name+".pem", "", 0644) + tools.Write("/www/server/vhost/ssl/"+website.Name+".key", "", 0644) tools.Chmod(r.setting.Get(models.SettingKeyWebsitePath), 0755) tools.Chmod(website.Path, 0755) tools.Chown(r.setting.Get(models.SettingKeyWebsitePath), "www", "www") tools.Chown(website.Path, "www", "www") - tools.ExecShell("systemctl reload openresty") + tools.Exec("systemctl reload openresty") rootPassword := r.setting.Get(models.SettingKeyMysqlRootPassword) if website.Db && website.DbType == "mysql" { - tools.ExecShell(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "CREATE DATABASE IF NOT EXISTS ` + website.DbName + ` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;"`) - tools.ExecShell(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "CREATE USER '` + website.DbUser + `'@'localhost' IDENTIFIED BY '` + website.DbPassword + `';"`) - tools.ExecShell(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "GRANT ALL PRIVILEGES ON ` + website.DbName + `.* TO '` + website.DbUser + `'@'localhost';"`) - tools.ExecShell(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "FLUSH PRIVILEGES;"`) + tools.Exec(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "CREATE DATABASE IF NOT EXISTS ` + website.DbName + ` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;"`) + tools.Exec(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "CREATE USER '` + website.DbUser + `'@'localhost' IDENTIFIED BY '` + website.DbPassword + `';"`) + tools.Exec(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "GRANT ALL PRIVILEGES ON ` + website.DbName + `.* TO '` + website.DbUser + `'@'localhost';"`) + tools.Exec(`/www/server/mysql/bin/mysql -uroot -p` + rootPassword + ` -e "FLUSH PRIVILEGES;"`) } return w, nil @@ -251,13 +251,13 @@ func (r *WebsiteImpl) Delete(id int) error { return err } - tools.RemoveFile("/www/server/vhost/" + website.Name + ".conf") - tools.RemoveFile("/www/server/vhost/rewrite/" + website.Name + ".conf") - tools.RemoveFile("/www/server/vhost/ssl/" + website.Name + ".pem") - tools.RemoveFile("/www/server/vhost/ssl/" + website.Name + ".key") - tools.RemoveFile(website.Path) + tools.Remove("/www/server/vhost/" + website.Name + ".conf") + tools.Remove("/www/server/vhost/rewrite/" + website.Name + ".conf") + tools.Remove("/www/server/vhost/ssl/" + website.Name + ".pem") + tools.Remove("/www/server/vhost/ssl/" + website.Name + ".key") + tools.Remove(website.Path) - tools.ExecShell("systemctl reload openresty") + tools.Exec("systemctl reload openresty") return nil } @@ -269,7 +269,7 @@ func (r *WebsiteImpl) GetConfig(id int) (WebsiteSetting, error) { return WebsiteSetting{}, err } - config := tools.ReadFile("/www/server/vhost/" + website.Name + ".conf") + config := tools.Read("/www/server/vhost/" + website.Name + ".conf") var setting WebsiteSetting setting.Name = website.Name @@ -303,7 +303,7 @@ func (r *WebsiteImpl) GetConfig(id int) (WebsiteSetting, error) { } if tools.Exists(setting.Root + "/.user.ini") { - userIni := tools.ReadFile(setting.Path + "/.user.ini") + userIni := tools.Read(setting.Path + "/.user.ini") if strings.Contains(userIni, "open_basedir") { setting.OpenBasedir = true } else { @@ -313,8 +313,8 @@ func (r *WebsiteImpl) GetConfig(id int) (WebsiteSetting, error) { setting.OpenBasedir = false } - setting.SslCertificate = tools.ReadFile("/www/server/vhost/ssl/" + website.Name + ".pem") - setting.SslCertificateKey = tools.ReadFile("/www/server/vhost/ssl/" + website.Name + ".key") + setting.SslCertificate = tools.Read("/www/server/vhost/ssl/" + website.Name + ".pem") + setting.SslCertificateKey = tools.Read("/www/server/vhost/ssl/" + website.Name + ".key") if setting.Ssl { ssl := tools.Cut(config, "# ssl标记位开始", "# ssl标记位结束") setting.HttpRedirect = strings.Contains(ssl, "# http重定向标记位") @@ -339,8 +339,8 @@ func (r *WebsiteImpl) GetConfig(id int) (WebsiteSetting, error) { setting.WafCache = match[1] } - setting.Rewrite = tools.ReadFile("/www/server/vhost/rewrite/" + website.Name + ".conf") - setting.Log = tools.Escape(tools.ExecShell(`tail -n 100 '/www/wwwlogs/` + website.Name + `.log'`)) + setting.Rewrite = tools.Read("/www/server/vhost/rewrite/" + website.Name + ".conf") + setting.Log = tools.Escape(tools.Exec(`tail -n 100 '/www/wwwlogs/` + website.Name + `.log'`)) return setting, nil } diff --git a/pkg/ssh/ssh.go b/pkg/ssh/ssh.go index 1048d919..911dde2f 100644 --- a/pkg/ssh/ssh.go +++ b/pkg/ssh/ssh.go @@ -68,5 +68,5 @@ func NewSSHClient(conf *SSHClientConfig) (*ssh.Client, error) { } func getKey(keyPath string) (ssh.Signer, error) { - return ssh.ParsePrivateKey([]byte(tools.ReadFile(keyPath))) + return ssh.ParsePrivateKey([]byte(tools.Read(keyPath))) } diff --git a/pkg/tools/system.go b/pkg/tools/system.go index 778f201b..94b796c2 100644 --- a/pkg/tools/system.go +++ b/pkg/tools/system.go @@ -9,8 +9,8 @@ import ( "github.com/goravel/framework/facades" ) -// WriteFile 写入文件 -func WriteFile(path string, data string, permission os.FileMode) bool { +// Write 写入文件 +func Write(path string, data string, permission os.FileMode) bool { if err := os.MkdirAll(filepath.Dir(path), permission); err != nil { facades.Log().Errorf("[面板][Helpers] 创建目录失败: %s", err.Error()) return false @@ -25,8 +25,8 @@ func WriteFile(path string, data string, permission os.FileMode) bool { return true } -// ReadFile 读取文件 -func ReadFile(path string) string { +// Read 读取文件 +func Read(path string) string { data, err := os.ReadFile(path) if err != nil { facades.Log().Errorf("[面板][Helpers] 读取文件 %s 失败: %s", path, err.Error()) @@ -36,8 +36,8 @@ func ReadFile(path string) string { return string(data) } -// RemoveFile 删除文件 -func RemoveFile(path string) bool { +// Remove 删除文件 +func Remove(path string) bool { if err := os.Remove(path); err != nil { facades.Log().Errorf("[面板][Helpers] 删除文件 %s 失败: %s", path, err.Error()) return false @@ -46,8 +46,8 @@ func RemoveFile(path string) bool { return true } -// ExecShell 执行 shell 命令 -func ExecShell(shell string) string { +// Exec 执行 shell 命令 +func Exec(shell string) string { cmd := exec.Command("bash", "-c", shell) output, err := cmd.CombinedOutput() @@ -59,8 +59,8 @@ func ExecShell(shell string) string { return strings.TrimSpace(string(output)) } -// ExecShellAsync 异步执行 shell 命令 -func ExecShellAsync(shell string) { +// ExecAsync 异步执行 shell 命令 +func ExecAsync(shell string) { cmd := exec.Command("bash", "-c", shell) err := cmd.Start() @@ -126,33 +126,32 @@ func Empty(path string) bool { } // Mv 移动路径 -func Mv(src, dst string) bool { +func Mv(src, dst string) (bool, error) { cmd := exec.Command("mv", src, dst) err := cmd.Run() if err != nil { facades.Log().Errorf("[面板][Helpers] 移动 %s 到 %s 失败: %s", src, dst, err.Error()) - return false + return false, err } - return true + return true, nil } // Cp 复制路径 -func Cp(src, dst string) bool { +func Cp(src, dst string) (bool, error) { cmd := exec.Command("cp", "-r", src, dst) err := cmd.Run() if err != nil { - facades.Log().Errorf("[面板][Helpers] 复制 %s 到 %s 失败: %s", src, dst, err.Error()) - return false + return false, err } - return true + return true, nil } // Size 获取路径大小 -func Size(path string) int64 { +func Size(path string) (int64, error) { var size int64 err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { @@ -160,20 +159,18 @@ func Size(path string) int64 { return nil }) if err != nil { - facades.Log().Errorf("[面板][Helpers] 获取路径 %s 大小失败: %s", path, err.Error()) - return 0 + return 0, err } - return size + return size, nil } // FileSize 获取文件大小 -func FileSize(path string) int64 { +func FileSize(path string) (int64, error) { info, err := os.Stat(path) if err != nil { - facades.Log().Errorf("[面板][Helpers] 获取文件 %s 大小失败: %s", path, err.Error()) - return 0 + return 0, err } - return info.Size() + return info.Size(), nil } diff --git a/pkg/tools/system_test.go b/pkg/tools/system_test.go index 1dc1f9c0..e2c6ccf9 100644 --- a/pkg/tools/system_test.go +++ b/pkg/tools/system_test.go @@ -17,45 +17,45 @@ func TestSystemHelperTestSuite(t *testing.T) { suite.Run(t, &SystemHelperTestSuite{}) } -func (s *SystemHelperTestSuite) TestWriteFile() { +func (s *SystemHelperTestSuite) TestWrite() { filePath := "/tmp/testfile" defer os.Remove(filePath) - s.True(WriteFile(filePath, "test data", 0644)) + s.True(Write(filePath, "test data", 0644)) s.FileExists(filePath) content, _ := os.ReadFile(filePath) s.Equal("test data", string(content)) } -func (s *SystemHelperTestSuite) TestReadFile() { +func (s *SystemHelperTestSuite) TestRead() { filePath := "/tmp/testfile" defer os.Remove(filePath) err := os.WriteFile(filePath, []byte("test data"), 0644) s.Nil(err) - s.Equal("test data", ReadFile(filePath)) + s.Equal("test data", Read(filePath)) } -func (s *SystemHelperTestSuite) TestRemoveFile() { +func (s *SystemHelperTestSuite) TestRemove() { filePath := "/tmp/testfile" err := os.WriteFile(filePath, []byte("test data"), 0644) s.Nil(err) - s.True(RemoveFile(filePath)) + s.True(Remove(filePath)) } -func (s *SystemHelperTestSuite) TestExecShell() { - s.Equal("test", ExecShell("echo 'test'")) +func (s *SystemHelperTestSuite) TestExec() { + s.Equal("test", Exec("echo 'test'")) } -func (s *SystemHelperTestSuite) TestExecShellAsync() { +func (s *SystemHelperTestSuite) TestExecAsync() { command := "echo 'test' > /tmp/testfile" defer os.Remove("/tmp/testfile") - ExecShellAsync(command) + ExecAsync(command) time.Sleep(time.Second) @@ -105,12 +105,36 @@ func (s *SystemHelperTestSuite) TestEmpty() { s.False(Empty("/tmp")) } +func (s *SystemHelperTestSuite) TestMv() { + filePath := "/tmp/testfile" + defer os.Remove(filePath) + + err := os.WriteFile(filePath, []byte("test data"), 0644) + s.Nil(err) + + s.True(Mv(filePath, "/tmp/testfile2")) + s.False(Exists(filePath)) +} + +func (s *SystemHelperTestSuite) TestCp() { + filePath := "/tmp/testfile" + defer os.Remove(filePath) + + err := os.WriteFile(filePath, []byte("test data"), 0644) + s.Nil(err) + + s.True(Cp(filePath, "/tmp/testfile2")) + s.True(Exists(filePath)) +} + func (s *SystemHelperTestSuite) TestSize() { - s.Equal(int64(0), Size("/tmp/123")) - s.NotEqual(int64(0), Size("/tmp")) + size, err := Size("/tmp/123") + s.Equal(int64(0), size) + s.Error(err) } func (s *SystemHelperTestSuite) TestFileSize() { - s.Equal(int64(0), FileSize("/tmp/123")) - s.NotEqual(int64(0), FileSize("/tmp")) + size, err := FileSize("/tmp/123") + s.Equal(int64(0), size) + s.Error(err) } diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index 411d32d0..84e394c6 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -127,42 +127,42 @@ func UpdatePanel(proxy bool) error { color.Greenln("使用代理: " + strconv.FormatBool(proxy)) color.Greenln("备份面板配置...") - ExecShell("cp -f /www/panel/database/panel.db /tmp/panel.db.bak") - ExecShell("cp -f /www/panel/panel.conf /tmp/panel.conf.bak") + Exec("cp -f /www/panel/database/panel.db /tmp/panel.db.bak") + Exec("cp -f /www/panel/panel.conf /tmp/panel.conf.bak") if !Exists("/tmp/panel.db.bak") || !Exists("/tmp/panel.conf.bak") { return errors.New("备份面板配置失败") } color.Greenln("备份完成") color.Greenln("清理旧版本...") - ExecShell("rm -rf /www/panel/*") + Exec("rm -rf /www/panel/*") color.Greenln("清理完成") color.Greenln("正在下载...") if proxy { - ExecShell("wget -O /www/panel/panel.zip https://ghproxy.com/" + panelInfo.DownloadUrl) + Exec("wget -O /www/panel/panel.zip https://ghproxy.com/" + panelInfo.DownloadUrl) } else { - ExecShell("wget -O /www/panel/panel.zip " + panelInfo.DownloadUrl) + Exec("wget -O /www/panel/panel.zip " + panelInfo.DownloadUrl) } color.Greenln("下载完成") color.Greenln("更新新版本...") - ExecShell("cd /www/panel && unzip -o panel.zip && rm -rf panel.zip && chmod 700 panel && bash scripts/update_panel.sh") + Exec("cd /www/panel && unzip -o panel.zip && rm -rf panel.zip && chmod 700 panel && bash scripts/update_panel.sh") color.Greenln("更新完成") color.Greenln("恢复面板配置...") - ExecShell("cp -f /tmp/panel.db.bak /www/panel/database/panel.db") - ExecShell("cp -f /tmp/panel.conf.bak /www/panel/panel.conf") + Exec("cp -f /tmp/panel.db.bak /www/panel/database/panel.db") + Exec("cp -f /tmp/panel.conf.bak /www/panel/panel.conf") if !Exists("/www/panel/database/panel.db") || !Exists("/www/panel/panel.conf") { return errors.New("恢复面板配置失败") } - ExecShell("/www/panel/panel --env=panel.conf artisan migrate") + Exec("/www/panel/panel --env=panel.conf artisan migrate") color.Greenln("恢复完成") - ExecShell("panel writeSetting version " + panelInfo.Version) + Exec("panel writeSetting version " + panelInfo.Version) color.Greenln("重启面板...") - ExecShell("systemctl restart panel") + Exec("systemctl restart panel") color.Greenln("重启完成") return nil