3 * Functions for dealing with proxies
7 * Extracts the XFF string from the request header
8 * Checks first for "X-Forwarded-For", then "Client-ip"
9 * Note: headers are spoofable
12 function wfGetForwardedFor() {
13 if( function_exists( 'apache_request_headers' ) ) {
14 // More reliable than $_SERVER due to case and -/_ folding
15 $set = apache_request_headers();
16 $index = 'X-Forwarded-For';
17 $index2 = 'Client-ip';
19 // Subject to spoofing with headers like X_Forwarded_For
21 $index = 'HTTP_X_FORWARDED_FOR';
22 $index2 = 'CLIENT-IP';
24 #Try a couple of headers
25 if( isset( $set[$index] ) ) {
27 } else if( isset( $set[$index2] ) ) {
35 * @todo FUCKING DOCUMENT THIS FUCKING FUNCTION
37 function wfGetLastIPfromXFF( $xff ) {
39 // Avoid annoyingly long xff hacks
40 $xff = substr( $xff, 0, 255 );
41 // Look for the last IP, assuming they are separated by commas or spaces
42 $n = ( strrpos($xff, ',') ) ?
strrpos($xff, ',') : strrpos($xff, ' ');
44 $last = trim( substr( $xff, $n +
1 ) );
45 // Make sure it is an IP
46 $m = preg_match('#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#', $last);
47 $n = preg_match('#^:(:[0-9A-Fa-f]{1,4}){1,7}|[0-9A-Fa-f]{1,4}(:{1,2}[0-9A-Fa-f]{1,4}|::$){1,7}$#', $last);
64 * Returns the browser/OS data from the request header
65 * Note: headers are spoofable
68 function wfGetAgent() {
69 if( function_exists( 'apache_request_headers' ) ) {
70 // More reliable than $_SERVER due to case and -/_ folding
71 $set = apache_request_headers();
72 $index = 'User-Agent';
74 // Subject to spoofing with headers like X_Forwarded_For
76 $index = 'HTTP_USER_AGENT';
78 if( isset( $set[$index] ) ) {
86 * Work out the IP address based on various globals
87 * For trusted proxies, use the XFF client IP (first of the chain)
93 # Return cached result
94 if ( !empty( $wgIP ) ) {
98 /* collect the originating ips */
99 # Client connecting to this webserver
100 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
101 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
104 $ipchain = array( '127.0.0.1' );
108 # Append XFF on to $ipchain
109 $forwardedFor = wfGetForwardedFor();
110 if ( isset( $forwardedFor ) ) {
111 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
112 $xff = array_reverse( $xff );
113 $ipchain = array_merge( $ipchain, $xff );
116 # Step through XFF list and find the last address in the list which is a trusted server
117 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
118 foreach ( $ipchain as $i => $curIP ) {
119 $curIP = IP
::canonicalize( $curIP );
120 if ( wfIsTrustedProxy( $curIP ) ) {
121 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
122 $ip = $ipchain[$i +
1];
129 wfDebug( "IP: $ip\n" );
135 * Checks if an IP is a trusted proxy providor
136 * Useful to tell if X-Fowarded-For data is possibly bogus
137 * Squid cache servers for the site and AOL are whitelisted
141 function wfIsTrustedProxy( $ip ) {
142 global $wgSquidServers, $wgSquidServersNoPurge;
144 if ( in_array( $ip, $wgSquidServers ) ||
145 in_array( $ip, $wgSquidServersNoPurge ) ||
152 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
157 * Forks processes to scan the originating IP for an open proxy server
158 * MemCached can be used to skip IPs that have already been scanned
160 function wfProxyCheck() {
161 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
162 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
165 if ( !$wgBlockOpenProxies ) {
173 if ( $wgUseMemCached ) {
174 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
175 $mcValue = $wgMemc->get( $mcKey );
183 $title = SpecialPage
::getTitleFor( 'Blockme' );
184 $iphash = md5( $ip . $wgProxyKey );
185 $url = $title->getFullURL( 'ip='.$iphash );
187 foreach ( $wgProxyPorts as $port ) {
188 $params = implode( ' ', array(
189 escapeshellarg( $wgProxyScriptPath ),
190 escapeshellarg( $ip ),
191 escapeshellarg( $port ),
192 escapeshellarg( $url )
194 exec( "php $params &>/dev/null &" );
197 if ( $wgUseMemCached ) {
198 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
204 * Convert a network specification in CIDR notation to an integer network and a number of bits
205 * @return array(string, int)
207 function wfParseCIDR( $range ) {
208 return IP
::parseCIDR( $range );
212 * Check if an IP address is in the local proxy list
215 function wfIsLocallyBlockedProxy( $ip ) {
217 $fname = 'wfIsLocallyBlockedProxy';
219 if ( !$wgProxyList ) {
222 wfProfileIn( $fname );
224 if ( !is_array( $wgProxyList ) ) {
225 # Load from the specified file
226 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
229 if ( !is_array( $wgProxyList ) ) {
231 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
233 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
234 # Old-style flipped proxy list
239 wfProfileOut( $fname );
244 * TODO: move this list to the database in a global IP info table incorporating
245 * trusted ISP proxies, blocked IP addresses and open proxies.
248 function wfIsAOLProxy( $ip ) {
269 '205.188.146.144/30',
273 static $parsedRanges;
274 if ( is_null( $parsedRanges ) ) {
275 $parsedRanges = array();
276 foreach ( $ranges as $range ) {
277 $parsedRanges[] = IP
::parseRange( $range );
281 $hex = IP
::toHex( $ip );
282 foreach ( $parsedRanges as $range ) {
283 if ( $hex >= $range[0] && $hex <= $range[1] ) {