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/rpc.js ADDED
@@ -0,0 +1,399 @@
1
+ import { Rpc } from "@opencode/plugin/rpc"
2
+
3
+ const nodeSchema = {
4
+ type: "object",
5
+ properties: {
6
+ id: { type: "string" },
7
+ name: { type: "string" },
8
+ online: { type: "boolean" },
9
+ status: { type: "string" },
10
+ quarantine_reason: { type: "string" },
11
+ connected_ip: { type: "string" },
12
+ cert_days_left: { type: ["integer", "null"] },
13
+ },
14
+ required: ["id", "name", "online", "status"],
15
+ additionalProperties: false,
16
+ }
17
+
18
+ const snapshotSchema = {
19
+ type: "object",
20
+ properties: {
21
+ sessionID: { type: "string" },
22
+ defaultAccess: { type: "string", enum: ["deny", "all"] },
23
+ grantAll: { type: "boolean" },
24
+ grantNodes: { type: "array", items: { type: "string" } },
25
+ expiresAt: { type: ["integer", "null"] },
26
+ nodes: { type: "array", items: nodeSchema },
27
+ },
28
+ required: [
29
+ "sessionID",
30
+ "defaultAccess",
31
+ "grantAll",
32
+ "grantNodes",
33
+ "expiresAt",
34
+ "nodes",
35
+ ],
36
+ additionalProperties: false,
37
+ }
38
+
39
+ const sessionInput = {
40
+ type: "object",
41
+ properties: { sessionID: { type: "string" } },
42
+ required: ["sessionID"],
43
+ additionalProperties: false,
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
+
55
+ const nodeChangeInput = {
56
+ type: "object",
57
+ properties: {
58
+ sessionID: { type: "string" },
59
+ node: { type: "string" },
60
+ },
61
+ required: ["sessionID", "node"],
62
+ additionalProperties: false,
63
+ }
64
+
65
+ const methods = {
66
+ snapshot: {
67
+ input: sessionInput,
68
+ output: snapshotSchema,
69
+ },
70
+ grantAll: {
71
+ input: sessionInput,
72
+ output: snapshotSchema,
73
+ },
74
+ grantNodes: {
75
+ input: {
76
+ type: "object",
77
+ properties: {
78
+ sessionID: { type: "string" },
79
+ nodes: {
80
+ type: "array",
81
+ minItems: 1,
82
+ items: { type: "string" },
83
+ },
84
+ },
85
+ required: ["sessionID", "nodes"],
86
+ additionalProperties: false,
87
+ },
88
+ output: snapshotSchema,
89
+ },
90
+ revoke: {
91
+ input: sessionInput,
92
+ output: snapshotSchema,
93
+ },
94
+ nodeList: {
95
+ input: {
96
+ type: "object",
97
+ properties: {},
98
+ additionalProperties: false,
99
+ },
100
+ output: {
101
+ type: "object",
102
+ properties: {
103
+ nodes: { type: "array", items: nodeSchema },
104
+ },
105
+ required: ["nodes"],
106
+ additionalProperties: false,
107
+ },
108
+ },
109
+ nodeAdd: {
110
+ input: {
111
+ type: "object",
112
+ properties: {
113
+ sessionID: { type: "string" },
114
+ name: { type: "string" },
115
+ },
116
+ required: ["sessionID", "name"],
117
+ additionalProperties: false,
118
+ },
119
+ output: {
120
+ type: "object",
121
+ properties: {
122
+ name: { type: "string" },
123
+ command: { type: "string" },
124
+ expiresAt: { type: ["integer", "null"] },
125
+ },
126
+ required: ["name", "command", "expiresAt"],
127
+ additionalProperties: false,
128
+ },
129
+ },
130
+ nodeRevoke: {
131
+ input: nodeChangeInput,
132
+ output: {
133
+ type: "object",
134
+ properties: { node: { type: "string" } },
135
+ required: ["node"],
136
+ additionalProperties: false,
137
+ },
138
+ },
139
+ nodeReactivate: {
140
+ input: nodeChangeInput,
141
+ output: {
142
+ type: "object",
143
+ properties: { node: { type: "string" } },
144
+ required: ["node"],
145
+ additionalProperties: false,
146
+ },
147
+ },
148
+ nodeRename: {
149
+ input: {
150
+ type: "object",
151
+ properties: {
152
+ sessionID: { type: "string" },
153
+ node: { type: "string" },
154
+ name: { type: "string" },
155
+ },
156
+ required: ["sessionID", "node", "name"],
157
+ additionalProperties: false,
158
+ },
159
+ output: {
160
+ type: "object",
161
+ properties: { node: { type: "string" } },
162
+ required: ["node"],
163
+ additionalProperties: false,
164
+ },
165
+ },
166
+ nodeRetire: {
167
+ input: nodeChangeInput,
168
+ output: {
169
+ type: "object",
170
+ properties: { node: { type: "string" } },
171
+ required: ["node"],
172
+ additionalProperties: false,
173
+ },
174
+ },
175
+ nodeUpdate: {
176
+ input: {
177
+ type: "object",
178
+ properties: {
179
+ sessionID: { type: "string" },
180
+ node: { type: "string" },
181
+ rollback: { type: "boolean" },
182
+ },
183
+ required: ["sessionID", "node", "rollback"],
184
+ additionalProperties: false,
185
+ },
186
+ output: {
187
+ type: "object",
188
+ properties: {
189
+ node: { type: "string" },
190
+ rollback: { type: "boolean" },
191
+ output: { type: "string" },
192
+ },
193
+ required: ["node", "rollback", "output"],
194
+ additionalProperties: false,
195
+ },
196
+ },
197
+ nodeUninstall: {
198
+ input: nodeChangeInput,
199
+ output: {
200
+ type: "object",
201
+ properties: {
202
+ node: { type: "string" },
203
+ output: { type: "string" },
204
+ },
205
+ required: ["node", "output"],
206
+ additionalProperties: false,
207
+ },
208
+ },
209
+ fleetList: {
210
+ input: {
211
+ type: "object",
212
+ properties: {},
213
+ additionalProperties: false,
214
+ },
215
+ output: {
216
+ type: "object",
217
+ properties: {
218
+ tokens: {
219
+ type: "array",
220
+ items: {
221
+ type: "object",
222
+ properties: {
223
+ id: { type: "string" },
224
+ label: { type: "string" },
225
+ uses: { type: "integer" },
226
+ maxUses: { type: "integer" },
227
+ valid: { type: "boolean" },
228
+ expiresAt: { type: ["integer", "null"] },
229
+ },
230
+ required: ["id", "label", "uses", "maxUses", "valid", "expiresAt"],
231
+ additionalProperties: false,
232
+ },
233
+ },
234
+ },
235
+ required: ["tokens"],
236
+ additionalProperties: false,
237
+ },
238
+ },
239
+ fleetRevoke: {
240
+ input: {
241
+ type: "object",
242
+ properties: {
243
+ sessionID: { type: "string" },
244
+ id: { type: "string" },
245
+ },
246
+ required: ["sessionID", "id"],
247
+ additionalProperties: false,
248
+ },
249
+ output: {
250
+ type: "object",
251
+ properties: { id: { type: "string" } },
252
+ required: ["id"],
253
+ additionalProperties: false,
254
+ },
255
+ },
256
+ fleetAdd: {
257
+ input: {
258
+ type: "object",
259
+ properties: {
260
+ sessionID: { type: "string" },
261
+ label: { type: "string" },
262
+ ttlSeconds: { type: "integer", minimum: 60, maximum: 86400 },
263
+ maxUses: { type: "integer", minimum: 1, maximum: 1000 },
264
+ },
265
+ required: ["sessionID"],
266
+ additionalProperties: false,
267
+ },
268
+ output: {
269
+ type: "object",
270
+ properties: {
271
+ id: { type: "string" },
272
+ label: { type: "string" },
273
+ command: { type: "string" },
274
+ expiresAt: { type: ["integer", "null"] },
275
+ maxUses: { type: "integer" },
276
+ },
277
+ required: ["id", "label", "command", "expiresAt", "maxUses"],
278
+ additionalProperties: false,
279
+ },
280
+ },
281
+ autoStatus: {
282
+ input: {
283
+ type: "object",
284
+ properties: {},
285
+ additionalProperties: false,
286
+ },
287
+ output: {
288
+ type: "object",
289
+ properties: { output: { type: "string" } },
290
+ required: ["output"],
291
+ additionalProperties: false,
292
+ },
293
+ },
294
+ autoPause: {
295
+ input: {
296
+ type: "object",
297
+ properties: {
298
+ sessionID: { type: "string" },
299
+ reason: { type: "string" },
300
+ },
301
+ required: ["sessionID"],
302
+ additionalProperties: false,
303
+ },
304
+ output: {
305
+ type: "object",
306
+ properties: { output: { type: "string" } },
307
+ required: ["output"],
308
+ additionalProperties: false,
309
+ },
310
+ },
311
+ autoResume: {
312
+ input: {
313
+ type: "object",
314
+ properties: { sessionID: { type: "string" } },
315
+ required: ["sessionID"],
316
+ additionalProperties: false,
317
+ },
318
+ output: {
319
+ type: "object",
320
+ properties: { output: { type: "string" } },
321
+ required: ["output"],
322
+ additionalProperties: false,
323
+ },
324
+ },
325
+ autoRun: {
326
+ input: {
327
+ type: "object",
328
+ properties: { sessionID: { type: "string" } },
329
+ required: ["sessionID"],
330
+ additionalProperties: false,
331
+ },
332
+ output: {
333
+ type: "object",
334
+ properties: { output: { type: "string" } },
335
+ required: ["output"],
336
+ additionalProperties: false,
337
+ },
338
+ },
339
+ setDefault: {
340
+ input: {
341
+ type: "object",
342
+ properties: {
343
+ sessionID: { type: "string" },
344
+ mode: { type: "string", enum: ["deny", "all"] },
345
+ confirm: { type: "boolean" },
346
+ },
347
+ required: ["sessionID", "mode", "confirm"],
348
+ additionalProperties: false,
349
+ },
350
+ output: {
351
+ type: "object",
352
+ properties: { default: { type: "string", enum: ["deny", "all"] } },
353
+ required: ["default"],
354
+ additionalProperties: false,
355
+ },
356
+ },
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,
398
+ events: {},
399
+ })
package/service.js ADDED
@@ -0,0 +1,255 @@
1
+ const SESSION_RE = /^ses_[A-Za-z0-9_-]{4,128}$/
2
+ const NODE_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/
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
+
13
+ export class TethergateService {
14
+ /**
15
+ * @param {(args: string[]) => Promise<string>} run
16
+ */
17
+ constructor(run) {
18
+ this.run = run
19
+ }
20
+
21
+ static validateSession(sessionID) {
22
+ if (typeof sessionID !== "string" || !SESSION_RE.test(sessionID)) {
23
+ throw new Error("invalid OpenCode session id (expected ses_...)")
24
+ }
25
+ return sessionID
26
+ }
27
+
28
+ static validateNode(name) {
29
+ if (typeof name !== "string" || !NODE_RE.test(name)) {
30
+ throw new Error(`invalid node name ${JSON.stringify(name)}`)
31
+ }
32
+ return name
33
+ }
34
+
35
+ async _json(args) {
36
+ const output = await this.run(args)
37
+ try {
38
+ return JSON.parse(output)
39
+ } catch {
40
+ throw new Error(`tethergate returned non-JSON output for ${args.join(" ")}`)
41
+ }
42
+ }
43
+
44
+ async nodeList() {
45
+ const nodes = await this._json(["node", "list", "--json"])
46
+ return (Array.isArray(nodes) ? nodes : []).map((node) => ({
47
+ id: String(node.id),
48
+ name: String(node.name),
49
+ online: Boolean(node.online),
50
+ status: String(node.status),
51
+ quarantine_reason: String(node.quarantine_reason || ""),
52
+ connected_ip: String(node.connected_ip || ""),
53
+ cert_days_left:
54
+ typeof node.cert_days_left === "number" ? node.cert_days_left : null,
55
+ }))
56
+ }
57
+
58
+ async nodeAdd(name) {
59
+ name = TethergateService.validateNode(name)
60
+ const data = await this._json(["node", "add", name, "--json"])
61
+ return {
62
+ name: String(data.name),
63
+ command: String(data.command),
64
+ expiresAt:
65
+ typeof data.expires_at === "number" ? data.expires_at : null,
66
+ }
67
+ }
68
+
69
+ async nodeRevoke(name) {
70
+ name = TethergateService.validateNode(name)
71
+ await this.run(["node", "revoke", name])
72
+ return { node: name }
73
+ }
74
+
75
+ async nodeReactivate(name) {
76
+ name = TethergateService.validateNode(name)
77
+ await this.run(["node", "reset", name])
78
+ return { node: name }
79
+ }
80
+
81
+ async nodeRename(name, newName) {
82
+ name = TethergateService.validateNode(name)
83
+ newName = TethergateService.validateNode(newName)
84
+ await this.run(["node", "rename", name, newName])
85
+ return { node: newName }
86
+ }
87
+
88
+ async nodeRetire(name) {
89
+ name = TethergateService.validateNode(name)
90
+ await this.run(["node", "retire", name])
91
+ return { node: name }
92
+ }
93
+
94
+ async nodeUpdate(name, rollback = false) {
95
+ name = TethergateService.validateNode(name)
96
+ const args = ["node", "update", name]
97
+ if (rollback) args.push("--rollback")
98
+ const output = await this.run(args)
99
+ return { node: name, rollback, output }
100
+ }
101
+
102
+ async nodeUninstall(name) {
103
+ name = TethergateService.validateNode(name)
104
+ const output = await this.run(["node", "uninstall", name])
105
+ return { node: name, output }
106
+ }
107
+
108
+ async autoStatus() {
109
+ const output = await this.run(["auto", "status"])
110
+ return { output: String(output || "").trim() }
111
+ }
112
+
113
+ async autoPause(reason = "") {
114
+ const output = await this.run([
115
+ "auto",
116
+ "pause",
117
+ "--reason",
118
+ String(reason || ""),
119
+ ])
120
+ return { output: String(output || "").trim() }
121
+ }
122
+
123
+ async autoResume() {
124
+ const output = await this.run(["auto", "resume"])
125
+ return { output: String(output || "").trim() }
126
+ }
127
+
128
+ async autoRun() {
129
+ const output = await this.run(["auto", "run", "--yes"])
130
+ return { output: String(output || "").trim() }
131
+ }
132
+
133
+ async fleetAdd(label = "", ttlSeconds = 3600, maxUses = 50) {
134
+ const args = [
135
+ "fleet",
136
+ "add",
137
+ "--json",
138
+ "--label",
139
+ String(label || ""),
140
+ "--ttl",
141
+ String(Math.max(60, Math.min(Number(ttlSeconds) || 3600, 86400))),
142
+ "--max-uses",
143
+ String(Math.max(1, Math.min(Number(maxUses) || 50, 1000))),
144
+ ]
145
+ const data = await this._json(args)
146
+ return {
147
+ id: String(data.id),
148
+ label: String(data.label || ""),
149
+ command: String(data.command),
150
+ expiresAt:
151
+ typeof data.expires_at === "number" ? data.expires_at : null,
152
+ maxUses: Number(data.max_uses || 0),
153
+ }
154
+ }
155
+
156
+ async fleetList() {
157
+ const tokens = await this._json(["fleet", "list", "--json"])
158
+ return (Array.isArray(tokens) ? tokens : []).map((item) => ({
159
+ id: String(item.id),
160
+ label: String(item.label || ""),
161
+ uses: Number(item.uses || 0),
162
+ maxUses: Number(item.max_uses || 0),
163
+ valid: Boolean(item.valid),
164
+ expiresAt:
165
+ typeof item.expires_at === "number" ? item.expires_at : null,
166
+ }))
167
+ }
168
+
169
+ async fleetRevoke(id) {
170
+ if (typeof id !== "string" || !/^flt_[A-Za-z0-9_-]{4,64}$/.test(id)) {
171
+ throw new Error(`invalid fleet installer id ${JSON.stringify(id)}`)
172
+ }
173
+ await this.run(["fleet", "revoke", id])
174
+ return { id }
175
+ }
176
+
177
+ async snapshot(sessionID) {
178
+ sessionID = TethergateService.validateSession(sessionID)
179
+ const [nodes, access] = await Promise.all([
180
+ this._json(["node", "list", "--json"]),
181
+ this._json(["access", "show", "--json"]),
182
+ ])
183
+ const grants = Array.isArray(access.session_grants)
184
+ ? access.session_grants
185
+ : []
186
+ const grant = grants.find((entry) => entry.session_id === sessionID)
187
+ const defaultAccess = access.default === "all" ? "all" : "deny"
188
+ const grantAll = defaultAccess === "all" || grant?.all_nodes === true
189
+ const grantNodes = grantAll
190
+ ? []
191
+ : Array.isArray(grant?.node_names)
192
+ ? grant.node_names.map(String)
193
+ : []
194
+ return {
195
+ sessionID,
196
+ defaultAccess,
197
+ grantAll,
198
+ grantNodes,
199
+ expiresAt:
200
+ typeof grant?.expires_at === "number" ? grant.expires_at : null,
201
+ nodes: (Array.isArray(nodes) ? nodes : []).map((node) => ({
202
+ id: String(node.id),
203
+ name: String(node.name),
204
+ online: Boolean(node.online),
205
+ status: String(node.status),
206
+ quarantine_reason: String(node.quarantine_reason || ""),
207
+ connected_ip: String(node.connected_ip || ""),
208
+ cert_days_left:
209
+ typeof node.cert_days_left === "number" ? node.cert_days_left : null,
210
+ })),
211
+ }
212
+ }
213
+
214
+ async grantAll(sessionID) {
215
+ sessionID = TethergateService.validateSession(sessionID)
216
+ await this.run(["session", "grant", sessionID, "--all"])
217
+ return this.snapshot(sessionID)
218
+ }
219
+
220
+ async grantNodes(sessionID, nodes) {
221
+ sessionID = TethergateService.validateSession(sessionID)
222
+ if (!Array.isArray(nodes) || nodes.length === 0) {
223
+ throw new Error("choose at least one node")
224
+ }
225
+ const names = [...new Set(nodes.map(TethergateService.validateNode))]
226
+ const args = ["session", "grant", sessionID]
227
+ for (const name of names) args.push("--node", name)
228
+ await this.run(args)
229
+ return this.snapshot(sessionID)
230
+ }
231
+
232
+ async revoke(sessionID) {
233
+ sessionID = TethergateService.validateSession(sessionID)
234
+ await this.run(["session", "revoke", sessionID])
235
+ return this.snapshot(sessionID)
236
+ }
237
+
238
+ async setDefault(mode, confirm = false) {
239
+ if (mode !== "deny" && mode !== "all") {
240
+ throw new Error("default access must be deny or all")
241
+ }
242
+ const args = ["access", "default", mode]
243
+ if (mode === "all") {
244
+ if (!confirm) throw new Error("allowing every session requires confirmation")
245
+ args.push("--yes")
246
+ }
247
+ await this.run(args)
248
+ const access = await this._json(["access", "show", "--json"])
249
+ return { default: access.default === "all" ? "all" : "deny" }
250
+ }
251
+
252
+ describe(snapshot) {
253
+ return describeSnapshot(snapshot)
254
+ }
255
+ }