Delete ip-whitelist.js
Browse files- ip-whitelist.js +0 -33
ip-whitelist.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
| 1 |
-
import { internalApiWhitelist } from '../config.default.js';
|
| 2 |
-
|
| 3 |
-
/**
|
| 4 |
-
* 检查IP是否在白名单中的中间件
|
| 5 |
-
* @param {import('express').Request} req
|
| 6 |
-
* @param {import('express').Response} res
|
| 7 |
-
* @param {import('express').NextFunction} next
|
| 8 |
-
*/
|
| 9 |
-
export function ipWhitelistMiddleware(req, res, next) {
|
| 10 |
-
const clientIp = req.ip || req.connection.remoteAddress;
|
| 11 |
-
|
| 12 |
-
// 检查IP是否在白名单中
|
| 13 |
-
const isWhitelisted = internalApiWhitelist.some(whitelistedIp => {
|
| 14 |
-
if (whitelistedIp.includes('/')) {
|
| 15 |
-
// 简单的CIDR检查 (仅支持 /24 格式)
|
| 16 |
-
const [network] = whitelistedIp.split('/');
|
| 17 |
-
const networkPrefix = network.split('.').slice(0, 3).join('.');
|
| 18 |
-
const clientPrefix = clientIp.split('.').slice(0, 3).join('.');
|
| 19 |
-
return networkPrefix === clientPrefix;
|
| 20 |
-
}
|
| 21 |
-
// 精确IP匹配
|
| 22 |
-
return clientIp === whitelistedIp;
|
| 23 |
-
});
|
| 24 |
-
|
| 25 |
-
if (!isWhitelisted) {
|
| 26 |
-
console.warn(`未经授权的访问 IP: ${clientIp}`);
|
| 27 |
-
return res.status(403).json({
|
| 28 |
-
error: 'Access denied: Your IP is not whitelisted'
|
| 29 |
-
});
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
next();
|
| 33 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|