@maiyunnet/kebab 2.0.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 (114) hide show
  1. package/.VSCodeCounter/2025-02-14_14-46-44/details.md +82 -0
  2. package/.VSCodeCounter/2025-02-14_14-46-44/diff-details.md +15 -0
  3. package/.VSCodeCounter/2025-02-14_14-46-44/diff.csv +2 -0
  4. package/.VSCodeCounter/2025-02-14_14-46-44/diff.md +19 -0
  5. package/.VSCodeCounter/2025-02-14_14-46-44/diff.txt +22 -0
  6. package/.VSCodeCounter/2025-02-14_14-46-44/results.csv +69 -0
  7. package/.VSCodeCounter/2025-02-14_14-46-44/results.json +1 -0
  8. package/.VSCodeCounter/2025-02-14_14-46-44/results.md +48 -0
  9. package/.VSCodeCounter/2025-02-14_14-46-44/results.txt +118 -0
  10. package/.vscode/tasks.json +15 -0
  11. package/LICENSE +201 -0
  12. package/README.md +201 -0
  13. package/bin/kebab.js +2 -0
  14. package/eslint.config.js +22 -0
  15. package/index.js +19 -0
  16. package/index.ts +33 -0
  17. package/lib/buffer.js +108 -0
  18. package/lib/buffer.ts +152 -0
  19. package/lib/captcha/zcool-addict-italic.ttf +0 -0
  20. package/lib/captcha.js +71 -0
  21. package/lib/captcha.ts +63 -0
  22. package/lib/consistent.js +171 -0
  23. package/lib/consistent.ts +219 -0
  24. package/lib/core.js +663 -0
  25. package/lib/core.ts +880 -0
  26. package/lib/crypto.js +256 -0
  27. package/lib/crypto.ts +384 -0
  28. package/lib/db.js +521 -0
  29. package/lib/db.ts +719 -0
  30. package/lib/dns.js +321 -0
  31. package/lib/dns.ts +405 -0
  32. package/lib/fs.js +405 -0
  33. package/lib/fs.ts +527 -0
  34. package/lib/jwt.js +223 -0
  35. package/lib/jwt.ts +276 -0
  36. package/lib/kv.js +1004 -0
  37. package/lib/kv.ts +1489 -0
  38. package/lib/lan.js +99 -0
  39. package/lib/lan.ts +87 -0
  40. package/lib/net/cacert.pem +3480 -0
  41. package/lib/net/formdata.js +137 -0
  42. package/lib/net/formdata.ts +166 -0
  43. package/lib/net/request.js +102 -0
  44. package/lib/net/request.ts +150 -0
  45. package/lib/net/response.js +28 -0
  46. package/lib/net/response.ts +59 -0
  47. package/lib/net.js +462 -0
  48. package/lib/net.ts +662 -0
  49. package/lib/s3.js +180 -0
  50. package/lib/s3.ts +235 -0
  51. package/lib/scan.js +276 -0
  52. package/lib/scan.ts +364 -0
  53. package/lib/session.js +177 -0
  54. package/lib/session.ts +230 -0
  55. package/lib/sql.js +818 -0
  56. package/lib/sql.ts +1151 -0
  57. package/lib/ssh/sftp.js +373 -0
  58. package/lib/ssh/sftp.ts +508 -0
  59. package/lib/ssh/shell.js +109 -0
  60. package/lib/ssh/shell.ts +123 -0
  61. package/lib/ssh.js +171 -0
  62. package/lib/ssh.ts +191 -0
  63. package/lib/text/tld.json +1 -0
  64. package/lib/text.js +452 -0
  65. package/lib/text.ts +607 -0
  66. package/lib/time.js +216 -0
  67. package/lib/time.ts +254 -0
  68. package/lib/ws.js +373 -0
  69. package/lib/ws.ts +523 -0
  70. package/lib/zip.js +381 -0
  71. package/lib/zip.ts +447 -0
  72. package/lib/zlib.js +289 -0
  73. package/lib/zlib.ts +350 -0
  74. package/main.js +51 -0
  75. package/main.ts +27 -0
  76. package/package.json +37 -0
  77. package/sys/child.js +585 -0
  78. package/sys/child.ts +678 -0
  79. package/sys/cmd.js +226 -0
  80. package/sys/cmd.ts +225 -0
  81. package/sys/ctr.js +608 -0
  82. package/sys/ctr.ts +904 -0
  83. package/sys/master.js +314 -0
  84. package/sys/master.ts +355 -0
  85. package/sys/mod.js +1273 -0
  86. package/sys/mod.ts +1871 -0
  87. package/sys/route.js +922 -0
  88. package/sys/route.ts +1113 -0
  89. package/types/index.d.ts +283 -0
  90. package/www/example/ctr/main.js +42 -0
  91. package/www/example/ctr/main.ts +9 -0
  92. package/www/example/ctr/middle.js +57 -0
  93. package/www/example/ctr/middle.ts +26 -0
  94. package/www/example/ctr/test.js +2818 -0
  95. package/www/example/ctr/test.ts +3218 -0
  96. package/www/example/data/locale/en.test.json +8 -0
  97. package/www/example/data/locale/index.html +1 -0
  98. package/www/example/data/locale/ja.test.json +8 -0
  99. package/www/example/data/locale/sc.test.json +8 -0
  100. package/www/example/data/locale/tc.test.json +8 -0
  101. package/www/example/data/test.zip +0 -0
  102. package/www/example/kebab.json +24 -0
  103. package/www/example/mod/test.js +49 -0
  104. package/www/example/mod/test.ts +47 -0
  105. package/www/example/mod/testdata.js +11 -0
  106. package/www/example/mod/testdata.ts +30 -0
  107. package/www/example/route.json +6 -0
  108. package/www/example/view/test.ejs +11 -0
  109. package/www/example/ws/mproxy.js +49 -0
  110. package/www/example/ws/mproxy.ts +16 -0
  111. package/www/example/ws/rproxy.js +47 -0
  112. package/www/example/ws/rproxy.ts +14 -0
  113. package/www/example/ws/test.js +68 -0
  114. package/www/example/ws/test.ts +36 -0
package/lib/jwt.js ADDED
@@ -0,0 +1,223 @@
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
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.Jwt = void 0;
37
+ exports.getOrigin = getOrigin;
38
+ exports.decode = decode;
39
+ exports.block = block;
40
+ exports.get = get;
41
+ const lCore = __importStar(require("~/lib/core"));
42
+ const lTime = __importStar(require("~/lib/time"));
43
+ const lText = __importStar(require("~/lib/text"));
44
+ const lCrypto = __importStar(require("~/lib/crypto"));
45
+ class Jwt {
46
+ async init(ctr, opt = {}, link) {
47
+ const config = ctr.getPrototype('_config');
48
+ this._ctr = ctr;
49
+ this._link = link;
50
+ this._name = opt.name ?? config.jwt.name;
51
+ this._ttl = opt.ttl ?? config.jwt.ttl;
52
+ this._ssl = opt.ssl ?? config.jwt.ssl;
53
+ this._secret = opt.secret ?? config.jwt.secret;
54
+ this._auth = opt.auth ?? config.jwt.auth;
55
+ let jwt = '';
56
+ if (this._auth) {
57
+ const a = this._ctr.getAuthorization();
58
+ if (typeof a !== 'string') {
59
+ return false;
60
+ }
61
+ jwt = a;
62
+ }
63
+ if (!jwt) {
64
+ const cookie = this._ctr.getPrototype('_cookie');
65
+ if (!cookie[this._name]) {
66
+ return false;
67
+ }
68
+ jwt = cookie[this._name];
69
+ }
70
+ const data = await decode(this._ctr, jwt, this._link, this._name, this._secret);
71
+ if (!data) {
72
+ const cookie = this._ctr.getPrototype('_cookie');
73
+ if (cookie[this._name]) {
74
+ delete cookie[this._name];
75
+ }
76
+ return false;
77
+ }
78
+ this._ctr.setPrototype('_jwt', data);
79
+ return true;
80
+ }
81
+ renew() {
82
+ const time = lTime.stamp();
83
+ const data = this._ctr.getPrototype('_jwt');
84
+ const token = data['token'] ? data['token'] : lCore.random(16, lCore.RANDOM_LUN);
85
+ data['exp'] = time + this._ttl;
86
+ data['token'] = token;
87
+ const header = lCrypto.base64Encode(lText.stringifyJson({
88
+ 'alg': 'HS256',
89
+ 'typ': 'JWT'
90
+ }));
91
+ const payload = lCrypto.base64Encode(lText.stringifyJson(data));
92
+ const signature = lCrypto.hashHmac('sha256', header + '.' + payload, this._secret, 'base64');
93
+ const jwt = header + '.' + payload + '.' + signature;
94
+ if (!this._auth) {
95
+ lCore.setCookie(this._ctr, this._name, jwt, {
96
+ 'ttl': this._ttl,
97
+ 'ssl': this._ssl
98
+ });
99
+ }
100
+ return jwt;
101
+ }
102
+ clearCookie() {
103
+ if (this._auth) {
104
+ return;
105
+ }
106
+ lCore.setCookie(this._ctr, this._name, '', {
107
+ 'ttl': 0,
108
+ 'ssl': this._ssl
109
+ });
110
+ }
111
+ async destory() {
112
+ if (!this._link) {
113
+ return false;
114
+ }
115
+ const jwt = this._ctr.getPrototype('_jwt');
116
+ if (!jwt.token) {
117
+ return false;
118
+ }
119
+ const time = lTime.stamp();
120
+ const token = jwt.token;
121
+ const exp = jwt.exp;
122
+ const ttl = exp - time;
123
+ if (ttl <= 0) {
124
+ lCore.emptyObject(jwt);
125
+ return {
126
+ token,
127
+ exp
128
+ };
129
+ }
130
+ lCore.emptyObject(jwt);
131
+ await this._link.set(`${this._name}_block_${token}`, '1', ttl + 1);
132
+ return {
133
+ token,
134
+ exp
135
+ };
136
+ }
137
+ }
138
+ exports.Jwt = Jwt;
139
+ function getOrigin(ctr, name = '', auth = false) {
140
+ if (!name) {
141
+ name = ctr.getPrototype('_config').jwt.name;
142
+ }
143
+ let jwt = '';
144
+ if (auth) {
145
+ const a = ctr.getAuthorization();
146
+ if (typeof a !== 'string') {
147
+ return jwt;
148
+ }
149
+ jwt = a;
150
+ }
151
+ if (!jwt) {
152
+ const cookie = ctr.getPrototype('_cookie');
153
+ if (!cookie[name]) {
154
+ return jwt;
155
+ }
156
+ jwt = cookie[name];
157
+ }
158
+ return jwt;
159
+ }
160
+ async function decode(ctr, val, link, name = '', secret = '') {
161
+ if (!val) {
162
+ return false;
163
+ }
164
+ const config = ctr.getPrototype('_config');
165
+ if (!secret) {
166
+ secret = config.jwt.secret;
167
+ }
168
+ if (!name) {
169
+ name = config.jwt.name;
170
+ }
171
+ const jwtArray = val.split('.');
172
+ if (!jwtArray[2]) {
173
+ return false;
174
+ }
175
+ const nsignature = lCrypto.hashHmac('sha256', jwtArray[0] + '.' + jwtArray[1], secret, 'base64');
176
+ if (nsignature !== jwtArray[2]) {
177
+ return false;
178
+ }
179
+ try {
180
+ const payload = lCrypto.base64Decode(jwtArray[1]);
181
+ if (!payload) {
182
+ return false;
183
+ }
184
+ const data = lText.parseJson(payload);
185
+ if (!data) {
186
+ return false;
187
+ }
188
+ if (!data['token']) {
189
+ return false;
190
+ }
191
+ if (!data['exp']) {
192
+ return false;
193
+ }
194
+ const time = lTime.stamp();
195
+ if (data['exp'] < time) {
196
+ return false;
197
+ }
198
+ if (!link || !await link.get(name + '_block_' + data['token'])) {
199
+ return data;
200
+ }
201
+ return false;
202
+ }
203
+ catch {
204
+ return false;
205
+ }
206
+ }
207
+ async function block(ctr, token, exp, link, name = '') {
208
+ const time = lTime.stamp();
209
+ if (!name) {
210
+ name = ctr.getPrototype('_config').jwt.name;
211
+ }
212
+ const ttl = exp - time;
213
+ if (ttl <= 0) {
214
+ return true;
215
+ }
216
+ await link.set(name + '_block_' + token, '1', ttl + 1);
217
+ return true;
218
+ }
219
+ async function get(ctr, opt = {}, link) {
220
+ const jwt = new Jwt();
221
+ await jwt.init(ctr, opt, link);
222
+ return jwt;
223
+ }
package/lib/jwt.ts ADDED
@@ -0,0 +1,276 @@
1
+ /**
2
+ * Project: Kebab, User: JianSuoQiYue
3
+ * Date: 2023-1-31 20:34:47
4
+ * Last: 2023-1-31 20:34:47, 2023-12-8 13:55:09
5
+ */
6
+
7
+ // --- 库和定义 ---
8
+ import * as lCore from '~/lib/core';
9
+ import * as lTime from '~/lib/time';
10
+ import * as lText from '~/lib/text';
11
+ import * as lCrypto from '~/lib/crypto';
12
+ import * as kv from '~/lib/kv';
13
+ import * as ctr from '~/sys/ctr';
14
+ import * as types from '~/types';
15
+
16
+ export interface IOptions {
17
+ 'name'?: string;
18
+ 'ttl'?: number;
19
+ 'ssl'?: boolean;
20
+ 'secret'?: string;
21
+ 'auth'?: boolean;
22
+ }
23
+
24
+ export class Jwt {
25
+
26
+ /** --- Kv --- */
27
+ private _link?: kv.Pool;
28
+
29
+ /** --- 在前端或 Kv 中储存的名前缀 --- */
30
+ private _name!: string;
31
+
32
+ /** --- 有效期 --- */
33
+ private _ttl!: number;
34
+
35
+ /** --- cookie 模式时是否仅支持 SSL --- */
36
+ private _ssl!: boolean;
37
+
38
+ /** --- 验证密钥 --- */
39
+ private _secret!: string;
40
+
41
+ /** --- 是否从头部读取 --- */
42
+ private _auth!: boolean;
43
+
44
+ /** --- ctr 对象 --- */
45
+ private _ctr!: ctr.Ctr;
46
+
47
+ /**
48
+ * --- 初始化函数,相当于 construct ---
49
+ * @param ctr 模型实例
50
+ * @param link Kv 或 Db 实例
51
+ * @param auth 设为 true 则优先从头 Authorization 或 post _auth 值读取 token
52
+ * @param opt 选项
53
+ */
54
+ public async init(
55
+ ctr: ctr.Ctr,
56
+ opt: IOptions = {},
57
+ link?: kv.Pool
58
+ ): Promise<boolean> {
59
+ const config = ctr.getPrototype('_config');
60
+ this._ctr = ctr;
61
+ this._link = link;
62
+ this._name = opt.name ?? config.jwt.name;
63
+ this._ttl = opt.ttl ?? config.jwt.ttl;
64
+ this._ssl = opt.ssl ?? config.jwt.ssl;
65
+ this._secret = opt.secret ?? config.jwt.secret;
66
+ this._auth = opt.auth ?? config.jwt.auth;
67
+
68
+ let jwt = '';
69
+ if (this._auth) {
70
+ const a = this._ctr.getAuthorization();
71
+ if (typeof a !== 'string') {
72
+ return false;
73
+ }
74
+ jwt = a;
75
+ }
76
+ if (!jwt) {
77
+ const cookie = this._ctr.getPrototype('_cookie');
78
+ if (!cookie[this._name]) {
79
+ return false;
80
+ }
81
+ jwt = cookie[this._name];
82
+ }
83
+
84
+ const data = await decode(this._ctr, jwt, this._link, this._name, this._secret);
85
+ if (!data) {
86
+ // --- 清除 cookie ---
87
+ const cookie = this._ctr.getPrototype('_cookie');
88
+ if (cookie[this._name]) {
89
+ delete cookie[this._name];
90
+ }
91
+ return false;
92
+ }
93
+ this._ctr.setPrototype('_jwt', data);
94
+ return true;
95
+ }
96
+
97
+ /**
98
+ * --- 将 _jwt 数据封装并返回(创建新的或者续期老的 token),默认会同时设置一个 cookie(data 值会自动设置 token、exp) ---
99
+ */
100
+ public renew(): string {
101
+ const time = lTime.stamp();
102
+ const data = this._ctr.getPrototype('_jwt');
103
+ const token = data['token'] ? data['token'] : lCore.random(16, lCore.RANDOM_LUN);
104
+ data['exp'] = time + this._ttl;
105
+ data['token'] = token;
106
+ // --- 拼装 ---
107
+ const header = lCrypto.base64Encode(lText.stringifyJson({
108
+ 'alg': 'HS256',
109
+ 'typ': 'JWT'
110
+ }));
111
+ const payload = lCrypto.base64Encode(lText.stringifyJson(data));
112
+ const signature = lCrypto.hashHmac('sha256', header + '.' + payload, this._secret, 'base64');
113
+ const jwt = header + '.' + payload + '.' + signature;
114
+ if (!this._auth) {
115
+ lCore.setCookie(this._ctr, this._name, jwt, {
116
+ 'ttl': this._ttl,
117
+ 'ssl': this._ssl
118
+ });
119
+ }
120
+ return jwt;
121
+ }
122
+
123
+ /**
124
+ * --- 清除 cookie,仅仅清除 cookie,jwt 并不会失效 ---
125
+ */
126
+ public clearCookie(): void {
127
+ if (this._auth) {
128
+ return;
129
+ }
130
+ lCore.setCookie(this._ctr, this._name, '', {
131
+ 'ttl': 0,
132
+ 'ssl': this._ssl
133
+ });
134
+ }
135
+
136
+ /**
137
+ * --- 销毁 jwt,其实就是将 token block 信息写入 redis,如果没有 redis 则不能销毁,返回数组代表销毁成功的 token 和原 exp,否则失败返回 false ---
138
+ */
139
+ public async destory(): Promise<{ token: string; exp: number; } | boolean> {
140
+ if (!this._link) {
141
+ return false;
142
+ }
143
+ const jwt = this._ctr.getPrototype('_jwt');
144
+ if (!jwt.token) {
145
+ return false;
146
+ }
147
+ const time = lTime.stamp();
148
+ const token = jwt.token;
149
+ const exp = jwt.exp;
150
+ const ttl = exp - time;
151
+ if (ttl <= 0) {
152
+ lCore.emptyObject(jwt);
153
+ return {
154
+ token,
155
+ exp
156
+ };
157
+ }
158
+ lCore.emptyObject(jwt);
159
+ await this._link.set(`${this._name}_block_${token}`, '1', ttl + 1);
160
+ return {
161
+ token,
162
+ exp
163
+ };
164
+ }
165
+
166
+ }
167
+
168
+ /**
169
+ * --- 获取 jwt 原始字符串,不保证有效 ---
170
+ */
171
+ export function getOrigin(ctr: ctr.Ctr, name: string = '', auth: boolean = false): string {
172
+ if (!name) {
173
+ name = ctr.getPrototype('_config').jwt.name;
174
+ }
175
+ let jwt = '';
176
+ if (auth) {
177
+ const a = ctr.getAuthorization();
178
+ if (typeof a !== 'string') {
179
+ return jwt;
180
+ }
181
+ jwt = a;
182
+ }
183
+ if (!jwt) {
184
+ const cookie = ctr.getPrototype('_cookie');
185
+ if (!cookie[name]) {
186
+ return jwt;
187
+ }
188
+ jwt = cookie[name];
189
+ }
190
+ return jwt;
191
+ }
192
+
193
+ /**
194
+ * --- decode ---
195
+ * 不传入 link 的话,将不做 block 有效校验,只做本身的 exp 有效校验
196
+ */
197
+ export async function decode(ctr: ctr.Ctr, val: string, link?: kv.Pool, name: string = '', secret: string = ''): Promise<Record<string, types.DbValue> | false> {
198
+ if (!val) {
199
+ return false;
200
+ }
201
+ const config = ctr.getPrototype('_config');
202
+ if (!secret) {
203
+ secret = config.jwt.secret;
204
+ }
205
+ if (!name) {
206
+ name = config.jwt.name;
207
+ }
208
+ const jwtArray = val.split('.');
209
+ if (!jwtArray[2]) {
210
+ return false;
211
+ }
212
+ // jwtArray[1]: payload, jwtArray[2]: signature
213
+ // --- 判断是否合法 ---
214
+ const nsignature = lCrypto.hashHmac('sha256', jwtArray[0] + '.' + jwtArray[1], secret, 'base64');
215
+ if (nsignature !== jwtArray[2]) {
216
+ return false;
217
+ }
218
+ try {
219
+ const payload = lCrypto.base64Decode(jwtArray[1]);
220
+ if (!payload) {
221
+ return false;
222
+ }
223
+ const data = lText.parseJson(payload);
224
+ if (!data) {
225
+ return false;
226
+ }
227
+ // --- 检测 token ---
228
+ if (!data['token']) {
229
+ return false;
230
+ }
231
+ // --- 检测 exp ---
232
+ if (!data['exp']) {
233
+ return false;
234
+ }
235
+ const time = lTime.stamp();
236
+ if (data['exp'] < time) {
237
+ // --- 过期 ---
238
+ return false;
239
+ }
240
+ // --- 检测 token 是否有效 ---
241
+ if (!link || !await link.get(name + '_block_' + data['token'])) {
242
+ return data;
243
+ }
244
+ return false;
245
+ }
246
+ catch {
247
+ return false;
248
+ }
249
+ }
250
+
251
+ /**
252
+ * --- 仅往 redis 写禁止相关 token 的数据,一般用于异步通知时在异处的服务器来调用的 ---
253
+ */
254
+ export async function block(ctr: ctr.Ctr, token: string, exp: number, link: kv.Pool, name: string = ''): Promise<boolean> {
255
+ const time = lTime.stamp();
256
+ if (!name) {
257
+ name = ctr.getPrototype('_config').jwt.name;
258
+ }
259
+ const ttl = exp - time;
260
+ if (ttl <= 0) {
261
+ return true;
262
+ }
263
+ await link.set(name + '_block_' + token, '1', ttl + 1);
264
+ return true;
265
+ }
266
+
267
+ /**
268
+ * @param ctr 模型实例
269
+ * @param opt name, ttl, ssl, secret, auth: false, true 则优先从头 Authorization 或 post _auth 值读取 token
270
+ * @param link 实例
271
+ */
272
+ export async function get(ctr: ctr.Ctr, opt: IOptions = {}, link?: kv.Pool): Promise<Jwt> {
273
+ const jwt = new Jwt();
274
+ await jwt.init(ctr, opt, link);
275
+ return jwt;
276
+ }