chenpei 383772d39f 🛡️ 服务器安全加固和FRP问题修复
主要更新:
1. FRP服务器诊断与修复
   - 发现并修复文件描述符耗尽问题(65,536 → 1,048,576)
   - 分析并解决用户上传失败问题
   - 确认SSH服务器安全未被攻破

2. 安全防护系统部署
   - 部署IP黑名单防火墙(封禁20+攻击者)
   - 设置SSH端口速率限制
   - 配置自动化定时任务(每小时更新)
   - 安装Fail2ban实时防护系统
   - 添加白名单保护机制

3. 内网访问优化
   - 改进内网检测逻辑
   - 添加网络状态API
   - 优化钉钉登录流程
   - 完善信息补充功能

4. 文档完善
   - FRP服务器诊断修复报告
   - SSH安全评估报告
   - Fail2ban使用指南
   - 文件描述符详解
   - 用户上传失败原因分析
   - 防火墙配置说明
   - 内网检测改进报告

修复的问题:
- ✅ 文件描述符耗尽导致的服务异常
- ✅ 用户上传失败(影响9.5小时)
- ✅ SSH暴力破解攻击(91,299+次)
- ✅ 第三方项目攻击影响
- ✅ 127.0.0.1误封禁问题

部署的安全措施:
- 三重防护体系(Fail2ban + 防火墙 + 强密码)
- 自动化监控和封禁
- 实时攻击拦截(秒级响应)
- 完整的管理和监控工具

测试环境:开发环境
影响范围:服务器安全、用户上传、系统稳定性
当前状态:✅ 所有问题已修复,系统运行正常
2025-11-06 13:42:36 +08:00

106 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server'
/**
* 判断IP是否为内网地址
*/
function isIntranetIP(ip: string): boolean {
if (!ip) return false
// 处理可能存在的端口号
const cleanIP = ip.split(':')[0]
// localhost
if (cleanIP === '127.0.0.1' || cleanIP === 'localhost' || cleanIP === '::1') {
return true
}
// IPv4 内网IP段判断
const ipv4Regex = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/
const match = cleanIP.match(ipv4Regex)
if (match) {
const [, a, b, c, d] = match.map(Number)
// 验证IP有效性
if (a > 255 || b > 255 || c > 255 || d > 255) {
return false
}
// 10.0.0.0 - 10.255.255.255 (Class A)
if (a === 10) return true
// 172.16.0.0 - 172.31.255.255 (Class B)
if (a === 172 && b >= 16 && b <= 31) return true
// 192.168.0.0 - 192.168.255.255 (Class C)
if (a === 192 && b === 168) return true
// 169.254.0.0 - 169.254.255.255 (Link-local)
if (a === 169 && b === 254) return true
}
return false
}
/**
* 获取客户端真实IP地址
*/
function getClientIP(request: NextRequest): string {
// 优先级:x-real-ip > x-forwarded-for > connection
const xRealIP = request.headers.get('x-real-ip')
if (xRealIP) return xRealIP
const xForwardedFor = request.headers.get('x-forwarded-for')
if (xForwardedFor) {
// x-forwarded-for 可能包含多个IP,取第一个(客户端真实IP)
return xForwardedFor.split(',')[0].trim()
}
const cfConnectingIP = request.headers.get('cf-connecting-ip') // Cloudflare
if (cfConnectingIP) return cfConnectingIP
const trueClientIP = request.headers.get('true-client-ip') // Akamai
if (trueClientIP) return trueClientIP
// 如果都没有,返回默认值
return 'unknown'
}
/**
* GET /api/network-status
* 返回用户的网络环境状态
*/
export async function GET(request: NextRequest) {
try {
const clientIP = getClientIP(request)
const isIntranet = isIntranetIP(clientIP)
// 获取当前访问的域名
const hostname = request.headers.get('host') || ''
const isPublicDomain = hostname.includes('yydd.ai') || hostname.includes('3d.yydd.ai')
console.log(`[Network Status] IP: ${clientIP}, isIntranet: ${isIntranet}, hostname: ${hostname}`)
return NextResponse.json({
success: true,
isIntranet,
isPublicDomain,
// 不返回完整IP地址(隐私保护)
ipMasked: clientIP.split('.').slice(0, 2).join('.') + '.xxx.xxx',
message: isIntranet
? '检测到内网访问'
: '检测到公网访问',
intranetUrl: isIntranet && isPublicDomain
? 'http://192.168.58.20:4320'
: null
})
} catch (error) {
console.error('[Network Status] Error:', error)
return NextResponse.json({
success: false,
error: '网络状态检测失败'
}, { status: 500 })
}
}