package ai import ( "context" "fmt" "strings" "time" "github.com/miaopanel/miaopanel/internal/site" "github.com/miaopanel/miaopanel/internal/system" ) var commandWhitelist = map[string]bool{ "systemctl": true, "journalctl": true, "df": true, "du": true, "free": true, "ps": true, "ss": true, "nginx": true, "ip": true, "uptime": true, "uname": true, "lsblk": true, "whoami": true, "hostname": true, "date": true, // Common read-only inspection commands. Mutations remain behind write_file, // site actions or the terminal's explicit confirmation path. "ls": true, "find": true, "cat": true, "head": true, "tail": true, "grep": true, "wc": true, "sort": true, "uniq": true, "cut": true, "tr": true, "stat": true, "file": true, "readlink": true, "realpath": true, "pwd": true, "echo": true, "printf": true, "id": true, "env": true, "mount": true, "lsof": true, "netstat": true, "tree": true, } // registerWriteTools wires operations that change panel or host state. Every // call creates an approval card; nothing runs until the operator confirms. func (s *Service) registerWriteTools(r *Registry) { r.Register(Tool{ Name: "create_site", Description: "创建一个新站点。kind 为 static、php、go、node 或 proxy。", Parameters: objectSchema(map[string]any{ "name": prop("string", "站点名称"), "kind": prop("string", "站点类型:static / php / go / node / proxy"), "root": prop("string", "网站根目录,必须位于面板 web_root 内"), "domains": arrayProp("绑定域名列表", "string"), "port": prop("integer", "运行端口,0 表示自动分配"), "php_version": prop("string", "PHP 版本,php 站点可填"), "run_user": prop("string", "运行用户,默认 www"), "upstream": prop("string", "反向代理上游,proxy 站点可填"), "remark": prop("string", "备注"), }, "name", "kind", "root"), Risk: RiskWrite, Summary: func(args map[string]any) string { return fmt.Sprintf("创建站点「%s」(%s)", argString(args, "name"), argString(args, "kind")) }, Run: func(ctx context.Context, args map[string]any) (string, error) { name := argString(args, "name") kind := argString(args, "kind") root := argString(args, "root") if name == "" || kind == "" || root == "" { return "", fmt.Errorf("name、kind、root 均为必填") } switch kind { case "static", "php", "go", "node", "proxy": default: return "", fmt.Errorf("kind 不支持: %s", kind) } port, _ := argInt(args, "port") in := site.Input{ Name: name, Kind: kind, Domains: argStrings(args, "domains"), Root: root, Port: port, PHPVersion: argString(args, "php_version"), RunUser: argString(args, "run_user"), Upstream: argString(args, "upstream"), Remark: argString(args, "remark"), } st, err := s.sites.Create(ctx, in) if err != nil { return "", err } return jsonText(map[string]any{ "id": st.ID, "name": st.Name, "slug": st.Slug, "kind": st.Kind, "domains": st.Domains, "port": st.Port, }) }, }) r.Register(Tool{ Name: "site_action", Description: "启动、停止或重启一个站点。", Parameters: objectSchema(map[string]any{ "id": prop("integer", "站点 id"), "action": prop("string", "start / stop / restart"), }, "id", "action"), Risk: RiskWrite, Summary: func(args map[string]any) string { id, _ := argInt(args, "id") return fmt.Sprintf("对站点 #%d 执行 %s", id, argString(args, "action")) }, Run: func(ctx context.Context, args map[string]any) (string, error) { id, ok := argInt(args, "id") if !ok || id <= 0 { return "", fmt.Errorf("缺少站点 id") } action := argString(args, "action") switch action { case "start", "stop", "restart": default: return "", fmt.Errorf("action 只能是 start、stop 或 restart") } if err := s.sites.Action(ctx, int64(id), action); err != nil { return "", err } return jsonText(map[string]any{"id": id, "action": action, "status": "ok"}) }, }) r.Register(Tool{ Name: "write_file", Description: "写入网站根目录内的文本文件,内容不超过 64KB。", Parameters: objectSchema(map[string]any{ "path": prop("string", "文件路径,相对于网站根目录"), "content": prop("string", "文件内容"), }, "path", "content"), Risk: RiskWrite, Summary: func(args map[string]any) string { return "写入文件 " + argString(args, "path") }, Run: func(ctx context.Context, args map[string]any) (string, error) { path := argString(args, "path") content, ok := args["content"].(string) if path == "" || !ok { return "", fmt.Errorf("path 和 content 均为必填") } if len(content) > 64<<10 { return "", fmt.Errorf("内容超过 64KB 上限") } if err := s.files.Write(path, content); err != nil { return "", err } return jsonText(map[string]any{"path": path, "bytes": len(content)}) }, }) r.Register(Tool{ Name: "reload_nginx", Description: "校验并重载 nginx 配置。", Parameters: objectSchema(map[string]any{}), Risk: RiskWrite, Summary: func(map[string]any) string { return "重载 nginx 配置" }, Run: func(ctx context.Context, args map[string]any) (string, error) { if err := system.ReloadNginx(ctx, s.runner, s.cfg.Nginx.Bin); err != nil { return "", err } return jsonText(map[string]any{"status": "ok"}) }, }) r.Register(Tool{ Name: "run_command", Description: "执行一条只读系统命令(argv 形式,不允许管道与重定向)。", Parameters: objectSchema(map[string]any{ "program": prop("string", "命令程序,例如 df、systemctl、journalctl"), "args": arrayProp("命令参数", "string"), "timeout_s": prop("integer", "超时秒数,默认 30,最大 60"), }, "program"), Risk: RiskDanger, Summary: func(args map[string]any) string { parts := []string{argString(args, "program")} parts = append(parts, argStrings(args, "args")...) return "执行命令: " + strings.Join(parts, " ") }, Run: func(ctx context.Context, args map[string]any) (string, error) { program := argString(args, "program") if !commandWhitelist[program] { return "", fmt.Errorf("程序 %q 不在允许列表中", program) } cmdArgs := argStrings(args, "args") if len(cmdArgs) > 20 { return "", fmt.Errorf("参数过多") } for _, a := range cmdArgs { if len(a) > 500 || strings.ContainsAny(a, "\n\r\x00") { return "", fmt.Errorf("参数包含非法字符或过长") } } timeout, _ := argInt(args, "timeout_s") if timeout <= 0 || timeout > 60 { timeout = 30 } res, err := s.runner.RunLong(ctx, time.Duration(timeout)*time.Second, program, cmdArgs...) out := map[string]any{"program": program, "args": cmdArgs} if res != nil { out["stdout"] = clipResult(res.Stdout, 8<<10) out["stderr"] = clipResult(res.Stderr, 4<<10) out["code"] = res.Code } if err != nil { out["error"] = err.Error() } raw, _ := jsonText(out) return raw, nil }, }) }