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