@@ -71,11 +71,11 @@ private static string ParseCookie(string cookieString, string cookieName)
7171 {
7272 if ( cookie . Contains ( cookieName ) && ! cookie . Contains ( "_sncid=" ) )
7373 {
74- return cookie . Replace ( cookieName + "=" , "" ) ;
74+ return cookie . Replace ( cookieName + "=" , "" ) . Trim ( ) ;
7575 }
7676 }
7777
78- return "" ;
78+ return string . Empty ;
7979 }
8080
8181 public static string GetClientIpFromRequest ( HttpWebRequest request , SecureNativeOptions options )
@@ -84,9 +84,15 @@ public static string GetClientIpFromRequest(HttpWebRequest request, SecureNative
8484 {
8585 foreach ( var header in options . GetProxyHeaders ( ) )
8686 {
87- if ( request . Headers . Get ( header ) != null )
87+ if ( string . IsNullOrEmpty ( request . Headers . Get ( header ) ) )
8888 {
89- return request . Headers . Get ( header ) ;
89+ continue ;
90+ }
91+ var ips = request . Headers . Get ( header ) . Split ( "," ) ;
92+ var extracted = GetValidIp ( ips ) ;
93+ if ( ! string . IsNullOrEmpty ( extracted ) )
94+ {
95+ return extracted ;
9096 }
9197 }
9298 }
@@ -95,15 +101,20 @@ public static string GetClientIpFromRequest(HttpWebRequest request, SecureNative
95101 {
96102 foreach ( var header in IpHeaders . Where ( header => request . Headers . Get ( header ) != null ) )
97103 {
98- return request . Headers . Get ( header ) ;
104+ var ips = request . Headers . Get ( header ) . Split ( "," ) ;
105+ var extracted = GetValidIp ( ips ) ;
106+ if ( ! string . IsNullOrEmpty ( extracted ) )
107+ {
108+ return extracted ;
109+ }
99110 }
100111 }
101112 catch ( Exception )
102113 {
103- return "" ;
114+ return string . Empty ;
104115 }
105116
106- return "" ;
117+ return string . Empty ;
107118 }
108119
109120 public static string GetRemoteIpFromRequest ( HttpWebRequest request )
@@ -112,15 +123,21 @@ public static string GetRemoteIpFromRequest(HttpWebRequest request)
112123 {
113124 foreach ( var header in IpHeaders . Where ( header => request . Headers . Get ( header ) != null ) )
114125 {
115- return request . Headers . Get ( header ) ;
126+ var ips = request . Headers . Get ( header ) . Split ( "," ) ;
127+ var extracted = GetValidIp ( ips ) ;
128+ if ( ! string . IsNullOrEmpty ( extracted ) )
129+ {
130+ return extracted ;
131+ }
132+
116133 }
117134 }
118135 catch ( Exception )
119136 {
120- return "" ;
137+ return string . Empty ;
121138 }
122139
123- return "" ;
140+ return string . Empty ;
124141 }
125142
126143 public static Dictionary < string , string > GetHeadersFromRequest ( HttpRequest request )
@@ -190,9 +207,11 @@ public static string GetClientIpFromRequest(HttpRequest request, SecureNativeOpt
190207 {
191208 foreach ( var header in options . GetProxyHeaders ( ) )
192209 {
193- if ( request . Headers [ header ] [ 0 ] != null )
210+ if ( request . Headers [ header ] . ToArray ( ) == null ) continue ;
211+ var extracted = GetValidIp ( request . Headers [ header ] . ToArray ( ) ) ;
212+ if ( ! string . IsNullOrEmpty ( extracted ) )
194213 {
195- return request . Headers [ header ] [ 0 ] ;
214+ return extracted ;
196215 }
197216 }
198217 }
@@ -201,7 +220,11 @@ public static string GetClientIpFromRequest(HttpRequest request, SecureNativeOpt
201220 {
202221 foreach ( var header in IpHeaders . Where ( header => request . Headers [ header ] . Count > 0 ) )
203222 {
204- return request . Headers [ header ] [ 0 ] ;
223+ var extracted = GetValidIp ( request . Headers [ header ] . ToArray ( ) ) ;
224+ if ( ! string . IsNullOrEmpty ( extracted ) )
225+ {
226+ return extracted ;
227+ }
205228 }
206229
207230 if ( request . HttpContext . Connection . LocalIpAddress != null )
@@ -211,10 +234,10 @@ public static string GetClientIpFromRequest(HttpRequest request, SecureNativeOpt
211234 }
212235 catch ( Exception )
213236 {
214- return "" ;
237+ return string . Empty ;
215238 }
216239
217- return "" ;
240+ return string . Empty ;
218241 }
219242
220243 public static string GetRemoteIpFromRequest ( HttpRequest request )
@@ -223,7 +246,11 @@ public static string GetRemoteIpFromRequest(HttpRequest request)
223246 {
224247 foreach ( var header in IpHeaders . Where ( header => request . Headers [ header ] . Count > 0 ) )
225248 {
226- return request . Headers [ header ] [ 0 ] ;
249+ var extracted = GetValidIp ( request . Headers [ header ] . ToArray ( ) ) ;
250+ if ( ! string . IsNullOrEmpty ( extracted ) )
251+ {
252+ return extracted ;
253+ }
227254 }
228255
229256 if ( request . HttpContext . Connection . RemoteIpAddress != null )
@@ -233,10 +260,34 @@ public static string GetRemoteIpFromRequest(HttpRequest request)
233260 }
234261 catch ( Exception )
235262 {
236- return "" ;
263+ return string . Empty ;
264+ }
265+
266+ return string . Empty ;
267+ }
268+
269+ private static string GetValidIp ( IEnumerable < string > ipAddresses ) {
270+ foreach ( var extracted in ipAddresses )
271+ {
272+ var ips = extracted . Split ( "," ) ;
273+ foreach ( var ip in ips )
274+ {
275+ if ( IpUtils . IsValidPublicIp ( ip . Trim ( ) ) )
276+ {
277+ return ip . Trim ( ) ;
278+ }
279+ }
280+
281+ foreach ( var ip in ips )
282+ {
283+ if ( ! IpUtils . IsLoopBack ( ip . Trim ( ) ) )
284+ {
285+ return ip . Trim ( ) ;
286+ }
287+ }
237288 }
238289
239- return "" ;
290+ return string . Empty ;
240291 }
241292 }
242293}
0 commit comments