仰望星辰工作室

miaopanel

miaopanel/ internal/ai/tools_write.go 7.2 KB · 228 行 原始文件
1package ai
2
3import (
4 "context"
5 "fmt"
6 "strings"
7 "time"
8
9 "github.com/miaopanel/miaopanel/internal/site"
10 "github.com/miaopanel/miaopanel/internal/system"
11)
12
13var commandWhitelist = map[string]bool{
14 "systemctl": true,
15 "journalctl": true,
16 "df": true,
17 "du": true,
18 "free": true,
19 "ps": true,
20 "ss": true,
21 "nginx": true,
22 "ip": true,
23 "uptime": true,
24 "uname": true,
25 "lsblk": true,
26 "whoami": true,
27 "hostname": true,
28 "date": true,
29 // Common read-only inspection commands. Mutations remain behind write_file,
30 // site actions or the terminal's explicit confirmation path.
31 "ls": true,
32 "find": true,
33 "cat": true,
34 "head": true,
35 "tail": true,
36 "grep": true,
37 "wc": true,
38 "sort": true,
39 "uniq": true,
40 "cut": true,
41 "tr": true,
42 "stat": true,
43 "file": true,
44 "readlink": true,
45 "realpath": true,
46 "pwd": true,
47 "echo": true,
48 "printf": true,
49 "id": true,
50 "env": true,
51 "mount": true,
52 "lsof": true,
53 "netstat": true,
54 "tree": true,
55}
56
57// registerWriteTools wires operations that change panel or host state. Every
58// call creates an approval card; nothing runs until the operator confirms.
59func (s *Service) registerWriteTools(r *Registry) {
60 r.Register(Tool{
61 Name: "create_site",
62 Description: "创建一个新站点。kind 为 static、php、go、node 或 proxy。",
63 Parameters: objectSchema(map[string]any{
64 "name": prop("string", "站点名称"),
65 "kind": prop("string", "站点类型:static / php / go / node / proxy"),
66 "root": prop("string", "网站根目录,必须位于面板 web_root 内"),
67 "domains": arrayProp("绑定域名列表", "string"),
68 "port": prop("integer", "运行端口,0 表示自动分配"),
69 "php_version": prop("string", "PHP 版本,php 站点可填"),
70 "run_user": prop("string", "运行用户,默认 www"),
71 "upstream": prop("string", "反向代理上游,proxy 站点可填"),
72 "remark": prop("string", "备注"),
73 }, "name", "kind", "root"),
74 Risk: RiskWrite,
75 Summary: func(args map[string]any) string {
76 return fmt.Sprintf("创建站点「%s」(%s)", argString(args, "name"), argString(args, "kind"))
77 },
78 Run: func(ctx context.Context, args map[string]any) (string, error) {
79 name := argString(args, "name")
80 kind := argString(args, "kind")
81 root := argString(args, "root")
82 if name == "" || kind == "" || root == "" {
83 return "", fmt.Errorf("name、kind、root 均为必填")
84 }
85 switch kind {
86 case "static", "php", "go", "node", "proxy":
87 default:
88 return "", fmt.Errorf("kind 不支持: %s", kind)
89 }
90 port, _ := argInt(args, "port")
91 in := site.Input{
92 Name: name,
93 Kind: kind,
94 Domains: argStrings(args, "domains"),
95 Root: root,
96 Port: port,
97 PHPVersion: argString(args, "php_version"),
98 RunUser: argString(args, "run_user"),
99 Upstream: argString(args, "upstream"),
100 Remark: argString(args, "remark"),
101 }
102 st, err := s.sites.Create(ctx, in)
103 if err != nil {
104 return "", err
105 }
106 return jsonText(map[string]any{
107 "id": st.ID, "name": st.Name, "slug": st.Slug,
108 "kind": st.Kind, "domains": st.Domains, "port": st.Port,
109 })
110 },
111 })
112
113 r.Register(Tool{
114 Name: "site_action",
115 Description: "启动、停止或重启一个站点。",
116 Parameters: objectSchema(map[string]any{
117 "id": prop("integer", "站点 id"),
118 "action": prop("string", "start / stop / restart"),
119 }, "id", "action"),
120 Risk: RiskWrite,
121 Summary: func(args map[string]any) string {
122 id, _ := argInt(args, "id")
123 return fmt.Sprintf("对站点 #%d 执行 %s", id, argString(args, "action"))
124 },
125 Run: func(ctx context.Context, args map[string]any) (string, error) {
126 id, ok := argInt(args, "id")
127 if !ok || id <= 0 {
128 return "", fmt.Errorf("缺少站点 id")
129 }
130 action := argString(args, "action")
131 switch action {
132 case "start", "stop", "restart":
133 default:
134 return "", fmt.Errorf("action 只能是 start、stop 或 restart")
135 }
136 if err := s.sites.Action(ctx, int64(id), action); err != nil {
137 return "", err
138 }
139 return jsonText(map[string]any{"id": id, "action": action, "status": "ok"})
140 },
141 })
142
143 r.Register(Tool{
144 Name: "write_file",
145 Description: "写入网站根目录内的文本文件,内容不超过 64KB。",
146 Parameters: objectSchema(map[string]any{
147 "path": prop("string", "文件路径,相对于网站根目录"),
148 "content": prop("string", "文件内容"),
149 }, "path", "content"),
150 Risk: RiskWrite,
151 Summary: func(args map[string]any) string { return "写入文件 " + argString(args, "path") },
152 Run: func(ctx context.Context, args map[string]any) (string, error) {
153 path := argString(args, "path")
154 content, ok := args["content"].(string)
155 if path == "" || !ok {
156 return "", fmt.Errorf("path 和 content 均为必填")
157 }
158 if len(content) > 64<<10 {
159 return "", fmt.Errorf("内容超过 64KB 上限")
160 }
161 if err := s.files.Write(path, content); err != nil {
162 return "", err
163 }
164 return jsonText(map[string]any{"path": path, "bytes": len(content)})
165 },
166 })
167
168 r.Register(Tool{
169 Name: "reload_nginx",
170 Description: "校验并重载 nginx 配置。",
171 Parameters: objectSchema(map[string]any{}),
172 Risk: RiskWrite,
173 Summary: func(map[string]any) string { return "重载 nginx 配置" },
174 Run: func(ctx context.Context, args map[string]any) (string, error) {
175 if err := system.ReloadNginx(ctx, s.runner, s.cfg.Nginx.Bin); err != nil {
176 return "", err
177 }
178 return jsonText(map[string]any{"status": "ok"})
179 },
180 })
181
182 r.Register(Tool{
183 Name: "run_command",
184 Description: "执行一条只读系统命令(argv 形式,不允许管道与重定向)。",
185 Parameters: objectSchema(map[string]any{
186 "program": prop("string", "命令程序,例如 df、systemctl、journalctl"),
187 "args": arrayProp("命令参数", "string"),
188 "timeout_s": prop("integer", "超时秒数,默认 30,最大 60"),
189 }, "program"),
190 Risk: RiskDanger,
191 Summary: func(args map[string]any) string {
192 parts := []string{argString(args, "program")}
193 parts = append(parts, argStrings(args, "args")...)
194 return "执行命令: " + strings.Join(parts, " ")
195 },
196 Run: func(ctx context.Context, args map[string]any) (string, error) {
197 program := argString(args, "program")
198 if !commandWhitelist[program] {
199 return "", fmt.Errorf("程序 %q 不在允许列表中", program)
200 }
201 cmdArgs := argStrings(args, "args")
202 if len(cmdArgs) > 20 {
203 return "", fmt.Errorf("参数过多")
204 }
205 for _, a := range cmdArgs {
206 if len(a) > 500 || strings.ContainsAny(a, "\n\r\x00") {
207 return "", fmt.Errorf("参数包含非法字符或过长")
208 }
209 }
210 timeout, _ := argInt(args, "timeout_s")
211 if timeout <= 0 || timeout > 60 {
212 timeout = 30
213 }
214 res, err := s.runner.RunLong(ctx, time.Duration(timeout)*time.Second, program, cmdArgs...)
215 out := map[string]any{"program": program, "args": cmdArgs}
216 if res != nil {
217 out["stdout"] = clipResult(res.Stdout, 8<<10)
218 out["stderr"] = clipResult(res.Stderr, 4<<10)
219 out["code"] = res.Code
220 }
221 if err != nil {
222 out["error"] = err.Error()
223 }
224 raw, _ := jsonText(out)
225 return raw, nil
226 },
227 })
228}