-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscripts.go
More file actions
311 lines (290 loc) · 10.5 KB
/
scripts.go
File metadata and controls
311 lines (290 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"
"time"
)
func (s *server) refreshScriptMarket(ctx context.Context) commandResult {
manifest, err := fetchMarketManifest(ctx)
if err != nil {
return failed("脚本市场加载失败:"+err.Error(), failedScriptMarketPayload("脚本市场加载失败:"+err.Error()))
}
return ok("脚本市场已刷新。", scriptMarketPayload(manifest, "ok", "脚本市场已刷新。"))
}
func (s *server) installMarketScript(ctx context.Context, id string) commandResult {
id = strings.TrimSpace(id)
if id == "" {
return failed("脚本 id 不能为空。", failedScriptMarketPayload("脚本 id 不能为空。"))
}
manifest, err := fetchMarketManifest(ctx)
if err != nil {
return failed("脚本市场加载失败:"+err.Error(), failedScriptMarketPayload("脚本市场加载失败:"+err.Error()))
}
var selected *marketScript
for i := range manifest.Scripts {
if manifest.Scripts[i].ID == id {
selected = &manifest.Scripts[i]
break
}
}
if selected == nil {
return failed("市场清单中未找到该脚本。", scriptMarketPayload(manifest, "failed", "市场清单中未找到该脚本。"))
}
content, err := getBytes(ctx, selected.ScriptURL)
if err != nil {
return failed("安装脚本失败:"+err.Error(), scriptMarketPayload(manifest, "failed", "安装脚本失败:"+err.Error()))
}
if err := verifySHA256(*selected, content); err != nil {
return failed("安装脚本失败:"+err.Error(), scriptMarketPayload(manifest, "failed", "安装脚本失败:"+err.Error()))
}
if err := installMarketScriptContent(*selected, content); err != nil {
return failed("安装脚本失败:"+err.Error(), scriptMarketPayload(manifest, "failed", "安装脚本失败:"+err.Error()))
}
return ok("脚本已安装。", scriptMarketPayload(manifest, "ok", "脚本已安装。"))
}
func fetchMarketManifest(ctx context.Context) (marketManifest, error) {
var raw map[string]any
if err := getJSONInto(ctx, scriptMarketIndexURL, &raw); err != nil {
return marketManifest{}, err
}
var manifest marketManifest
manifest.Version = uint64FromAny(raw["version"], 1)
manifest.UpdatedAt = stringFromAny(raw["updated_at"])
if list, ok := raw["scripts"].([]any); ok {
for _, item := range list {
var script marketScript
if err := remarshal(item, &script); err != nil {
continue
}
if strings.TrimSpace(script.ID) != "" && strings.TrimSpace(script.Name) != "" && strings.TrimSpace(script.Version) != "" && strings.TrimSpace(script.ScriptURL) != "" {
manifest.Scripts = append(manifest.Scripts, script)
}
}
}
return manifest, nil
}
func failedScriptMarketPayload(message string) map[string]any {
return map[string]any{
"market": map[string]any{
"status": "failed", "message": message, "indexUrl": scriptMarketIndexURL, "updatedAt": "", "scripts": []any{},
},
"user_scripts": userScriptInventoryValue(),
}
}
func scriptMarketPayload(manifest marketManifest, status string, message string) map[string]any {
inventory := userScriptInventoryValue()
installed := installedMarketVersions(inventory)
scripts := make([]map[string]any, 0, len(manifest.Scripts))
for _, script := range manifest.Scripts {
installedVersion := installed[script.ID]
scripts = append(scripts, map[string]any{
"id": script.ID, "name": script.Name, "description": script.Description, "version": script.Version,
"author": script.Author, "tags": script.Tags, "homepage": script.Homepage, "script_url": script.ScriptURL, "sha256": script.SHA256,
"installed": installedVersion != "", "installedVersion": installedVersion, "updateAvailable": installedVersion != "" && installedVersion != script.Version,
})
}
return map[string]any{
"market": map[string]any{"status": status, "message": message, "indexUrl": scriptMarketIndexURL, "updatedAt": manifest.UpdatedAt, "scripts": scripts},
"user_scripts": inventory,
}
}
func installedMarketVersions(inventory map[string]any) map[string]string {
out := map[string]string{}
items, _ := inventory["scripts"].([]userScriptInventoryItem)
for _, script := range items {
if script.MarketID != "" {
out[script.MarketID] = script.Version
}
}
return out
}
func verifySHA256(script marketScript, content []byte) error {
expected := strings.ToLower(strings.TrimSpace(script.SHA256))
if expected == "" {
return nil
}
sum := sha256.Sum256(content)
actual := hex.EncodeToString(sum[:])
if actual != expected {
return fmt.Errorf("checksum mismatch for market script %s", script.ID)
}
return nil
}
func installMarketScriptContent(script marketScript, content []byte) error {
path := filepath.Join(userScriptsDir(), marketScriptFilename(script.ID))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
if err := atomicWrite(path, content); err != nil {
return err
}
config := loadUserScriptConfig()
key := "user:" + marketScriptFilename(script.ID)
if _, ok := config.Scripts[key]; !ok {
config.Scripts[key] = true
}
config.Market[key] = marketScriptInstall{
ID: script.ID, Name: script.Name, Version: script.Version, ScriptURL: script.ScriptURL, Homepage: script.Homepage, InstalledAt: strconv.FormatInt(time.Now().Unix(), 10),
}
return saveUserScriptConfig(config)
}
func userScriptsConfigDir() string {
if runtime.GOOS == "windows" && os.Getenv("APPDATA") != "" {
return filepath.Join(os.Getenv("APPDATA"), "Codex++")
}
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "Codex++")
}
home, err := os.UserHomeDir()
if err != nil {
return filepath.Join(".config", "Codex++")
}
return filepath.Join(home, ".config", "Codex++")
}
func userScriptsDir() string {
return filepath.Join(userScriptsConfigDir(), "user_scripts")
}
func userScriptsConfigPath() string {
return filepath.Join(userScriptsConfigDir(), "user_scripts.json")
}
func builtinUserScriptsDir() string {
exe, err := os.Executable()
if err != nil {
return "user_scripts"
}
return filepath.Join(filepath.Dir(exe), "user_scripts")
}
func loadUserScriptConfig() userScriptConfig {
config := userScriptConfig{Enabled: true, Scripts: map[string]bool{}, Market: map[string]marketScriptInstall{}}
_ = readJSON(userScriptsConfigPath(), &config)
if config.Scripts == nil {
config.Scripts = map[string]bool{}
}
if config.Market == nil {
config.Market = map[string]marketScriptInstall{}
}
return config
}
func saveUserScriptConfig(config userScriptConfig) error {
if config.Scripts == nil {
config.Scripts = map[string]bool{}
}
if config.Market == nil {
config.Market = map[string]marketScriptInstall{}
}
return atomicWriteJSON(userScriptsConfigPath(), config)
}
func userScriptInventoryValue() map[string]any {
inventory := scanUserScripts()
return map[string]any{
"enabled": inventory.Enabled, "builtin_dir": inventory.BuiltinDir, "user_dir": inventory.UserDir, "scripts": inventory.Scripts,
}
}
func scanUserScripts() userScriptInventory {
config := loadUserScriptConfig()
inventory := userScriptInventory{Enabled: config.Enabled, BuiltinDir: builtinUserScriptsDir(), UserDir: userScriptsDir(), Scripts: []userScriptInventoryItem{}}
_ = os.MkdirAll(userScriptsDir(), 0o755)
appendScripts := func(source, dir string) {
entries, err := os.ReadDir(dir)
if err != nil {
return
}
sort.Slice(entries, func(i, j int) bool { return strings.ToLower(entries[i].Name()) < strings.ToLower(entries[j].Name()) })
for _, entry := range entries {
if entry.IsDir() || filepath.Ext(entry.Name()) != ".js" {
continue
}
key := source + ":" + entry.Name()
enabled, ok := config.Scripts[key]
if !ok {
enabled = true
}
status := "not_loaded"
if !config.Enabled || !enabled {
status = "disabled"
}
item := userScriptInventoryItem{Key: key, Name: entry.Name(), Source: source, Enabled: enabled, Status: status, Error: ""}
if market, ok := config.Market[key]; ok {
item.MarketID = market.ID
item.Version = market.Version
item.Installed = true
item.SourceURL = market.ScriptURL
item.Homepage = market.Homepage
}
inventory.Scripts = append(inventory.Scripts, item)
}
}
appendScripts("builtin", inventory.BuiltinDir)
appendScripts("user", inventory.UserDir)
return inventory
}
func (s *server) setUserScriptEnabled(key string, enabled bool) commandResult {
key = strings.TrimSpace(key)
if key == "" {
return failed("脚本 key 不能为空。", settingsPayloadValue(loadSettings()))
}
config := loadUserScriptConfig()
config.Scripts[key] = enabled
if err := saveUserScriptConfig(config); err != nil {
return failed("脚本启停失败:"+err.Error(), settingsPayloadValue(loadSettings()))
}
if enabled {
return settingsPayload("脚本已启用。")
}
return settingsPayload("脚本已禁用。")
}
func (s *server) deleteUserScript(key string) commandResult {
key = strings.TrimSpace(key)
if key == "" {
return failed("脚本 key 不能为空。", settingsPayloadValue(loadSettings()))
}
fileName, ok := strings.CutPrefix(key, "user:")
if !ok || fileName == "" || strings.ContainsAny(fileName, `/\`) || fileName == "." || fileName == ".." {
return failed("脚本删除失败:only user scripts can be deleted", settingsPayloadValue(loadSettings()))
}
path := filepath.Join(userScriptsDir(), fileName)
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
return failed("脚本删除失败:"+err.Error(), settingsPayloadValue(loadSettings()))
}
config := loadUserScriptConfig()
delete(config.Scripts, key)
delete(config.Market, key)
if err := saveUserScriptConfig(config); err != nil {
return failed("脚本删除失败:"+err.Error(), settingsPayloadValue(loadSettings()))
}
return settingsPayload("脚本已删除。")
}
func marketScriptFilename(id string) string {
var b strings.Builder
for _, ch := range id {
if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' {
b.WriteRune(ch)
} else {
b.WriteByte('-')
}
}
safe := strings.Trim(b.String(), "-")
if safe == "" {
safe = "script"
}
return "market-" + safe + ".js"
}
func (s *server) openExternalURL(rawURL string) commandResult {
rawURL = strings.TrimSpace(rawURL)
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
return failed("只允许打开 http 或 https 链接。", map[string]any{})
}
if err := openURL(rawURL); err != nil {
return failed("打开链接失败:"+err.Error(), map[string]any{"url": rawURL})
}
return ok("已在系统浏览器打开链接。", map[string]any{"url": rawURL})
}