define no_proxy cases in hostMatchesNoProxyList

This commit is contained in:
Malak El Kouri 2025-01-09 12:11:12 +01:00
parent ef510d9e9a
commit 2fa886109b
1 changed files with 22 additions and 12 deletions

View File

@ -17,7 +17,7 @@
import { log } from './logging'; import { log } from './logging';
import { LogVerbosity } from './constants'; import { LogVerbosity } from './constants';
import { Socket } from 'net'; import { isIPv4, Socket } from 'net';
import * as http from 'http'; import * as http from 'http';
import * as logging from './logging'; import * as logging from './logging';
import { import {
@ -129,17 +129,20 @@ interface CIDRNotation {
* 1. ip * 1. ip
* 2. prefixLength * 2. prefixLength
*/ */
const CIDR_REGEX = /^([0-9.]+)(?:\/([0-9]+))?$/;
export function parseCIDR(cidrString: string): CIDRNotation | null { export function parseCIDR(cidrString: string): CIDRNotation | null {
const parsedCIDR = CIDR_REGEX.exec(cidrString); const splitRange = cidrString.split('/');
if (parsedCIDR && parsedCIDR.length === 3) { if (splitRange.length !== 2) {
return { return null;
ip: ipToInt(parsedCIDR[1]),
prefixLength: parseInt(parsedCIDR[2] ?? '32', 10),
};
} }
return null; const prefixLength = parseInt(splitRange[1], 10);
if (!isIPv4(splitRange[0]) || Number.isNaN(prefixLength) || prefixLength < 0 || prefixLength > 32) {
return null;
}
return {
ip: ipToInt(splitRange[0]),
prefixLength: prefixLength
};
} }
function ipToInt(ip: string) { function ipToInt(ip: string) {
@ -157,8 +160,15 @@ function isIpInCIDR(cidr: CIDRNotation, serverHost: string) {
function hostMatchesNoProxyList(serverHost: string): boolean { function hostMatchesNoProxyList(serverHost: string): boolean {
for (const host of getNoProxyHostList()) { for (const host of getNoProxyHostList()) {
const parsedCIDR = parseCIDR(host); const parsedCIDR = parseCIDR(host);
// host is a single IP address or a CIDR notation or a domain // host is a single IP or a domain name suffix
if (parsedCIDR && isIpInCIDR(parsedCIDR, serverHost) || serverHost.endsWith(host)) { if (!parsedCIDR) {
if (host === serverHost || serverHost.includes(host)) {
trace('Not using proxy for target in no_proxy list: ' + serverHost);
return true;
}
}
// host is a CIDR or a domain
else if (isIpInCIDR(parsedCIDR, serverHost)) {
trace('Not using proxy for target in no_proxy list: ' + serverHost); trace('Not using proxy for target in no_proxy list: ' + serverHost);
return true; return true;
} }