update
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { ipBlacklistService } from '../services/ip-blacklist.service';
|
||||
import { settingsService } from '../services/settings.service'; // <-- Import settingsService
|
||||
|
||||
/**
|
||||
* IP 黑名单检查中间件
|
||||
@@ -17,6 +18,13 @@ export const ipBlacklistCheckMiddleware = async (req: Request, res: Response, ne
|
||||
}
|
||||
|
||||
try {
|
||||
// 首先检查 IP 黑名单功能是否启用
|
||||
const isEnabled = await settingsService.isIpBlacklistEnabled();
|
||||
if (!isEnabled) {
|
||||
// console.log('[IP Blacklist Check] 功能已禁用,跳过检查。');
|
||||
return next(); // 功能禁用,直接放行
|
||||
}
|
||||
|
||||
const isBlocked = await ipBlacklistService.isBlocked(clientIp);
|
||||
if (isBlocked) {
|
||||
console.warn(`[IP Blacklist Check] 已阻止来自被封禁 IP ${clientIp} 的访问。`);
|
||||
|
||||
@@ -51,10 +51,16 @@ export class IpBlacklistService {
|
||||
* @param ip IP 地址
|
||||
* @returns 如果被封禁则返回 true,否则返回 false
|
||||
*/
|
||||
async isBlocked(ip: string): Promise<boolean> {
|
||||
try {
|
||||
const entry = await this.getEntry(ip);
|
||||
if (!entry) {
|
||||
async isBlocked(ip: string): Promise<boolean> {
|
||||
// 首先检查功能是否启用
|
||||
if (!(await settingsService.isIpBlacklistEnabled())) {
|
||||
// console.log('[IP Blacklist] 功能已禁用,跳过 isBlocked 检查。');
|
||||
return false; // 如果禁用,则认为 IP 未被阻止
|
||||
}
|
||||
|
||||
try {
|
||||
const entry = await this.getEntry(ip);
|
||||
if (!entry) {
|
||||
return false; // 不在黑名单中
|
||||
}
|
||||
// 检查封禁时间是否已过
|
||||
@@ -75,6 +81,12 @@ export class IpBlacklistService {
|
||||
* @param ip IP 地址
|
||||
*/
|
||||
async recordFailedAttempt(ip: string): Promise<void> {
|
||||
// 首先检查功能是否启用
|
||||
if (!(await settingsService.isIpBlacklistEnabled())) {
|
||||
// console.log('[IP Blacklist] 功能已禁用,跳过 recordFailedAttempt。');
|
||||
return; // 如果禁用,则不记录失败尝试
|
||||
}
|
||||
|
||||
if (LOCAL_IPS.includes(ip)) {
|
||||
console.log(`[IP Blacklist] 检测到本地 IP ${ip} 登录失败,跳过黑名单处理。`);
|
||||
return;
|
||||
|
||||
@@ -30,6 +30,7 @@ const LAYOUT_TREE_KEY = 'layoutTree'; // 布局树设置键
|
||||
const AUTO_COPY_ON_SELECT_KEY = 'autoCopyOnSelect'; // 终端选中自动复制设置键
|
||||
const STATUS_MONITOR_INTERVAL_SECONDS_KEY = 'statusMonitorIntervalSeconds'; // 状态监控间隔设置键
|
||||
const DEFAULT_STATUS_MONITOR_INTERVAL_SECONDS = 3; // 默认状态监控间隔
|
||||
const IP_BLACKLIST_ENABLED_KEY = 'ipBlacklistEnabled'; // IP 黑名单启用设置键
|
||||
|
||||
export const settingsService = {
|
||||
/**
|
||||
@@ -108,6 +109,24 @@ export const settingsService = {
|
||||
]);
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查 IP 黑名单功能是否已启用
|
||||
* @returns 返回是否启用 (boolean),如果未设置则默认为 true
|
||||
*/
|
||||
async isIpBlacklistEnabled(): Promise<boolean> {
|
||||
console.log(`[Service] Attempting to get setting for key: ${IP_BLACKLIST_ENABLED_KEY}`);
|
||||
try {
|
||||
const enabledStr = await settingsRepository.getSetting(IP_BLACKLIST_ENABLED_KEY);
|
||||
console.log(`[Service] Raw value from repository for ${IP_BLACKLIST_ENABLED_KEY}:`, enabledStr);
|
||||
// 如果设置存在且值为 'false',则返回 false,否则都返回 true (包括未设置的情况)
|
||||
return enabledStr !== 'false';
|
||||
} catch (error) {
|
||||
console.error(`[Service] Error getting IP blacklist enabled setting (key: ${IP_BLACKLIST_ENABLED_KEY}):`, error);
|
||||
// 出错时返回默认值 true (安全起见,默认启用)
|
||||
return true;
|
||||
}
|
||||
}, // *** 确保这里有逗号 ***
|
||||
|
||||
/**
|
||||
* 获取焦点切换顺序
|
||||
* @returns 返回存储的完整焦点切换配置对象,如果未设置或无效则返回默认空配置
|
||||
|
||||
@@ -46,7 +46,8 @@ export const settingsController = {
|
||||
'commandInputSyncTarget', // +++ 添加命令输入同步目标键 +++
|
||||
'timezone', // NEW: 添加时区键
|
||||
'rdpModalWidth', // NEW: 添加 RDP 模态框宽度键
|
||||
'rdpModalHeight' // NEW: 添加 RDP 模态框高度键
|
||||
'rdpModalHeight', // NEW: 添加 RDP 模态框高度键
|
||||
'ipBlacklistEnabled' // <-- 添加 IP 黑名单启用键
|
||||
];
|
||||
const filteredSettings: Record<string, string> = {};
|
||||
for (const key in settingsToUpdate) {
|
||||
|
||||
Reference in New Issue
Block a user