mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 03:07:20 +08:00
* Initial plan * feat: 添加容器编排实时进度显示功能 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 修复代码审查问题,使用动态路径和安全的字体大小操作 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * docs: 完成容器编排实时进度显示功能 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 容器优化 * fix: 用户关闭命令窗口时杀死正在运行的进程 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * refactor: 改进 WebSocket 断开检测,支持用户输入转发到 PTY Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 启用终端用户输入支持,转发键盘输入到 PTY Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 容器优化 * fix: lint * feat: 添加 PTY 窗口大小调整支持 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * feat: 支持读取输入 --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> Co-authored-by: 耗子 <haozi@loli.email>
230 lines
5.5 KiB
Go
230 lines
5.5 KiB
Go
package shell
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/creack/pty"
|
|
)
|
|
|
|
// Exec 执行 shell 命令
|
|
func Exec(shell string) (string, error) {
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.Command("bash", "-c", shell)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return strings.TrimSpace(stdout.String()), fmt.Errorf("run %s failed, err: %s", shell, strings.TrimSpace(stderr.String()))
|
|
}
|
|
|
|
return strings.TrimSpace(stdout.String()), nil
|
|
}
|
|
|
|
// Execf 安全执行 shell 命令
|
|
func Execf(shell string, args ...any) (string, error) {
|
|
if !preCheckArg(args) {
|
|
return "", errors.New("command contains illegal characters")
|
|
}
|
|
if len(args) > 0 {
|
|
shell = fmt.Sprintf(shell, args...)
|
|
}
|
|
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.Command("bash", "-c", shell)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return strings.TrimSpace(stdout.String()), fmt.Errorf("run %s failed, err: %s", shell, strings.TrimSpace(stderr.String()))
|
|
}
|
|
|
|
return strings.TrimSpace(stdout.String()), nil
|
|
}
|
|
|
|
// ExecfAsync 异步执行 shell 命令
|
|
func ExecfAsync(shell string, args ...any) error {
|
|
if !preCheckArg(args) {
|
|
return errors.New("command contains illegal characters")
|
|
}
|
|
if len(args) > 0 {
|
|
shell = fmt.Sprintf(shell, args...)
|
|
}
|
|
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.Command("bash", "-c", shell)
|
|
|
|
err := cmd.Start()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
go func() {
|
|
if err = cmd.Wait(); err != nil {
|
|
fmt.Println(fmt.Errorf("run %s failed, err: %s", shell, strings.TrimSpace(err.Error())))
|
|
}
|
|
}()
|
|
|
|
return nil
|
|
}
|
|
|
|
// ExecfWithTimeout 执行 shell 命令并设置超时时间
|
|
func ExecfWithTimeout(timeout time.Duration, shell string, args ...any) (string, error) {
|
|
if !preCheckArg(args) {
|
|
return "", errors.New("command contains illegal characters")
|
|
}
|
|
if len(args) > 0 {
|
|
shell = fmt.Sprintf(shell, args...)
|
|
}
|
|
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.Command("bash", "-c", shell)
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
|
|
err := cmd.Start()
|
|
if err != nil {
|
|
return strings.TrimSpace(stdout.String()), fmt.Errorf("run %s failed, err: %s", shell, strings.TrimSpace(stderr.String()))
|
|
}
|
|
|
|
done := make(chan error)
|
|
go func() {
|
|
done <- cmd.Wait()
|
|
}()
|
|
|
|
select {
|
|
case <-time.After(timeout):
|
|
_ = cmd.Process.Kill()
|
|
return strings.TrimSpace(stdout.String()), fmt.Errorf("run %s failed, err: %s", shell, "timeout")
|
|
case err = <-done:
|
|
if err != nil {
|
|
return strings.TrimSpace(stdout.String()), fmt.Errorf("run %s failed, err: %s", shell, strings.TrimSpace(stderr.String()))
|
|
}
|
|
}
|
|
|
|
return strings.TrimSpace(stdout.String()), err
|
|
}
|
|
|
|
// ExecfWithOutput 执行 shell 命令并输出到终端
|
|
func ExecfWithOutput(shell string, args ...any) error {
|
|
if !preCheckArg(args) {
|
|
return errors.New("command contains illegal characters")
|
|
}
|
|
if len(args) > 0 {
|
|
shell = fmt.Sprintf(shell, args...)
|
|
}
|
|
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.Command("bash", "-c", shell)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd.Run()
|
|
}
|
|
|
|
// ExecfWithPipe 执行 shell 命令并返回管道
|
|
func ExecfWithPipe(ctx context.Context, shell string, args ...any) (io.ReadCloser, error) {
|
|
if !preCheckArg(args) {
|
|
return nil, errors.New("command contains illegal characters")
|
|
}
|
|
if len(args) > 0 {
|
|
shell = fmt.Sprintf(shell, args...)
|
|
}
|
|
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.CommandContext(ctx, "bash", "-c", shell)
|
|
|
|
out, err := cmd.StdoutPipe()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
cmd.Stderr = cmd.Stdout
|
|
err = cmd.Start()
|
|
|
|
go func() { _ = cmd.Wait() }()
|
|
|
|
return out, err
|
|
}
|
|
|
|
// ExecfWithDir 在指定目录下执行 shell 命令
|
|
func ExecfWithDir(dir, shell string, args ...any) (string, error) {
|
|
if !preCheckArg(args) {
|
|
return "", errors.New("command contains illegal characters")
|
|
}
|
|
if len(args) > 0 {
|
|
shell = fmt.Sprintf(shell, args...)
|
|
}
|
|
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.Command("bash", "-c", shell)
|
|
cmd.Dir = dir
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
cmd.Stdout = &stdout
|
|
cmd.Stderr = &stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
return strings.TrimSpace(stdout.String()), fmt.Errorf("run %s failed, err: %s", shell, strings.TrimSpace(stderr.String()))
|
|
}
|
|
|
|
return strings.TrimSpace(stdout.String()), nil
|
|
}
|
|
|
|
// ExecfWithTTY 在伪终端下执行 shell 命令
|
|
func ExecfWithTTY(shell string, args ...any) (string, error) {
|
|
if !preCheckArg(args) {
|
|
return "", errors.New("command contains illegal characters")
|
|
}
|
|
if len(args) > 0 {
|
|
shell = fmt.Sprintf(shell, args...)
|
|
}
|
|
|
|
_ = os.Setenv("LC_ALL", "C")
|
|
cmd := exec.Command("bash", "-i", "-c", shell)
|
|
|
|
var out bytes.Buffer
|
|
var stderr bytes.Buffer
|
|
cmd.Stderr = &stderr // https://github.com/creack/pty/issues/147 取 stderr
|
|
|
|
f, err := pty.Start(cmd)
|
|
if err != nil {
|
|
return "", fmt.Errorf("run %s failed", shell)
|
|
}
|
|
defer func(f *os.File) { _ = f.Close() }(f)
|
|
|
|
if _, err = io.Copy(&out, f); IsPTYError(err) != nil {
|
|
return "", fmt.Errorf("run %s failed, out: %s, err: %w", shell, strings.TrimSpace(out.String()), err)
|
|
}
|
|
if stderr.Len() > 0 {
|
|
return "", fmt.Errorf("run %s failed, out: %s", shell, strings.TrimSpace(stderr.String()))
|
|
}
|
|
|
|
return strings.TrimSpace(out.String()), nil
|
|
}
|
|
|
|
func preCheckArg(args []any) bool {
|
|
illegals := []any{`&`, `|`, `;`, `$`, `'`, `"`, "`", `(`, `)`, "\n", "\r", `>`, `<`}
|
|
for arg := range slices.Values(args) {
|
|
if slices.Contains(illegals, arg) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
}
|