@maiyunnet/kebab 3.2.7 → 3.2.9
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/index.d.ts +1 -1
- package/index.js +1 -1
- package/lib/crypto.d.ts +7 -3
- package/lib/crypto.js +15 -11
- package/lib/db.js +2 -3
- package/lib/kv.js +1 -1
- package/lib/socket.js +5 -4
- package/package.json +1 -1
- package/sys/ctr.d.ts +2 -2
- package/sys/ctr.js +4 -4
- package/sys/route.js +1 -1
- package/www/example/ctr/test.js +1 -1
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* --- 本文件用来定义每个目录实体地址的常量 ---
|
|
7
7
|
*/
|
|
8
8
|
/** --- 当前系统版本号 --- */
|
|
9
|
-
export const VER = '3.2.
|
|
9
|
+
export const VER = '3.2.9';
|
|
10
10
|
// --- 服务端用的路径 ---
|
|
11
11
|
const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
|
|
12
12
|
/** --- /xxx/xxx --- */
|
package/lib/crypto.d.ts
CHANGED
|
@@ -58,9 +58,13 @@ export declare function publicDecrypt(key: crypto.RsaPublicKey | crypto.RsaPriva
|
|
|
58
58
|
* @param buffer 数据
|
|
59
59
|
*/
|
|
60
60
|
export declare function privateDecrypt(key: crypto.RsaPrivateKey | crypto.KeyLike, buffer: NodeJS.ArrayBufferView | string): Buffer;
|
|
61
|
+
/** --- 勿使用,无 iv 默认,但勿使用 --- */
|
|
61
62
|
export declare const AES_256_ECB = "aes-256-ecb";
|
|
63
|
+
/** --- 一般不用,兼容性场景下用 --- */
|
|
62
64
|
export declare const AES_256_CBC = "aes-256-cbc";
|
|
63
|
-
|
|
65
|
+
/** --- 设置 iv 会自动切换为 CTR,流式下使用,非流直接使用 GCM --- */
|
|
66
|
+
export declare const AES_256_CTR = "aes-256-ctr";
|
|
67
|
+
/** --- 非流直接使用 GCM --- */
|
|
64
68
|
export declare const AES_256_GCM = "aes-256-gcm";
|
|
65
69
|
export declare const SM4_ECB = "sm4-ecb";
|
|
66
70
|
export declare const SM4_CBC = "sm4-cbc";
|
|
@@ -69,7 +73,7 @@ export declare const SM4_CFB = "sm4-cfb";
|
|
|
69
73
|
* --- cipher 加密,强烈不建议使用 AES_256_ECB ---
|
|
70
74
|
* @param original 原始字符串
|
|
71
75
|
* @param key 密钥 32 个英文字母和数字
|
|
72
|
-
* @param iv 向量 16(
|
|
76
|
+
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
73
77
|
* @param method 加密方法
|
|
74
78
|
*/
|
|
75
79
|
export declare function cipherEncrypt(original: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'base64' | 'buffer'): string | Buffer | false;
|
|
@@ -83,7 +87,7 @@ export declare function sm4Encrypt(original: string | Buffer, key: crypto.Cipher
|
|
|
83
87
|
* --- cipher 解密 ---
|
|
84
88
|
* @param encrypt 需解密的字符串
|
|
85
89
|
* @param key 密钥 32 个英文字母和数字
|
|
86
|
-
* @param iv 向量 16(
|
|
90
|
+
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
87
91
|
* @param method 加密方法
|
|
88
92
|
*/
|
|
89
93
|
export declare function cipherDecrypt(encrypt: string | Buffer, key: crypto.CipherKey, iv?: string, method?: string, output?: 'binary' | 'buffer'): string | Buffer | false;
|
package/lib/crypto.js
CHANGED
|
@@ -96,10 +96,14 @@ export function privateDecrypt(key, buffer) {
|
|
|
96
96
|
return crypto.privateDecrypt(key, buffer);
|
|
97
97
|
}
|
|
98
98
|
// --- Cipher (AES/SM4...) 加/解密 ---
|
|
99
|
-
|
|
99
|
+
/** --- 勿使用,无 iv 默认,但勿使用 --- */
|
|
100
|
+
export const AES_256_ECB = 'aes-256-ecb';
|
|
101
|
+
/** --- 一般不用,兼容性场景下用 --- */
|
|
100
102
|
export const AES_256_CBC = 'aes-256-cbc';
|
|
101
|
-
|
|
102
|
-
export const
|
|
103
|
+
/** --- 设置 iv 会自动切换为 CTR,流式下使用,非流直接使用 GCM --- */
|
|
104
|
+
export const AES_256_CTR = 'aes-256-ctr';
|
|
105
|
+
/** --- 非流直接使用 GCM --- */
|
|
106
|
+
export const AES_256_GCM = 'aes-256-gcm';
|
|
103
107
|
export const SM4_ECB = 'sm4-ecb'; // --- SM4 如果未设置 iv,则默认这个 ---
|
|
104
108
|
export const SM4_CBC = 'sm4-cbc';
|
|
105
109
|
export const SM4_CFB = 'sm4-cfb'; // --- SM4 一般用这个,设置 iv,自动就切换成了这个 ---
|
|
@@ -107,7 +111,7 @@ export const SM4_CFB = 'sm4-cfb'; // --- SM4 一般用这个,设置 iv,自
|
|
|
107
111
|
* --- cipher 加密,强烈不建议使用 AES_256_ECB ---
|
|
108
112
|
* @param original 原始字符串
|
|
109
113
|
* @param key 密钥 32 个英文字母和数字
|
|
110
|
-
* @param iv 向量 16(
|
|
114
|
+
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
111
115
|
* @param method 加密方法
|
|
112
116
|
*/
|
|
113
117
|
export function cipherEncrypt(original, key, iv = '', method = AES_256_ECB, output = 'base64') {
|
|
@@ -116,7 +120,7 @@ export function cipherEncrypt(original, key, iv = '', method = AES_256_ECB, outp
|
|
|
116
120
|
key = hashHmac('md5', key, 'MaiyunSalt');
|
|
117
121
|
}
|
|
118
122
|
if (iv) {
|
|
119
|
-
if (method ===
|
|
123
|
+
if (method === AES_256_CTR) {
|
|
120
124
|
if (iv.length !== 16) {
|
|
121
125
|
return false;
|
|
122
126
|
}
|
|
@@ -165,13 +169,13 @@ export function cipherEncrypt(original, key, iv = '', method = AES_256_ECB, outp
|
|
|
165
169
|
* --- AES 加密 ---
|
|
166
170
|
* @param original 原始字符串
|
|
167
171
|
* @param key 密钥尽量 32 个英文字母和数字,不是 32 个系统会自动处理
|
|
168
|
-
* @param iv 向量 16 个英文字母和数字
|
|
172
|
+
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
169
173
|
* @param method 加密方法
|
|
170
174
|
* @param output 输出类型
|
|
171
175
|
*/
|
|
172
176
|
export function aesEncrypt(original, key, iv = '', method = AES_256_ECB, output = 'base64') {
|
|
173
177
|
if (iv !== '') {
|
|
174
|
-
method = method === AES_256_ECB ?
|
|
178
|
+
method = method === AES_256_ECB ? AES_256_CTR : method;
|
|
175
179
|
}
|
|
176
180
|
return cipherEncrypt(original, key, iv, method, output);
|
|
177
181
|
}
|
|
@@ -206,7 +210,7 @@ export function sm4Encrypt(original, key, iv = '', method = SM4_ECB, output = 'b
|
|
|
206
210
|
* --- cipher 解密 ---
|
|
207
211
|
* @param encrypt 需解密的字符串
|
|
208
212
|
* @param key 密钥 32 个英文字母和数字
|
|
209
|
-
* @param iv 向量 16(
|
|
213
|
+
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
210
214
|
* @param method 加密方法
|
|
211
215
|
*/
|
|
212
216
|
export function cipherDecrypt(encrypt, key, iv = '', method = AES_256_ECB, output = 'binary') {
|
|
@@ -215,7 +219,7 @@ export function cipherDecrypt(encrypt, key, iv = '', method = AES_256_ECB, outpu
|
|
|
215
219
|
key = hashHmac('md5', key, 'MaiyunSalt');
|
|
216
220
|
}
|
|
217
221
|
if (iv) {
|
|
218
|
-
if (method ===
|
|
222
|
+
if (method === AES_256_CTR) {
|
|
219
223
|
if (iv.length !== 16) {
|
|
220
224
|
return false;
|
|
221
225
|
}
|
|
@@ -268,12 +272,12 @@ export function cipherDecrypt(encrypt, key, iv = '', method = AES_256_ECB, outpu
|
|
|
268
272
|
* --- AES 解密 ---
|
|
269
273
|
* @param encrypt 需解密的字符串
|
|
270
274
|
* @param key 密钥 32 个英文字母和数字
|
|
271
|
-
* @param iv 向量 16 个英文字母和数字
|
|
275
|
+
* @param iv 向量 16(CTR) 或 12(GCM) 个英文字母和数字
|
|
272
276
|
* @param method 加密方法
|
|
273
277
|
*/
|
|
274
278
|
export function aesDecrypt(encrypt, key, iv = '', method = AES_256_ECB, output = 'binary') {
|
|
275
279
|
if (iv !== '') {
|
|
276
|
-
method = method === AES_256_ECB ?
|
|
280
|
+
method = method === AES_256_ECB ? AES_256_CTR : method;
|
|
277
281
|
}
|
|
278
282
|
return cipherDecrypt(encrypt, key, iv, method, output);
|
|
279
283
|
}
|
package/lib/db.js
CHANGED
|
@@ -9,7 +9,6 @@ import * as mysql2 from 'mysql2/promise';
|
|
|
9
9
|
import * as lTime from '#kebab/lib/time.js';
|
|
10
10
|
import * as lSql from '#kebab/lib/sql.js';
|
|
11
11
|
import * as lCore from '#kebab/lib/core.js';
|
|
12
|
-
import * as lText from '#kebab/lib/text.js';
|
|
13
12
|
import * as sCtr from '#kebab/sys/ctr.js';
|
|
14
13
|
/** --- 连接列表池 --- */
|
|
15
14
|
const connections = [];
|
|
@@ -170,7 +169,7 @@ export class Pool {
|
|
|
170
169
|
link.on('error', function (err) {
|
|
171
170
|
c.setLost();
|
|
172
171
|
if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
|
|
173
|
-
lCore.debug('[DB][_getConnection][error]', err);
|
|
172
|
+
lCore.debug('[DB][_getConnection][error]', err.message);
|
|
174
173
|
}
|
|
175
174
|
}).on('end', () => {
|
|
176
175
|
// lCore.debug('[DB][_getConnection] connection end.');
|
|
@@ -182,7 +181,7 @@ export class Pool {
|
|
|
182
181
|
connections.push(conn);
|
|
183
182
|
}
|
|
184
183
|
catch (e) {
|
|
185
|
-
const msg = '[DB][_getConnection] ' +
|
|
184
|
+
const msg = '[DB][_getConnection] ' + e.message;
|
|
186
185
|
lCore.debug(msg);
|
|
187
186
|
lCore.log({}, msg, '-error');
|
|
188
187
|
}
|
package/lib/kv.js
CHANGED
|
@@ -618,7 +618,7 @@ export class Pool {
|
|
|
618
618
|
link.on('error', function (err) {
|
|
619
619
|
conn.setLost();
|
|
620
620
|
// console.log(`--- redis [${conn._etc.host}:${conn._etc.port}] error ---`);
|
|
621
|
-
lCore.debug('[KV][ERROR]', err);
|
|
621
|
+
lCore.debug('[KV][ERROR]', err.message);
|
|
622
622
|
}).on('end', () => {
|
|
623
623
|
conn.setLost();
|
|
624
624
|
}).on('close', () => {
|
package/lib/socket.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* Last: 2025-9-25 16:49:48
|
|
5
5
|
*/
|
|
6
6
|
import net from 'net';
|
|
7
|
+
import * as lTime from '#kebab/lib/time.js';
|
|
7
8
|
import * as lWs from '#kebab/lib/ws.js';
|
|
8
9
|
import * as lCore from '#kebab/lib/core.js';
|
|
9
10
|
/**
|
|
@@ -17,7 +18,7 @@ export function rwebsocket(port, url, opt = {}) {
|
|
|
17
18
|
const server = net.createServer(socket => {
|
|
18
19
|
(async () => {
|
|
19
20
|
// --- 每次进一个新连接都反代到一个新 WebSocket ---
|
|
20
|
-
lCore.display('New client: ' + socket.remoteAddress + ':' + socket.remotePort);
|
|
21
|
+
lCore.display('[' + lTime.format(null, 'Y-m-d H:i:s') + '] New client: ' + socket.remoteAddress + ':' + socket.remotePort);
|
|
21
22
|
/** --- 远程端的双向 websocket --- */
|
|
22
23
|
const rws = await lWs.connect(url, opt);
|
|
23
24
|
if (!rws) {
|
|
@@ -52,13 +53,13 @@ export function rwebsocket(port, url, opt = {}) {
|
|
|
52
53
|
rws.writeBinary(data);
|
|
53
54
|
}).on('end', () => {
|
|
54
55
|
rws.end();
|
|
55
|
-
lCore.display('Client disconnected: ' + socket.remoteAddress + ':' + socket.remotePort);
|
|
56
|
+
lCore.display('[' + lTime.format(null, 'Y-m-d H:i:s') + '] Client disconnected: ' + socket.remoteAddress + ':' + socket.remotePort);
|
|
56
57
|
}).on('error', err => {
|
|
57
|
-
lCore.display('Client error: ' + socket.remoteAddress + ':' + socket.remotePort + ', ' + err.message);
|
|
58
|
+
lCore.display('[' + lTime.format(null, 'Y-m-d H:i:s') + '] Client error: ' + socket.remoteAddress + ':' + socket.remotePort + ', ' + err.message);
|
|
58
59
|
});
|
|
59
60
|
})().catch(() => { });
|
|
60
61
|
}).listen(port, () => {
|
|
61
|
-
lCore.display('Listening:' + port);
|
|
62
|
+
lCore.display('[' + lTime.format(null, 'Y-m-d H:i:s') + '] Listening: ' + port);
|
|
62
63
|
});
|
|
63
64
|
return server;
|
|
64
65
|
}
|
package/package.json
CHANGED
package/sys/ctr.d.ts
CHANGED
|
@@ -253,12 +253,12 @@ export declare class Ctr {
|
|
|
253
253
|
* --- 发送 socket ping ---
|
|
254
254
|
* @param data 要发送的信息
|
|
255
255
|
*/
|
|
256
|
-
protected _ping(): boolean;
|
|
256
|
+
protected _ping(data?: Buffer | string): boolean;
|
|
257
257
|
/**
|
|
258
258
|
* --- 发送 socket pong ---
|
|
259
259
|
* @param data 要发送的信息
|
|
260
260
|
*/
|
|
261
|
-
protected _pong(): boolean;
|
|
261
|
+
protected _pong(data?: Buffer | string): boolean;
|
|
262
262
|
/**
|
|
263
263
|
* --- 主动关闭当前 socket 连接 ---
|
|
264
264
|
*/
|
package/sys/ctr.js
CHANGED
|
@@ -725,15 +725,15 @@ export class Ctr {
|
|
|
725
725
|
* --- 发送 socket ping ---
|
|
726
726
|
* @param data 要发送的信息
|
|
727
727
|
*/
|
|
728
|
-
_ping() {
|
|
729
|
-
return this._socket.ping();
|
|
728
|
+
_ping(data) {
|
|
729
|
+
return this._socket.ping(data);
|
|
730
730
|
}
|
|
731
731
|
/**
|
|
732
732
|
* --- 发送 socket pong ---
|
|
733
733
|
* @param data 要发送的信息
|
|
734
734
|
*/
|
|
735
|
-
_pong() {
|
|
736
|
-
return this._socket.pong();
|
|
735
|
+
_pong(data) {
|
|
736
|
+
return this._socket.pong(data);
|
|
737
737
|
}
|
|
738
738
|
/**
|
|
739
739
|
* --- 主动关闭当前 socket 连接 ---
|
package/sys/route.js
CHANGED
|
@@ -235,7 +235,7 @@ export async function run(data) {
|
|
|
235
235
|
// --- 先处理 web socket 的情况 ---
|
|
236
236
|
let wsSocket;
|
|
237
237
|
try {
|
|
238
|
-
const options =
|
|
238
|
+
const options = cctr.onUpgrade();
|
|
239
239
|
// --- 默认无消息发送 3 分钟 ---
|
|
240
240
|
options.timeout ??= 60_000 * 3;
|
|
241
241
|
wsSocket = lWs.createServer(data.req, data.socket, options);
|
package/www/example/ctr/test.js
CHANGED
|
@@ -31,7 +31,7 @@ export default class extends sCtr.Ctr {
|
|
|
31
31
|
}
|
|
32
32
|
onLoad() {
|
|
33
33
|
if (this._config.const.hostname !== '127.0.0.1' && this._config.const.hostname !== '172.17.0.1' &&
|
|
34
|
-
this._config.const.hostname !== 'localhost' && this._config.const.hostname
|
|
34
|
+
this._config.const.hostname !== 'localhost' && !this._config.const.hostname.endsWith('.local.brc-app.com') &&
|
|
35
35
|
!this._config.const.hostname.startsWith('192.168.')) {
|
|
36
36
|
return [0, 'Please use 127.0.0.1 or local to access the file (' + this._config.const.host + ').'];
|
|
37
37
|
}
|