mirror of
https://github.com/umami-software/umami.git
synced 2026-02-18 03:25:40 +01:00
fix: Optimize IP detection logic and add error handling
- #4038 - Check existence of clientIp and ignoreIps in advance - Add error handling for CIDR parsing
This commit is contained in:
parent
09bc2ee5c8
commit
9a66a2a461
1 changed files with 16 additions and 16 deletions
|
|
@ -140,31 +140,31 @@ export async function getClientInfo(request: Request, payload: Record<string, an
|
|||
export function hasBlockedIp(clientIp: string) {
|
||||
const ignoreIps = process.env.IGNORE_IP;
|
||||
|
||||
if (ignoreIps) {
|
||||
const ips = [];
|
||||
if (!clientIp || !ignoreIps) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ignoreIps) {
|
||||
ips.push(...ignoreIps.split(',').map(n => n.trim()));
|
||||
const ips = ignoreIps.split(',').map(n => n.trim());
|
||||
|
||||
return ips.find(ip => {
|
||||
if (ip === clientIp) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ips.find(ip => {
|
||||
if (ip === clientIp) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// CIDR notation
|
||||
if (ip.indexOf('/') > 0) {
|
||||
// CIDR notation
|
||||
if (ip.indexOf('/') > 0) {
|
||||
try {
|
||||
const addr = ipaddr.parse(clientIp);
|
||||
const range = ipaddr.parseCIDR(ip);
|
||||
|
||||
if (addr.kind() === range[0].kind() && addr.match(range)) {
|
||||
return true;
|
||||
}
|
||||
} catch {
|
||||
// Ignore parsing errors
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue