@whyour/qinglong 2.22.0 → 2.23.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.
Files changed (150) hide show
  1. package/back/protos/cron.proto +4 -0
  2. package/docs/development/node-cron-migration.md +49 -0
  3. package/docs/development/task-startup.md +78 -0
  4. package/docs/releases/2.22.0.md +8 -0
  5. package/docs/releases/2.23.0.md +33 -0
  6. package/docs/script-history.md +53 -0
  7. package/package.json +9 -8
  8. package/scripts/benchmark-script-history.cjs +39 -0
  9. package/scripts/publish-static.sh +34 -0
  10. package/scripts/sync-static.sh +20 -0
  11. package/scripts/wait-for-mirror.sh +214 -0
  12. package/shell/api.sh +56 -70
  13. package/shell/node_path_cache.sh +10 -2
  14. package/shell/otask.sh +7 -9
  15. package/shell/preload/sitecustomize.js +6 -2
  16. package/shell/preload/sitecustomize.py +4 -2
  17. package/shell/share.sh +1 -1
  18. package/shell/task-timeout.sh +63 -0
  19. package/shell/task.sh +8 -6
  20. package/shell/update.sh +40 -95
  21. package/shell/upgrade.cjs +8 -0
  22. package/static/build/api/config.js +2 -6
  23. package/static/build/api/env.js +15 -14
  24. package/static/build/api/open.js +2 -1
  25. package/static/build/api/script.js +155 -9
  26. package/static/build/api/subscription.js +3 -6
  27. package/static/build/api/system.js +35 -3
  28. package/static/build/config/grpcCerts.js +89 -40
  29. package/static/build/config/subscription.js +19 -11
  30. package/static/build/config/util.js +6 -4
  31. package/static/build/loaders/express.js +2 -0
  32. package/static/build/loaders/initData.js +20 -30
  33. package/static/build/loaders/initTask.js +11 -1
  34. package/static/build/middlewares/uniqueConstraintError.js +15 -0
  35. package/static/build/protos/cron.js +63 -2
  36. package/static/build/schedule/addCron.js +45 -63
  37. package/static/build/schedule/client.js +19 -1
  38. package/static/build/schedule/delCron.js +1 -1
  39. package/static/build/schedule/setConcurrency.js +27 -0
  40. package/static/build/services/config.js +2 -3
  41. package/static/build/services/cron.js +36 -10
  42. package/static/build/services/env.js +1 -1
  43. package/static/build/services/grpc.js +2 -1
  44. package/static/build/services/log.js +1 -1
  45. package/static/build/services/open.js +106 -31
  46. package/static/build/services/schedule.js +20 -10
  47. package/static/build/services/script.js +1 -1
  48. package/static/build/services/scriptHistory.js +30 -0
  49. package/static/build/services/sshKey.js +39 -14
  50. package/static/build/services/subscription.js +104 -53
  51. package/static/build/services/system.js +67 -32
  52. package/static/build/services/user.js +15 -10
  53. package/static/build/shared/cronCommand.js +17 -0
  54. package/static/build/shared/cronSchedule.js +101 -0
  55. package/static/build/shared/cronScheduler.js +73 -0
  56. package/static/build/shared/i18n.js +3 -2
  57. package/static/build/shared/logPath.js +15 -0
  58. package/static/build/shared/loginThrottle.js +45 -0
  59. package/static/build/shared/pLimit.js +19 -6
  60. package/static/build/shared/runCron.js +13 -4
  61. package/static/build/shared/scriptHistory.js +445 -0
  62. package/static/build/shared/shellQuote.js +11 -0
  63. package/static/build/shared/subscriptionMutationLock.js +34 -0
  64. package/static/build/shared/subscriptionPath.js +41 -0
  65. package/static/build/shared/subscriptionStorage.js +98 -0
  66. package/static/build/shared/utils.js +2 -1
  67. package/static/build/token.js +3 -14
  68. package/static/build/validation/schedule.js +3 -18
  69. package/static/dist/292.264757f0.async.js +1 -0
  70. package/static/dist/3512.95e41255.async.js +1 -0
  71. package/static/dist/index.html +2 -2
  72. package/static/dist/preload_helper.01fcd534.js +1 -0
  73. package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
  74. package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
  75. package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
  76. package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
  77. package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
  78. package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
  79. package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
  80. package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
  81. package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
  82. package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
  83. package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
  84. package/static/dist/{umi.fc019050.js → umi.3a24a9fa.js} +1 -1
  85. package/test/back/advisory-input-security.test.cjs +416 -0
  86. package/test/back/code-scanning-security.test.cjs +189 -0
  87. package/test/back/cron-macros.test.cjs +51 -0
  88. package/test/back/cron-schedule-compatibility.test.cjs +69 -0
  89. package/test/back/cron-scheduler-routing.test.cjs +59 -0
  90. package/test/back/draft-path-security.test.cjs +235 -0
  91. package/test/back/env-import-security.test.cjs +91 -0
  92. package/test/back/execution-lifecycle.test.cjs +1 -1
  93. package/test/back/file-access-scope.test.cjs +180 -0
  94. package/test/back/grpc-mtls-security.test.cjs +136 -0
  95. package/test/back/http-security.test.cjs +105 -5
  96. package/test/back/legacy-cron-recovery.test.cjs +76 -0
  97. package/test/back/linux-mirror-result.test.cjs +176 -0
  98. package/test/back/listen-security.test.cjs +1 -0
  99. package/test/back/mirror-status.test.cjs +257 -0
  100. package/test/back/node-cron-migration.test.cjs +65 -0
  101. package/test/back/node-path-cache.test.cjs +29 -0
  102. package/test/back/node-path-lock.test.cjs +6 -2
  103. package/test/back/preload-lazy-notify.test.cjs +168 -0
  104. package/test/back/queued-cron-invalidation.test.cjs +98 -0
  105. package/test/back/restart-instance-recovery.test.cjs +116 -0
  106. package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
  107. package/test/back/scheduler-concurrency.test.cjs +411 -0
  108. package/test/back/scheduler-reconciliation.test.cjs +58 -4
  109. package/test/back/script-file-access.test.cjs +210 -0
  110. package/test/back/script-history-api.test.cjs +551 -0
  111. package/test/back/script-history.test.cjs +383 -0
  112. package/test/back/shell-api-parsing.test.cjs +4 -4
  113. package/test/back/shell-translation-output.test.cjs +16 -0
  114. package/test/back/shell-upgrade.test.cjs +312 -0
  115. package/test/back/shellApiToken.test.cjs +151 -0
  116. package/test/back/ssh-config-home.test.cjs +113 -0
  117. package/test/back/static-mirror-trigger.test.cjs +441 -0
  118. package/test/back/static-mirror.test.cjs +179 -0
  119. package/test/back/stop-race.test.cjs +5 -1
  120. package/test/back/subscription-mutation-lock.test.cjs +83 -0
  121. package/test/back/subscription-review-regression.test.cjs +607 -0
  122. package/test/back/subscription-storage-regression.test.cjs +553 -0
  123. package/test/back/system-dependence-cache.test.cjs +1 -0
  124. package/test/back/system-export-history.test.cjs +119 -0
  125. package/test/back/systemTokenRenewal.test.cjs +497 -0
  126. package/test/back/task-shell-lifecycle.test.cjs +194 -0
  127. package/test/back/unique-constraint-error.test.cjs +72 -0
  128. package/test/back/upgrade-guards.test.cjs +285 -0
  129. package/test/back/upgrade-lifecycle.test.cjs +288 -0
  130. package/test/back/upgrade-transaction.test.cjs +133 -0
  131. package/test/back/user-security.test.cjs +102 -1
  132. package/test/back/wpush-grpc.test.cjs +1 -0
  133. package/test/fixtures/legacy-cron.json +1371 -0
  134. package/test/front/diff-file-routing.test.cjs +126 -0
  135. package/test/front/script-history-save.test.cjs +121 -0
  136. package/test/helpers/upgrade-fixture.cjs +30 -0
  137. package/version.yaml +14 -19
  138. package/static/dist/2096.6d98fd12.async.js +0 -1
  139. package/static/dist/4859.97d6653c.async.js +0 -1
  140. package/static/dist/6707.801b886b.async.js +0 -1
  141. package/static/dist/preload_helper.cf7de3fa.js +0 -1
  142. package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +0 -1
  143. package/static/dist/src__pages__diff__index.1714b941.async.js +0 -1
  144. package/static/dist/src__pages__script__editModal.22acdf7c.async.js +0 -1
  145. package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +0 -1
  146. package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
  147. package/static/dist/src__pages__script__index.a18b2692.async.js +0 -1
  148. package/static/dist/src__pages__script__saveModal.8965b74c.async.js +0 -1
  149. package/static/dist/src__pages__subscription__modal.38991e1e.async.js +0 -1
  150. /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
@@ -1,33 +1,10 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
2
  var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19
3
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20
4
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21
5
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22
6
  return c > 3 && r && Object.defineProperty(target, key, r), r;
23
7
  };
24
- var __importStar = (this && this.__importStar) || function (mod) {
25
- if (mod && mod.__esModule) return mod;
26
- var result = {};
27
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
- __setModuleDefault(result, mod);
29
- return result;
30
- };
31
8
  var __metadata = (this && this.__metadata) || function (k, v) {
32
9
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
33
10
  };
@@ -45,7 +22,7 @@ const subscription_1 = require("../data/subscription");
45
22
  const util_1 = require("../config/util");
46
23
  const promises_1 = __importDefault(require("fs/promises"));
47
24
  const sequelize_1 = require("sequelize");
48
- const path_1 = __importStar(require("path"));
25
+ const path_1 = __importDefault(require("path"));
49
26
  const schedule_1 = __importDefault(require("./schedule"));
50
27
  const sock_1 = __importDefault(require("./sock"));
51
28
  const i18n_1 = require("../shared/i18n");
@@ -53,6 +30,10 @@ const sshKey_1 = __importDefault(require("./sshKey"));
53
30
  const const_1 = require("../config/const");
54
31
  const subscription_2 = require("../config/subscription");
55
32
  const cron_1 = require("../data/cron");
33
+ const subscriptionPath_1 = require("../shared/subscriptionPath");
34
+ const logPath_1 = require("../shared/logPath");
35
+ const subscriptionStorage_1 = require("../shared/subscriptionStorage");
36
+ const subscriptionMutationLock_1 = require("../shared/subscriptionMutationLock");
56
37
  const cron_2 = __importDefault(require("./cron"));
57
38
  const logStreamManager_1 = require("../shared/logStreamManager");
58
39
  const logReader_1 = require("../shared/logReader");
@@ -100,8 +81,11 @@ let SubscriptionService = class SubscriptionService {
100
81
  }
101
82
  }
102
83
  async handleTask(doc, needCreate = true, runImmediately = false) {
103
- const { url } = (0, subscription_2.formatUrl)(doc);
104
- doc.command = (0, subscription_2.formatCommand)(doc, url);
84
+ // Cancelling by ID must also work for invalid historical subscriptions.
85
+ if (needCreate) {
86
+ const { url } = (0, subscription_2.formatUrl)(doc);
87
+ doc.command = (0, subscription_2.formatCommand)(doc, url);
88
+ }
105
89
  if (doc.schedule_type === 'crontab') {
106
90
  this.scheduleService.cancelCronTask(doc);
107
91
  needCreate &&
@@ -201,28 +185,49 @@ let SubscriptionService = class SubscriptionService {
201
185
  };
202
186
  }
203
187
  async create(payload) {
204
- const tab = new subscription_1.Subscription(payload);
205
- const doc = await this.insert(tab);
206
- await this.handleTask(doc.get({ plain: true }));
207
- await this.setSshConfig();
208
- return doc;
188
+ (0, subscriptionPath_1.assertSubscriptionAlias)(payload.alias);
189
+ return (0, subscriptionMutationLock_1.withSubscriptionMutation)(async () => {
190
+ const tab = new subscription_1.Subscription(payload);
191
+ const doc = await this.insert(tab);
192
+ await this.handleTask(doc.get({ plain: true }));
193
+ await this.setSshConfig();
194
+ return doc;
195
+ });
209
196
  }
210
197
  async insert(payload) {
211
198
  return await subscription_1.SubscriptionModel.create(payload, { returning: true });
212
199
  }
213
200
  async update(payload) {
214
- const doc = await this.getDb({ id: payload.id });
215
- const tab = new subscription_1.Subscription(Object.assign(Object.assign({}, doc), payload));
216
- const newDoc = await this.updateDb(tab);
217
- await this.handleTask(newDoc, !newDoc.is_disabled);
218
- await this.setSshConfig();
219
- return newDoc;
201
+ (0, subscriptionPath_1.assertSubscriptionAlias)(payload.alias);
202
+ return (0, subscriptionMutationLock_1.withSubscriptionMutation)(async () => {
203
+ const doc = await this.getDb({ id: payload.id });
204
+ const tab = new subscription_1.Subscription(Object.assign(Object.assign({}, doc), payload));
205
+ const newDoc = await this.updateDb(tab);
206
+ await this.handleTask(newDoc, !newDoc.is_disabled);
207
+ await this.removeSshConfigForSubscription(doc);
208
+ await this.setSshConfig();
209
+ return newDoc;
210
+ });
211
+ }
212
+ async removeSshConfigForSubscription(doc) {
213
+ if (doc.type !== 'private-repo' || doc.pull_type !== 'ssh-key')
214
+ return;
215
+ // Invalid historical rows must remain deletable without deriving unsafe paths.
216
+ try {
217
+ (0, subscriptionPath_1.assertSubscriptionAlias)(doc.alias);
218
+ }
219
+ catch (_a) {
220
+ return;
221
+ }
222
+ await this.sshKeyService.removeSSHKey(doc.alias, '');
220
223
  }
221
224
  async updateDb(payload) {
222
225
  await subscription_1.SubscriptionModel.update(payload, { where: { id: payload.id } });
223
226
  return await this.getDb({ id: payload.id });
224
227
  }
225
228
  async status({ ids, status, pid, log_path, last_running_time = 0, last_execution_time = 0, }) {
229
+ if (log_path)
230
+ (0, logPath_1.resolveLogPath)(config_1.default.logPath, log_path);
226
231
  const options = {
227
232
  status,
228
233
  pid,
@@ -235,24 +240,70 @@ let SubscriptionService = class SubscriptionService {
235
240
  return await subscription_1.SubscriptionModel.update(Object.assign({}, options), { where: { id: ids } });
236
241
  }
237
242
  async remove(ids, query) {
238
- const docs = await subscription_1.SubscriptionModel.findAll({ where: { id: ids } });
239
- for (const doc of docs) {
240
- await this.handleTask(doc.get({ plain: true }), false);
241
- }
242
- await subscription_1.SubscriptionModel.destroy({ where: { id: ids } });
243
- await this.setSshConfig();
244
- if ((query === null || query === void 0 ? void 0 : query.force) === true) {
245
- const crons = await cron_1.CrontabModel.findAll({ where: { sub_id: ids } });
246
- if (crons === null || crons === void 0 ? void 0 : crons.length) {
247
- await this.crontabService.remove(crons.map((x) => x.id));
243
+ return (0, subscriptionMutationLock_1.withSubscriptionMutation)(async () => {
244
+ const docs = await subscription_1.SubscriptionModel.findAll({ where: { id: ids } });
245
+ const storagePaths = [];
246
+ const retainedPaths = [];
247
+ const collectPaths = (doc, targets, deleting = false) => {
248
+ for (const names of (0, subscriptionStorage_1.getSubscriptionStorageCandidates)(doc)) {
249
+ // A URL accepted by one engine can produce an invalid name in another.
250
+ // Validate each candidate independently, retaining the safe candidate.
251
+ if (!Object.values(names).every(subscriptionStorage_1.isSubscriptionStorageName))
252
+ continue;
253
+ const resolve = deleting ? subscriptionStorage_1.resolveSubscriptionStoragePath : path_1.default.resolve;
254
+ const scriptPath = resolve(config_1.default.scriptPath, names.script);
255
+ if (scriptPath)
256
+ targets.push(scriptPath);
257
+ if (names.repo) {
258
+ const repoPath = resolve(config_1.default.repoPath, names.repo);
259
+ if (repoPath)
260
+ targets.push(repoPath);
261
+ }
262
+ if (names.raw) {
263
+ const rawPath = resolve(path_1.default.join(config_1.default.dataPath, 'raw'), names.raw);
264
+ if (rawPath)
265
+ targets.push(rawPath);
266
+ }
267
+ }
268
+ };
269
+ // Validate all deletion targets before changing rows or removing any files.
270
+ if ((query === null || query === void 0 ? void 0 : query.force) === true) {
271
+ for (const doc of docs) {
272
+ (0, subscriptionPath_1.assertSubscriptionAlias)(doc.alias);
273
+ (0, subscriptionStorage_1.resolveSubscriptionStoragePath)(config_1.default.scriptPath, doc.alias);
274
+ (0, subscriptionStorage_1.resolveSubscriptionStoragePath)(config_1.default.repoPath, doc.alias);
275
+ collectPaths(doc, storagePaths, true);
276
+ }
277
+ const remaining = await subscription_1.SubscriptionModel.findAll({ where: {} });
278
+ for (const doc of remaining) {
279
+ if (!ids.includes(doc.id))
280
+ collectPaths(doc, retainedPaths);
281
+ }
248
282
  }
249
283
  for (const doc of docs) {
250
- const filePath = (0, path_1.join)(config_1.default.scriptPath, doc.alias);
251
- const repoPath = (0, path_1.join)(config_1.default.repoPath, doc.alias);
252
- await (0, util_1.rmPath)(filePath);
253
- await (0, util_1.rmPath)(repoPath);
284
+ await this.handleTask(doc.get({ plain: true }), false);
254
285
  }
255
- }
286
+ await subscription_1.SubscriptionModel.destroy({ where: { id: ids } });
287
+ for (const doc of docs) {
288
+ await this.removeSshConfigForSubscription(doc);
289
+ }
290
+ await this.setSshConfig();
291
+ if ((query === null || query === void 0 ? void 0 : query.force) === true) {
292
+ const crons = await cron_1.CrontabModel.findAll({ where: { sub_id: ids } });
293
+ if (crons === null || crons === void 0 ? void 0 : crons.length) {
294
+ await this.crontabService.remove(crons.map((x) => x.id));
295
+ }
296
+ for (const storagePath of new Set(storagePaths)) {
297
+ // rm is recursive: an ancestor must also be retained if a surviving
298
+ // subscription uses a nested branch, and vice versa.
299
+ const shared = retainedPaths.some((retained) => retained === storagePath ||
300
+ retained.startsWith(`${storagePath}${path_1.default.sep}`) ||
301
+ storagePath.startsWith(`${retained}${path_1.default.sep}`));
302
+ if (!shared)
303
+ await (0, util_1.rmPath)(storagePath);
304
+ }
305
+ }
306
+ });
256
307
  }
257
308
  async getDb(query) {
258
309
  const doc = await subscription_1.SubscriptionModel.findOne({ where: Object.assign({}, query) });
@@ -337,7 +388,7 @@ let SubscriptionService = class SubscriptionService {
337
388
  }
338
389
  if (doc.log_path) {
339
390
  const relativeDir = path_1.default.dirname(`${doc.log_path}`);
340
- const dir = path_1.default.resolve(config_1.default.logPath, relativeDir);
391
+ const dir = path_1.default.dirname((0, logPath_1.resolveLogPath)(config_1.default.logPath, doc.log_path));
341
392
  const _exist = await (0, util_1.fileExist)(dir);
342
393
  if (_exist) {
343
394
  let files = await promises_1.default.readdir(dir);
@@ -27,6 +27,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  const cross_spawn_1 = require("cross-spawn");
30
+ const child_process_1 = require("child_process");
31
+ const util_1 = require("util");
32
+ const shellQuote_1 = require("../shared/shellQuote");
30
33
  const fs_1 = __importDefault(require("fs"));
31
34
  const undici_1 = require("undici");
32
35
  const path_1 = __importDefault(require("path"));
@@ -34,17 +37,19 @@ const typedi_1 = require("typedi");
34
37
  const winston_1 = __importDefault(require("winston"));
35
38
  const config_1 = __importDefault(require("../config"));
36
39
  const const_1 = require("../config/const");
37
- const util_1 = require("../config/util");
40
+ const util_2 = require("../config/util");
38
41
  const dependence_1 = require("../data/dependence");
39
42
  const system_1 = require("../data/system");
40
43
  const pLimit_1 = __importDefault(require("../shared/pLimit"));
44
+ const client_1 = __importDefault(require("../schedule/client"));
45
+ const schedulerMutationLock_1 = require("../shared/schedulerMutationLock");
41
46
  const notify_1 = __importDefault(require("./notify"));
42
47
  const schedule_1 = __importDefault(require("./schedule"));
43
48
  const sock_1 = __importDefault(require("./sock"));
44
49
  const os_1 = __importDefault(require("os"));
45
50
  const dayjs_1 = __importDefault(require("dayjs"));
46
51
  const i18n_1 = require("../shared/i18n");
47
- const util_2 = require("../config/util");
52
+ const util_3 = require("../config/util");
48
53
  let SystemService = class SystemService {
49
54
  constructor(logger, scheduleService, sockService) {
50
55
  this.logger = logger;
@@ -104,18 +109,33 @@ let SystemService = class SystemService {
104
109
  return { code: 200, data: info };
105
110
  }
106
111
  async updateCronConcurrency(info) {
107
- const oDoc = await this.getSystemConfig();
108
- await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign({}, oDoc.info), info) }));
109
- if (info.cronConcurrency) {
110
- await pLimit_1.default.setCustomLimit(info.cronConcurrency);
111
- }
112
- return { code: 200, data: info };
112
+ var _a;
113
+ const limit = (_a = info.cronConcurrency) !== null && _a !== void 0 ? _a : 0;
114
+ if (!Number.isInteger(limit) || limit < 0 || limit > 2147483647) {
115
+ throw Object.assign(new Error((0, i18n_1.t)('参数错误')), { status: 400 });
116
+ }
117
+ return (0, schedulerMutationLock_1.withSchedulerMutation)(async () => {
118
+ const oDoc = await this.getSystemConfig();
119
+ await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign(Object.assign({}, oDoc.info), info), { cronConcurrency: limit }) }));
120
+ await pLimit_1.default.setCustomLimit(limit || Math.max(os_1.default.cpus().length, 4));
121
+ await client_1.default.setConcurrency(limit);
122
+ return { code: 200, data: info };
123
+ });
124
+ }
125
+ async restoreCronConcurrency() {
126
+ await (0, schedulerMutationLock_1.withSchedulerMutation)(async () => {
127
+ var _a, _b;
128
+ const doc = await this.getSystemConfig();
129
+ const limit = (_b = (_a = doc.info) === null || _a === void 0 ? void 0 : _a.cronConcurrency) !== null && _b !== void 0 ? _b : 0;
130
+ await pLimit_1.default.setCustomLimit(limit || Math.max(os_1.default.cpus().length, 4));
131
+ await client_1.default.setConcurrency(limit);
132
+ });
113
133
  }
114
134
  async updateDependenceProxy(info) {
115
135
  const oDoc = await this.getSystemConfig();
116
136
  await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign({}, oDoc.info), info) }));
117
137
  if (info.dependenceProxy) {
118
- await fs_1.default.promises.writeFile(config_1.default.dependenceProxyFile, `export http_proxy="${info.dependenceProxy}"\nexport https_proxy="${info.dependenceProxy}"`);
138
+ await fs_1.default.promises.writeFile(config_1.default.dependenceProxyFile, `export http_proxy=${(0, shellQuote_1.shellQuote)(info.dependenceProxy)}\nexport https_proxy=${(0, shellQuote_1.shellQuote)(info.dependenceProxy)}`);
119
139
  }
120
140
  else {
121
141
  await fs_1.default.promises.rm(config_1.default.dependenceProxyFile);
@@ -127,7 +147,7 @@ let SystemService = class SystemService {
127
147
  await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign({}, oDoc.info), info) }));
128
148
  let cmd = 'pnpm config delete registry';
129
149
  if (info.nodeMirror) {
130
- cmd = `pnpm config set registry ${info.nodeMirror}`;
150
+ cmd = `pnpm config set registry ${(0, shellQuote_1.shellQuote)(info.nodeMirror)}`;
131
151
  }
132
152
  let command = `cd && ${cmd}`;
133
153
  const docs = await dependence_1.DependenceModel.findAll({
@@ -168,9 +188,9 @@ let SystemService = class SystemService {
168
188
  await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign({}, oDoc.info), info) }));
169
189
  let cmd = 'pip config unset global.index-url';
170
190
  if (info.pythonMirror) {
171
- cmd = `pip3 config set global.index-url ${info.pythonMirror}`;
191
+ cmd = `pip3 config set global.index-url ${(0, shellQuote_1.shellQuote)(info.pythonMirror)}`;
172
192
  }
173
- await (0, util_1.promiseExec)(cmd);
193
+ await (0, util_2.promiseExec)(cmd);
174
194
  return { code: 200, data: info };
175
195
  }
176
196
  async updateLinuxMirror(info, res, onEnd) {
@@ -178,26 +198,25 @@ let SystemService = class SystemService {
178
198
  if (os_1.default.platform() !== 'linux') {
179
199
  return;
180
200
  }
181
- const command = await (0, util_2.updateLinuxMirrorFile)(info.linuxMirror || '');
182
- let hasError = false;
201
+ const command = await (0, util_3.updateLinuxMirrorFile)(info.linuxMirror || '');
183
202
  this.scheduleService.runTask(command, {
184
203
  onStart: async (cp) => {
185
204
  res === null || res === void 0 ? void 0 : res.setHeader('QL-Task-Pid', `${cp.pid}`);
186
205
  res === null || res === void 0 ? void 0 : res.end();
187
206
  },
188
- onEnd: async () => {
207
+ onEnd: async (cp) => {
189
208
  this.sockService.sendMessage({
190
209
  type: 'updateLinuxMirror',
191
210
  message: 'update linux mirror end',
192
211
  status: 'completed',
193
212
  });
194
213
  onEnd === null || onEnd === void 0 ? void 0 : onEnd();
195
- if (!hasError) {
214
+ // Tools such as sudo and apt may write warnings to stderr on success.
215
+ if ((cp === null || cp === void 0 ? void 0 : cp.exitCode) === 0 && cp.signalCode === null) {
196
216
  await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign({}, oDoc.info), info) }));
197
217
  }
198
218
  },
199
219
  onError: async (message) => {
200
- hasError = true;
201
220
  this.sockService.sendMessage({ type: 'updateLinuxMirror', message });
202
221
  },
203
222
  onLog: async (message) => {
@@ -211,7 +230,7 @@ let SystemService = class SystemService {
211
230
  }
212
231
  async checkUpdate() {
213
232
  try {
214
- const currentVersionContent = await (0, util_1.parseVersion)(config_1.default.versionFile);
233
+ const currentVersionContent = await (0, util_2.parseVersion)(config_1.default.versionFile);
215
234
  let lastVersionContent;
216
235
  try {
217
236
  const { body } = await (0, undici_1.request)(`${config_1.default.lastVersionFile}?t=${Date.now()}`, {
@@ -221,7 +240,7 @@ let SystemService = class SystemService {
221
240
  }),
222
241
  });
223
242
  const text = await body.text();
224
- lastVersionContent = (0, util_1.parseContentVersion)(text);
243
+ lastVersionContent = (0, util_2.parseContentVersion)(text);
225
244
  }
226
245
  catch (error) { }
227
246
  if (!lastVersionContent) {
@@ -291,7 +310,10 @@ let SystemService = class SystemService {
291
310
  return { code: 200 };
292
311
  }
293
312
  async reloadSystem(target) {
294
- const cmd = `real_time=true ql reload ${target || ''}`;
313
+ if (target && !['system', 'data'].includes(target)) {
314
+ return { code: 400, message: (0, i18n_1.t)('参数错误') };
315
+ }
316
+ const cmd = `real_time=true ql reload ${(0, shellQuote_1.shellQuote)(target || '')}`;
295
317
  const cp = (0, cross_spawn_1.spawn)(cmd, {
296
318
  shell: '/bin/bash',
297
319
  detached: true,
@@ -334,15 +356,15 @@ let SystemService = class SystemService {
334
356
  return { code: 400, message: (0, i18n_1.t)('参数错误') };
335
357
  }
336
358
  if (pid) {
337
- await (0, util_1.killTask)(pid);
359
+ await (0, util_2.killTask)(pid);
338
360
  return { code: 200 };
339
361
  }
340
362
  if (!command.startsWith(const_1.TASK_COMMAND)) {
341
363
  command = `${const_1.TASK_COMMAND} ${command}`;
342
364
  }
343
- const _pid = await (0, util_1.getPid)(command);
365
+ const _pid = await (0, util_2.getPid)(command);
344
366
  if (_pid) {
345
- await (0, util_1.killTask)(_pid);
367
+ await (0, util_2.killTask)(_pid);
346
368
  return { code: 200 };
347
369
  }
348
370
  else {
@@ -355,8 +377,21 @@ let SystemService = class SystemService {
355
377
  if (type && type.length) {
356
378
  dataDirs = dataDirs.concat(type.filter((x) => x !== 'base'));
357
379
  }
358
- const dataPaths = dataDirs.map((dir) => `data/${dir}`);
359
- await (0, util_1.promiseExec)(`cd ${config_1.default.dataPath} && cd ../ && tar -zcvf ${config_1.default.dataTgzFile} ${dataPaths.join(' ')}`);
380
+ const allowed = new Set(['db', 'upload', 'config', 'scripts', 'log', 'deps',
381
+ 'syslog', 'dep_cache', 'raw', 'repo', 'ssh.d', 'script-history']);
382
+ if (dataDirs.some((dir) => !allowed.has(dir))) {
383
+ return res.status(400).send({ code: 400, message: (0, i18n_1.t)('参数错误') });
384
+ }
385
+ if (dataDirs.includes('scripts'))
386
+ dataDirs.push('script-history');
387
+ // History is created lazily; old/fresh installations must still export.
388
+ if (!fs_1.default.existsSync(path_1.default.join(config_1.default.dataPath, 'script-history'))) {
389
+ dataDirs = dataDirs.filter((dir) => dir !== 'script-history');
390
+ }
391
+ const dataPaths = [...new Set(dataDirs)].map((dir) => `data/${dir}`);
392
+ await (0, util_1.promisify)(child_process_1.execFile)('tar', ['-zcf', config_1.default.dataTgzFile, '--', ...dataPaths], {
393
+ cwd: path_1.default.dirname(config_1.default.dataPath),
394
+ });
360
395
  res.download(config_1.default.dataTgzFile);
361
396
  }
362
397
  catch (error) {
@@ -365,8 +400,8 @@ let SystemService = class SystemService {
365
400
  }
366
401
  async importData() {
367
402
  try {
368
- await (0, util_1.promiseExec)(`rm -rf ${path_1.default.join(config_1.default.tmpPath, 'data')}`);
369
- const res = await (0, util_1.promiseExec)(`cd ${config_1.default.tmpPath} && tar -zxvf ${config_1.default.dataTgzFile}`);
403
+ await (0, util_2.promiseExec)(`rm -rf ${path_1.default.join(config_1.default.tmpPath, 'data')}`);
404
+ const res = await (0, util_2.promiseExec)(`cd ${config_1.default.tmpPath} && tar -zxvf ${config_1.default.dataTgzFile}`);
370
405
  return { code: 200, data: res };
371
406
  }
372
407
  catch (error) {
@@ -380,7 +415,7 @@ let SystemService = class SystemService {
380
415
  const endTime = (0, dayjs_1.default)(query.endTime || undefined)
381
416
  .endOf('d')
382
417
  .valueOf();
383
- const result = await (0, util_1.readDirs)(config_1.default.systemLogPath, config_1.default.systemLogPath);
418
+ const result = await (0, util_2.readDirs)(config_1.default.systemLogPath, config_1.default.systemLogPath);
384
419
  const logs = result
385
420
  .reverse()
386
421
  .filter((x) => x.title.endsWith('.log'))
@@ -423,10 +458,10 @@ let SystemService = class SystemService {
423
458
  })(res, selected);
424
459
  }
425
460
  async deleteSystemLog() {
426
- const result = await (0, util_1.readDirs)(config_1.default.systemLogPath, config_1.default.systemLogPath);
461
+ const result = await (0, util_2.readDirs)(config_1.default.systemLogPath, config_1.default.systemLogPath);
427
462
  const logs = result.reverse().filter((x) => x.title.endsWith('.log'));
428
463
  for (const log of logs) {
429
- await (0, util_1.rmPath)(path_1.default.join(config_1.default.systemLogPath, log.title));
464
+ await (0, util_2.rmPath)(path_1.default.join(config_1.default.systemLogPath, log.title));
430
465
  }
431
466
  }
432
467
  async updateTimezone(info) {
@@ -435,7 +470,7 @@ let SystemService = class SystemService {
435
470
  }
436
471
  const oDoc = await this.getSystemConfig();
437
472
  await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign({}, oDoc.info), info) }));
438
- const success = await (0, util_1.setSystemTimezone)(info.timezone);
473
+ const success = await (0, util_2.setSystemTimezone)(info.timezone);
439
474
  if (success) {
440
475
  return { code: 200, data: info };
441
476
  }
@@ -449,7 +484,7 @@ let SystemService = class SystemService {
449
484
  await this.updateAuthDb(Object.assign(Object.assign({}, oDoc), { info: Object.assign(Object.assign({}, oDoc.info), { lang }) }));
450
485
  // Write to standalone lang_env.sh, sourced by shell scripts
451
486
  try {
452
- await fs_1.default.promises.writeFile(config_1.default.langEnvFile, `export QL_LANG='${lang}'\n`);
487
+ await fs_1.default.promises.writeFile(config_1.default.langEnvFile, `export QL_LANG=${(0, shellQuote_1.shellQuote)(lang)}\n`);
453
488
  }
454
489
  catch (error) {
455
490
  this.logger.error(`Failed to write lang_env.sh: ${error}`);
@@ -36,11 +36,13 @@ const clientIp_1 = require("../shared/clientIp");
36
36
  const auth_1 = require("../shared/auth");
37
37
  const password_1 = require("../shared/password");
38
38
  const authMutation_1 = require("../shared/authMutation");
39
+ const loginThrottle_1 = require("../shared/loginThrottle");
39
40
  let UserService = class UserService {
40
41
  constructor(logger, scheduleService, sockService) {
41
42
  this.logger = logger;
42
43
  this.scheduleService = scheduleService;
43
44
  this.sockService = sockService;
45
+ this.loginThrottle = new loginThrottle_1.LoginThrottle();
44
46
  }
45
47
  async login(payloads, req) {
46
48
  return this.authenticate(payloads, req);
@@ -76,9 +78,8 @@ let UserService = class UserService {
76
78
  this.getLoginLog();
77
79
  return { code: 403, message: (0, i18n_1.t)('该 IP 已被列入黑名单') };
78
80
  }
79
- const retriesTime = Math.pow(3, retries) * 1000;
80
- if (retries > 2 && timestamp - lastlogon < retriesTime) {
81
- const waitTime = Math.ceil((retriesTime - (timestamp - lastlogon)) / 1000);
81
+ const waitTime = this.loginThrottle.retryAfter(ip, timestamp);
82
+ if (waitTime > 0) {
82
83
  return {
83
84
  code: 410,
84
85
  message: (0, i18n_1.tf)('失败次数过多,请%s秒后重试', waitTime),
@@ -97,6 +98,7 @@ let UserService = class UserService {
97
98
  };
98
99
  }
99
100
  if (passwordMatches) {
101
+ this.loginThrottle.reset(ip);
100
102
  const data = (0, util_1.createRandomString)(50, 100);
101
103
  const expiration = twoFactorActivated ? '60d' : '20d';
102
104
  let token = jsonwebtoken_1.default.sign({ data }, config_1.default.jwt.secret, {
@@ -161,8 +163,9 @@ let UserService = class UserService {
161
163
  };
162
164
  }
163
165
  else {
166
+ const failures = this.loginThrottle.fail(ip, timestamp);
164
167
  await this.updateAuthInfo(content, {
165
- retries: retries + 1,
168
+ retries: failures,
166
169
  lastlogon: timestamp,
167
170
  lastip: ip,
168
171
  lastaddr: address,
@@ -191,8 +194,8 @@ let UserService = class UserService {
191
194
  },
192
195
  });
193
196
  this.getLoginLog();
194
- if (retries > 2) {
195
- const waitTime = Math.round(Math.pow(3, retries + 1));
197
+ if (failures > 3) {
198
+ const waitTime = this.loginThrottle.retryAfter(ip, timestamp);
196
199
  return {
197
200
  code: 410,
198
201
  message: (0, i18n_1.tf)('失败次数过多,请%s秒后重试', waitTime),
@@ -334,8 +337,8 @@ let UserService = class UserService {
334
337
  const authInfo = await this.getAuthInfo();
335
338
  const { isTwoFactorChecking, twoFactorSecret } = authInfo;
336
339
  const now = Date.now();
337
- const retries = authInfo.retries || 0;
338
- if (retries > 2 && now - authInfo.lastlogon < Math.pow(3, retries) * 1000) {
340
+ const source = (0, clientIp_1.getClientIp)(req);
341
+ if (this.loginThrottle.retryAfter(source, now) > 0) {
339
342
  return { code: 410, message: (0, i18n_1.t)('失败次数过多,请稍后重试') };
340
343
  }
341
344
  if (!isTwoFactorChecking ||
@@ -365,9 +368,8 @@ let UserService = class UserService {
365
368
  .join(' ');
366
369
  }
367
370
  await this.updateAuthInfo(authInfo, {
368
- retries: retries + 1,
371
+ retries: this.loginThrottle.fail(source, now),
369
372
  lastlogon: now,
370
- isTwoFactorChecking: retries + 1 < 5,
371
373
  lastip: ip,
372
374
  lastaddr: address,
373
375
  platform: req.platform,
@@ -530,6 +532,9 @@ let UserService = class UserService {
530
532
  });
531
533
  }
532
534
  await this.updateAuthInfo(authInfo, payload);
535
+ if (retries === 0 || password !== undefined || username !== undefined) {
536
+ this.loginThrottle.clear();
537
+ }
533
538
  }
534
539
  };
535
540
  __decorate([
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.findCronId = void 0;
4
+ // Match command text literally: looking up a task must never execute its input.
5
+ function findCronId(crontab, command) {
6
+ if (!command)
7
+ return '';
8
+ for (const line of crontab.split('\n')) {
9
+ const id = /\bID=(\d+)\s/.exec(line);
10
+ if (id && line.includes(command, id.index + id[0].length)) {
11
+ return id[1];
12
+ }
13
+ }
14
+ return '';
15
+ }
16
+ exports.findCronId = findCronId;
17
+ //# sourceMappingURL=cronCommand.js.map