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

- 优化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

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