2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 23:27:17 +08:00

refactor: 重构 tools.Read 函数

This commit is contained in:
耗子
2023-11-14 02:08:26 +08:00
parent 2b1b58ea2b
commit 83cbae034c
21 changed files with 147 additions and 64 deletions

View File

@@ -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)
@@ -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, "规则已存在")
}
@@ -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, "规则不存在")
}
@@ -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) {
@@ -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]

View File

@@ -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配置失败")
}

View File

@@ -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配置失败")
}

View File

@@ -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配置失败")
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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 端口")
@@ -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{
@@ -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失败")
}

View File

@@ -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配置失败")
}
@@ -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配置失败")
}

View File

@@ -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配置失败")
}
@@ -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配置失败")
}

View File

@@ -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配置失败")
}

View File

@@ -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)
@@ -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)

View File

@@ -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 信息")
@@ -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 信息