opencode-tethergate 0.0.0-stage → 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/tui.js ADDED
@@ -0,0 +1,431 @@
1
+ import { Plugin } from "@opencode/plugin/tui"
2
+ import { buildMenuOptions } from "./menu.js"
3
+ import { Tethergate } from "./rpc.js"
4
+ import { describeSnapshot } from "./service.js"
5
+ import { formatError, runCommand } from "./setup.js"
6
+
7
+ function scopeLabel(data) {
8
+ if (data.defaultAccess === "all") return "all nodes (default)"
9
+ if (data.grantAll) return "all nodes"
10
+ if (data.grantNodes.length) return data.grantNodes.join(", ")
11
+ return "no nodes"
12
+ }
13
+
14
+ export default Plugin.define({
15
+ id: "tethergate-tui",
16
+ async setup(ctx) {
17
+ const rpc = ctx.client.rpc(Tethergate)
18
+ const where = () => ({
19
+ location: ctx.location ?? ctx.data.location.default(),
20
+ })
21
+
22
+ function activeSession() {
23
+ const route = ctx.ui.router.current()
24
+ return route.type === "session" ? route.sessionID : undefined
25
+ }
26
+
27
+ function toast(message, variant = "success") {
28
+ ctx.ui.toast.show({ message, variant, duration: 4000 })
29
+ }
30
+
31
+ async function chooseNodes(data, sessionID) {
32
+ if (data.nodes.length === 0) {
33
+ toast("No nodes are enrolled", "warning")
34
+ return []
35
+ }
36
+ const selected = new Set(data.grantNodes)
37
+ for (;;) {
38
+ const options = [
39
+ {
40
+ title: "Done",
41
+ value: "__done__",
42
+ description: selected.size
43
+ ? `${selected.size} selected`
44
+ : "no nodes selected",
45
+ },
46
+ ]
47
+ for (const node of data.nodes) {
48
+ options.push({
49
+ title: node.name,
50
+ value: node.name,
51
+ description: `${node.online ? "online" : "offline"} · ${node.status}${
52
+ selected.has(node.name) ? " · selected" : ""
53
+ }`,
54
+ category: selected.has(node.name) ? "Selected" : "Available",
55
+ disabled: node.status !== "active" || !node.online,
56
+ })
57
+ }
58
+ const value = await ctx.ui.dialog.select({
59
+ title: "tethergate · choose nodes",
60
+ options,
61
+ })
62
+ if (value === undefined) return null
63
+ if (value === "__done__") return [...selected]
64
+ if (selected.has(value)) selected.delete(value)
65
+ else selected.add(value)
66
+ }
67
+ }
68
+
69
+ async function enrollNode(sessionID) {
70
+ const name = await ctx.ui.dialog.prompt({
71
+ title: "tethergate · enroll node",
72
+ placeholder: "node name (letters, digits, . _ -)",
73
+ })
74
+ if (name === undefined) return
75
+ const data = await rpc.nodeAdd({ name, sessionID }, where())
76
+ await ctx.ui.dialog.alert({
77
+ title: `tethergate · ${data.name}`,
78
+ message: `Run this on the node as root:\n\n${data.command}`,
79
+ })
80
+ }
81
+
82
+ async function revokeNode(sessionID) {
83
+ const result = await rpc.nodeList({}, where())
84
+ const active = result.nodes.filter((node) => node.status === "active")
85
+ if (active.length === 0) {
86
+ toast("No active nodes to revoke", "warning")
87
+ return
88
+ }
89
+ const node = await ctx.ui.dialog.select({
90
+ title: "tethergate · revoke node",
91
+ options: active.map((entry) => ({
92
+ title: entry.name,
93
+ value: entry.name,
94
+ description: `${entry.online ? "online" : "offline"} · revoking closes its agent`,
95
+ })),
96
+ })
97
+ if (!node) return
98
+ const confirmed = await ctx.ui.dialog.confirm({
99
+ title: `Revoke ${node}?`,
100
+ message:
101
+ "The node's certificate will stop working immediately. Re-enrollment requires a new node name.",
102
+ label: { confirm: "Revoke node", cancel: "Cancel" },
103
+ })
104
+ if (!confirmed) return
105
+ await rpc.nodeRevoke({ node, sessionID }, where())
106
+ toast(`Revoked node ${node}`)
107
+ }
108
+
109
+ async function selectNode(sessionID, filter, title) {
110
+ const result = await rpc.nodeList({}, where())
111
+ const candidates = result.nodes.filter(filter)
112
+ if (candidates.length === 0) {
113
+ toast("No matching nodes", "warning")
114
+ return undefined
115
+ }
116
+ return ctx.ui.dialog.select({
117
+ title,
118
+ options: candidates.map((node) => ({
119
+ title: node.name,
120
+ value: node.name,
121
+ description: `${node.online ? "online" : "offline"} · ${node.status}${
122
+ node.cert_days_left === null
123
+ ? ""
124
+ : ` · cert ${node.cert_days_left}d`
125
+ }`,
126
+ })),
127
+ })
128
+ }
129
+
130
+ async function updateNode(sessionID) {
131
+ const node = await selectNode(
132
+ sessionID,
133
+ (entry) => entry.status === "active",
134
+ "tethergate · update agent",
135
+ )
136
+ if (!node) return
137
+ const action = await ctx.ui.dialog.select({
138
+ title: `tethergate · ${node}`,
139
+ options: [
140
+ { title: "Install current release", value: "update" },
141
+ { title: "Roll back to previous release", value: "rollback" },
142
+ ],
143
+ })
144
+ if (!action) return
145
+ const confirmed = await ctx.ui.dialog.confirm({
146
+ title: `${action === "rollback" ? "Roll back" : "Update"} ${node}?`,
147
+ message:
148
+ "The agent verifies the bundle checksum, swaps releases atomically, and restarts under systemd.",
149
+ label: {
150
+ confirm: action === "rollback" ? "Roll back" : "Update",
151
+ cancel: "Cancel",
152
+ },
153
+ })
154
+ if (!confirmed) return
155
+ const result = await rpc.nodeUpdate(
156
+ { node, rollback: action === "rollback", sessionID },
157
+ where(),
158
+ )
159
+ toast(result.output)
160
+ }
161
+
162
+ async function renameNode(sessionID) {
163
+ const node = await selectNode(
164
+ sessionID,
165
+ (entry) => entry.status !== "retired",
166
+ "tethergate · rename node",
167
+ )
168
+ if (!node) return
169
+ const name = await ctx.ui.dialog.prompt({
170
+ title: `tethergate · rename ${node}`,
171
+ placeholder: "new node name",
172
+ })
173
+ if (name === undefined) return
174
+ await rpc.nodeRename({ node, name, sessionID }, where())
175
+ toast(`Renamed ${node} to ${name}`)
176
+ }
177
+
178
+ async function retireNode(sessionID) {
179
+ const node = await selectNode(
180
+ sessionID,
181
+ (entry) =>
182
+ entry.status === "revoked" || entry.status === "quarantined",
183
+ "tethergate · retire node",
184
+ )
185
+ if (!node) return
186
+ const confirmed = await ctx.ui.dialog.confirm({
187
+ title: `Retire ${node}?`,
188
+ message:
189
+ "Audit history is kept. The original hostname becomes available for a fresh enrollment.",
190
+ label: { confirm: "Retire node", cancel: "Cancel" },
191
+ })
192
+ if (!confirmed) return
193
+ await rpc.nodeRetire({ node, sessionID }, where())
194
+ toast(`Retired ${node}`)
195
+ }
196
+
197
+ async function uninstallNode(sessionID) {
198
+ const node = await selectNode(
199
+ sessionID,
200
+ (entry) => entry.status === "active" && entry.online,
201
+ "tethergate · uninstall agent",
202
+ )
203
+ if (!node) return
204
+ const confirmed = await ctx.ui.dialog.confirm({
205
+ title: `Uninstall the agent on ${node}?`,
206
+ message:
207
+ "This removes the systemd unit and agent files. The server record remains until you revoke it.",
208
+ label: { confirm: "Uninstall agent", cancel: "Cancel" },
209
+ })
210
+ if (!confirmed) return
211
+ const result = await rpc.nodeUninstall({ node, sessionID }, where())
212
+ toast(result.output)
213
+ }
214
+
215
+ async function fleetInstaller(sessionID) {
216
+ const label = await ctx.ui.dialog.prompt({
217
+ title: "tethergate · fleet installer",
218
+ placeholder: "label (optional), e.g. tailnet",
219
+ })
220
+ if (label === undefined) return
221
+ const data = await rpc.fleetAdd(
222
+ {
223
+ sessionID,
224
+ label: label || "",
225
+ ttlSeconds: 3600,
226
+ maxUses: 50,
227
+ },
228
+ where(),
229
+ )
230
+ await ctx.ui.dialog.alert({
231
+ title: "tethergate · fleet installer",
232
+ message: `Run this on each node:\n\n${data.command}`,
233
+ })
234
+ }
235
+
236
+ async function manageFleet(sessionID) {
237
+ const result = await rpc.fleetList({}, where())
238
+ const active = result.tokens.filter((token) => token.valid)
239
+ if (active.length === 0) {
240
+ toast("No active fleet installers", "warning")
241
+ return
242
+ }
243
+ const id = await ctx.ui.dialog.select({
244
+ title: "tethergate · fleet installers",
245
+ options: active.map((token) => ({
246
+ title: token.label || token.id,
247
+ value: token.id,
248
+ description: `${token.uses}/${token.maxUses} uses · expires ${
249
+ token.expiresAt
250
+ ? new Date(token.expiresAt * 1000).toISOString()
251
+ : "never"
252
+ }`,
253
+ })),
254
+ })
255
+ if (!id) return
256
+ const confirmed = await ctx.ui.dialog.confirm({
257
+ title: "Revoke this fleet installer?",
258
+ message:
259
+ "New nodes can no longer use this one-liner. Already enrolled nodes keep their certificates until revoked individually.",
260
+ label: { confirm: "Revoke installer", cancel: "Cancel" },
261
+ })
262
+ if (!confirmed) return
263
+ await rpc.fleetRevoke({ sessionID, id }, where())
264
+ toast("Fleet installer revoked")
265
+ }
266
+
267
+ async function reactivateNode(sessionID) {
268
+ const result = await rpc.nodeList({}, where())
269
+ const quarantined = result.nodes.filter(
270
+ (node) => node.status === "quarantined",
271
+ )
272
+ if (quarantined.length === 0) {
273
+ toast("No quarantined nodes", "warning")
274
+ return
275
+ }
276
+ const node = await ctx.ui.dialog.select({
277
+ title: "tethergate · re-enable node",
278
+ options: quarantined.map((entry) => ({
279
+ title: entry.name,
280
+ value: entry.name,
281
+ description: entry.quarantine_reason || "quarantined by tripwire",
282
+ })),
283
+ })
284
+ if (!node) return
285
+ const confirmed = await ctx.ui.dialog.confirm({
286
+ title: `Re-enable ${node}?`,
287
+ message:
288
+ "Only do this after reviewing the tripwire audit entry. The agent will reconnect and the session grant still applies.",
289
+ label: { confirm: "Re-enable", cancel: "Cancel" },
290
+ })
291
+ if (!confirmed) return
292
+ await rpc.nodeReactivate({ node, sessionID }, where())
293
+ toast(`Re-enabled node ${node}`)
294
+ }
295
+
296
+ async function menu() {
297
+ const sessionID = activeSession()
298
+ if (!sessionID) {
299
+ await ctx.ui.dialog.alert({
300
+ title: "tethergate",
301
+ message: "Open a session first; access is granted per session.",
302
+ })
303
+ return
304
+ }
305
+ try {
306
+ let data = await rpc.snapshot({ sessionID }, where())
307
+ const options = buildMenuOptions(data)
308
+ const choice = await ctx.ui.dialog.select({
309
+ title: `tethergate · ${scopeLabel(data)}`,
310
+ options,
311
+ })
312
+ if (!choice) return
313
+
314
+ if (choice === "all") {
315
+ data = await rpc.grantAll({ sessionID }, where())
316
+ toast(`Granted all nodes to this session`)
317
+ return
318
+ }
319
+ if (choice === "choose") {
320
+ const nodes = await chooseNodes(data, sessionID)
321
+ if (!nodes || nodes.length === 0) return
322
+ data = await rpc.grantNodes({ sessionID, nodes }, where())
323
+ toast(`Granted ${nodes.join(", ")}`)
324
+ return
325
+ }
326
+ if (choice === "revoke") {
327
+ data = await rpc.revoke({ sessionID }, where())
328
+ toast("Revoked this session's access")
329
+ return
330
+ }
331
+ if (choice === "node-add") {
332
+ await enrollNode(sessionID)
333
+ return
334
+ }
335
+ if (choice === "fleet-add") {
336
+ await fleetInstaller(sessionID)
337
+ return
338
+ }
339
+ if (choice === "fleet-manage") {
340
+ await manageFleet(sessionID)
341
+ return
342
+ }
343
+ if (choice === "node-revoke") {
344
+ await revokeNode(sessionID)
345
+ return
346
+ }
347
+ if (choice === "node-update") {
348
+ await updateNode(sessionID)
349
+ return
350
+ }
351
+ if (choice === "node-rename") {
352
+ await renameNode(sessionID)
353
+ return
354
+ }
355
+ if (choice === "node-retire") {
356
+ await retireNode(sessionID)
357
+ return
358
+ }
359
+ if (choice === "node-uninstall") {
360
+ await uninstallNode(sessionID)
361
+ return
362
+ }
363
+ if (choice === "node-reset") {
364
+ await reactivateNode(sessionID)
365
+ return
366
+ }
367
+ if (choice === "default") {
368
+ const next = data.defaultAccess === "all" ? "deny" : "all"
369
+ if (next === "all") {
370
+ const confirmed = await ctx.ui.dialog.confirm({
371
+ title: "Allow every new session?",
372
+ message:
373
+ "This makes all future OpenCode sessions able to access every enrolled node without an explicit grant. Continue?",
374
+ label: { confirm: "Allow all", cancel: "Cancel" },
375
+ })
376
+ if (!confirmed) return
377
+ }
378
+ const result = await rpc.setDefault(
379
+ { mode: next, confirm: true, sessionID },
380
+ where(),
381
+ )
382
+ toast(`Default access for new sessions: ${result.default}`)
383
+ }
384
+ } catch (error) {
385
+ toast(
386
+ `tethergate: ${
387
+ error instanceof Error ? error.message : String(error)
388
+ }`,
389
+ "error",
390
+ )
391
+ }
392
+ }
393
+
394
+ ctx.keymap.layer(() => ({
395
+ mode: "global",
396
+ commands: [
397
+ {
398
+ id: "tethergate.panel",
399
+ title: "tethergate access",
400
+ description: "Grant this OpenCode session access to remote nodes",
401
+ group: "tethergate",
402
+ palette: true,
403
+ slash: { name: "tethergate", arguments: true },
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
+ },
427
+ },
428
+ ],
429
+ }))
430
+ },
431
+ })