重构命令执行器和防火墙管理逻辑

- 优化Base.go中防火墙清空命令,使用更结构化的命令执行方式
- 修改Proxy.go中Xray安装命令,改用SingleLineCommandExecutor
- 重构Excutor.go,新增RealTimeCommandExecutor方法,支持实时输出命令执行日志
- 简化命令执行日志输出,移除冗余的日志前缀
- 改进管道命令执行的日志记录,增加可读性
This commit is contained in:
zeaslity
2025-03-11 16:07:19 +08:00
parent 35646ff89f
commit 34b5f80704
3 changed files with 89 additions and 24 deletions

View File

@@ -146,8 +146,57 @@ func addBaseSubcommands(cmd *cobra.Command) {
// 清空路由表 // 清空路由表
log.Info("清空路由表...") log.Info("清空路由表...")
op.HardCodeCommandExecutor("iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -t raw -F")
op.HardCodeCommandExecutor("ip6tables -F && ip6tables -t nat -F && ip6tables -t mangle -F && ip6tables -t raw -F") commandExecutor := [][]string{
{
"iptables",
"-F",
},
{
"iptables",
"-t",
"nat",
"-F",
},
{
"iptables",
"-t",
"mangle",
"-F",
},
{
"iptables",
"-t",
"raw",
"-F",
},
{
"ip6tables",
"-F",
},
{
"ip6tables",
"-t",
"nat",
"-F",
},
{
"ip6tables",
"-t",
"mangle",
"-F",
},
{
"ip6tables",
"-t",
"raw",
"-F",
},
}
for _, command := range commandExecutor {
op.SingleLineCommandExecutor(command)
}
log.Info("Firewall 关闭成功!") log.Info("Firewall 关闭成功!")
}, },
@@ -272,7 +321,7 @@ func addHarborSubcommands(harborCmd *cobra.Command) {
// 运行install.sh // 运行install.sh
op.SingleLineCommandExecutor([]string{"cd", "/root/wdd/harbor"}) op.SingleLineCommandExecutor([]string{"cd", "/root/wdd/harbor"})
ok, log1 := op.SingleLineCommandExecutor([]string{"/bin/bash", "/root/wdd/harbor/install.sh"}) ok, log1 := op.RealTimeCommandExecutor([]string{"/bin/bash", "/root/wdd/harbor/install.sh"})
if !ok { if !ok {
log.Error("安装harbor失败: %s", log1) log.Error("安装harbor失败: %s", log1)
return return

View File

@@ -288,7 +288,7 @@ func installXray() {
os.Chmod("/tmp/install-release.sh", 0777) os.Chmod("/tmp/install-release.sh", 0777)
// 执行安装脚本 // 执行安装脚本
ok, resultLog := op.HardCodeCommandExecutor("/bin/bash /tmp/install-release.sh -u root install") ok, resultLog := op.SingleLineCommandExecutor([]string{"/bin/bash", "/tmp/install-release.sh", "-u", "root", "install"})
if !ok { if !ok {
log.Error("Install Xray failed ! error is %s", resultLog) log.Error("Install Xray failed ! error is %s", resultLog)
return return

View File

@@ -5,6 +5,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
"io"
"os/exec" "os/exec"
"strings" "strings"
) )
@@ -17,7 +18,7 @@ import (
// []string - 合并后的标准输出和标准错误内容(按行分割) // []string - 合并后的标准输出和标准错误内容(按行分割)
func SingleLineCommandExecutor(singleLineCommand []string) (ok bool, resultLog []string) { func SingleLineCommandExecutor(singleLineCommand []string) (ok bool, resultLog []string) {
log.Info("[Excutor] - start => %v", singleLineCommand) log.Info("start => %v", singleLineCommand)
if len(singleLineCommand) == 0 { if len(singleLineCommand) == 0 {
return false, nil return false, nil
@@ -79,13 +80,21 @@ func PipeLineCommandExecutor(pipeLineCommand [][]string) (ok bool, resultLog []s
return false, nil return false, nil
} }
// 转换为 管道命令的字符串格式
// [][]string{{"ps", "aux"}, {"grep", "nginx"}, {"wc", "-l"}} 转换为 ps aux | grep nginx | wc -l
pipeLineCommandStr := ""
// 预检所有子命令 // 预检所有子命令
for _, cmd := range pipeLineCommand { for _, cmd := range pipeLineCommand {
if len(cmd) == 0 { if len(cmd) == 0 {
return false, nil return false, nil
} }
pipeLineCommandStr += fmt.Sprintf("%v | ", cmd)
} }
pipeLineCommandStr = strings.TrimSuffix(pipeLineCommandStr, " | ")
log.Info("start => %v", pipeLineCommandStr)
// 创建命令组 // 创建命令组
cmds := make([]*exec.Cmd, len(pipeLineCommand)) cmds := make([]*exec.Cmd, len(pipeLineCommand))
for i, args := range pipeLineCommand { for i, args := range pipeLineCommand {
@@ -161,37 +170,44 @@ func PipeLineCommandExecutor(pipeLineCommand [][]string) (ok bool, resultLog []s
return success, output return success, output
} }
// HardCodeCommandExecutor 执行硬编码命令,返回执行结果和输出内容 // RealTimeCommandExecutor 执行命令,需要实时打印输出执行命令的日志,并返回执行结果和输出内容
// hardCodeCommand: 硬编码命令,如 "echo hello" // realTimeCommand: 实时命令,如 "docker load -i /root/wdd/harbor/harbor-offline-installer-v2.10.1.tgz"
// 返回值: // 返回值:
// //
// bool - 命令是否执行成功true为成功false为失败 // bool - 命令是否执行成功true为成功false为失败
// []string - 合并后的标准输出和标准错误内容(按行分割) // []string - 合并后的标准输出和标准错误内容(按行分割)
func HardCodeCommandExecutor(hardCodeCommand string) (ok bool, resultLog []string) { func RealTimeCommandExecutor(realTimeCommand []string) (ok bool, resultLog []string) {
if len(realTimeCommand) == 0 {
log.Info("[HardCodeCommandExecutor] - start => %v", hardCodeCommand)
if hardCodeCommand == "" {
return false, nil return false, nil
} }
// 执行命令 cmd := exec.Command(realTimeCommand[0], realTimeCommand[1:]...)
cmd := exec.Command(hardCodeCommand)
// 执行命令并获取错误信息
err := cmd.Run()
stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
return false, []string{"HardCodeCommandExecutor " + hardCodeCommand + " 执行命令失败", err.Error()} return false, []string{err.Error()}
} }
// 合并输出结果 stderr, err := cmd.StderrPipe()
stdoutBuf := bytes.Buffer{} if err != nil {
stderrBuf := bytes.Buffer{} return false, []string{err.Error()}
cmd.Stdout = &stdoutBuf }
cmd.Stderr = &stderrBuf
output := mergeOutput(&stdoutBuf, &stderrBuf) if err := cmd.Start(); err != nil {
return false, []string{err.Error()}
}
output := make([]string, 0)
scanner := bufio.NewScanner(io.MultiReader(stdout, stderr))
for scanner.Scan() {
line := scanner.Text()
fmt.Println(line)
output = append(output, line)
}
if err := cmd.Wait(); err != nil {
return false, append(output, err.Error())
}
return true, output return true, output
} }