@maiyunnet/kebab 2.0.2 → 2.0.4
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.js +1 -1
- package/lib/sql.js +1 -3
- package/lib/text.js +5 -1
- package/package.json +1 -1
- package/tsconfig.json +1 -1
- package/index.ts +0 -33
- package/lib/buffer.ts +0 -152
- package/lib/captcha.ts +0 -63
- package/lib/consistent.ts +0 -219
- package/lib/core.ts +0 -880
- package/lib/crypto.ts +0 -384
- package/lib/db.ts +0 -719
- package/lib/dns.ts +0 -405
- package/lib/fs.ts +0 -527
- package/lib/jwt.ts +0 -276
- package/lib/kv.ts +0 -1489
- package/lib/lan.ts +0 -87
- package/lib/net/formdata.ts +0 -166
- package/lib/net/request.ts +0 -150
- package/lib/net/response.ts +0 -59
- package/lib/net.ts +0 -662
- package/lib/s3.ts +0 -235
- package/lib/scan.ts +0 -364
- package/lib/session.ts +0 -230
- package/lib/sql.ts +0 -1151
- package/lib/ssh/sftp.ts +0 -508
- package/lib/ssh/shell.ts +0 -123
- package/lib/ssh.ts +0 -191
- package/lib/text.ts +0 -615
- package/lib/time.ts +0 -254
- package/lib/ws.ts +0 -523
- package/lib/zip.ts +0 -447
- package/lib/zlib.ts +0 -350
- package/main.ts +0 -27
- package/sys/child.ts +0 -678
- package/sys/cmd.ts +0 -225
- package/sys/ctr.ts +0 -904
- package/sys/master.ts +0 -355
- package/sys/mod.ts +0 -1871
- package/sys/route.ts +0 -1113
- package/types/index.d.ts +0 -283
- package/www/example/ctr/main.ts +0 -9
- package/www/example/ctr/middle.ts +0 -26
- package/www/example/ctr/test.ts +0 -3218
- package/www/example/mod/test.ts +0 -47
- package/www/example/mod/testdata.ts +0 -30
- package/www/example/ws/mproxy.ts +0 -16
- package/www/example/ws/rproxy.ts +0 -14
- package/www/example/ws/test.ts +0 -36
package/lib/session.ts
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Project: Kebab, User: JianSuoQiYue
|
|
3
|
-
* Date: 2019-6-5 22:01:40
|
|
4
|
-
* Last: 2020-3-30 00:11:15, 2022-12-29 00:10:28, 2023-5-24 18:59:27
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
/*
|
|
8
|
-
* --- Mysql ---
|
|
9
|
-
CREATE TABLE `session` (
|
|
10
|
-
`id` bigint NOT NULL AUTO_INCREMENT,
|
|
11
|
-
`token` char(16) CHARACTER SET ascii COLLATE ascii_bin NOT NULL,
|
|
12
|
-
`data` text NOT NULL,
|
|
13
|
-
`time_update` bigint NOT NULL,
|
|
14
|
-
`time_add` bigint NOT NULL,
|
|
15
|
-
PRIMARY KEY (`id`),
|
|
16
|
-
UNIQUE KEY `token` (`token`),
|
|
17
|
-
KEY `time_update` (`time_update`)
|
|
18
|
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
// --- 库和定义 ---
|
|
22
|
-
import * as core from '~/lib/core';
|
|
23
|
-
import * as time from '~/lib/time';
|
|
24
|
-
import * as db from '~/lib/db';
|
|
25
|
-
import * as kv from '~/lib/kv';
|
|
26
|
-
import * as sql from '~/lib/sql';
|
|
27
|
-
import * as text from '~/lib/text';
|
|
28
|
-
import * as ctr from '~/sys/ctr';
|
|
29
|
-
|
|
30
|
-
export interface IOptions {
|
|
31
|
-
'name'?: string;
|
|
32
|
-
'ttl'?: number;
|
|
33
|
-
'ssl'?: boolean;
|
|
34
|
-
'sqlPre'?: string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export class Session {
|
|
38
|
-
|
|
39
|
-
/** --- 数据库操作对象 --- */
|
|
40
|
-
private _link!: db.Pool | kv.Pool;
|
|
41
|
-
|
|
42
|
-
/** --- Sql 对象 --- */
|
|
43
|
-
private _sql!: sql.Sql;
|
|
44
|
-
|
|
45
|
-
/** --- session 在前端或 Kv 中储存的名前缀 --- */
|
|
46
|
-
private _name!: string;
|
|
47
|
-
|
|
48
|
-
/** --- 当前 Session 的 token --- */
|
|
49
|
-
private _token: string = '';
|
|
50
|
-
|
|
51
|
-
/** --- Session 有效期 --- */
|
|
52
|
-
private _ttl: number = 0;
|
|
53
|
-
|
|
54
|
-
/** --- ctr 对象 --- */
|
|
55
|
-
private _ctr!: ctr.Ctr;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* --- 初始化函数,相当于 construct ---
|
|
59
|
-
* @param ctr 模型实例
|
|
60
|
-
* @param link Kv 或 Db 实例
|
|
61
|
-
* @param auth 设为 true 则优先从头 Authorization 或 post _auth 值读取 token
|
|
62
|
-
* @param opt 选项
|
|
63
|
-
*/
|
|
64
|
-
public async init(
|
|
65
|
-
ctr: ctr.Ctr,
|
|
66
|
-
link: db.Pool | kv.Pool,
|
|
67
|
-
auth: boolean = false,
|
|
68
|
-
opt: IOptions = {}
|
|
69
|
-
): Promise<boolean> {
|
|
70
|
-
const config = ctr.getPrototype('_config');
|
|
71
|
-
const ssl = opt.ssl ?? config.session.ssl;
|
|
72
|
-
const pre = opt.sqlPre ?? null;
|
|
73
|
-
this._name = opt.name ?? config.session.name;
|
|
74
|
-
this._ttl = opt.ttl ?? config.session.ttl;
|
|
75
|
-
const tim: number = time.stamp();
|
|
76
|
-
this._ctr = ctr;
|
|
77
|
-
|
|
78
|
-
if (auth) {
|
|
79
|
-
const a = ctr.getAuthorization();
|
|
80
|
-
if (a && typeof a !== 'string' && (a.user === 'token')) {
|
|
81
|
-
this._token = a.pwd;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
const cookie = ctr.getPrototype('_cookie');
|
|
85
|
-
if ((this._token === '') && cookie[this._name]) {
|
|
86
|
-
this._token = cookie[this._name];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
this._link = link;
|
|
90
|
-
if (link instanceof db.Pool) {
|
|
91
|
-
this._sql = sql.get(pre ? pre : ctr);
|
|
92
|
-
await this._gc(); // --- 执行 gc ---
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
// --- 初始化 Session 数组 ---
|
|
96
|
-
let session: Record<string, any> = {};
|
|
97
|
-
let needInsert: boolean = false;
|
|
98
|
-
// --- 有 token 则查看 token 的信息是否存在
|
|
99
|
-
if (this._token !== '') {
|
|
100
|
-
// --- 如果启用了内存加速则在内存找 ---
|
|
101
|
-
if (this._link instanceof kv.Pool) {
|
|
102
|
-
// --- Kv ---
|
|
103
|
-
let data;
|
|
104
|
-
if ((data = await this._link.getJson(this._name + '_' + this._token)) === null) {
|
|
105
|
-
needInsert = true;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
session = data;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
// --- 数据库 ---
|
|
113
|
-
this._sql.select('*', 'session').where([
|
|
114
|
-
['time_update', '>=', tim - this._ttl],
|
|
115
|
-
{ 'token': this._token }
|
|
116
|
-
]);
|
|
117
|
-
const data = await this._link.query(this._sql.getSql(), this._sql.getData());
|
|
118
|
-
if (data.rows?.[0]) {
|
|
119
|
-
session = text.parseJson(data.rows[0].data);
|
|
120
|
-
}
|
|
121
|
-
else {
|
|
122
|
-
needInsert = true;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
ctr.setPrototype('_session', session);
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
// --- 全新的机子 ---
|
|
129
|
-
needInsert = true;
|
|
130
|
-
}
|
|
131
|
-
// --- 本来就该添加个新 Session ---
|
|
132
|
-
// --- 内存和数据库里没找到的也该添加个新 Session ---
|
|
133
|
-
// --- 数据库的 Session 已经过期加新 Session ---
|
|
134
|
-
// --- 如果不存在不允许加新则返回错误 ---
|
|
135
|
-
if (needInsert) {
|
|
136
|
-
if (this._link instanceof kv.Pool) {
|
|
137
|
-
let count = 0;
|
|
138
|
-
do {
|
|
139
|
-
if (count === 5) {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
this._token = core.random(16, core.RANDOM_LUN);
|
|
143
|
-
++count;
|
|
144
|
-
} while (!await this._link.set(this._name + '_' + this._token, [], this._ttl, 'nx'));
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
let count = 0;
|
|
148
|
-
while (true) {
|
|
149
|
-
if (count === 5) {
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
this._token = core.random(16, core.RANDOM_LUN);
|
|
153
|
-
this._sql.insert('session').values({
|
|
154
|
-
'token': this._token,
|
|
155
|
-
'data': '{}',
|
|
156
|
-
'time_update': tim,
|
|
157
|
-
'time_add': tim
|
|
158
|
-
});
|
|
159
|
-
++count;
|
|
160
|
-
const r = await this._link.execute(this._sql.getSql(), this._sql.getData());
|
|
161
|
-
if (r.error) {
|
|
162
|
-
if (r.error.errno !== 1062) {
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
break;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
core.setCookie(ctr, this._name, this._token, {
|
|
174
|
-
'ttl': this._ttl,
|
|
175
|
-
'ssl': ssl
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
return true;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* --- 获取当前的 token 值 ---
|
|
183
|
-
*/
|
|
184
|
-
public getToken(): string {
|
|
185
|
-
return this._token;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* --- 获取当前的 cookie 的 name 值 ---
|
|
190
|
-
*/
|
|
191
|
-
public getName(): string {
|
|
192
|
-
return this._name;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* --- 页面整体结束时,要写入到 Kv 或 数据库 ---
|
|
197
|
-
*/
|
|
198
|
-
public async update(): Promise<void> {
|
|
199
|
-
if (this._link instanceof kv.Pool) {
|
|
200
|
-
await this._link.set(this._name + '_' + this._token, this._ctr.getPrototype('_session'), this._ttl);
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
this._sql.update('session', [{
|
|
204
|
-
'data': text.stringifyJson(this._ctr.getPrototype('_session')),
|
|
205
|
-
'time_update': time.stamp()
|
|
206
|
-
}]).where([{
|
|
207
|
-
'token': this._token
|
|
208
|
-
}]);
|
|
209
|
-
await this._link.execute(this._sql.getSql(), this._sql.getData());
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* --- 根据情况清空 Db 状态下的 session 表垃圾数据 ---
|
|
215
|
-
* --- 仅能在 Db 模式执行,非 Db 模式则等于没运行 ---
|
|
216
|
-
*/
|
|
217
|
-
private async _gc(): Promise<void> {
|
|
218
|
-
if (!(this._link instanceof db.Pool)) {
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
if (core.rand(0, 20) !== 10) {
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
this._sql.delete('session').where([
|
|
225
|
-
['time_update', '<', time.stamp() - this._ttl]
|
|
226
|
-
]);
|
|
227
|
-
await this._link.execute(this._sql.getSql(), this._sql.getData());
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
}
|