17
17
18
18
import { log } from './logging' ;
19
19
import { LogVerbosity } from './constants' ;
20
- import { Socket } from 'net' ;
20
+ import { isIPv4 , Socket } from 'net' ;
21
21
import * as http from 'http' ;
22
22
import * as logging from './logging' ;
23
23
import {
@@ -129,17 +129,20 @@ interface CIDRNotation {
129
129
* 1. ip
130
130
* 2. prefixLength
131
131
*/
132
- const CIDR_REGEX = / ^ ( [ 0 - 9 . ] + ) (?: \/ ( [ 0 - 9 ] + ) ) ? $ / ;
133
132
134
133
export function parseCIDR ( cidrString : string ) : CIDRNotation | null {
135
- const parsedCIDR = CIDR_REGEX . exec ( cidrString ) ;
136
- if ( parsedCIDR && parsedCIDR . length === 3 ) {
137
- return {
138
- ip : ipToInt ( parsedCIDR [ 1 ] ) ,
139
- prefixLength : parseInt ( parsedCIDR [ 2 ] ?? '32' , 10 ) ,
140
- } ;
141
- }
142
- return null ;
134
+ const splitRange = cidrString . split ( '/' ) ;
135
+ if ( splitRange . length !== 2 ) {
136
+ return null ;
137
+ }
138
+ const prefixLength = parseInt ( splitRange [ 1 ] , 10 ) ;
139
+ if ( ! isIPv4 ( splitRange [ 0 ] ) || Number . isNaN ( prefixLength ) || prefixLength < 0 || prefixLength > 32 ) {
140
+ return null ;
141
+ }
142
+ return {
143
+ ip : ipToInt ( splitRange [ 0 ] ) ,
144
+ prefixLength : prefixLength
145
+ } ;
143
146
}
144
147
145
148
function ipToInt ( ip : string ) {
@@ -157,8 +160,15 @@ function isIpInCIDR(cidr: CIDRNotation, serverHost: string) {
157
160
function hostMatchesNoProxyList ( serverHost : string ) : boolean {
158
161
for ( const host of getNoProxyHostList ( ) ) {
159
162
const parsedCIDR = parseCIDR ( host ) ;
160
- // host is a single IP address or a CIDR notation or a domain
161
- if ( parsedCIDR && isIpInCIDR ( parsedCIDR , serverHost ) || serverHost . endsWith ( host ) ) {
163
+ // host is a single IP or a domain name suffix
164
+ if ( ! parsedCIDR ) {
165
+ if ( host === serverHost || serverHost . includes ( host ) ) {
166
+ trace ( 'Not using proxy for target in no_proxy list: ' + serverHost ) ;
167
+ return true ;
168
+ }
169
+ }
170
+ // host is a CIDR or a domain
171
+ else if ( isIpInCIDR ( parsedCIDR , serverHost ) ) {
162
172
trace ( 'Not using proxy for target in no_proxy list: ' + serverHost ) ;
163
173
return true ;
164
174
}
0 commit comments