feat: 实现电脑端被控与控制界面
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
//go:build !windows
|
||||
|
||||
package shell
|
||||
|
||||
import "os/exec"
|
||||
|
||||
func hide(cmd *exec.Cmd) {}
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build windows
|
||||
|
||||
package shell
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func hide(cmd *exec.Cmd) {
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
HideWindow: true,
|
||||
CreationFlags: 0x08000000, // CREATE_NO_WINDOW
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Runner 执行一段 shell。测试注入假实现。
|
||||
type Runner interface {
|
||||
Run(ctx context.Context, script string) (output string, err error)
|
||||
}
|
||||
|
||||
// Interpreter 返回当前系统的解释器和参数。
|
||||
// Windows 用 cmd.exe /c,macOS 和 Linux 用 /bin/sh -c。
|
||||
func Interpreter(script string) (name string, args []string) {
|
||||
if runtime.GOOS == "windows" {
|
||||
return "cmd.exe", []string{"/c", script}
|
||||
}
|
||||
return "/bin/sh", []string{"-c", script}
|
||||
}
|
||||
|
||||
// System 在当前登录用户的权限下执行。
|
||||
type System struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func (s System) Run(ctx context.Context, script string) (string, error) {
|
||||
timeout := s.Timeout
|
||||
if timeout <= 0 {
|
||||
timeout = 45 * time.Second
|
||||
}
|
||||
cctx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
name, args := Interpreter(script)
|
||||
cmd := exec.CommandContext(cctx, name, args...)
|
||||
hide(cmd)
|
||||
var buf limited
|
||||
cmd.Stdout = &buf
|
||||
cmd.Stderr = &buf
|
||||
err := cmd.Run()
|
||||
out := strings.TrimSpace(buf.String())
|
||||
if cctx.Err() == context.DeadlineExceeded {
|
||||
return out, context.DeadlineExceeded
|
||||
}
|
||||
return out, err
|
||||
}
|
||||
|
||||
// Summarize 把执行结果收成一句短话,不把系统内部错误原样抛出。
|
||||
func Summarize(output string, err error) (ok bool, detail string) {
|
||||
line := firstLine(output)
|
||||
if err == nil {
|
||||
if line == "" {
|
||||
return true, "已完成"
|
||||
}
|
||||
return true, clip(line, 120)
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return false, "执行超时"
|
||||
}
|
||||
if line != "" {
|
||||
return false, clip(line, 120)
|
||||
}
|
||||
return false, "没有成功"
|
||||
}
|
||||
|
||||
func firstLine(s string) string {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
if i := strings.IndexAny(s, "\r\n"); i >= 0 {
|
||||
s = s[:i]
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func clip(s string, n int) string {
|
||||
if utf8.RuneCountInString(s) <= n {
|
||||
return s
|
||||
}
|
||||
r := []rune(s)
|
||||
return string(r[:n]) + "…"
|
||||
}
|
||||
|
||||
// Summary 是日志里的命令摘要。
|
||||
func Summary(script string) string {
|
||||
s := firstLine(script)
|
||||
return clip(s, 80)
|
||||
}
|
||||
|
||||
type limited struct {
|
||||
buf bytes.Buffer
|
||||
}
|
||||
|
||||
func (l *limited) Write(p []byte) (int, error) {
|
||||
const max = 8192
|
||||
n := len(p)
|
||||
if l.buf.Len() >= max {
|
||||
return n, nil
|
||||
}
|
||||
room := max - l.buf.Len()
|
||||
if len(p) > room {
|
||||
p = p[:room]
|
||||
}
|
||||
_, _ = l.buf.Write(p)
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (l *limited) String() string { return l.buf.String() }
|
||||
@@ -0,0 +1,42 @@
|
||||
package shell
|
||||
|
||||
import (
|
||||
"context"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestInterpreter(t *testing.T) {
|
||||
name, args := Interpreter("echo hi")
|
||||
if runtime.GOOS == "windows" {
|
||||
if name != "cmd.exe" || len(args) != 2 || args[0] != "/c" || args[1] != "echo hi" {
|
||||
t.Fatalf("windows interpreter: %s %v", name, args)
|
||||
}
|
||||
return
|
||||
}
|
||||
if name != "/bin/sh" || len(args) != 2 || args[0] != "-c" || args[1] != "echo hi" {
|
||||
t.Fatalf("unix interpreter: %s %v", name, args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSystemEcho(t *testing.T) {
|
||||
out, err := (System{Timeout: 5 * time.Second}).Run(context.Background(), "echo tapremly-ok")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(out, "tapremly-ok") {
|
||||
t.Fatalf("output %q", out)
|
||||
}
|
||||
}
|
||||
func TestSummarize(t *testing.T) {
|
||||
ok, detail := Summarize("hello\r\nworld", nil)
|
||||
if !ok || detail != "hello" {
|
||||
t.Fatalf("got %v %q", ok, detail)
|
||||
}
|
||||
ok, detail = Summarize("", context.DeadlineExceeded)
|
||||
if ok || detail != "执行超时" {
|
||||
t.Fatalf("got %v %q", ok, detail)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user