| import { internalApiWhitelist } from '../config.default.js';
|
|
|
| |
| |
| |
| |
| |
|
|
| export function ipWhitelistMiddleware(req, res, next) {
|
| const clientIp = req.ip || req.connection.remoteAddress;
|
|
|
|
|
| const isWhitelisted = internalApiWhitelist.some(whitelistedIp => {
|
| if (whitelistedIp.includes('/')) {
|
|
|
| const [network] = whitelistedIp.split('/');
|
| const networkPrefix = network.split('.').slice(0, 3).join('.');
|
| const clientPrefix = clientIp.split('.').slice(0, 3).join('.');
|
| return networkPrefix === clientPrefix;
|
| }
|
|
|
| return clientIp === whitelistedIp;
|
| });
|
|
|
| if (!isWhitelisted) {
|
| console.warn(`未经授权的访问 IP: ${clientIp}`);
|
| return res.status(403).json({
|
| error: 'Access denied: Your IP is not whitelisted'
|
| });
|
| }
|
|
|
| next();
|
| } |