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