Skip to content

Commit

Permalink
refactor: 重构 tools.Read 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 13, 2023
1 parent 2b1b58e commit 83cbae0
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 64 deletions.
7 changes: 6 additions & 1 deletion app/http/controllers/cron_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ func (r *CronController) Script(ctx http.Context) http.Response {
return Error(ctx, http.StatusUnprocessableEntity, "计划任务不存在")
}

return Success(ctx, tools.Read(cron.Shell))
shell, err := tools.Read(cron.Shell)
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

return Success(ctx, shell)
}

// Update 更新计划任务
Expand Down
26 changes: 19 additions & 7 deletions app/http/controllers/plugins/fail2ban_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func (r *Fail2banController) List(ctx http.Context) http.Response {

page := ctx.Request().QueryInt("page", 1)
limit := ctx.Request().QueryInt("limit", 10)
raw := tools.Read("/etc/fail2ban/jail.local")
if len(raw) == 0 {
return controllers.Error(ctx, http.StatusUnprocessableEntity, "Fail2ban 规则为空")
raw, err := tools.Read("/etc/fail2ban/jail.local")
if err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}

jailList := regexp.MustCompile(`\[(.*?)]`).FindAllStringSubmatch(raw, -1)
Expand Down Expand Up @@ -200,7 +200,10 @@ func (r *Fail2banController) Add(ctx http.Context) http.Response {
jailWebsiteMode := ctx.Request().Input("website_mode")
jailWebsitePath := ctx.Request().Input("website_path")

raw := tools.Read("/etc/fail2ban/jail.local")
raw, err := tools.Read("/etc/fail2ban/jail.local")
if err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if (strings.Contains(raw, "["+jailName+"]") && jailType == "service") || (strings.Contains(raw, "["+jailWebsiteName+"]"+"-cc") && jailType == "website" && jailWebsiteMode == "cc") || (strings.Contains(raw, "["+jailWebsiteName+"]"+"-path") && jailType == "website" && jailWebsiteMode == "path") {
return controllers.Error(ctx, http.StatusUnprocessableEntity, "规则已存在")
}
Expand Down Expand Up @@ -324,7 +327,10 @@ func (r *Fail2banController) Delete(ctx http.Context) http.Response {
}

jailName := ctx.Request().Input("name")
raw := tools.Read("/etc/fail2ban/jail.local")
raw, err := tools.Read("/etc/fail2ban/jail.local")
if err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
if !strings.Contains(raw, "["+jailName+"]") {
return controllers.Error(ctx, http.StatusUnprocessableEntity, "规则不存在")
}
Expand Down Expand Up @@ -421,7 +427,10 @@ func (r *Fail2banController) SetWhiteList(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusUnprocessableEntity, "缺少参数")
}

raw := tools.Read("/etc/fail2ban/jail.local")
raw, err := tools.Read("/etc/fail2ban/jail.local")
if err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
// 正则替换
reg := regexp.MustCompile(`ignoreip\s*=\s*.*\n`)
if reg.MatchString(raw) {
Expand All @@ -447,7 +456,10 @@ func (r *Fail2banController) GetWhiteList(ctx http.Context) http.Response {
return check
}

raw := tools.Read("/etc/fail2ban/jail.local")
raw, err := tools.Read("/etc/fail2ban/jail.local")
if err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}
reg := regexp.MustCompile(`ignoreip\s*=\s*(.*)\n`)
if reg.MatchString(raw) {
ignoreIp := reg.FindStringSubmatch(raw)[1]
Expand Down
5 changes: 2 additions & 3 deletions app/http/controllers/plugins/mysql57_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ func (r *Mysql57Controller) GetConfig(ctx http.Context) http.Response {
return check
}

// 获取配置
config := tools.Read("/www/server/mysql/conf/my.cnf")
if len(config) == 0 {
config, err := tools.Read("/www/server/mysql/conf/my.cnf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL配置失败")
}

Expand Down
5 changes: 2 additions & 3 deletions app/http/controllers/plugins/mysql80_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,8 @@ func (r *Mysql80Controller) GetConfig(ctx http.Context) http.Response {
return check
}

// 获取配置
config := tools.Read("/www/server/mysql/conf/my.cnf")
if len(config) == 0 {
config, err := tools.Read("/www/server/mysql/conf/my.cnf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取MySQL配置失败")
}

Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/openresty_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (r *OpenRestyController) GetConfig(ctx http.Context) http.Response {
return check
}

config := tools.Read("/www/server/openresty/conf/nginx.conf")
if len(config) == 0 {
config, err := tools.Read("/www/server/openresty/conf/nginx.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取OpenResty配置失败")
}

Expand Down
6 changes: 5 additions & 1 deletion app/http/controllers/plugins/php74_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ func (r *Php74Controller) GetConfig(ctx http.Context) http.Response {
return check
}

config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
}

return controllers.Success(ctx, config)
}

Expand Down
6 changes: 5 additions & 1 deletion app/http/controllers/plugins/php80_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ func (r *Php80Controller) GetConfig(ctx http.Context) http.Response {
return check
}

config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
}

return controllers.Success(ctx, config)
}

Expand Down
6 changes: 5 additions & 1 deletion app/http/controllers/plugins/php81_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ func (r *Php81Controller) GetConfig(ctx http.Context) http.Response {
return check
}

config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
}

return controllers.Success(ctx, config)
}

Expand Down
6 changes: 5 additions & 1 deletion app/http/controllers/plugins/php82_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ func (r *Php82Controller) GetConfig(ctx http.Context) http.Response {
return check
}

config := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
config, err := tools.Read("/www/server/php/" + r.version + "/etc/php.ini")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PHP-"+r.version+"配置失败")
}

return controllers.Success(ctx, config)
}

Expand Down
13 changes: 9 additions & 4 deletions app/http/controllers/plugins/phpmyadmin_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func (r *PhpMyAdminController) Info(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "找不到 phpMyAdmin 目录")
}

conf := tools.Read("/www/server/vhost/phpmyadmin.conf")
conf, err := tools.Read("/www/server/vhost/phpmyadmin.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
match := regexp.MustCompile(`listen\s+(\d+);`).FindStringSubmatch(conf)
if len(match) == 0 {
return controllers.Error(ctx, http.StatusInternalServerError, "找不到 phpMyAdmin 端口")
Expand All @@ -64,7 +67,10 @@ func (r *PhpMyAdminController) SetPort(ctx http.Context) http.Response {
return controllers.Error(ctx, http.StatusInternalServerError, "端口不能为空")
}

conf := tools.Read("/www/server/vhost/phpmyadmin.conf")
conf, err := tools.Read("/www/server/vhost/phpmyadmin.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
conf = regexp.MustCompile(`listen\s+(\d+);`).ReplaceAllString(conf, "listen "+port+";")
if err := tools.Write("/www/server/vhost/phpmyadmin.conf", conf, 0644); err != nil {
facades.Log().Request(ctx.Request()).Tags("插件", "phpMyAdmin").With(map[string]any{
Expand All @@ -89,8 +95,7 @@ func (r *PhpMyAdminController) SetPort(ctx http.Context) http.Response {
}
}

err := tools.ServiceReload("openresty")
if err != nil {
if err := tools.ServiceReload("openresty"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重载OpenResty失败")
}

Expand Down
8 changes: 4 additions & 4 deletions app/http/controllers/plugins/postgresql15_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (r *Postgresql15Controller) GetConfig(ctx http.Context) http.Response {
}

// 获取配置
config := tools.Read("/www/server/postgresql/data/postgresql.conf")
if len(config) == 0 {
config, err := tools.Read("/www/server/postgresql/data/postgresql.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
}

Expand All @@ -119,8 +119,8 @@ func (r *Postgresql15Controller) GetUserConfig(ctx http.Context) http.Response {
}

// 获取配置
config := tools.Read("/www/server/postgresql/data/pg_hba.conf")
if len(config) == 0 {
config, err := tools.Read("/www/server/postgresql/data/pg_hba.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
}

Expand Down
8 changes: 4 additions & 4 deletions app/http/controllers/plugins/postgresql16_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (r *Postgresql16Controller) GetConfig(ctx http.Context) http.Response {
}

// 获取配置
config := tools.Read("/www/server/postgresql/data/postgresql.conf")
if len(config) == 0 {
config, err := tools.Read("/www/server/postgresql/data/postgresql.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
}

Expand All @@ -119,8 +119,8 @@ func (r *Postgresql16Controller) GetUserConfig(ctx http.Context) http.Response {
}

// 获取配置
config := tools.Read("/www/server/postgresql/data/pg_hba.conf")
if len(config) == 0 {
config, err := tools.Read("/www/server/postgresql/data/pg_hba.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取PostgreSQL配置失败")
}

Expand Down
4 changes: 2 additions & 2 deletions app/http/controllers/plugins/redis_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func (r *RedisController) GetConfig(ctx http.Context) http.Response {
}

// 获取配置
config := tools.Read("/www/server/redis/redis.conf")
if len(config) == 0 {
config, err := tools.Read("/www/server/redis/redis.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取Redis配置失败")
}

Expand Down
18 changes: 14 additions & 4 deletions app/http/controllers/plugins/supervisor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ func (r *SupervisorController) Config(ctx http.Context) http.Response {
}

var config string
var err error
if tools.IsRHEL() {
config = tools.Read(`/etc/supervisord.conf`)
config, err = tools.Read(`/etc/supervisord.conf`)
} else {
config = tools.Read(`/etc/supervisor/supervisord.conf`)
config, err = tools.Read(`/etc/supervisor/supervisord.conf`)
}

if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, config)
Expand Down Expand Up @@ -342,10 +347,15 @@ func (r *SupervisorController) ProcessConfig(ctx http.Context) http.Response {

process := ctx.Request().Query("process")
var config string
var err error
if tools.IsRHEL() {
config = tools.Read(`/etc/supervisord.d/` + process + `.conf`)
config, err = tools.Read(`/etc/supervisord.d/` + process + `.conf`)
} else {
config = tools.Read(`/etc/supervisor/conf.d/` + process + `.conf`)
config, err = tools.Read(`/etc/supervisor/conf.d/` + process + `.conf`)
}

if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, config)
Expand Down
12 changes: 10 additions & 2 deletions app/http/controllers/plugins/toolbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func (r *ToolBoxController) GetDNS(ctx http.Context) http.Response {
return check
}

raw := tools.Read("/etc/resolv.conf")
raw, err := tools.Read("/etc/resolv.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}
match := regexp.MustCompile(`nameserver\s+(\S+)`).FindAllStringSubmatch(raw, -1)
if len(match) == 0 {
return controllers.Error(ctx, http.StatusInternalServerError, "找不到 DNS 信息")
Expand Down Expand Up @@ -228,7 +231,12 @@ func (r *ToolBoxController) GetHosts(ctx http.Context) http.Response {
return check
}

return controllers.Success(ctx, tools.Read("/etc/hosts"))
hosts, err := tools.Read("/etc/hosts")
if err != nil {
return controllers.Error(ctx, http.StatusUnprocessableEntity, err.Error())
}

return controllers.Success(ctx, hosts)
}

// SetHosts 设置 hosts 信息
Expand Down
5 changes: 4 additions & 1 deletion app/http/controllers/safe_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ func (r *SafeController) GetPingStatus(ctx http.Context) http.Response {
return Success(ctx, false)
}
} else {
config := tools.Read("/etc/ufw/before.rules")
config, err := tools.Read("/etc/ufw/before.rules")
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if strings.Contains(config, "-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT") {
return Success(ctx, true)
} else {
Expand Down
17 changes: 12 additions & 5 deletions app/http/controllers/website_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,14 @@ func (r *WebsiteController) GetDefaultConfig(ctx http.Context) http.Response {
if check != nil {
return check
}
index := tools.Read("/www/server/openresty/html/index.html")
stop := tools.Read("/www/server/openresty/html/stop.html")
index, err := tools.Read("/www/server/openresty/html/index.html")
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
stop, err := tools.Read("/www/server/openresty/html/stop.html")
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

return Success(ctx, http.Json{
"index": index,
Expand Down Expand Up @@ -642,17 +648,18 @@ func (r *WebsiteController) Status(ctx http.Context) http.Response {

website := models.Website{}
if err := facades.Orm().Query().Where("id", idRequest.ID).Get(&website); err != nil {
facades.Log().Info("[面板][WebsiteController] 获取网站信息失败 ", err)
return ErrorSystem(ctx)
}

website.Status = ctx.Request().InputBool("status")
if err := facades.Orm().Query().Save(&website); err != nil {
facades.Log().Info("[面板][WebsiteController] 保存网站配置失败 ", err)
return ErrorSystem(ctx)
}

raw := tools.Read("/www/server/vhost/" + website.Name + ".conf")
raw, err := tools.Read("/www/server/vhost/" + website.Name + ".conf")
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}

// 运行目录
rootConfig := tools.Cut(raw, "# root标记位开始\n", "# root标记位结束")
Expand Down
Loading

0 comments on commit 83cbae0

Please sign in to comment.