@rainyun/cli 0.0.0-stage → 1.0.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/README.md +176 -2
- package/bin/postinstall.mjs +13 -0
- package/bin/rainyun-cli.mjs +13 -0
- package/catalog/human-names.json +340 -0
- package/catalog/list.openapi.json +354 -0
- package/catalog/openapi.json +34527 -0
- package/catalog/product-commands.json +332 -0
- package/catalog/public.openapi.json +1891 -0
- package/docs/AGENT_GUIDE.md +64 -0
- package/docs/install.md +44 -0
- package/docs/product-lists.md +48 -0
- package/docs/rgp.md +77 -0
- package/docs/terminal-exec.md +111 -0
- package/examples/public-panel.json +1 -0
- package/examples/public-rcs-price.json +1 -0
- package/examples/rca-terminal-input.json +1 -0
- package/examples/rcs-get.json +1 -0
- package/examples/rcs-list.json +1 -0
- package/examples/rcs-reboot.json +1 -0
- package/examples/terminal-controls.ndjson +3 -0
- package/examples/terminal-input.json +1 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +539 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +142 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +751 -0
- package/node_modules/ws/lib/sender.js +610 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +565 -0
- package/node_modules/ws/lib/websocket.js +1471 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +29 -4
- package/skills/rainyun-cli/SKILL.md +46 -0
- package/src/api/catalog.mjs +230 -0
- package/src/api/http.mjs +222 -0
- package/src/api/request.mjs +178 -0
- package/src/api/titles.mjs +9 -0
- package/src/auth/commands.mjs +192 -0
- package/src/auth/store.mjs +162 -0
- package/src/cli.mjs +154 -0
- package/src/commands/dispatch.mjs +128 -0
- package/src/commands/fields.mjs +77 -0
- package/src/commands/products.mjs +209 -0
- package/src/commands/rgs.mjs +44 -0
- package/src/json.mjs +81 -0
- package/src/output/business.mjs +188 -0
- package/src/output/discovery.mjs +197 -0
- package/src/output/examples.mjs +71 -0
- package/src/output/format.mjs +164 -0
- package/src/output/help.json +181 -0
- package/src/output/help.mjs +334 -0
- package/src/rgp/commands.mjs +251 -0
- package/src/rgp/input.mjs +144 -0
- package/src/rgp/operations.json +625 -0
- package/src/rgp/result.mjs +103 -0
- package/src/rgp/transfer.mjs +213 -0
- package/src/terminal/commands.mjs +155 -0
- package/src/terminal/session.mjs +233 -0
- package/src/terminal/target.mjs +155 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { setTimeout as pause } from 'node:timers/promises';
|
|
3
|
+
import { resolveRGS } from '../terminal/target.mjs';
|
|
4
|
+
import { baseAddress, defaultBase } from '../api/http.mjs';
|
|
5
|
+
import { check, fail, collect, prepare, object, integer, duration } from './input.mjs';
|
|
6
|
+
import { isWrite } from '../api/catalog.mjs';
|
|
7
|
+
import { readInput } from '../api/request.mjs';
|
|
8
|
+
import { sanitizer, interpret } from './result.mjs';
|
|
9
|
+
import { readSource, upload, download } from './transfer.mjs';
|
|
10
|
+
|
|
11
|
+
const ops = JSON.parse(
|
|
12
|
+
readFileSync(new URL('./operations.json', import.meta.url), 'utf8').replace(/^\uFEFF/, '')
|
|
13
|
+
).map((op) => ({ ...op, is_write: isWrite(op.method) }));
|
|
14
|
+
const operations = new Map(ops.map((op) => [op.operation_id, op]));
|
|
15
|
+
function lookup(key) {
|
|
16
|
+
const op = operations.get(key);
|
|
17
|
+
check(op, 'unknown RGP operation; use rainyun-cli rgp commands');
|
|
18
|
+
return op;
|
|
19
|
+
}
|
|
20
|
+
function allow(flags, keys) {
|
|
21
|
+
for (const key of Object.keys(flags)) check(keys.includes(key), 'unknown option --' + key);
|
|
22
|
+
}
|
|
23
|
+
function success(data) {
|
|
24
|
+
return { ok: true, data };
|
|
25
|
+
}
|
|
26
|
+
function commands(args, flags) {
|
|
27
|
+
check(args.length === 1, 'rgp commands takes no positional arguments');
|
|
28
|
+
allow(flags, ['query', 'offset', 'limit']);
|
|
29
|
+
const limit = integer(flags.limit ?? 20, 1, 100, 'limit'),
|
|
30
|
+
offset = integer(flags.offset ?? 0, 0, Number.MAX_SAFE_INTEGER, 'offset');
|
|
31
|
+
const query = (flags.query ?? '').toLowerCase();
|
|
32
|
+
const matches = ops.filter((op) =>
|
|
33
|
+
(op.operation_id + ' ' + op.summary + ' ' + op.path).toLowerCase().includes(query)
|
|
34
|
+
);
|
|
35
|
+
const items = matches.slice(offset, offset + limit),
|
|
36
|
+
end = offset + items.length,
|
|
37
|
+
more = end < matches.length;
|
|
38
|
+
return success({
|
|
39
|
+
items,
|
|
40
|
+
total: matches.length,
|
|
41
|
+
offset,
|
|
42
|
+
limit,
|
|
43
|
+
has_more: more,
|
|
44
|
+
next_offset: more ? end : null,
|
|
45
|
+
product_subtype: 'k8s_panel',
|
|
46
|
+
required_scope: 'rgs:read',
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function schema(args, flags) {
|
|
50
|
+
check(args.length === 2, 'usage: rainyun-cli rgp schema OPERATION');
|
|
51
|
+
allow(flags, []);
|
|
52
|
+
return success({
|
|
53
|
+
operation: lookup(args[1]),
|
|
54
|
+
product_subtype: 'k8s_panel',
|
|
55
|
+
metadata_scope: 'rgs:read',
|
|
56
|
+
input_shape: { query: 'object', body: 'object' },
|
|
57
|
+
file_flags: {
|
|
58
|
+
source: 'file.upload / file.write / config.write only',
|
|
59
|
+
output: 'file.download / backup.download only; must not exist',
|
|
60
|
+
},
|
|
61
|
+
task_wait_flags: 'task.wait: --wait-timeout 120s --poll-interval 1500ms',
|
|
62
|
+
notes: [
|
|
63
|
+
'HTTP helper uses the existing webpage app-token authorization',
|
|
64
|
+
'config.read/write edits original file content; no visual configuration schema',
|
|
65
|
+
'async accepted does not mean completed; track an explicit id using task.get/task.wait',
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
function validatedOptions(flags, op, ctx) {
|
|
70
|
+
allow(flags, [
|
|
71
|
+
'input',
|
|
72
|
+
'query',
|
|
73
|
+
'body',
|
|
74
|
+
'yes',
|
|
75
|
+
'dry-run',
|
|
76
|
+
'base-url',
|
|
77
|
+
'timeout',
|
|
78
|
+
'max-response-bytes',
|
|
79
|
+
'source',
|
|
80
|
+
'output',
|
|
81
|
+
'max-file-bytes',
|
|
82
|
+
'wait-timeout',
|
|
83
|
+
'poll-interval',
|
|
84
|
+
]);
|
|
85
|
+
check(
|
|
86
|
+
op.operation_id === 'task.wait' || !('wait-timeout' in flags || 'poll-interval' in flags),
|
|
87
|
+
'poll options require task.wait'
|
|
88
|
+
);
|
|
89
|
+
check(
|
|
90
|
+
!('max-file-bytes' in flags) ||
|
|
91
|
+
op.transfer ||
|
|
92
|
+
['config.write', 'file.write'].includes(op.operation_id),
|
|
93
|
+
'file limit requires file transfer or source write'
|
|
94
|
+
);
|
|
95
|
+
const poll = duration(flags['poll-interval'] ?? '1500ms', 'poll-interval', 60000);
|
|
96
|
+
check(poll >= 100, 'poll-interval must be at least 100ms');
|
|
97
|
+
baseAddress(flags['base-url'] ?? ((ctx.env ?? process.env).RAINYUN_API_URL || defaultBase));
|
|
98
|
+
return {
|
|
99
|
+
poll,
|
|
100
|
+
wait: duration(flags['wait-timeout'] ?? '120s', 'wait-timeout', 3600000),
|
|
101
|
+
maxFile: integer(flags['max-file-bytes'] ?? 1 << 30, 1, Number.MAX_SAFE_INTEGER, 'max-file-bytes'),
|
|
102
|
+
timeout: duration(flags.timeout ?? '30s', 'timeout'),
|
|
103
|
+
maxResponse: integer(flags['max-response-bytes'] ?? 16 << 20, 1, 64 << 20, 'max-response-bytes'),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function signalFor(parent, time) {
|
|
107
|
+
const timer = AbortSignal.timeout(time);
|
|
108
|
+
return parent ? AbortSignal.any([parent, timer]) : timer;
|
|
109
|
+
}
|
|
110
|
+
async function helperJSON(url, op, body, token, ctx, signal, maxResponse) {
|
|
111
|
+
const unknown = op.is_write ? { outcome: 'unknown' } : {};
|
|
112
|
+
const serialized = body ? ctx.jsonStringify(body) : undefined;
|
|
113
|
+
check(!serialized || Buffer.byteLength(serialized) <= 16 << 20, 'RGP JSON request exceeds 16 MiB');
|
|
114
|
+
if (signal.aborted) fail('network', 'request canceled before sending');
|
|
115
|
+
let response;
|
|
116
|
+
try {
|
|
117
|
+
response = await fetch(url, {
|
|
118
|
+
method: op.method,
|
|
119
|
+
body: serialized,
|
|
120
|
+
headers: {
|
|
121
|
+
'app-token': token,
|
|
122
|
+
Accept: 'application/json',
|
|
123
|
+
...(serialized ? { 'Content-Type': 'application/json' } : {}),
|
|
124
|
+
},
|
|
125
|
+
redirect: 'manual',
|
|
126
|
+
signal,
|
|
127
|
+
});
|
|
128
|
+
} catch {
|
|
129
|
+
fail('network', 'RGP request failed', unknown);
|
|
130
|
+
}
|
|
131
|
+
const status = response.status;
|
|
132
|
+
const extras = { http_status: status, ...unknown };
|
|
133
|
+
if (status >= 300 && status < 400) {
|
|
134
|
+
await response.body?.cancel();
|
|
135
|
+
fail('protocol', 'RGP redirects are not permitted', extras);
|
|
136
|
+
}
|
|
137
|
+
if ([401, 403].includes(status)) {
|
|
138
|
+
await response.body?.cancel();
|
|
139
|
+
fail('auth', 'RGP helper rejected the credential', { http_status: status });
|
|
140
|
+
}
|
|
141
|
+
let envelope, raw;
|
|
142
|
+
try {
|
|
143
|
+
raw = await collect(response.body ?? [], maxResponse, 'protocol', 'response too large');
|
|
144
|
+
} catch (e) {
|
|
145
|
+
fail(e.kind ?? 'network', 'RGP response incomplete or too large', extras);
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
envelope = ctx.jsonParse(new TextDecoder('utf-8', { fatal: true }).decode(raw));
|
|
149
|
+
} catch {
|
|
150
|
+
fail('protocol', 'RGP response was not valid JSON', extras);
|
|
151
|
+
}
|
|
152
|
+
if (!Number.isInteger(envelope?.code) && typeof envelope?.code !== 'bigint')
|
|
153
|
+
fail('protocol', 'RGP response has no valid business code', extras);
|
|
154
|
+
if (!response.ok || envelope.code !== 200)
|
|
155
|
+
fail('api', 'RGP helper rejected the request', {
|
|
156
|
+
http_status: status,
|
|
157
|
+
business_code: envelope.code,
|
|
158
|
+
});
|
|
159
|
+
return envelope;
|
|
160
|
+
}
|
|
161
|
+
async function execute(op, id, flags, ctx) {
|
|
162
|
+
const opts = validatedOptions(flags, op, ctx);
|
|
163
|
+
const input = await readInput(flags, ctx);
|
|
164
|
+
check(
|
|
165
|
+
!Object.keys(input.path ?? {}).length && !Object.keys(input.headers ?? {}).length,
|
|
166
|
+
'RGP only accepts query and body'
|
|
167
|
+
);
|
|
168
|
+
const query = input.query ?? {},
|
|
169
|
+
body = Object.hasOwn(input, 'body') ? input.body : {};
|
|
170
|
+
check(object(body), 'body must be an object');
|
|
171
|
+
prepare(op, query, body, flags, opts.maxFile);
|
|
172
|
+
const hasBody = Object.keys(body).length > 0;
|
|
173
|
+
if (flags['dry-run'])
|
|
174
|
+
return {
|
|
175
|
+
ok: true,
|
|
176
|
+
dry_run: true,
|
|
177
|
+
product: 'rgs',
|
|
178
|
+
subtype: 'k8s_panel',
|
|
179
|
+
id,
|
|
180
|
+
operation: op.operation_id,
|
|
181
|
+
method: op.method,
|
|
182
|
+
helper_path: op.path,
|
|
183
|
+
query: Object.fromEntries(Object.entries(query).map(([k, v]) => [k, [String(v)]])),
|
|
184
|
+
body: hasBody ? body : null,
|
|
185
|
+
is_write: op.is_write,
|
|
186
|
+
requires_yes: op.requires_yes,
|
|
187
|
+
metadata_source: 'rgs_api_detail',
|
|
188
|
+
source: flags.source ?? '',
|
|
189
|
+
output: flags.output ?? '',
|
|
190
|
+
source_content_read: false,
|
|
191
|
+
restore_requires_status: 'stopped',
|
|
192
|
+
async: op.async,
|
|
193
|
+
};
|
|
194
|
+
const target = await resolveRGS(id, flags, ctx);
|
|
195
|
+
check(
|
|
196
|
+
op.operation_id !== 'backup.restore' || target.status === 'stopped',
|
|
197
|
+
'backup restore requires a freshly verified stopped instance'
|
|
198
|
+
);
|
|
199
|
+
const base = target.origin + '/' + target.namespace,
|
|
200
|
+
url = new URL(base + op.path);
|
|
201
|
+
for (const [k, v] of Object.entries(query)) url.searchParams.set(k, String(v));
|
|
202
|
+
const clean = sanitizer([target.token, ...(ctx.redactionSecrets ?? [])]);
|
|
203
|
+
const json = (signal) =>
|
|
204
|
+
helperJSON(url, op, hasBody ? body : null, target.token, ctx, signal, opts.maxResponse);
|
|
205
|
+
if (op.operation_id === 'task.wait') {
|
|
206
|
+
const signal = signalFor(ctx.signal, opts.wait);
|
|
207
|
+
let last;
|
|
208
|
+
try {
|
|
209
|
+
while (!signal.aborted) {
|
|
210
|
+
last = interpret(op, await json(signalFor(signal, opts.timeout)), query, clean);
|
|
211
|
+
if (last.outcome === 'succeeded') return last;
|
|
212
|
+
await pause(opts.poll, undefined, { signal });
|
|
213
|
+
}
|
|
214
|
+
} catch (e) {
|
|
215
|
+
if (!signal.aborted) {
|
|
216
|
+
if (!e.outcome) {
|
|
217
|
+
e.outcome = 'unknown';
|
|
218
|
+
e.details = { task_id: clean.text(query.id) };
|
|
219
|
+
}
|
|
220
|
+
throw e;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
fail('network', 'stopped waiting; remote task completion remains unknown', {
|
|
224
|
+
outcome: 'unknown',
|
|
225
|
+
details: { task_id: clean.text(query.id), ...(last ? { response: last.response } : {}) },
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
const signal = signalFor(ctx.signal, opts.timeout);
|
|
229
|
+
if (op.transfer === 'download')
|
|
230
|
+
return download(base, op.operation_id, () => json(signal), flags.output, opts.maxFile, signal);
|
|
231
|
+
if (op.transfer === 'upload')
|
|
232
|
+
return upload(url, target.token, body.path_with_file_name, flags.source, opts.maxFile, signal);
|
|
233
|
+
if (flags.source) body.content = await readSource(flags.source, Math.min(opts.maxFile, 16 << 20));
|
|
234
|
+
return interpret(op, await json(signal), query, clean);
|
|
235
|
+
}
|
|
236
|
+
export async function rgp(args, flags, ctx) {
|
|
237
|
+
if (args[0] === 'commands') return commands(args, flags);
|
|
238
|
+
if (args[0] === 'schema') return schema(args, flags);
|
|
239
|
+
check(
|
|
240
|
+
args[0] === 'call' && args.length === 3,
|
|
241
|
+
'usage: rainyun-cli rgp commands | schema OPERATION | call ID OPERATION'
|
|
242
|
+
);
|
|
243
|
+
const id = integer(args[1], 1, Number.MAX_SAFE_INTEGER, 'RGP id'),
|
|
244
|
+
op = lookup(args[2]);
|
|
245
|
+
try {
|
|
246
|
+
return await execute(op, id, flags, ctx);
|
|
247
|
+
} catch (e) {
|
|
248
|
+
e.operation ??= op.operation_id;
|
|
249
|
+
throw e;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
export function fail(kind, message, extra) {
|
|
2
|
+
throw Object.assign(new Error(message), { kind }, extra);
|
|
3
|
+
}
|
|
4
|
+
export function check(ok, message) {
|
|
5
|
+
if (!ok) fail('usage', message);
|
|
6
|
+
}
|
|
7
|
+
export const object = (x) => x !== null && typeof x === 'object' && !Array.isArray(x);
|
|
8
|
+
export function integer(value, min, max, name) {
|
|
9
|
+
const n = Number(value);
|
|
10
|
+
check(
|
|
11
|
+
/^\d+$/.test(String(value)) && Number.isSafeInteger(n) && n >= min && n <= max,
|
|
12
|
+
'invalid ' + name
|
|
13
|
+
);
|
|
14
|
+
return n;
|
|
15
|
+
}
|
|
16
|
+
export function duration(value, name, max = 2147483647) {
|
|
17
|
+
const text = String(value);
|
|
18
|
+
const units = { ns: 1e-6, us: 0.001, µs: 0.001, μs: 0.001, ms: 1, s: 1000, m: 60000, h: 3600000 };
|
|
19
|
+
let pos = 0,
|
|
20
|
+
ms = 0;
|
|
21
|
+
for (const match of text.matchAll(/(\d+(?:\.\d*)?|\.\d+)(ns|us|µs|μs|ms|s|m|h)/g)) {
|
|
22
|
+
check(match.index === pos, 'invalid ' + name);
|
|
23
|
+
pos += match[0].length;
|
|
24
|
+
ms += Number(match[1]) * units[match[2]];
|
|
25
|
+
}
|
|
26
|
+
check(pos === text.length && ms > 0 && ms <= max, 'invalid ' + name);
|
|
27
|
+
return Math.ceil(ms);
|
|
28
|
+
}
|
|
29
|
+
export async function collect(stream, limit, kind, message) {
|
|
30
|
+
const parts = [];
|
|
31
|
+
let bytes = 0;
|
|
32
|
+
for await (const part of stream) {
|
|
33
|
+
const b = Buffer.from(part);
|
|
34
|
+
bytes += b.length;
|
|
35
|
+
if (bytes > limit) fail(kind, message);
|
|
36
|
+
parts.push(b);
|
|
37
|
+
}
|
|
38
|
+
return Buffer.concat(parts, bytes);
|
|
39
|
+
}
|
|
40
|
+
export function bounded(value, max) {
|
|
41
|
+
return (
|
|
42
|
+
typeof value === 'string' &&
|
|
43
|
+
!!value &&
|
|
44
|
+
Buffer.byteLength(value) <= max &&
|
|
45
|
+
!/[\x00-\x1f\x7f]/.test(value)
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
function remote(path) {
|
|
49
|
+
return (
|
|
50
|
+
typeof path === 'string' &&
|
|
51
|
+
path.startsWith('/') &&
|
|
52
|
+
!path.startsWith('//') &&
|
|
53
|
+
!/[\\\x00\r\n]/.test(path)
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
export function prepare(op, query, body, flags, maxFile) {
|
|
57
|
+
const id = op.operation_id;
|
|
58
|
+
const sourceWrite = ['file.write', 'config.write'].includes(id);
|
|
59
|
+
for (const name of ['source', 'output'])
|
|
60
|
+
if (name in flags) check(flags[name] && flags[name] !== '-', 'invalid --' + name);
|
|
61
|
+
check(
|
|
62
|
+
!flags.source || sourceWrite || op.transfer === 'upload',
|
|
63
|
+
'this operation does not accept --source'
|
|
64
|
+
);
|
|
65
|
+
check(!flags.output || op.transfer === 'download', 'this operation does not accept --output');
|
|
66
|
+
check(op.transfer !== 'download' || flags.output, 'download requires --output');
|
|
67
|
+
check(op.transfer !== 'upload' || flags.source, 'upload requires --source');
|
|
68
|
+
for (const [location, values] of Object.entries({ query, body })) {
|
|
69
|
+
for (const [key, v] of Object.entries(values)) {
|
|
70
|
+
const f = op.fields.find((f) => f.in === location && f.name === key);
|
|
71
|
+
check(f, 'unknown RGP input field');
|
|
72
|
+
const valid =
|
|
73
|
+
f.type === 'integer'
|
|
74
|
+
? Number.isSafeInteger(v)
|
|
75
|
+
: f.type === 'array'
|
|
76
|
+
? Array.isArray(v)
|
|
77
|
+
: typeof v === f.type;
|
|
78
|
+
check(valid, 'invalid ' + key);
|
|
79
|
+
if (['uuid', 'id', 'tag'].includes(key)) check(bounded(v, 1024), 'invalid identifier');
|
|
80
|
+
if (
|
|
81
|
+
['path', 'old_path', 'new_path', 'dst_path', 'zip_file_path', 'path_with_file_name'].includes(
|
|
82
|
+
key
|
|
83
|
+
)
|
|
84
|
+
)
|
|
85
|
+
check(
|
|
86
|
+
remote(v) && !(key === 'path_with_file_name' && v.endsWith('/')),
|
|
87
|
+
'invalid remote absolute path'
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
for (const f of op.fields.filter((f) => f.required && f.in === location))
|
|
91
|
+
check(
|
|
92
|
+
Object.hasOwn(values, f.name) || (f.name === 'content' && sourceWrite && flags.source),
|
|
93
|
+
'missing ' + f.name
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (Object.hasOwn(body, 'content'))
|
|
97
|
+
check(
|
|
98
|
+
!flags.source && Buffer.byteLength(body.content) <= Math.min(maxFile, 16 << 20),
|
|
99
|
+
'invalid content length or conflicting --source'
|
|
100
|
+
);
|
|
101
|
+
if (body.paths) check(body.paths.length && body.paths.every(remote), 'invalid paths');
|
|
102
|
+
if ('mode' in body) check(/^[0-7]{3,4}$/.test(body.mode), 'invalid mode');
|
|
103
|
+
if ('zip_file_name' in body)
|
|
104
|
+
check(
|
|
105
|
+
body.zip_file_name &&
|
|
106
|
+
!['.', '..'].includes(body.zip_file_name) &&
|
|
107
|
+
!/[/\\\0\r\n]/.test(body.zip_file_name),
|
|
108
|
+
'invalid zip file name'
|
|
109
|
+
);
|
|
110
|
+
if (id === 'backup.list') {
|
|
111
|
+
query.type ??= 'app';
|
|
112
|
+
check(query.type === 'app', 'invalid backup type');
|
|
113
|
+
}
|
|
114
|
+
if (id === 'task.list' && 'type' in query)
|
|
115
|
+
check(
|
|
116
|
+
['backup', 'restore', 'zip', 'unzip', 'delete', 'copy'].includes(query.type),
|
|
117
|
+
'invalid task type'
|
|
118
|
+
);
|
|
119
|
+
if (id === 'cron.create') {
|
|
120
|
+
check(
|
|
121
|
+
bounded(body.name, 128) && [...body.name].length <= 32 && bounded(body.cron_expression, 512),
|
|
122
|
+
'invalid cron name or expression'
|
|
123
|
+
);
|
|
124
|
+
check(
|
|
125
|
+
['shell', 'backup_app', 'restart_app', 'start_app', 'stop_app'].includes(body.type),
|
|
126
|
+
'invalid cron type'
|
|
127
|
+
);
|
|
128
|
+
body.logs_limit ??= 7;
|
|
129
|
+
integer(body.logs_limit, 1, 7, 'logs_limit');
|
|
130
|
+
if (body.type === 'shell') {
|
|
131
|
+
check(
|
|
132
|
+
body.shell_content && Buffer.byteLength(body.shell_content) <= 65536,
|
|
133
|
+
'invalid shell_content'
|
|
134
|
+
);
|
|
135
|
+
body.execute_method ??= 'attach';
|
|
136
|
+
check(body.execute_method === 'attach', 'invalid execute_method');
|
|
137
|
+
} else
|
|
138
|
+
check(!('shell_content' in body || 'execute_method' in body), 'shell options require shell type');
|
|
139
|
+
if (body.type === 'backup_app') {
|
|
140
|
+
body.backup_keep_count ??= 2;
|
|
141
|
+
integer(body.backup_keep_count, 1, 30, 'backup_keep_count');
|
|
142
|
+
} else check(!('backup_keep_count' in body), 'backup_keep_count requires backup_app');
|
|
143
|
+
}
|
|
144
|
+
}
|