mirror of
https://github.com/vastxie/99AI.git
synced 2025-11-12 11:43:42 +08:00
NineAI 2.4.2
This commit is contained in:
43
dist/common/utils/getClientIp.js
vendored
Normal file
43
dist/common/utils/getClientIp.js
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.getClientIp = void 0;
|
||||
function getClientIp(request) {
|
||||
let ipAddress = '';
|
||||
const headerList = [
|
||||
'X-Client-IP',
|
||||
'X-Real-IP',
|
||||
'X-Forwarded-For',
|
||||
'CF-Connecting-IP',
|
||||
'True-Client-IP',
|
||||
'X-Cluster-Client-IP',
|
||||
'Proxy-Client-IP',
|
||||
'WL-Proxy-Client-IP',
|
||||
'HTTP_CLIENT_IP',
|
||||
'HTTP_X_FORWARDED_FOR',
|
||||
];
|
||||
for (const header of headerList) {
|
||||
const value = request.headers[header];
|
||||
if (value && typeof value === 'string') {
|
||||
const ips = value.split(',');
|
||||
ipAddress = ips[0].trim();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ipAddress) {
|
||||
ipAddress = request.connection.remoteAddress || '';
|
||||
}
|
||||
if (ipAddress && ipAddress.includes('::')) {
|
||||
const isLocal = /^(::1|fe80(:1)?::1(%.*)?)$/i.test(ipAddress);
|
||||
if (isLocal) {
|
||||
ipAddress = '';
|
||||
}
|
||||
else if (ipAddress.includes('::ffff:')) {
|
||||
ipAddress = ipAddress.split(':').pop() || '';
|
||||
}
|
||||
}
|
||||
if (!ipAddress || !/\d+\.\d+\.\d+\.\d+/.test(ipAddress)) {
|
||||
ipAddress = '';
|
||||
}
|
||||
return ipAddress;
|
||||
}
|
||||
exports.getClientIp = getClientIp;
|
||||
Reference in New Issue
Block a user