@nocobase/server 1.6.0-alpha.17 → 1.6.0-alpha.18

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.
@@ -0,0 +1,20 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ /// <reference types="node" />
10
+ import Application from './application';
11
+ export declare class AesEncryptor {
12
+ private key;
13
+ constructor(key: Buffer);
14
+ encrypt(text: string): Promise<string>;
15
+ decrypt(encryptedText: string): Promise<string>;
16
+ static getOrGenerateKey(keyFilePath: string): Promise<Buffer>;
17
+ static getKeyPath(appName: string): Promise<string>;
18
+ static create(app: Application): Promise<AesEncryptor>;
19
+ }
20
+ export default AesEncryptor;
@@ -0,0 +1,126 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
28
+ };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var aes_encryptor_exports = {};
39
+ __export(aes_encryptor_exports, {
40
+ AesEncryptor: () => AesEncryptor,
41
+ default: () => aes_encryptor_default
42
+ });
43
+ module.exports = __toCommonJS(aes_encryptor_exports);
44
+ var import_crypto = __toESM(require("crypto"));
45
+ var import_fs_extra = __toESM(require("fs-extra"));
46
+ var import_path = __toESM(require("path"));
47
+ const _AesEncryptor = class _AesEncryptor {
48
+ key;
49
+ constructor(key) {
50
+ if (key.length !== 32) {
51
+ throw new Error("Key must be 32 bytes for AES-256 encryption.");
52
+ }
53
+ this.key = key;
54
+ }
55
+ async encrypt(text) {
56
+ return new Promise((resolve, reject) => {
57
+ try {
58
+ const iv = import_crypto.default.randomBytes(16);
59
+ const cipher = import_crypto.default.createCipheriv("aes-256-cbc", this.key, iv);
60
+ const encrypted = Buffer.concat([cipher.update(Buffer.from(text, "utf8")), cipher.final()]);
61
+ resolve(iv.toString("hex") + encrypted.toString("hex"));
62
+ } catch (error) {
63
+ reject(error);
64
+ }
65
+ });
66
+ }
67
+ async decrypt(encryptedText) {
68
+ return new Promise((resolve, reject) => {
69
+ try {
70
+ const iv = Buffer.from(encryptedText.slice(0, 32), "hex");
71
+ const encrypted = Buffer.from(encryptedText.slice(32), "hex");
72
+ const decipher = import_crypto.default.createDecipheriv("aes-256-cbc", this.key, iv);
73
+ const decrypted = Buffer.concat([decipher.update(encrypted), decipher.final()]);
74
+ resolve(decrypted.toString("utf8"));
75
+ } catch (error) {
76
+ reject(error);
77
+ }
78
+ });
79
+ }
80
+ static async getOrGenerateKey(keyFilePath) {
81
+ try {
82
+ const key = await import_fs_extra.default.readFile(keyFilePath);
83
+ if (key.length !== 32) {
84
+ throw new Error("Invalid key length in file.");
85
+ }
86
+ return key;
87
+ } catch (error) {
88
+ if (error.code === "ENOENT") {
89
+ const key = import_crypto.default.randomBytes(32);
90
+ await import_fs_extra.default.mkdir(import_path.default.dirname(keyFilePath), { recursive: true });
91
+ await import_fs_extra.default.writeFile(keyFilePath, key);
92
+ return key;
93
+ } else {
94
+ throw new Error(`Failed to load key: ${error.message}`);
95
+ }
96
+ }
97
+ }
98
+ static async getKeyPath(appName) {
99
+ const appKeyPath = import_path.default.resolve(process.cwd(), "storage", "apps", appName, "aes_key.dat");
100
+ const appKeyExists = await import_fs_extra.default.exists(appKeyPath);
101
+ if (appKeyExists) {
102
+ return appKeyPath;
103
+ }
104
+ const envKeyPath = import_path.default.resolve(process.cwd(), "storage", "environment-variables", appName, "aes_key.dat");
105
+ const envKeyExists = await import_fs_extra.default.exists(envKeyPath);
106
+ if (envKeyExists) {
107
+ return envKeyPath;
108
+ }
109
+ return appKeyPath;
110
+ }
111
+ static async create(app) {
112
+ let key = process.env.APP_AES_SECRET_KEY;
113
+ if (!key) {
114
+ const keyPath = await this.getKeyPath(app.name);
115
+ key = await _AesEncryptor.getOrGenerateKey(keyPath);
116
+ }
117
+ return new _AesEncryptor(key);
118
+ }
119
+ };
120
+ __name(_AesEncryptor, "AesEncryptor");
121
+ let AesEncryptor = _AesEncryptor;
122
+ var aes_encryptor_default = AesEncryptor;
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ AesEncryptor
126
+ });
@@ -31,9 +31,10 @@ import { Plugin } from './plugin';
31
31
  import { InstallOptions, PluginManager } from './plugin-manager';
32
32
  import { PubSubManager, PubSubManagerOptions } from './pub-sub-manager';
33
33
  import { SyncMessageManager } from './sync-message-manager';
34
- import { ServiceContainer } from './service-container';
34
+ import AesEncryptor from './aes-encryptor';
35
35
  import { AuditManager } from './audit-manager';
36
36
  import { Environment } from './environment';
37
+ import { ServiceContainer } from './service-container';
37
38
  export type PluginType = string | typeof Plugin;
38
39
  export type PluginConfiguration = PluginType | [PluginType, any];
39
40
  export interface ResourceManagerOptions {
@@ -243,6 +244,8 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
243
244
  get name(): string;
244
245
  protected _dataSourceManager: DataSourceManager;
245
246
  get dataSourceManager(): DataSourceManager;
247
+ protected _aesEncryptor: AesEncryptor;
248
+ get aesEncryptor(): AesEncryptor;
246
249
  /**
247
250
  * @internal
248
251
  */
@@ -76,10 +76,11 @@ var import_plugin_manager = require("./plugin-manager");
76
76
  var import_pub_sub_manager = require("./pub-sub-manager");
77
77
  var import_sync_message_manager = require("./sync-message-manager");
78
78
  var import_package = __toESM(require("../package.json"));
79
- var import_service_container = require("./service-container");
80
79
  var import_available_action = require("./acl/available-action");
80
+ var import_aes_encryptor = __toESM(require("./aes-encryptor"));
81
81
  var import_audit_manager = require("./audit-manager");
82
82
  var import_environment = require("./environment");
83
+ var import_service_container = require("./service-container");
83
84
  const _Application = class _Application extends import_koa.default {
84
85
  constructor(options) {
85
86
  super();
@@ -258,6 +259,10 @@ const _Application = class _Application extends import_koa.default {
258
259
  get dataSourceManager() {
259
260
  return this._dataSourceManager;
260
261
  }
262
+ _aesEncryptor;
263
+ get aesEncryptor() {
264
+ return this._aesEncryptor;
265
+ }
261
266
  /**
262
267
  * @internal
263
268
  */
@@ -404,6 +409,7 @@ const _Application = class _Application extends import_koa.default {
404
409
  await oldDb.close();
405
410
  }
406
411
  }
412
+ this._aesEncryptor = await import_aes_encryptor.default.create(this);
407
413
  if (this.cacheManager) {
408
414
  await this.cacheManager.close();
409
415
  }
@@ -66,6 +66,14 @@ var import_ws_server = require("./ws-server");
66
66
  var import_node_worker_threads = require("node:worker_threads");
67
67
  var import_node_process = __toESM(require("node:process"));
68
68
  const compress = (0, import_node_util.promisify)((0, import_compression.default)());
69
+ function getSocketPath() {
70
+ const { SOCKET_PATH } = import_node_process.default.env;
71
+ if ((0, import_path.isAbsolute)(SOCKET_PATH)) {
72
+ return SOCKET_PATH;
73
+ }
74
+ return (0, import_path.resolve)(import_node_process.default.cwd(), SOCKET_PATH);
75
+ }
76
+ __name(getSocketPath, "getSocketPath");
69
77
  const _Gateway = class _Gateway extends import_events.EventEmitter {
70
78
  /**
71
79
  * use main app as default app to handle request
@@ -81,9 +89,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
81
89
  constructor() {
82
90
  super();
83
91
  this.reset();
84
- if (import_node_process.default.env.SOCKET_PATH) {
85
- this.socketPath = (0, import_path.resolve)(import_node_process.default.cwd(), import_node_process.default.env.SOCKET_PATH);
86
- }
92
+ this.socketPath = getSocketPath();
87
93
  }
88
94
  static getInstance(options = {}) {
89
95
  if (!_Gateway.instance) {
@@ -92,7 +98,7 @@ const _Gateway = class _Gateway extends import_events.EventEmitter {
92
98
  return _Gateway.instance;
93
99
  }
94
100
  static async getIPCSocketClient() {
95
- const socketPath = (0, import_path.resolve)(import_node_process.default.cwd(), import_node_process.default.env.SOCKET_PATH || "storage/gateway.sock");
101
+ const socketPath = getSocketPath();
96
102
  try {
97
103
  return await import_ipc_socket_client.IPCSocketClient.getConnection(socketPath);
98
104
  } catch (error) {
package/lib/helper.js CHANGED
@@ -56,6 +56,7 @@ var import_i18next = __toESM(require("i18next"));
56
56
  var import_koa_bodyparser = __toESM(require("koa-bodyparser"));
57
57
  var import_perf_hooks = require("perf_hooks");
58
58
  var import_data_wrapping = require("./middlewares/data-wrapping");
59
+ var import_extract_client_ip = require("./middlewares/extract-client-ip");
59
60
  var import_i18n = require("./middlewares/i18n");
60
61
  function createI18n(options) {
61
62
  const instance = import_i18next.default.createInstance();
@@ -121,6 +122,7 @@ function registerMiddlewares(app, options) {
121
122
  app.use((0, import_data_wrapping.dataWrapping)(), { tag: "dataWrapping", after: "cors" });
122
123
  }
123
124
  app.use(app.dataSourceManager.middleware(), { tag: "dataSource", after: "dataWrapping" });
125
+ app.use((0, import_extract_client_ip.extractClientIp)(), { tag: "extractClientIp", before: "cors" });
124
126
  }
125
127
  __name(registerMiddlewares, "registerMiddlewares");
126
128
  const createAppProxy = /* @__PURE__ */ __name((app) => {
package/lib/index.d.ts CHANGED
@@ -6,15 +6,16 @@
6
6
  * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
+ export * from './aes-encryptor';
9
10
  export * from './app-supervisor';
10
11
  export * from './application';
11
12
  export { Application as default } from './application';
13
+ export * from './audit-manager';
12
14
  export * from './gateway';
13
15
  export * as middlewares from './middlewares';
14
16
  export * from './migration';
15
17
  export * from './plugin';
16
18
  export * from './plugin-manager';
17
- export * from './audit-manager';
18
19
  export * from './pub-sub-manager';
19
20
  export declare const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
20
21
  export { appendToBuiltInPlugins, findAllPlugins, findBuiltInPlugins, findLocalPlugins, packageNameTrim, } from './plugin-manager/findPackageNames';
package/lib/index.js CHANGED
@@ -48,15 +48,16 @@ __export(src_exports, {
48
48
  runPluginStaticImports: () => import_run_plugin_static_imports.runPluginStaticImports
49
49
  });
50
50
  module.exports = __toCommonJS(src_exports);
51
+ __reExport(src_exports, require("./aes-encryptor"), module.exports);
51
52
  __reExport(src_exports, require("./app-supervisor"), module.exports);
52
53
  __reExport(src_exports, require("./application"), module.exports);
53
54
  var import_application = require("./application");
55
+ __reExport(src_exports, require("./audit-manager"), module.exports);
54
56
  __reExport(src_exports, require("./gateway"), module.exports);
55
57
  var middlewares = __toESM(require("./middlewares"));
56
58
  __reExport(src_exports, require("./migration"), module.exports);
57
59
  __reExport(src_exports, require("./plugin"), module.exports);
58
60
  __reExport(src_exports, require("./plugin-manager"), module.exports);
59
- __reExport(src_exports, require("./audit-manager"), module.exports);
60
61
  __reExport(src_exports, require("./pub-sub-manager"), module.exports);
61
62
  var import_findPackageNames = require("./plugin-manager/findPackageNames");
62
63
  var import_run_plugin_static_imports = require("./run-plugin-static-imports");
@@ -71,12 +72,13 @@ const OFFICIAL_PLUGIN_PREFIX = "@nocobase/plugin-";
71
72
  middlewares,
72
73
  packageNameTrim,
73
74
  runPluginStaticImports,
75
+ ...require("./aes-encryptor"),
74
76
  ...require("./app-supervisor"),
75
77
  ...require("./application"),
78
+ ...require("./audit-manager"),
76
79
  ...require("./gateway"),
77
80
  ...require("./migration"),
78
81
  ...require("./plugin"),
79
82
  ...require("./plugin-manager"),
80
- ...require("./audit-manager"),
81
83
  ...require("./pub-sub-manager")
82
84
  });
@@ -0,0 +1,10 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { Context, Next } from '@nocobase/actions';
10
+ export declare function extractClientIp(): (ctx: Context, next: Next) => Promise<void>;
@@ -0,0 +1,46 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var extract_client_ip_exports = {};
29
+ __export(extract_client_ip_exports, {
30
+ extractClientIp: () => extractClientIp
31
+ });
32
+ module.exports = __toCommonJS(extract_client_ip_exports);
33
+ function extractClientIp() {
34
+ return /* @__PURE__ */ __name(async function extractClientIp2(ctx, next) {
35
+ const forwardedFor = ctx.get("X-Forwarded-For");
36
+ const ipArray = forwardedFor ? forwardedFor.split(",") : [];
37
+ const clientIp = ipArray.length > 0 ? ipArray[0].trim() : ctx.request.ip;
38
+ ctx.state.clientIp = clientIp;
39
+ await next();
40
+ }, "extractClientIp");
41
+ }
42
+ __name(extractClientIp, "extractClientIp");
43
+ // Annotate the CommonJS export names for ESM import in node:
44
+ 0 && (module.exports = {
45
+ extractClientIp
46
+ });
@@ -7,4 +7,5 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
  export * from './data-wrapping';
10
+ export * from './extract-client-ip';
10
11
  export { parseVariables } from './parse-variables';
@@ -31,9 +31,11 @@ __export(middlewares_exports, {
31
31
  });
32
32
  module.exports = __toCommonJS(middlewares_exports);
33
33
  __reExport(middlewares_exports, require("./data-wrapping"), module.exports);
34
+ __reExport(middlewares_exports, require("./extract-client-ip"), module.exports);
34
35
  var import_parse_variables = require("./parse-variables");
35
36
  // Annotate the CommonJS export names for ESM import in node:
36
37
  0 && (module.exports = {
37
38
  parseVariables,
38
- ...require("./data-wrapping")
39
+ ...require("./data-wrapping"),
40
+ ...require("./extract-client-ip")
39
41
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "1.6.0-alpha.17",
3
+ "version": "1.6.0-alpha.18",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "AGPL-3.0",
@@ -10,19 +10,19 @@
10
10
  "@koa/cors": "^3.1.0",
11
11
  "@koa/multer": "^3.0.2",
12
12
  "@koa/router": "^9.4.0",
13
- "@nocobase/acl": "1.6.0-alpha.17",
14
- "@nocobase/actions": "1.6.0-alpha.17",
15
- "@nocobase/auth": "1.6.0-alpha.17",
16
- "@nocobase/cache": "1.6.0-alpha.17",
17
- "@nocobase/data-source-manager": "1.6.0-alpha.17",
18
- "@nocobase/database": "1.6.0-alpha.17",
19
- "@nocobase/evaluators": "1.6.0-alpha.17",
20
- "@nocobase/lock-manager": "1.6.0-alpha.17",
21
- "@nocobase/logger": "1.6.0-alpha.17",
22
- "@nocobase/resourcer": "1.6.0-alpha.17",
23
- "@nocobase/sdk": "1.6.0-alpha.17",
24
- "@nocobase/telemetry": "1.6.0-alpha.17",
25
- "@nocobase/utils": "1.6.0-alpha.17",
13
+ "@nocobase/acl": "1.6.0-alpha.18",
14
+ "@nocobase/actions": "1.6.0-alpha.18",
15
+ "@nocobase/auth": "1.6.0-alpha.18",
16
+ "@nocobase/cache": "1.6.0-alpha.18",
17
+ "@nocobase/data-source-manager": "1.6.0-alpha.18",
18
+ "@nocobase/database": "1.6.0-alpha.18",
19
+ "@nocobase/evaluators": "1.6.0-alpha.18",
20
+ "@nocobase/lock-manager": "1.6.0-alpha.18",
21
+ "@nocobase/logger": "1.6.0-alpha.18",
22
+ "@nocobase/resourcer": "1.6.0-alpha.18",
23
+ "@nocobase/sdk": "1.6.0-alpha.18",
24
+ "@nocobase/telemetry": "1.6.0-alpha.18",
25
+ "@nocobase/utils": "1.6.0-alpha.18",
26
26
  "@types/decompress": "4.2.7",
27
27
  "@types/ini": "^1.3.31",
28
28
  "@types/koa-send": "^4.1.3",
@@ -56,5 +56,5 @@
56
56
  "@types/serve-handler": "^6.1.1",
57
57
  "@types/ws": "^8.5.5"
58
58
  },
59
- "gitHead": "7212bf5f8f59bc3fcfa1e62387c60665112310fb"
59
+ "gitHead": "db361bebe9ed4a5ca19fc14183fe4576443568ef"
60
60
  }