-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathadmin.js
More file actions
286 lines (247 loc) · 9.3 KB
/
admin.js
File metadata and controls
286 lines (247 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Admin Controller - 后台管理主控制器
* 重构版:移除 MCP 依赖,直接实现业务逻辑
*/
import path from 'path';
import fs from '../utils/fsWrapper.js';
import { validateBasicAuth } from '../utils/api_validate.js';
// 导入子控制器
import * as systemController from './admin/systemController.js';
import * as logsController from './admin/logsController.js';
import * as sourcesController from './admin/sourcesController.js';
import * as filesController from './admin/filesController.js';
import * as dbController from './admin/dbController.js';
import * as subController from './admin/subController.js';
import * as backupController from './admin/backupController.js';
// 配置常量
const CONFIG_PATH = path.join(process.cwd(), 'config/env.json');
const FULL_ENV_TEMPLATE = {
"ali_token": "",
"ali_refresh_token": "",
"quark_cookie": "",
"quark_token_cookie": "",
"uc_cookie": "",
"uc_token_cookie": "",
"baidu_cookie": "",
"xun_username": "",
"xun_password": "",
"cloud_account": "",
"cloud_password": "",
"cloud_cookie": "",
"yun_account": "",
"yun_cookie": "",
"pan_passport": "",
"pan_password": "",
"pan_auth": "",
"pikpak_token": "",
"now_ai": "1",
"spark_ai_authKey": "",
"deepseek_apiKey": "",
"kimi_apiKey": "",
"sparkBotObject": {},
"thread": "6",
"api_pwd": "",
"hide_adult": "1",
"enable_old_config": "0",
"show_curl": "0",
"show_req": "0",
"enable_rule_name": "0",
"enable_dr2": "1",
"enable_py": "1",
"enable_php": "1",
"enable_cat": "1",
"enable_self_jx": "0",
"enable_system_proxy": "1",
"play_proxy_mode": "1",
"play_local_proxy_type": "1",
"PROXY_AUTH": "drpys",
"enable_doh": "0",
"allow_forward": "0",
"allow_ftp_cache_clear": "0",
"allow_webdav_cache_clear": "0",
"link_url": "",
"enable_link_data": "0",
"enable_link_push": "0",
"enable_link_jar": "0",
"cat_sub_code": "all",
"must_sub_code": "0",
"bili_cookie": "",
"mg_hz": "4"
};
// 导出路由配置
export default async function adminController(fastify, options) {
// 注册 Basic Auth 验证钩子
fastify.addHook('preHandler', (request, reply, done) => {
// 只对 /api/admin/* 接口进行验证
if (request.url.startsWith('/api/admin')) {
validateBasicAuth(request, reply, done);
} else {
done();
}
});
// ==================== 系统管理 API ====================
fastify.get('/api/admin/health', systemController.getHealth);
fastify.post('/api/admin/restart', systemController.restartService);
// ==================== 日志 API ====================
fastify.get('/api/admin/logs', logsController.getLogs);
// ==================== 配置管理 API ====================
fastify.get('/api/admin/config', getConfig);
fastify.post('/api/admin/config', updateConfig);
fastify.get('/api/admin/env', getEnv);
fastify.get('/api/admin/version', getVersion);
// ==================== 源管理 API ====================
fastify.get('/api/admin/sources', sourcesController.listSources);
fastify.post('/api/admin/sources/validate', sourcesController.validateSpider);
fastify.post('/api/admin/sources/syntax', sourcesController.checkSyntax);
fastify.get('/api/admin/sources/template', sourcesController.getTemplate);
fastify.get('/api/admin/sources/libs', sourcesController.getLibsInfo);
// ==================== 文件管理 API ====================
fastify.get('/api/admin/files/list', filesController.listDirectory);
fastify.get('/api/admin/files/read', filesController.readFile);
fastify.post('/api/admin/files/write', filesController.writeFile);
fastify.delete('/api/admin/files/delete', filesController.deleteFile);
// ==================== 数据库 API ====================
fastify.post('/api/admin/db/query', dbController.executeQuery);
fastify.get('/api/admin/db/tables', dbController.getTables);
fastify.get('/api/admin/db/tables/:table/schema', dbController.getTableSchema);
// ==================== Sub文件管理 API ====================
fastify.get('/api/admin/sub/files', subController.getSubFiles);
fastify.get('/api/admin/sub/file', subController.getSubFileContent);
fastify.post('/api/admin/sub/file', subController.saveSubFileContent);
// ==================== 备份恢复 API ====================
fastify.get('/api/admin/backup/config', backupController.getBackupConfig);
fastify.post('/api/admin/backup/config', backupController.updateBackupConfig);
fastify.post('/api/admin/backup/config/reset', backupController.resetBackupConfig);
fastify.post('/api/admin/backup/create', backupController.createBackup);
fastify.post('/api/admin/backup/restore', backupController.restoreBackup);
// ==================== 路由信息 API ====================
fastify.get('/api/admin/routes', getRoutesInfo);
fastify.get('/api/admin/docs', systemController.getApiDocs);
// MCP 兼容层
const ENABLE_MCP_COMPAT = process.env.ENABLE_MCP_COMPAT !== 'false';
if (ENABLE_MCP_COMPAT) {
fastify.post('/admin/mcp', async (req, reply) => {
const { name, arguments: args } = req.body;
try {
// 仅作最低限度的兼容,或者提示用户升级
return reply.code(400).send({ error: 'MCP API 已弃用,请更新 drpy-node-admin 到最新版本' });
} catch (e) {
return reply.code(500).send({ error: e.message });
}
});
}
}
// ==================== 辅助函数 ====================
async function getConfig(req, reply) {
try {
const { key } = req.query;
let config = {};
if (await fs.pathExists(CONFIG_PATH)) {
const configContent = await fs.readFile(CONFIG_PATH, 'utf-8');
try {
config = JSON.parse(configContent);
} catch (e) {
// ignore parse error
}
}
// Merge with template to ensure all keys exist
config = { ...FULL_ENV_TEMPLATE, ...config };
if (key) {
const keys = key.split('.');
let value = config;
for (const k of keys) {
value = value?.[k];
}
return reply.send(value !== undefined ? value : null);
}
return reply.send(config);
} catch (e) {
reply.code(500).send({ error: e.message });
}
}
async function updateConfig(req, reply) {
try {
const { key, value } = req.body;
if (!key) {
return reply.code(400).send({ error: 'Key is required' });
}
if (!await fs.pathExists(CONFIG_PATH)) {
return reply.code(404).send({ error: 'Config file not found' });
}
const configContent = await fs.readFile(CONFIG_PATH, 'utf-8');
let config = JSON.parse(configContent);
// 设置嵌套值
const keys = key.split('.');
let target = config;
for (let i = 0; i < keys.length - 1; i++) {
if (!target[keys[i]]) {
target[keys[i]] = {};
}
target = target[keys[i]];
}
// 尝试解析为 JSON
let parsedValue = value;
try {
parsedValue = JSON.parse(value);
} catch {
// 保持字符串
}
target[keys[keys.length - 1]] = parsedValue;
// 写回文件
await fs.writeFile(CONFIG_PATH, JSON.stringify(config, null, 2));
return reply.send({
success: true,
message: `配置项 ${key} 已更新`
});
} catch (e) {
reply.code(500).send({ error: e.message });
}
}
async function getEnv(req, reply) {
try {
const envData = {};
// 从 process.env 读取关键配置
const keys = [
'PORT', 'NODE_ENV', 'MAX_TEXT_SIZE', 'MAX_IMAGE_SIZE',
'QUARK_COOKIE', 'ALI_TOKEN', 'bili_cookie'
];
for (const key of keys) {
if (process.env[key]) {
envData[key] = process.env[key];
}
}
return reply.send(envData);
} catch (e) {
reply.code(500).send({ error: e.message });
}
}
async function getVersion(req, reply) {
try {
const packageJson = await fs.readJson(path.join(process.cwd(), 'package.json'));
return reply.send({ version: packageJson.version });
} catch (e) {
reply.code(500).send({ error: e.message });
}
}
async function getRoutesInfo(req, reply) {
try {
const indexControllerPath = path.join(process.cwd(), 'controllers/index.js');
if (!await fs.pathExists(indexControllerPath)) {
return reply.send({
file: 'controllers/index.js',
registered_controllers: []
});
}
const content = await fs.readFile(indexControllerPath, 'utf-8');
const lines = content.split('\n');
const registered = lines
.filter(l => l.trim().startsWith('fastify.register('))
.map(l => l.trim());
return reply.send({
file: 'controllers/index.js',
registered_controllers: registered
});
} catch (e) {
reply.code(500).send({ error: e.message });
}
}