@gluztm/dsh-guard 0.0.0-stage → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.mjs ADDED
@@ -0,0 +1,283 @@
1
+ import {
2
+ RELEASE_URL,
3
+ formatHealthText,
4
+ formatSnapshotsText,
5
+ inspectHealth,
6
+ inspectSnapshots,
7
+ locateGuardInstallation,
8
+ openGuard,
9
+ } from './inspector.mjs'
10
+
11
+ let defineTool = (def) => def
12
+ try {
13
+ const mod = await import('@deepseek-ai/dsh-tools')
14
+ if (mod && typeof mod.defineTool === 'function') {
15
+ defineTool = mod.defineTool
16
+ }
17
+ } catch {
18
+ // Standalone CLI / unit-test execution without @deepseek-ai/dsh-tools falls back cleanly.
19
+ }
20
+
21
+ const name = 'dsh-guard'
22
+ const API_BASE = '/api/dsh-guard'
23
+
24
+ function sendJson(res, status, value) {
25
+ const body = JSON.stringify(value)
26
+ res.statusCode = status
27
+ res.setHeader('content-type', 'application/json; charset=utf-8')
28
+ res.setHeader('cache-control', 'no-store')
29
+ res.end(body)
30
+ }
31
+
32
+ function isRequestAllowed(ctx, req, res) {
33
+ try {
34
+ const connection = typeof ctx.get === 'function' ? ctx.get('connection') : null
35
+ if (connection && typeof connection.requestRejection === 'function') {
36
+ const rejection = connection.requestRejection({ headers: (req && req.headers) || {} })
37
+ if (rejection) {
38
+ sendJson(res, rejection, {
39
+ ok: false,
40
+ error: rejection === 401 ? 'unauthorized' : 'forbidden',
41
+ })
42
+ return false
43
+ }
44
+ }
45
+ } catch {
46
+ // Fallback to permissive when connection service is unavailable in local unit mocks.
47
+ }
48
+ return true
49
+ }
50
+
51
+ function buildRouteTable(ctx) {
52
+ return [
53
+ {
54
+ path: `${API_BASE}/overview`,
55
+ handler: async (req, res) => {
56
+ if (!isRequestAllowed(ctx, req, res)) return
57
+ const method = String((req && req.method) || 'GET').toUpperCase()
58
+ if (method !== 'GET' && method !== 'HEAD') {
59
+ sendJson(res, 405, { ok: false, error: 'method_not_allowed' })
60
+ return
61
+ }
62
+ try {
63
+ // Server-side env vars only (DSH_HOME / DSHGUARD_HOME); never accept path traversal params from HTTP requests.
64
+ const health = inspectHealth({ target: 'all' })
65
+ const snapshots = inspectSnapshots({ target: 'all', limit: 15 })
66
+ sendJson(res, 200, {
67
+ ok: true,
68
+ platform: process.platform,
69
+ health,
70
+ snapshots,
71
+ checkedAt: Date.now(),
72
+ })
73
+ } catch (err) {
74
+ sendJson(res, 500, {
75
+ ok: false,
76
+ error: err && err.message ? String(err.message) : 'internal_error',
77
+ })
78
+ }
79
+ },
80
+ },
81
+ {
82
+ path: `${API_BASE}/open`,
83
+ handler: async (req, res) => {
84
+ if (!isRequestAllowed(ctx, req, res)) return
85
+ const method = String((req && req.method) || '').toUpperCase()
86
+ if (method !== 'POST') {
87
+ sendJson(res, 405, { ok: false, error: 'method_not_allowed' })
88
+ return
89
+ }
90
+ try {
91
+ const result = openGuard({ launch: true })
92
+ sendJson(res, 200, {
93
+ ok: true,
94
+ platform: process.platform,
95
+ ...result,
96
+ })
97
+ } catch (err) {
98
+ sendJson(res, 500, {
99
+ ok: false,
100
+ error: err && err.message ? String(err.message) : 'internal_error',
101
+ })
102
+ }
103
+ },
104
+ },
105
+ ]
106
+ }
107
+
108
+ function buildToolDefinitions() {
109
+ return [
110
+ defineTool({
111
+ name: 'dsh_guard_status',
112
+ description:
113
+ 'Read-only health check for DSH Web and Desktop profiles: checks package.json bundle/dependency drift, '
114
+ + 'cordis.patch.yml structural integrity, masked .credentials.yaml key readiness (never reads or exposes secret values), '
115
+ + 'and local Windows DSHGuard supervisor installation status.',
116
+ parameters: {
117
+ target: {
118
+ type: 'string',
119
+ enum: ['all', 'web', 'desktop'],
120
+ description: 'Which DSH profile track to inspect (default: all).',
121
+ },
122
+ },
123
+ output: {
124
+ schema: {
125
+ type: 'object',
126
+ additionalProperties: false,
127
+ properties: {
128
+ healthy: { type: 'boolean', required: true },
129
+ issuesCount: { type: 'integer', required: true },
130
+ issues: { type: 'array', required: true, items: { type: 'string' } },
131
+ summary: { type: 'string', required: true },
132
+ releaseUrl: { type: 'string', required: true },
133
+ },
134
+ },
135
+ render: (_args, value) => [{ type: 'text', text: value.summary }],
136
+ },
137
+ async execute(args = {}) {
138
+ const report = inspectHealth({ target: args.target })
139
+ return {
140
+ healthy: report.healthy,
141
+ issuesCount: report.issuesCount,
142
+ issues: report.issues,
143
+ summary: formatHealthText(report),
144
+ releaseUrl: RELEASE_URL,
145
+ }
146
+ },
147
+ }),
148
+
149
+ defineTool({
150
+ name: 'dsh_guard_snapshots',
151
+ description:
152
+ 'Read-only list of pre-change DSHGuard configuration and plugin snapshots stored on this Windows machine '
153
+ + '(separated by Web and Desktop tracks).',
154
+ parameters: {
155
+ target: {
156
+ type: 'string',
157
+ enum: ['all', 'web', 'desktop'],
158
+ description: 'Filter snapshots by track (default: all).',
159
+ },
160
+ limit: {
161
+ type: 'integer',
162
+ description: 'Maximum snapshots to return (1-50, default: 20).',
163
+ },
164
+ },
165
+ output: {
166
+ schema: {
167
+ type: 'object',
168
+ additionalProperties: false,
169
+ properties: {
170
+ guardInstalled: { type: 'boolean', required: true },
171
+ total: { type: 'integer', required: true },
172
+ summary: { type: 'string', required: true },
173
+ releaseUrl: { type: 'string', required: true },
174
+ },
175
+ },
176
+ render: (_args, value) => [{ type: 'text', text: value.summary }],
177
+ },
178
+ async execute(args = {}) {
179
+ const report = inspectSnapshots({ target: args.target, limit: args.limit })
180
+ return {
181
+ guardInstalled: report.guardInstalled,
182
+ total: report.total,
183
+ summary: formatSnapshotsText(report),
184
+ releaseUrl: report.releaseUrl,
185
+ }
186
+ },
187
+ }),
188
+
189
+ defineTool({
190
+ name: 'dsh_guard_open',
191
+ description:
192
+ 'Locate the local Windows out-of-band DSHGuard desktop supervisor executable or return its official release download link. '
193
+ + 'Pass launch=true only when the user asks to open the DSHGuard GUI window for snapshot rollback, credential vault, or CC Switch import.',
194
+ parameters: {
195
+ launch: {
196
+ type: 'boolean',
197
+ description: 'Set true to spawn DSHGuard.exe on Windows if installed (default: false).',
198
+ },
199
+ },
200
+ output: {
201
+ schema: {
202
+ type: 'object',
203
+ additionalProperties: false,
204
+ properties: {
205
+ installed: { type: 'boolean', required: true },
206
+ launched: { type: 'boolean', required: true },
207
+ message: { type: 'string', required: true },
208
+ releaseUrl: { type: 'string', required: true },
209
+ },
210
+ },
211
+ render: (_args, value) => [{ type: 'text', text: value.message + (value.installed ? '' : `\n下载链接:${value.releaseUrl}`) }],
212
+ },
213
+ async execute(args = {}) {
214
+ const res = openGuard({ launch: Boolean(args.launch) })
215
+ return {
216
+ installed: res.installed,
217
+ launched: res.launched,
218
+ message: res.message,
219
+ releaseUrl: res.releaseUrl,
220
+ }
221
+ },
222
+ }),
223
+ ]
224
+ }
225
+
226
+ export function apply(ctx) {
227
+ ctx.inject(['tools'], (toolsCtx) => {
228
+ const definitions = buildToolDefinitions()
229
+ const disposers = definitions.map((def) => toolsCtx.tools.register(def))
230
+ toolsCtx.effect(() => () => {
231
+ for (const dispose of disposers) {
232
+ if (typeof dispose === 'function') dispose()
233
+ }
234
+ })
235
+ })
236
+
237
+ ctx.inject(['commands'], (cmdCtx) => {
238
+ const dispose = cmdCtx.commands.register({
239
+ name: 'guard',
240
+ description: 'DSH 守护壳伴生命令:/guard [status|snapshots|open]',
241
+ input: { hint: '[status|snapshots|open]' },
242
+ async handler(invocation) {
243
+ const sub = String((invocation && invocation.rawInput) || '').trim().toLowerCase()
244
+ if (sub === 'snapshots' || sub === 'snap') {
245
+ return { kind: 'success', text: formatSnapshotsText(inspectSnapshots({})) }
246
+ }
247
+ if (sub === 'open') {
248
+ const res = openGuard({ launch: true })
249
+ return {
250
+ kind: 'success',
251
+ text: res.message + (res.installed ? '' : `\n下载地址:${res.releaseUrl}`),
252
+ }
253
+ }
254
+ return { kind: 'success', text: formatHealthText(inspectHealth({})) }
255
+ },
256
+ })
257
+ if (typeof cmdCtx.effect === 'function' && typeof dispose === 'function') {
258
+ cmdCtx.effect(() => dispose, 'dsh-guard: command /guard')
259
+ }
260
+ })
261
+
262
+ ctx.inject(['webServer'], (webCtx) => {
263
+ if (!webCtx || !webCtx.webServer || typeof webCtx.webServer.register !== 'function') return
264
+ for (const route of buildRouteTable(ctx)) {
265
+ if (typeof webCtx.effect === 'function') {
266
+ webCtx.effect(() => webCtx.webServer.register(route), `dsh-guard: route ${route.path}`)
267
+ } else {
268
+ webCtx.webServer.register(route)
269
+ }
270
+ }
271
+ })
272
+ }
273
+
274
+ export {
275
+ apply as default,
276
+ name,
277
+ inspectHealth,
278
+ inspectSnapshots,
279
+ locateGuardInstallation,
280
+ openGuard,
281
+ formatHealthText,
282
+ formatSnapshotsText,
283
+ }