opencode-tethergate 0.4.0 → 0.4.1
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/guard.js +58 -8
- package/package.json +1 -1
- package/rpc.js +52 -4
- package/service.js +10 -6
- package/setup.js +3 -2
- package/tui.js +25 -1
package/guard.js
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
|
-
const ADMIN_VERBS = new Set([
|
|
1
|
+
const ADMIN_VERBS = new Set([
|
|
2
|
+
"session",
|
|
3
|
+
"access",
|
|
4
|
+
"node",
|
|
5
|
+
"fleet",
|
|
6
|
+
"auto",
|
|
7
|
+
"backup",
|
|
8
|
+
"restore",
|
|
9
|
+
])
|
|
2
10
|
const WRAPPERS = new Set([
|
|
3
11
|
"command",
|
|
4
12
|
"env",
|
|
@@ -136,6 +144,16 @@ function commandIndex(tokens) {
|
|
|
136
144
|
}
|
|
137
145
|
|
|
138
146
|
function isAdminInvocation(tokens) {
|
|
147
|
+
// Wrapper options can move the real command anywhere in the vector. Detect
|
|
148
|
+
// a Tethergate executable followed by an admin verb independent of position.
|
|
149
|
+
for (let i = 0; i < tokens.length; i += 1) {
|
|
150
|
+
const words = tokens[i].trim().split(/\s+/)
|
|
151
|
+
const name = words[0].split("/").pop().toLowerCase()
|
|
152
|
+
if (name === "tethergate" || name === "tethergate.cli") {
|
|
153
|
+
const rest = [...words.slice(1), ...tokens.slice(i + 1)]
|
|
154
|
+
if (rest.some((token) => ADMIN_VERBS.has(token.toLowerCase()))) return true
|
|
155
|
+
}
|
|
156
|
+
}
|
|
139
157
|
const index = commandIndex(tokens)
|
|
140
158
|
if (index >= tokens.length) return false
|
|
141
159
|
const command = tokens[index].split("/").pop().toLowerCase()
|
|
@@ -163,13 +181,45 @@ function isAdminInvocation(tokens) {
|
|
|
163
181
|
}
|
|
164
182
|
}
|
|
165
183
|
if (["docker", "podman", "nerdctl"].includes(command)) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
184
|
+
let index = 0
|
|
185
|
+
while (index < rest.length) {
|
|
186
|
+
const token = rest[index]
|
|
187
|
+
if (token === "--") {
|
|
188
|
+
index += 1
|
|
189
|
+
break
|
|
190
|
+
}
|
|
191
|
+
if (!token.startsWith("-")) break
|
|
192
|
+
if (
|
|
193
|
+
["-H", "--host", "--context", "-c", "--config", "-l", "--log-level"].includes(
|
|
194
|
+
token,
|
|
195
|
+
) &&
|
|
196
|
+
index + 1 < rest.length
|
|
197
|
+
) {
|
|
198
|
+
index += 2
|
|
199
|
+
continue
|
|
200
|
+
}
|
|
201
|
+
index += 1
|
|
202
|
+
}
|
|
203
|
+
let sub = rest[index]
|
|
204
|
+
if (sub === "container") {
|
|
205
|
+
index += 1
|
|
206
|
+
sub = rest[index]
|
|
207
|
+
} else if (sub === "compose") {
|
|
208
|
+
index += 1
|
|
209
|
+
while (index < rest.length && rest[index].startsWith("-")) {
|
|
210
|
+
if (
|
|
211
|
+
["-f", "--file", "-p", "--project-name", "--project-directory", "--env-file"].includes(
|
|
212
|
+
rest[index],
|
|
213
|
+
) &&
|
|
214
|
+
index + 1 < rest.length
|
|
215
|
+
) {
|
|
216
|
+
index += 2
|
|
217
|
+
} else {
|
|
218
|
+
index += 1
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
sub = rest[index]
|
|
222
|
+
}
|
|
173
223
|
if (sub === "exec" || sub === "run" || sub === "create") return true
|
|
174
224
|
}
|
|
175
225
|
// Unresolved shell expansion of the binary name is treated as admin use.
|
package/package.json
CHANGED
package/rpc.js
CHANGED
|
@@ -8,6 +8,7 @@ const nodeSchema = {
|
|
|
8
8
|
online: { type: "boolean" },
|
|
9
9
|
status: { type: "string" },
|
|
10
10
|
quarantine_reason: { type: "string" },
|
|
11
|
+
connected_ip: { type: "string" },
|
|
11
12
|
cert_days_left: { type: ["integer", "null"] },
|
|
12
13
|
},
|
|
13
14
|
required: ["id", "name", "online", "status"],
|
|
@@ -42,6 +43,15 @@ const sessionInput = {
|
|
|
42
43
|
additionalProperties: false,
|
|
43
44
|
}
|
|
44
45
|
|
|
46
|
+
const rpcFailure = {
|
|
47
|
+
type: "object",
|
|
48
|
+
properties: { message: { type: "string" } },
|
|
49
|
+
required: ["message"],
|
|
50
|
+
additionalProperties: false,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const rpcErrors = { failed: rpcFailure, denied: rpcFailure }
|
|
54
|
+
|
|
45
55
|
const nodeChangeInput = {
|
|
46
56
|
type: "object",
|
|
47
57
|
properties: {
|
|
@@ -52,9 +62,7 @@ const nodeChangeInput = {
|
|
|
52
62
|
additionalProperties: false,
|
|
53
63
|
}
|
|
54
64
|
|
|
55
|
-
|
|
56
|
-
id: "tethergate",
|
|
57
|
-
methods: {
|
|
65
|
+
const methods = {
|
|
58
66
|
snapshot: {
|
|
59
67
|
input: sessionInput,
|
|
60
68
|
output: snapshotSchema,
|
|
@@ -346,6 +354,46 @@ export const Tethergate = Rpc.define({
|
|
|
346
354
|
additionalProperties: false,
|
|
347
355
|
},
|
|
348
356
|
},
|
|
349
|
-
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
for (const method of Object.values(methods)) {
|
|
360
|
+
method.errors = rpcErrors
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
export function formatRpcError(error) {
|
|
364
|
+
if (error && typeof error === "object") {
|
|
365
|
+
const tag = String(error._tag || error.name || "")
|
|
366
|
+
if (tag.includes("Declined") || tag.includes("Blocked")) {
|
|
367
|
+
return "permission declined; no changes were made"
|
|
368
|
+
}
|
|
369
|
+
if (typeof error.message === "string" && error.message) return error.message
|
|
370
|
+
if (error.data && typeof error.data.message === "string") return error.data.message
|
|
371
|
+
}
|
|
372
|
+
return String(error || "operation failed")
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
export function withRpcErrors(handlers) {
|
|
376
|
+
return Object.fromEntries(
|
|
377
|
+
Object.entries(handlers).map(([name, handler]) => [
|
|
378
|
+
name,
|
|
379
|
+
async (input, context) => {
|
|
380
|
+
try {
|
|
381
|
+
return await handler(input)
|
|
382
|
+
} catch (error) {
|
|
383
|
+
const message = formatRpcError(error)
|
|
384
|
+
const type = message.startsWith("permission declined") ? "denied" : "failed"
|
|
385
|
+
if (context && typeof context.error === "function") {
|
|
386
|
+
return context.error(type, message, { message })
|
|
387
|
+
}
|
|
388
|
+
throw error
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
]),
|
|
392
|
+
)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export const Tethergate = Rpc.define({
|
|
396
|
+
id: "tethergate",
|
|
397
|
+
methods,
|
|
350
398
|
events: {},
|
|
351
399
|
})
|
package/service.js
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
const SESSION_RE = /^ses_[A-Za-z0-9_-]{4,128}$/
|
|
2
2
|
const NODE_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
|
|
3
3
|
|
|
4
|
+
export function describeSnapshot(snapshot) {
|
|
5
|
+
const scope = snapshot.grantAll
|
|
6
|
+
? "all nodes"
|
|
7
|
+
: snapshot.grantNodes.length
|
|
8
|
+
? snapshot.grantNodes.join(", ")
|
|
9
|
+
: "no nodes"
|
|
10
|
+
return `tethergate: this session has ${scope}; default for new sessions: ${snapshot.defaultAccess}`
|
|
11
|
+
}
|
|
12
|
+
|
|
4
13
|
export class TethergateService {
|
|
5
14
|
/**
|
|
6
15
|
* @param {(args: string[]) => Promise<string>} run
|
|
@@ -241,11 +250,6 @@ export class TethergateService {
|
|
|
241
250
|
}
|
|
242
251
|
|
|
243
252
|
describe(snapshot) {
|
|
244
|
-
|
|
245
|
-
? "all nodes"
|
|
246
|
-
: snapshot.grantNodes.length
|
|
247
|
-
? snapshot.grantNodes.join(", ")
|
|
248
|
-
: "no nodes"
|
|
249
|
-
return `tethergate: this session has ${scope}; default for new sessions: ${snapshot.defaultAccess}`
|
|
253
|
+
return describeSnapshot(snapshot)
|
|
250
254
|
}
|
|
251
255
|
}
|
package/setup.js
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
assertNoAdminShell,
|
|
7
7
|
commandFromInput,
|
|
8
8
|
} from "./guard.js"
|
|
9
|
+
import { withRpcErrors } from "./rpc.js"
|
|
9
10
|
|
|
10
11
|
const execFileAsync = promisify(execFile)
|
|
11
12
|
export const DEFAULT_COMMAND = ["docker", "exec", "-i", "tethergate", "tethergate"]
|
|
@@ -323,7 +324,7 @@ export async function setupTethergate(ctx, service, rpcDefinition) {
|
|
|
323
324
|
}),
|
|
324
325
|
)
|
|
325
326
|
|
|
326
|
-
const registration = await ctx.rpc.register(rpcDefinition, {
|
|
327
|
+
const registration = await ctx.rpc.register(rpcDefinition, withRpcErrors({
|
|
327
328
|
snapshot: async (input) => service.snapshot(validSession(input.sessionID)),
|
|
328
329
|
grantAll: async (input) => {
|
|
329
330
|
validSession(input.sessionID)
|
|
@@ -566,7 +567,7 @@ export async function setupTethergate(ctx, service, rpcDefinition) {
|
|
|
566
567
|
)
|
|
567
568
|
return service.setDefault(input.mode, Boolean(input.confirm))
|
|
568
569
|
},
|
|
569
|
-
})
|
|
570
|
+
}))
|
|
570
571
|
const rpc = ctx.rpc(rpcDefinition)
|
|
571
572
|
|
|
572
573
|
// This command runs in the server plugin, not through the model. It is
|
package/tui.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Plugin } from "@opencode/plugin/tui"
|
|
2
2
|
import { buildMenuOptions } from "./menu.js"
|
|
3
3
|
import { Tethergate } from "./rpc.js"
|
|
4
|
+
import { describeSnapshot } from "./service.js"
|
|
5
|
+
import { formatError, runCommand } from "./setup.js"
|
|
4
6
|
|
|
5
7
|
function scopeLabel(data) {
|
|
6
8
|
if (data.defaultAccess === "all") return "all nodes (default)"
|
|
@@ -399,7 +401,29 @@ export default Plugin.define({
|
|
|
399
401
|
group: "tethergate",
|
|
400
402
|
palette: true,
|
|
401
403
|
slash: { name: "tethergate", arguments: true },
|
|
402
|
-
run:
|
|
404
|
+
run: async (input) => {
|
|
405
|
+
const text = typeof input === "string" ? input.trim() : ""
|
|
406
|
+
if (!text) return menu()
|
|
407
|
+
const sessionID = activeSession()
|
|
408
|
+
if (!sessionID) {
|
|
409
|
+
toast("Open a session before running tethergate commands", "warning")
|
|
410
|
+
return
|
|
411
|
+
}
|
|
412
|
+
try {
|
|
413
|
+
const message = await runCommand(
|
|
414
|
+
rpc,
|
|
415
|
+
{ describe: describeSnapshot },
|
|
416
|
+
sessionID,
|
|
417
|
+
text,
|
|
418
|
+
)
|
|
419
|
+
await ctx.ui.dialog.alert({ title: "tethergate", message })
|
|
420
|
+
} catch (error) {
|
|
421
|
+
await ctx.ui.dialog.alert({
|
|
422
|
+
title: "tethergate error",
|
|
423
|
+
message: formatError(error),
|
|
424
|
+
})
|
|
425
|
+
}
|
|
426
|
+
},
|
|
403
427
|
},
|
|
404
428
|
],
|
|
405
429
|
}))
|