node-karin 0.0.3 → 0.1.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.
- package/LICENSE +674 -674
- package/README.md +57 -57
- package/config/defSet/App.yaml +37 -37
- package/config/defSet/config.yaml +43 -43
- package/config/defSet/group.yaml +18 -18
- package/config/defSet/pm2.yaml +21 -21
- package/config/defSet/server.yaml +42 -42
- package/config/view/App.yaml +74 -74
- package/config/view/config.yaml +100 -100
- package/config/view/group.yaml +62 -62
- package/config/view/pm2.yaml +41 -41
- package/config/view/redis.yaml +25 -25
- package/config/view/server.yaml +93 -93
- package/lib/adapter/onebot/onebot11.d.ts +430 -430
- package/lib/adapter/onebot/onebot11.js +1265 -1302
- package/lib/core/init.js +4 -4
- package/lib/core/karin.d.ts +72 -72
- package/lib/core/karin.js +51 -51
- package/lib/core/listener.d.ts +121 -121
- package/lib/core/listener.js +178 -178
- package/lib/core/plugin.app.d.ts +15 -15
- package/lib/core/plugin.app.js +18 -18
- package/lib/core/plugin.d.ts +182 -182
- package/lib/core/plugin.js +132 -132
- package/lib/core/plugin.loader.d.ts +149 -149
- package/lib/core/plugin.loader.js +451 -451
- package/lib/core/server.d.ts +26 -26
- package/lib/core/server.js +209 -209
- package/lib/db/level.d.ts +20 -20
- package/lib/db/level.js +36 -36
- package/lib/db/redis.d.ts +41 -41
- package/lib/db/redis.js +131 -131
- package/lib/db/redis_level.d.ts +113 -113
- package/lib/db/redis_level.js +274 -274
- package/lib/event/event.d.ts +138 -138
- package/lib/event/event.handler.d.ts +29 -29
- package/lib/event/event.handler.js +138 -138
- package/lib/event/event.js +120 -120
- package/lib/event/message.d.ts +102 -102
- package/lib/event/message.handler.d.ts +25 -25
- package/lib/event/message.handler.js +237 -237
- package/lib/event/message.js +69 -69
- package/lib/event/notice.d.ts +49 -49
- package/lib/event/notice.js +14 -14
- package/lib/event/request.d.ts +49 -49
- package/lib/event/request.js +14 -14
- package/lib/event/review.handler.d.ts +54 -54
- package/lib/event/review.handler.js +374 -374
- package/lib/index.d.ts +23 -23
- package/lib/index.js +39 -40
- package/lib/renderer/app.d.ts +53 -53
- package/lib/renderer/app.js +88 -88
- package/lib/renderer/base.d.ts +30 -30
- package/lib/renderer/base.js +68 -68
- package/lib/renderer/client.d.ts +30 -30
- package/lib/renderer/client.js +155 -155
- package/lib/renderer/http.d.ts +19 -19
- package/lib/renderer/http.js +50 -50
- package/lib/renderer/server.d.ts +42 -42
- package/lib/renderer/server.js +110 -110
- package/lib/renderer/wormhole.d.ts +1 -1
- package/lib/renderer/wormhole.js +154 -154
- package/lib/types/adapter.d.ts +575 -575
- package/lib/types/adapter.js +1 -1
- package/lib/types/config.d.ts +327 -327
- package/lib/types/config.js +1 -1
- package/lib/types/element.d.ts +576 -576
- package/lib/types/element.js +1 -1
- package/lib/types/index.d.ts +8 -8
- package/lib/types/index.js +8 -8
- package/lib/types/logger.d.ts +109 -109
- package/lib/types/logger.js +1 -1
- package/lib/types/onebots11.d.ts +1371 -1371
- package/lib/types/onebots11.js +1 -1
- package/lib/types/plugin.d.ts +282 -282
- package/lib/types/plugin.js +1 -1
- package/lib/types/render.d.ts +111 -111
- package/lib/types/render.js +1 -1
- package/lib/types/reply.d.ts +40 -40
- package/lib/types/reply.js +1 -1
- package/lib/types/types.d.ts +898 -898
- package/lib/types/types.js +1 -1
- package/lib/utils/YamlEditor.d.ts +62 -62
- package/lib/utils/YamlEditor.js +197 -197
- package/lib/utils/button.d.ts +49 -49
- package/lib/utils/button.js +75 -75
- package/lib/utils/common.d.ts +123 -123
- package/lib/utils/common.js +396 -396
- package/lib/utils/config.d.ts +72 -72
- package/lib/utils/config.js +254 -254
- package/lib/utils/exec.d.ts +22 -22
- package/lib/utils/exec.js +36 -36
- package/lib/utils/ffmpeg.d.ts +12 -12
- package/lib/utils/ffmpeg.js +25 -25
- package/lib/utils/handler.d.ts +76 -76
- package/lib/utils/handler.js +98 -98
- package/lib/utils/logger.d.ts +3 -3
- package/lib/utils/logger.js +104 -104
- package/lib/utils/segment.d.ts +276 -276
- package/lib/utils/segment.js +420 -420
- package/lib/utils/update.d.ts +69 -69
- package/lib/utils/update.js +145 -145
- package/lib/utils/updateVersion.d.ts +33 -33
- package/lib/utils/updateVersion.js +140 -140
- package/package.json +92 -91
package/lib/core/server.d.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { Express } from 'express';
|
|
2
|
-
import { WebSocketServer } from 'ws';
|
|
3
|
-
import { Server as ServerType, ServerResponse, IncomingMessage } from 'http';
|
|
4
|
-
declare const _default: {
|
|
5
|
-
reg: RegExp;
|
|
6
|
-
list: string[];
|
|
7
|
-
app: Express;
|
|
8
|
-
server: ServerType<typeof IncomingMessage, typeof ServerResponse>;
|
|
9
|
-
WebSocketServer: WebSocketServer;
|
|
10
|
-
RegExp: RegExp;
|
|
11
|
-
/**
|
|
12
|
-
* 监听WebSocket连接并初始化http服务器
|
|
13
|
-
*/
|
|
14
|
-
init(): false | any;
|
|
15
|
-
/**
|
|
16
|
-
* HTTP渲染器
|
|
17
|
-
*/
|
|
18
|
-
static(): void;
|
|
19
|
-
/**
|
|
20
|
-
* 构建静态资源路径
|
|
21
|
-
*/
|
|
22
|
-
staticPath(): void;
|
|
23
|
-
/** 重启当前HTTP服务器 */
|
|
24
|
-
"__#3@#restartServer"(): Promise<void>;
|
|
25
|
-
};
|
|
26
|
-
export default _default;
|
|
1
|
+
import { Express } from 'express';
|
|
2
|
+
import { WebSocketServer } from 'ws';
|
|
3
|
+
import { Server as ServerType, ServerResponse, IncomingMessage } from 'http';
|
|
4
|
+
declare const _default: {
|
|
5
|
+
reg: RegExp;
|
|
6
|
+
list: string[];
|
|
7
|
+
app: Express;
|
|
8
|
+
server: ServerType<typeof IncomingMessage, typeof ServerResponse>;
|
|
9
|
+
WebSocketServer: WebSocketServer;
|
|
10
|
+
RegExp: RegExp;
|
|
11
|
+
/**
|
|
12
|
+
* 监听WebSocket连接并初始化http服务器
|
|
13
|
+
*/
|
|
14
|
+
init(): false | any;
|
|
15
|
+
/**
|
|
16
|
+
* HTTP渲染器
|
|
17
|
+
*/
|
|
18
|
+
static(): void;
|
|
19
|
+
/**
|
|
20
|
+
* 构建静态资源路径
|
|
21
|
+
*/
|
|
22
|
+
staticPath(): void;
|
|
23
|
+
/** 重启当前HTTP服务器 */
|
|
24
|
+
"__#3@#restartServer"(): Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
export default _default;
|
package/lib/core/server.js
CHANGED
|
@@ -1,213 +1,213 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import express from 'express'
|
|
3
|
-
import { createServer } from 'http'
|
|
4
|
-
import { WebSocketServer } from 'ws'
|
|
5
|
-
import { render } from '../index.js'
|
|
6
|
-
import connect from '../renderer/wormhole.js'
|
|
7
|
-
import HttpRenderer from '../renderer/http.js'
|
|
8
|
-
import logger from '../utils/logger.js'
|
|
9
|
-
import common from '../utils/common.js'
|
|
10
|
-
import config from '../utils/config.js'
|
|
11
|
-
import listener from './listener.js'
|
|
12
|
-
import exec from '../utils/exec.js'
|
|
13
|
-
export default new (class Server {
|
|
14
|
-
reg
|
|
15
|
-
list
|
|
16
|
-
app
|
|
17
|
-
server
|
|
18
|
-
WebSocketServer
|
|
19
|
-
RegExp
|
|
20
|
-
constructor () {
|
|
21
|
-
this.reg = /(?:)/
|
|
22
|
-
this.list = []
|
|
23
|
-
this.app = express()
|
|
24
|
-
this.server = createServer(this.app)
|
|
25
|
-
this.WebSocketServer = new WebSocketServer({ server: this.server })
|
|
26
|
-
this.RegExp = new RegExp(`(${process.cwd()}|${process.cwd().replace(/\\/g, '/')})`, 'g')
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import express from 'express'
|
|
3
|
+
import { createServer } from 'http'
|
|
4
|
+
import { WebSocketServer } from 'ws'
|
|
5
|
+
import { render } from '../index.js'
|
|
6
|
+
import connect from '../renderer/wormhole.js'
|
|
7
|
+
import HttpRenderer from '../renderer/http.js'
|
|
8
|
+
import logger from '../utils/logger.js'
|
|
9
|
+
import common from '../utils/common.js'
|
|
10
|
+
import config from '../utils/config.js'
|
|
11
|
+
import listener from './listener.js'
|
|
12
|
+
import exec from '../utils/exec.js'
|
|
13
|
+
export default new (class Server {
|
|
14
|
+
reg
|
|
15
|
+
list
|
|
16
|
+
app
|
|
17
|
+
server
|
|
18
|
+
WebSocketServer
|
|
19
|
+
RegExp
|
|
20
|
+
constructor () {
|
|
21
|
+
this.reg = /(?:)/
|
|
22
|
+
this.list = []
|
|
23
|
+
this.app = express()
|
|
24
|
+
this.server = createServer(this.app)
|
|
25
|
+
this.WebSocketServer = new WebSocketServer({ server: this.server })
|
|
26
|
+
this.RegExp = new RegExp(`(${process.cwd()}|${process.cwd().replace(/\\/g, '/')})`, 'g')
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 监听WebSocket连接并初始化http服务器
|
|
31
|
-
*/
|
|
32
|
-
init () {
|
|
33
|
-
try {
|
|
34
|
-
this.WebSocketServer.on('connection', (socket, request) => {
|
|
35
|
-
const path = request.url
|
|
36
|
-
const headers = request.headers
|
|
37
|
-
logger.debug('[反向WS]', path, JSON.stringify(headers, null, 2))
|
|
38
|
-
try {
|
|
39
|
-
const Adapter = listener.getAdapter(path)
|
|
40
|
-
if (!Adapter) {
|
|
41
|
-
logger.error(`[反向WS] 适配器不存在:${path}`)
|
|
42
|
-
return socket.close()
|
|
43
|
-
}
|
|
44
|
-
const KarinAdapter = new Adapter()
|
|
45
|
-
if (typeof KarinAdapter?.server === 'function') {
|
|
46
|
-
KarinAdapter.server(socket, request)
|
|
47
|
-
}
|
|
48
|
-
} catch (error) {
|
|
49
|
-
logger.error(`[反向WS] 注册适配器发生错误:${path}`, error.stack || error.message || error)
|
|
50
|
-
socket.close()
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
/** GET接口 - 获取当前启动信息 */
|
|
54
|
-
this.app.get('/api/info', (req, res) => {
|
|
55
|
-
/** 只允许本机ip访问 */
|
|
56
|
-
if (req.hostname === 'localhost' || req.hostname === '127.0.0.1') {
|
|
57
|
-
const data = {
|
|
58
|
-
start: process.env.pm_id ? 'pm2' : 'node',
|
|
59
|
-
start_time: process.uptime().toFixed(2),
|
|
60
|
-
memory: (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2),
|
|
61
|
-
time: Date.now(),
|
|
62
|
-
}
|
|
63
|
-
res.json(data)
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
/** GET接口 - 退出当前进程 */
|
|
67
|
-
this.app.get('/api/exit', async (req) => {
|
|
68
|
-
/** 只允许本机ip访问 */
|
|
69
|
-
if (req.hostname === 'localhost' || req.hostname === '127.0.0.1') {
|
|
70
|
-
logger.mark('[服务器][HTTP] 收到退出请求,即将退出')
|
|
71
|
-
/** 关闭服务器 */
|
|
72
|
-
listener.emit('exit.grpc')
|
|
73
|
-
this.server.close()
|
|
74
|
-
/** 如果是pm2 获取当前pm2ID 使用 */
|
|
75
|
-
if (process.env.pm_id) { await exec(`pm2 delete ${process.env.pm_id}`) }
|
|
76
|
-
/** 正常启动的进程 */
|
|
77
|
-
process.exit()
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
/** 监听端口 */
|
|
81
|
-
const { host, port } = config.Server.http
|
|
82
|
-
this.server.listen(port, host, () => {
|
|
83
|
-
logger.mark('[服务器][启动成功][HTTP]: ' + logger.green(`http://${host}:${port}`))
|
|
84
|
-
})
|
|
85
|
-
listener.once('restart.http', () => {
|
|
86
|
-
logger.mark('[服务器][重启][HTTP] 正在重启HTTP服务器...')
|
|
87
|
-
this.#restartServer()
|
|
88
|
-
})
|
|
89
|
-
const { enable, WormholeClient } = config.Server.HttpRender
|
|
90
|
-
if (enable) {
|
|
91
|
-
this.static()
|
|
92
|
-
if (WormholeClient) {
|
|
93
|
-
connect()
|
|
94
|
-
return this
|
|
95
|
-
}
|
|
96
|
-
const { host, post, token } = config.Server.HttpRender
|
|
97
|
-
/** 注册渲染器 */
|
|
98
|
-
const rd = new HttpRenderer(host, post, token)
|
|
99
|
-
render.app({ id: 'puppeteer', type: 'image', render: rd.render.bind(rd) })
|
|
100
|
-
}
|
|
101
|
-
return this
|
|
102
|
-
} catch (error) {
|
|
103
|
-
logger.error('初始化HTTP服务器失败: ', error)
|
|
104
|
-
return false
|
|
105
|
-
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 监听WebSocket连接并初始化http服务器
|
|
31
|
+
*/
|
|
32
|
+
init () {
|
|
33
|
+
try {
|
|
34
|
+
this.WebSocketServer.on('connection', (socket, request) => {
|
|
35
|
+
const path = request.url
|
|
36
|
+
const headers = request.headers
|
|
37
|
+
logger.debug('[反向WS]', path, JSON.stringify(headers, null, 2))
|
|
38
|
+
try {
|
|
39
|
+
const Adapter = listener.getAdapter(path)
|
|
40
|
+
if (!Adapter) {
|
|
41
|
+
logger.error(`[反向WS] 适配器不存在:${path}`)
|
|
42
|
+
return socket.close()
|
|
43
|
+
}
|
|
44
|
+
const KarinAdapter = new Adapter()
|
|
45
|
+
if (typeof KarinAdapter?.server === 'function') {
|
|
46
|
+
KarinAdapter.server(socket, request)
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
logger.error(`[反向WS] 注册适配器发生错误:${path}`, error.stack || error.message || error)
|
|
50
|
+
socket.close()
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
/** GET接口 - 获取当前启动信息 */
|
|
54
|
+
this.app.get('/api/info', (req, res) => {
|
|
55
|
+
/** 只允许本机ip访问 */
|
|
56
|
+
if (req.hostname === 'localhost' || req.hostname === '127.0.0.1') {
|
|
57
|
+
const data = {
|
|
58
|
+
start: process.env.pm_id ? 'pm2' : 'node',
|
|
59
|
+
start_time: process.uptime().toFixed(2),
|
|
60
|
+
memory: (process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2),
|
|
61
|
+
time: Date.now(),
|
|
62
|
+
}
|
|
63
|
+
res.json(data)
|
|
64
|
+
}
|
|
65
|
+
})
|
|
66
|
+
/** GET接口 - 退出当前进程 */
|
|
67
|
+
this.app.get('/api/exit', async (req) => {
|
|
68
|
+
/** 只允许本机ip访问 */
|
|
69
|
+
if (req.hostname === 'localhost' || req.hostname === '127.0.0.1') {
|
|
70
|
+
logger.mark('[服务器][HTTP] 收到退出请求,即将退出')
|
|
71
|
+
/** 关闭服务器 */
|
|
72
|
+
listener.emit('exit.grpc')
|
|
73
|
+
this.server.close()
|
|
74
|
+
/** 如果是pm2 获取当前pm2ID 使用 */
|
|
75
|
+
if (process.env.pm_id) { await exec(`pm2 delete ${process.env.pm_id}`) }
|
|
76
|
+
/** 正常启动的进程 */
|
|
77
|
+
process.exit()
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
/** 监听端口 */
|
|
81
|
+
const { host, port } = config.Server.http
|
|
82
|
+
this.server.listen(port, host, () => {
|
|
83
|
+
logger.mark('[服务器][启动成功][HTTP]: ' + logger.green(`http://${host}:${port}`))
|
|
84
|
+
})
|
|
85
|
+
listener.once('restart.http', () => {
|
|
86
|
+
logger.mark('[服务器][重启][HTTP] 正在重启HTTP服务器...')
|
|
87
|
+
this.#restartServer()
|
|
88
|
+
})
|
|
89
|
+
const { enable, WormholeClient } = config.Server.HttpRender
|
|
90
|
+
if (enable) {
|
|
91
|
+
this.static()
|
|
92
|
+
if (WormholeClient) {
|
|
93
|
+
connect()
|
|
94
|
+
return this
|
|
95
|
+
}
|
|
96
|
+
const { host, post, token } = config.Server.HttpRender
|
|
97
|
+
/** 注册渲染器 */
|
|
98
|
+
const rd = new HttpRenderer(host, post, token)
|
|
99
|
+
render.app({ id: 'puppeteer', type: 'image', render: rd.render.bind(rd) })
|
|
100
|
+
}
|
|
101
|
+
return this
|
|
102
|
+
} catch (error) {
|
|
103
|
+
logger.error('初始化HTTP服务器失败: ', error)
|
|
104
|
+
return false
|
|
105
|
+
}
|
|
106
106
|
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* HTTP渲染器
|
|
110
|
-
*/
|
|
111
|
-
static () {
|
|
112
|
-
this.staticPath()
|
|
113
|
-
/** GET接口 - 渲染 */
|
|
114
|
-
this.app.get('/api/renderHtml', (req, res) => {
|
|
115
|
-
try {
|
|
116
|
-
let { html } = req.query
|
|
117
|
-
if (!html) { return res.status(404).send(JSON.stringify({ code: 404, msg: 'Not Found' })) }
|
|
118
|
-
html = decodeURIComponent(html)
|
|
119
|
-
.replace(/\\/g, '/')
|
|
120
|
-
.replace(/^\.\//, '')
|
|
121
|
-
/** 判断是否为html文件且路径存在 */
|
|
122
|
-
if (!html.endsWith('.html') || !fs.existsSync(html)) {
|
|
123
|
-
const not_found = config.Server.HttpRender.not_found
|
|
124
|
-
if (not_found.startsWith('http')) {
|
|
125
|
-
return res.redirect(not_found)
|
|
126
|
-
} else {
|
|
127
|
-
return res.status(404).send(JSON.stringify({ code: 404, msg: not_found || '?' }))
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
let content = fs.readFileSync(html, 'utf-8')
|
|
131
|
-
/** 处理所有绝对路径、相对路径 */
|
|
132
|
-
content = content.replace(this.RegExp, '')
|
|
133
|
-
res.send(content)
|
|
134
|
-
} catch (e) {
|
|
135
|
-
logger.error('[服务器][GET接口 - 渲染]', e)
|
|
136
|
-
res.status(500).send(JSON.stringify({ code: 500, msg: 'Internal Server Error' }))
|
|
137
|
-
}
|
|
138
|
-
})
|
|
139
|
-
/** 拦截静态资源 防止恶意访问 */
|
|
140
|
-
this.app.use((req, res, next) => {
|
|
141
|
-
logger.debug(`[静态资源][${req.headers.host}] ${req.url}`)
|
|
142
|
-
/** 解码 */
|
|
143
|
-
req.url = decodeURIComponent(req.url)
|
|
144
|
-
req.url = req.url
|
|
145
|
-
.replace(/\\/g, '/')
|
|
146
|
-
.replace(/^\.\//, '')
|
|
147
|
-
.replace(/^(\.\.\/)+/, '')
|
|
148
|
-
/** 拦截非资源文件 */
|
|
149
|
-
this.reg.lastIndex = 0
|
|
150
|
-
if (!this.reg.test(req.url)) {
|
|
151
|
-
logger.mark(`[${req.ip}][拦截非资源文件]`, req.url)
|
|
152
|
-
res.status(404).send(JSON.stringify({ code: 404, msg: 'Not Found' }))
|
|
153
|
-
return
|
|
154
|
-
}
|
|
155
|
-
next()
|
|
156
|
-
})
|
|
157
|
-
/** 设置静态文件目录 */
|
|
158
|
-
this.app.use(express.static(process.cwd()))
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* HTTP渲染器
|
|
110
|
+
*/
|
|
111
|
+
static () {
|
|
112
|
+
this.staticPath()
|
|
113
|
+
/** GET接口 - 渲染 */
|
|
114
|
+
this.app.get('/api/renderHtml', (req, res) => {
|
|
115
|
+
try {
|
|
116
|
+
let { html } = req.query
|
|
117
|
+
if (!html) { return res.status(404).send(JSON.stringify({ code: 404, msg: 'Not Found' })) }
|
|
118
|
+
html = decodeURIComponent(html)
|
|
119
|
+
.replace(/\\/g, '/')
|
|
120
|
+
.replace(/^\.\//, '')
|
|
121
|
+
/** 判断是否为html文件且路径存在 */
|
|
122
|
+
if (!html.endsWith('.html') || !fs.existsSync(html)) {
|
|
123
|
+
const not_found = config.Server.HttpRender.not_found
|
|
124
|
+
if (not_found.startsWith('http')) {
|
|
125
|
+
return res.redirect(not_found)
|
|
126
|
+
} else {
|
|
127
|
+
return res.status(404).send(JSON.stringify({ code: 404, msg: not_found || '?' }))
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
let content = fs.readFileSync(html, 'utf-8')
|
|
131
|
+
/** 处理所有绝对路径、相对路径 */
|
|
132
|
+
content = content.replace(this.RegExp, '')
|
|
133
|
+
res.send(content)
|
|
134
|
+
} catch (e) {
|
|
135
|
+
logger.error('[服务器][GET接口 - 渲染]', e)
|
|
136
|
+
res.status(500).send(JSON.stringify({ code: 500, msg: 'Internal Server Error' }))
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
/** 拦截静态资源 防止恶意访问 */
|
|
140
|
+
this.app.use((req, res, next) => {
|
|
141
|
+
logger.debug(`[静态资源][${req.headers.host}] ${req.url}`)
|
|
142
|
+
/** 解码 */
|
|
143
|
+
req.url = decodeURIComponent(req.url)
|
|
144
|
+
req.url = req.url
|
|
145
|
+
.replace(/\\/g, '/')
|
|
146
|
+
.replace(/^\.\//, '')
|
|
147
|
+
.replace(/^(\.\.\/)+/, '')
|
|
148
|
+
/** 拦截非资源文件 */
|
|
149
|
+
this.reg.lastIndex = 0
|
|
150
|
+
if (!this.reg.test(req.url)) {
|
|
151
|
+
logger.mark(`[${req.ip}][拦截非资源文件]`, req.url)
|
|
152
|
+
res.status(404).send(JSON.stringify({ code: 404, msg: 'Not Found' }))
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
next()
|
|
156
|
+
})
|
|
157
|
+
/** 设置静态文件目录 */
|
|
158
|
+
this.app.use(express.static(process.cwd()))
|
|
159
159
|
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 构建静态资源路径
|
|
163
|
-
*/
|
|
164
|
-
staticPath () {
|
|
165
|
-
this.list = []
|
|
166
|
-
/** 读取./resources文件夹 */
|
|
167
|
-
const resDir = './resources'
|
|
168
|
-
const resdirs = fs.readdirSync(resDir)
|
|
169
|
-
for (const dir of resdirs) {
|
|
170
|
-
const file = `${resDir}/${dir}`
|
|
171
|
-
if (common.isDir(file)) { this.list.push(file.replace('.', '')) }
|
|
172
|
-
}
|
|
173
|
-
/** 读取./temp/html下所有文件夹 */
|
|
174
|
-
const htmlDir = './temp/html'
|
|
175
|
-
const dirs = fs.readdirSync(htmlDir)
|
|
176
|
-
for (const dir of dirs) {
|
|
177
|
-
const file = `${htmlDir}/${dir}`
|
|
178
|
-
if (common.isDir(file)) { this.list.push(file.replace('.', '')) }
|
|
179
|
-
}
|
|
180
|
-
/** 读取./plugins/html下所有文件夹 */
|
|
181
|
-
const pluginsDir = './plugins'
|
|
182
|
-
const plugins = fs.readdirSync(pluginsDir)
|
|
183
|
-
for (const dir of plugins) {
|
|
184
|
-
const file = `${pluginsDir}/${dir}`
|
|
185
|
-
const resFile = `${file}/resources`
|
|
186
|
-
/** 包含resources文件夹 */
|
|
187
|
-
if (common.isDir(file) && common.isDir(resFile)) { this.list.push(resFile.replace('.', '')) }
|
|
188
|
-
const componentsFile = `${file}/components`
|
|
189
|
-
/** 包含components文件夹 兼容mys */
|
|
190
|
-
if (common.isDir(file) && common.isDir(componentsFile)) { this.list.push(componentsFile.replace('.', '')) }
|
|
191
|
-
}
|
|
192
|
-
this.reg = new RegExp(`(${this.list.join('|')})`, 'g')
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 构建静态资源路径
|
|
163
|
+
*/
|
|
164
|
+
staticPath () {
|
|
165
|
+
this.list = []
|
|
166
|
+
/** 读取./resources文件夹 */
|
|
167
|
+
const resDir = './resources'
|
|
168
|
+
const resdirs = fs.readdirSync(resDir)
|
|
169
|
+
for (const dir of resdirs) {
|
|
170
|
+
const file = `${resDir}/${dir}`
|
|
171
|
+
if (common.isDir(file)) { this.list.push(file.replace('.', '')) }
|
|
172
|
+
}
|
|
173
|
+
/** 读取./temp/html下所有文件夹 */
|
|
174
|
+
const htmlDir = './temp/html'
|
|
175
|
+
const dirs = fs.readdirSync(htmlDir)
|
|
176
|
+
for (const dir of dirs) {
|
|
177
|
+
const file = `${htmlDir}/${dir}`
|
|
178
|
+
if (common.isDir(file)) { this.list.push(file.replace('.', '')) }
|
|
179
|
+
}
|
|
180
|
+
/** 读取./plugins/html下所有文件夹 */
|
|
181
|
+
const pluginsDir = './plugins'
|
|
182
|
+
const plugins = fs.readdirSync(pluginsDir)
|
|
183
|
+
for (const dir of plugins) {
|
|
184
|
+
const file = `${pluginsDir}/${dir}`
|
|
185
|
+
const resFile = `${file}/resources`
|
|
186
|
+
/** 包含resources文件夹 */
|
|
187
|
+
if (common.isDir(file) && common.isDir(resFile)) { this.list.push(resFile.replace('.', '')) }
|
|
188
|
+
const componentsFile = `${file}/components`
|
|
189
|
+
/** 包含components文件夹 兼容mys */
|
|
190
|
+
if (common.isDir(file) && common.isDir(componentsFile)) { this.list.push(componentsFile.replace('.', '')) }
|
|
191
|
+
}
|
|
192
|
+
this.reg = new RegExp(`(${this.list.join('|')})`, 'g')
|
|
193
193
|
}
|
|
194
|
-
|
|
195
|
-
/** 重启当前HTTP服务器 */
|
|
196
|
-
async #restartServer () {
|
|
197
|
-
try {
|
|
198
|
-
/** 断开所有 WebSocket 连接 */
|
|
199
|
-
for (const ws of this.WebSocketServer.clients) { ws.terminate() }
|
|
200
|
-
/** 关闭当前HTTP服务器 */
|
|
201
|
-
this.server.close()
|
|
202
|
-
/** 延迟1秒 */
|
|
203
|
-
await common.sleep(1000)
|
|
204
|
-
/** 创建一个新的服务器实例 */
|
|
205
|
-
this.server = createServer(this.app)
|
|
206
|
-
this.WebSocketServer = new WebSocketServer({ server: this.server })
|
|
207
|
-
this.init()
|
|
208
|
-
this.static()
|
|
209
|
-
} catch (err) {
|
|
210
|
-
logger.error('[服务器][重启失败]', err)
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
})()
|
|
194
|
+
|
|
195
|
+
/** 重启当前HTTP服务器 */
|
|
196
|
+
async #restartServer () {
|
|
197
|
+
try {
|
|
198
|
+
/** 断开所有 WebSocket 连接 */
|
|
199
|
+
for (const ws of this.WebSocketServer.clients) { ws.terminate() }
|
|
200
|
+
/** 关闭当前HTTP服务器 */
|
|
201
|
+
this.server.close()
|
|
202
|
+
/** 延迟1秒 */
|
|
203
|
+
await common.sleep(1000)
|
|
204
|
+
/** 创建一个新的服务器实例 */
|
|
205
|
+
this.server = createServer(this.app)
|
|
206
|
+
this.WebSocketServer = new WebSocketServer({ server: this.server })
|
|
207
|
+
this.init()
|
|
208
|
+
this.static()
|
|
209
|
+
} catch (err) {
|
|
210
|
+
logger.error('[服务器][重启失败]', err)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
})()
|
package/lib/db/level.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { Level } from 'level';
|
|
2
|
-
/**
|
|
3
|
-
* @type {Level}
|
|
4
|
-
*/
|
|
5
|
-
declare class LevelDB extends Level {
|
|
6
|
-
id: string;
|
|
7
|
-
constructor();
|
|
8
|
-
/**
|
|
9
|
-
* 对get方法进行重写 找不到数据时返回null
|
|
10
|
-
*/
|
|
11
|
-
get(key: string): Promise<string>;
|
|
12
|
-
/**
|
|
13
|
-
* 增加set方法
|
|
14
|
-
* @param {string} key 键
|
|
15
|
-
* @param {object|string} value 值
|
|
16
|
-
*/
|
|
17
|
-
set(key: string, value: string): Promise<void>;
|
|
18
|
-
}
|
|
19
|
-
declare const level: LevelDB;
|
|
20
|
-
export default level;
|
|
1
|
+
import { Level } from 'level';
|
|
2
|
+
/**
|
|
3
|
+
* @type {Level}
|
|
4
|
+
*/
|
|
5
|
+
declare class LevelDB extends Level {
|
|
6
|
+
id: string;
|
|
7
|
+
constructor();
|
|
8
|
+
/**
|
|
9
|
+
* 对get方法进行重写 找不到数据时返回null
|
|
10
|
+
*/
|
|
11
|
+
get(key: string): Promise<string>;
|
|
12
|
+
/**
|
|
13
|
+
* 增加set方法
|
|
14
|
+
* @param {string} key 键
|
|
15
|
+
* @param {object|string} value 值
|
|
16
|
+
*/
|
|
17
|
+
set(key: string, value: string): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
declare const level: LevelDB;
|
|
20
|
+
export default level;
|
package/lib/db/level.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { Level } from 'level'
|
|
2
|
-
const path = process.cwd() + '/data/db/Level'
|
|
3
|
-
/**
|
|
4
|
-
* @type {Level}
|
|
5
|
-
*/
|
|
6
|
-
class LevelDB extends Level {
|
|
7
|
-
id
|
|
8
|
-
constructor () {
|
|
9
|
-
super(path, { valueEncoding: 'json' })
|
|
10
|
-
/**
|
|
11
|
-
* @type {'Level'} 唯一标识符 用于区分不同的数据库
|
|
12
|
-
*/
|
|
13
|
-
this.id = 'Level'
|
|
1
|
+
import { Level } from 'level'
|
|
2
|
+
const path = process.cwd() + '/data/db/Level'
|
|
3
|
+
/**
|
|
4
|
+
* @type {Level}
|
|
5
|
+
*/
|
|
6
|
+
class LevelDB extends Level {
|
|
7
|
+
id
|
|
8
|
+
constructor () {
|
|
9
|
+
super(path, { valueEncoding: 'json' })
|
|
10
|
+
/**
|
|
11
|
+
* @type {'Level'} 唯一标识符 用于区分不同的数据库
|
|
12
|
+
*/
|
|
13
|
+
this.id = 'Level'
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 对get方法进行重写 找不到数据时返回null
|
|
18
|
-
*/
|
|
19
|
-
async get (key) {
|
|
20
|
-
try {
|
|
21
|
-
const res = await super.get(key)
|
|
22
|
-
return res
|
|
23
|
-
} catch {
|
|
24
|
-
return ''
|
|
25
|
-
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 对get方法进行重写 找不到数据时返回null
|
|
18
|
+
*/
|
|
19
|
+
async get (key) {
|
|
20
|
+
try {
|
|
21
|
+
const res = await super.get(key)
|
|
22
|
+
return res
|
|
23
|
+
} catch {
|
|
24
|
+
return ''
|
|
25
|
+
}
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 增加set方法
|
|
30
|
-
* @param {string} key 键
|
|
31
|
-
* @param {object|string} value 值
|
|
32
|
-
*/
|
|
33
|
-
async set (key, value) {
|
|
34
|
-
return await super.put(key, value)
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const level = new LevelDB()
|
|
38
|
-
export default level
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 增加set方法
|
|
30
|
+
* @param {string} key 键
|
|
31
|
+
* @param {object|string} value 值
|
|
32
|
+
*/
|
|
33
|
+
async set (key, value) {
|
|
34
|
+
return await super.put(key, value)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const level = new LevelDB()
|
|
38
|
+
export default level
|