feat: 添加导出连接功能

This commit is contained in:
Baobhan Sith
2025-05-10 16:35:20 +08:00
parent 4b9d086ae6
commit b702eb3b88
11 changed files with 379 additions and 29 deletions
@@ -3,6 +3,7 @@ import { settingsService } from '../services/settings.service';
import { AuditLogService } from '../services/audit.service';
import { NotificationService } from '../services/notification.service'; // 添加导入
import { ipBlacklistService } from '../services/ip-blacklist.service';
import { exportConnectionsAsEncryptedZip } from '../services/import-export.service'; // Import the new export service
import { UpdateSidebarConfigDto, UpdateCaptchaSettingsDto, CaptchaSettings } from '../types/settings.types'; // <-- Import CAPTCHA types
import i18next from '../i18n'; // +++ Import i18next +++
@@ -502,6 +503,32 @@ async setCaptchaConfig(req: Request, res: Response): Promise<void> {
console.error('[控制器] 设置“显示快捷指令标签”时出错:', error);
res.status(500).json({ message: '设置“显示快捷指令标签”失败', error: error.message });
}
} // <-- No comma after the last method
}, // <-- Add comma here for the new method
/**
* 导出所有连接配置为加密的 ZIP 文件
*/
async exportAllConnections(req: Request, res: Response): Promise<void> {
try {
console.log('[控制器] 收到导出所有连接的请求。');
const encryptedZipBuffer = await exportConnectionsAsEncryptedZip();
res.setHeader('Content-Type', 'application/zip');
res.setHeader('Content-Disposition', 'attachment; filename="nexus_connections_export.zip"');
res.send(encryptedZipBuffer);
// auditLogService.logAction('CONNECTIONS_EXPORTED', { userId: (req.user as any)?.id || 'unknown' }); // 移除审计日志
console.log('[控制器] 成功发送加密的连接导出文件。');
} catch (error: any) {
console.error('[控制器] 导出所有连接时出错:', error);
// 检查是否是因为 ENCRYPTION_KEY 未设置导致的错误
if (error.message && (error.message.includes('ENCRYPTION_KEY is not set') || error.message.includes('Failed to decode ENCRYPTION_KEY') || error.message.includes('Invalid ENCRYPTION_KEY length'))) {
res.status(500).json({ message: i18next.t('error.exportFailedEncryptionKey'), error: error.message });
} else {
res.status(500).json({ message: i18next.t('error.exportFailedGeneric'), error: error.message });
}
}
} // <-- No comma after the last method if it's truly the last one
};
@@ -65,9 +65,17 @@ router.get('/show-quick-command-tags', settingsController.getShowQuickCommandTag
// PUT /api/v1/settings/show-quick-command-tags - 更新设置
router.put('/show-quick-command-tags', settingsController.setShowQuickCommandTags);
// +++ 新增:导出所有连接路由 +++
// GET /api/v1/settings/export-connections - 导出所有连接为加密的 ZIP 文件
router.get('/export-connections', settingsController.exportAllConnections);
export default router;
// +++ 新增:CAPTCHA 配置路由 (需要认证更新) +++
// PUT /api/v1/settings/captcha - 更新 CAPTCHA 配置
// 注意:这个路由定义在 `export default router` 之后,这是不正确的。
// 我会将它移到 `export default router` 之前,并确保它也在 `isAuthenticated` 中间件的作用域内。
// 然而,既然它已经存在,并且在 `isAuthenticated` 之后(通过 router.use(isAuthenticated)),
// 我们只需要确保导出路由也在正确的位置。
router.put('/captcha', settingsController.setCaptchaConfig);