2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 03:07:20 +08:00

refactor: 重构os包

This commit is contained in:
耗子
2024-06-23 01:08:07 +08:00
parent 85c38e5bc5
commit c876f4c484
13 changed files with 54 additions and 71 deletions

View File

@@ -3,7 +3,7 @@ package controllers
import (
"fmt"
stdio "io"
"os"
stdos "os"
"path/filepath"
"strings"
"syscall"
@@ -13,6 +13,7 @@ import (
requests "github.com/TheTNB/panel/app/http/requests/file"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -353,7 +354,7 @@ func (r *FileController) Permission(ctx http.Context) http.Response {
return sanitize
}
if err := io.Chmod(request.Path, os.FileMode(request.Mode)); err != nil {
if err := io.Chmod(request.Path, stdos.FileMode(request.Mode)); err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
if err := io.Chown(request.Path, request.Owner, request.Group); err != nil {
@@ -432,8 +433,8 @@ func (r *FileController) Search(ctx http.Context) http.Response {
return sanitize
}
paths := make(map[string]os.FileInfo)
err := filepath.Walk(request.Path, func(path string, info os.FileInfo, err error) error {
paths := make(map[string]stdos.FileInfo)
err := filepath.Walk(request.Path, func(path string, info stdos.FileInfo, err error) error {
if err != nil {
return err
}
@@ -467,7 +468,7 @@ func (r *FileController) List(ctx http.Context) http.Response {
return sanitize
}
fileInfoList, err := os.ReadDir(request.Path)
fileInfoList, err := io.ReadDir(request.Path)
if err != nil {
return Error(ctx, http.StatusInternalServerError, err.Error())
}
@@ -483,8 +484,8 @@ func (r *FileController) List(ctx http.Context) http.Response {
"size": tools.FormatBytes(float64(info.Size())),
"mode_str": info.Mode().String(),
"mode": fmt.Sprintf("%04o", info.Mode().Perm()),
"owner": tools.GetUser(stat.Uid),
"group": tools.GetGroup(stat.Gid),
"owner": os.GetUser(stat.Uid),
"group": os.GetGroup(stat.Gid),
"uid": stat.Uid,
"gid": stat.Gid,
"hidden": io.IsHidden(info.Name()),
@@ -505,6 +506,6 @@ func (r *FileController) List(ctx http.Context) http.Response {
// setPermission
func (r *FileController) setPermission(path string, mode uint, owner, group string) {
_ = io.Chmod(path, os.FileMode(mode))
_ = io.Chmod(path, stdos.FileMode(mode))
_ = io.Chown(path, owner, group)
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
@@ -167,7 +168,7 @@ ignoreregex =
var err error
switch jailName {
case "ssh":
if tools.IsDebian() {
if os.IsDebian() {
logPath = "/var/log/auth.log"
} else {
logPath = "/var/log/secure"

View File

@@ -1,7 +1,6 @@
package plugins
import (
"os"
"regexp"
"strings"
@@ -11,9 +10,9 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
type PhpMyAdminController struct {
@@ -24,7 +23,7 @@ func NewPhpMyAdminController() *PhpMyAdminController {
}
func (r *PhpMyAdminController) Info(ctx http.Context) http.Response {
files, err := os.ReadDir("/www/server/phpmyadmin")
files, err := io.ReadDir("/www/server/phpmyadmin")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "找不到 phpMyAdmin 目录")
}
@@ -72,7 +71,7 @@ func (r *PhpMyAdminController) SetPort(ctx http.Context) http.Response {
return controllers.ErrorSystem(ctx)
}
if tools.IsRHEL() {
if os.IsRHEL() {
if out, err := shell.Execf("firewall-cmd --zone=public --add-port=%d/tcp --permanent", port); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, out)
}

View File

@@ -9,9 +9,9 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
"github.com/TheTNB/panel/types"
)
@@ -155,7 +155,7 @@ func (r *PureFtpdController) SetPort(ctx http.Context) http.Response {
if out, err := shell.Execf(`sed -i "s/Bind.*/Bind 0.0.0.0,%s/g" /www/server/pure-ftpd/etc/pure-ftpd.conf`, port); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, out)
}
if tools.IsRHEL() {
if os.IsRHEL() {
if out, err := shell.Execf("firewall-cmd --zone=public --add-port=%s/tcp --permanent", port); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, out)
}

View File

@@ -9,9 +9,9 @@ import (
"github.com/TheTNB/panel/app/http/controllers"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
type SupervisorController struct {
@@ -20,7 +20,7 @@ type SupervisorController struct {
func NewSupervisorController() *SupervisorController {
var service string
if tools.IsRHEL() {
if os.IsRHEL() {
service = "supervisord"
} else {
service = "supervisor"
@@ -59,7 +59,7 @@ func (r *SupervisorController) ClearLog(ctx http.Context) http.Response {
func (r *SupervisorController) Config(ctx http.Context) http.Response {
var config string
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
config, err = io.Read(`/etc/supervisord.conf`)
} else {
config, err = io.Read(`/etc/supervisor/supervisord.conf`)
@@ -76,7 +76,7 @@ func (r *SupervisorController) Config(ctx http.Context) http.Response {
func (r *SupervisorController) SaveConfig(ctx http.Context) http.Response {
config := ctx.Request().Input("config")
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
err = io.Write(`/etc/supervisord.conf`, config, 0644)
} else {
err = io.Write(`/etc/supervisor/supervisord.conf`, config, 0644)
@@ -173,7 +173,7 @@ func (r *SupervisorController) ProcessLog(ctx http.Context) http.Response {
process := ctx.Request().Input("process")
var logPath string
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, process)
} else {
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, process)
@@ -196,7 +196,7 @@ func (r *SupervisorController) ClearProcessLog(ctx http.Context) http.Response {
process := ctx.Request().Input("process")
var logPath string
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, process)
} else {
logPath, err = shell.Execf(`cat '/etc/supervisor/conf.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, process)
@@ -218,7 +218,7 @@ func (r *SupervisorController) ProcessConfig(ctx http.Context) http.Response {
process := ctx.Request().Query("process")
var config string
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
config, err = io.Read(`/etc/supervisord.d/` + process + `.conf`)
} else {
config, err = io.Read(`/etc/supervisor/conf.d/` + process + `.conf`)
@@ -236,7 +236,7 @@ func (r *SupervisorController) SaveProcessConfig(ctx http.Context) http.Response
process := ctx.Request().Input("process")
config := ctx.Request().Input("config")
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
err = io.Write(`/etc/supervisord.d/`+process+`.conf`, config, 0644)
} else {
err = io.Write(`/etc/supervisor/conf.d/`+process+`.conf`, config, 0644)
@@ -284,7 +284,7 @@ stdout_logfile_maxbytes=2MB
`
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
err = io.Write(`/etc/supervisord.d/`+name+`.conf`, config, 0644)
} else {
err = io.Write(`/etc/supervisor/conf.d/`+name+`.conf`, config, 0644)
@@ -310,7 +310,7 @@ func (r *SupervisorController) DeleteProcess(ctx http.Context) http.Response {
var logPath string
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
logPath, err = shell.Execf(`cat '/etc/supervisord.d/%s.conf' | grep stdout_logfile= | awk -F "=" '{print $2}'`, process)
if err := io.Remove(`/etc/supervisord.d/` + process + `.conf`); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())

View File

@@ -8,9 +8,9 @@ import (
"github.com/spf13/cast"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
type SafeController struct {
@@ -19,7 +19,7 @@ type SafeController struct {
func NewSafeController() *SafeController {
var ssh string
if tools.IsRHEL() {
if os.IsRHEL() {
ssh = "sshd"
} else {
ssh = "ssh"
@@ -39,7 +39,7 @@ func (r *SafeController) GetFirewallStatus(ctx http.Context) http.Response {
func (r *SafeController) SetFirewallStatus(ctx http.Context) http.Response {
var err error
if ctx.Request().InputBool("status") {
if tools.IsRHEL() {
if os.IsRHEL() {
err = systemctl.Start("firewalld")
if err == nil {
err = systemctl.Enable("firewalld")
@@ -54,7 +54,7 @@ func (r *SafeController) SetFirewallStatus(ctx http.Context) http.Response {
}
}
} else {
if tools.IsRHEL() {
if os.IsRHEL() {
err = systemctl.Stop("firewalld")
if err == nil {
err = systemctl.Disable("firewalld")
@@ -84,7 +84,7 @@ func (r *SafeController) GetFirewallRules(ctx http.Context) http.Response {
}
var rules []map[string]string
if tools.IsRHEL() {
if os.IsRHEL() {
out, err := shell.Execf("firewall-cmd --list-all")
if err != nil {
return Error(ctx, http.StatusInternalServerError, out)
@@ -160,7 +160,7 @@ func (r *SafeController) AddFirewallRule(ctx http.Context) http.Response {
}
}
if tools.IsRHEL() {
if os.IsRHEL() {
if out, err := shell.Execf("firewall-cmd --remove-port=%s/%s --permanent", port, protocol); err != nil {
return Error(ctx, http.StatusInternalServerError, out)
}
@@ -201,7 +201,7 @@ func (r *SafeController) DeleteFirewallRule(ctx http.Context) http.Response {
return Error(ctx, http.StatusUnprocessableEntity, "参数错误")
}
if tools.IsRHEL() {
if os.IsRHEL() {
if out, err := shell.Execf("firewall-cmd --remove-port=%s/%s --permanent", port, protocol); err != nil {
return Error(ctx, http.StatusInternalServerError, out)
}
@@ -223,7 +223,7 @@ func (r *SafeController) DeleteFirewallRule(ctx http.Context) http.Response {
// firewallStatus 获取防火墙状态
func (r *SafeController) firewallStatus() bool {
var running bool
if tools.IsRHEL() {
if os.IsRHEL() {
running, _ = systemctl.Status("firewalld")
} else {
running, _ = systemctl.Status("ufw")
@@ -297,7 +297,7 @@ func (r *SafeController) SetSshPort(ctx http.Context) http.Response {
// GetPingStatus 获取 Ping 状态
func (r *SafeController) GetPingStatus(ctx http.Context) http.Response {
if tools.IsRHEL() {
if os.IsRHEL() {
out, err := shell.Execf(`firewall-cmd --list-all`)
if err != nil {
return Error(ctx, http.StatusInternalServerError, out)
@@ -325,7 +325,7 @@ func (r *SafeController) GetPingStatus(ctx http.Context) http.Response {
func (r *SafeController) SetPingStatus(ctx http.Context) http.Response {
var out string
var err error
if tools.IsRHEL() {
if os.IsRHEL() {
if ctx.Request().InputBool("status") {
out, err = shell.Execf(`firewall-cmd --permanent --remove-rich-rule='rule protocol value=icmp drop'`)
} else {
@@ -343,7 +343,7 @@ func (r *SafeController) SetPingStatus(ctx http.Context) http.Response {
return Error(ctx, http.StatusInternalServerError, out)
}
if tools.IsRHEL() {
if os.IsRHEL() {
out, err = shell.Execf(`firewall-cmd --reload`)
} else {
out, err = shell.Execf(`ufw reload`)

View File

@@ -10,6 +10,7 @@ import (
"github.com/TheTNB/panel/internal"
"github.com/TheTNB/panel/internal/services"
"github.com/TheTNB/panel/pkg/io"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/tools"
)
@@ -162,7 +163,7 @@ func (r *SettingController) Update(ctx http.Context) http.Response {
if out, err := shell.Execf("sed -i 's/APP_PORT=%s/APP_PORT=%s/g' /www/panel/panel.conf", oldPort, port); err != nil {
return Error(ctx, http.StatusInternalServerError, out)
}
if tools.IsRHEL() {
if os.IsRHEL() {
if out, err := shell.Execf("firewall-cmd --remove-port=%s/tcp --permanent", oldPort); err != nil {
return Error(ctx, http.StatusInternalServerError, out)
}

View File

@@ -6,9 +6,9 @@ import (
"strings"
"github.com/TheTNB/panel/app/models"
"github.com/TheTNB/panel/pkg/os"
"github.com/TheTNB/panel/pkg/shell"
"github.com/TheTNB/panel/pkg/systemctl"
"github.com/TheTNB/panel/pkg/tools"
)
type CronImpl struct {
@@ -20,14 +20,14 @@ func NewCronImpl() *CronImpl {
// AddToSystem 添加到系统
func (r *CronImpl) AddToSystem(cron models.Cron) error {
if tools.IsRHEL() {
if os.IsRHEL() {
if _, err := shell.Execf(fmt.Sprintf(`echo "%s %s >> %s 2>&1" >> /var/spool/cron/root`, cron.Time, cron.Shell, cron.Log)); err != nil {
return err
}
return systemctl.Restart("crond")
}
if tools.IsDebian() {
if os.IsDebian() {
if _, err := shell.Execf(fmt.Sprintf(`echo "%s %s >> %s 2>&1" >> /var/spool/cron/crontabs/root`, cron.Time, cron.Shell, cron.Log)); err != nil {
return err
}
@@ -41,14 +41,14 @@ func (r *CronImpl) AddToSystem(cron models.Cron) error {
func (r *CronImpl) DeleteFromSystem(cron models.Cron) error {
// 需要转义 shell 路径的/为\/
cron.Shell = strings.ReplaceAll(cron.Shell, "/", "\\/")
if tools.IsRHEL() {
if os.IsRHEL() {
if _, err := shell.Execf("sed -i '/" + cron.Shell + "/d' /var/spool/cron/root"); err != nil {
return err
}
return systemctl.Restart("crond")
}
if tools.IsDebian() {
if os.IsDebian() {
if _, err := shell.Execf("sed -i '/" + cron.Shell + "/d' /var/spool/cron/crontabs/root"); err != nil {
return err
}

View File

@@ -1,14 +1,11 @@
package io
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/goravel/framework/support/env"
)
// Remove 删除文件/目录
@@ -23,20 +20,12 @@ func Mkdir(path string, permission os.FileMode) error {
// Chmod 修改文件/目录权限
func Chmod(path string, permission os.FileMode) error {
if env.IsWindows() {
return errors.New("chmod is not supported on Windows")
}
cmd := exec.Command("chmod", "-R", fmt.Sprintf("%o", permission), path)
return cmd.Run()
}
// Chown 修改文件或目录所有者
func Chown(path, user, group string) error {
if env.IsWindows() {
return errors.New("chown is not supported on Windows")
}
cmd := exec.Command("chown", "-R", user+":"+group, path)
return cmd.Run()
}
@@ -155,3 +144,8 @@ func Size(path string) (int64, error) {
func TempDir(prefix string) (string, error) {
return os.MkdirTemp("", prefix)
}
// ReadDir 读取目录
func ReadDir(path string) ([]os.DirEntry, error) {
return os.ReadDir(path)
}

View File

@@ -1,4 +1,4 @@
package tools
package os
import (
"os"

View File

@@ -1,9 +1,8 @@
package tools
package os
import (
"testing"
"github.com/goravel/framework/support/env"
"github.com/stretchr/testify/suite"
)
@@ -16,15 +15,9 @@ func TestOSHelperTestSuite(t *testing.T) {
}
func (s *OSHelperTestSuite) TestIsDebian() {
if env.IsWindows() {
return
}
s.True(IsDebian())
}
func (s *OSHelperTestSuite) TestIsRHEL() {
if env.IsWindows() {
return
}
s.False(IsRHEL())
}

View File

@@ -1,8 +1,9 @@
package tools
package os
import (
"github.com/spf13/cast"
"os/user"
"github.com/spf13/cast"
)
// GetUser 通过 uid 获取用户名

View File

@@ -3,7 +3,6 @@ package tools
import (
"testing"
"github.com/goravel/framework/support/env"
"github.com/stretchr/testify/suite"
)
@@ -80,18 +79,12 @@ func (s *HelperTestSuite) TestGenerateVersions() {
}
func (s *HelperTestSuite) TestGetLatestPanelVersion() {
if env.IsWindows() {
return
}
version, err := GetLatestPanelVersion()
s.NotEmpty(version)
s.Nil(err)
}
func (s *HelperTestSuite) TestGetPanelVersion() {
if env.IsWindows() {
return
}
version, err := GetPanelVersion("v2.1.29")
s.NotEmpty(version)
s.Nil(err)