mirror of
https://github.com/vastxie/99AI.git
synced 2026-01-17 12:26:01 +08:00
v4.1.0
This commit is contained in:
22
AIWebQuickDeploy/dist/common/utils/getClientIp.js
vendored
Normal file
22
AIWebQuickDeploy/dist/common/utils/getClientIp.js
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getClientIp = void 0;
|
||||
function getFirstValidIp(ipString) {
|
||||
const ips = ipString.split(',').map(ip => ip.trim());
|
||||
return ips.find(ip => isValidIp(ip)) || '';
|
||||
}
|
||||
function isValidIp(ip) {
|
||||
return /^\d{1,3}(\.\d{1,3}){3}$/.test(ip) || /^::ffff:\d{1,3}(\.\d{1,3}){3}$/.test(ip);
|
||||
}
|
||||
function getClientIp(req) {
|
||||
const forwardedFor = req.header('x-forwarded-for');
|
||||
let clientIp = forwardedFor ? getFirstValidIp(forwardedFor) : '';
|
||||
if (!clientIp) {
|
||||
clientIp = req.connection.remoteAddress || req.socket.remoteAddress || '';
|
||||
}
|
||||
if (clientIp.startsWith('::ffff:')) {
|
||||
clientIp = clientIp.substring(7);
|
||||
}
|
||||
return clientIp;
|
||||
}
|
||||
exports.getClientIp = getClientIp;
|
||||
Reference in New Issue
Block a user