3 * Functions for dealing with proxies
6 function wfGetForwardedFor() {
7 if( function_exists( 'apache_request_headers' ) ) {
8 // More reliable than $_SERVER due to case and -/_ folding
9 $set = apache_request_headers();
10 $index = 'X-Forwarded-For';
11 $index2 = 'Client-ip';
13 // Subject to spoofing with headers like X_Forwarded_For
15 $index = 'HTTP_X_FORWARDED_FOR';
16 $index2 = 'CLIENT-IP';
18 #Try a couple of headers
19 if( isset( $set[$index] ) ) {
21 } else if( isset( $set[$index2] ) ) {
28 function wfGetLastIPfromXFF( $xff ) {
30 // Avoid annoyingly long xff hacks
31 $xff = substr( $xff, 0, 511 );
32 // Look for the last IP, assuming they are separated by commas or spaces
33 $s = ( strrpos($xff, ',') ) ?
strrpos($xff, ',') : strrpos($xff, ' ');
35 $last = trim( substr( $xff, $s +
1 ) );
36 // Make sure it is an IP
37 $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $last, $last_ip4);
38 $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, $last_ip6);
54 function wfGetClientIPfromXFF( $xff ) {
56 // Avoid annoyingly long xff hacks
57 $xff = substr( $xff, 0, 511 );
58 // Look for the first IP
59 $m = preg_match('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#', $xff, $first_ip);
60 $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}#', $xff, $first_ip);
73 function wfGetAgent() {
74 if( function_exists( 'apache_request_headers' ) ) {
75 // More reliable than $_SERVER due to case and -/_ folding
76 $set = apache_request_headers();
77 $index = 'User-Agent';
79 // Subject to spoofing with headers like X_Forwarded_For
81 $index = 'HTTP_USER_AGENT';
83 if( isset( $set[$index] ) ) {
90 /** Work out the IP address based on various globals */
94 # Return cached result
95 if ( !empty( $wgIP ) ) {
99 /* collect the originating ips */
100 # Client connecting to this webserver
101 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
102 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
105 $ipchain = array( '127.0.0.1' );
109 # Append XFF on to $ipchain
110 $forwardedFor = wfGetForwardedFor();
111 if ( isset( $forwardedFor ) ) {
112 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
113 $xff = array_reverse( $xff );
114 $ipchain = array_merge( $ipchain, $xff );
117 # Step through XFF list and find the last address in the list which is a trusted server
118 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
119 foreach ( $ipchain as $i => $curIP ) {
120 $curIP = IP
::canonicalize( $curIP );
121 if ( wfIsTrustedProxy( $curIP ) ) {
122 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
123 $ip = $ipchain[$i +
1];
130 wfDebug( "IP: $ip\n" );
135 function wfIsTrustedProxy( $ip ) {
136 global $wgSquidServers, $wgSquidServersNoPurge;
138 if ( in_array( $ip, $wgSquidServers ) ||
139 in_array( $ip, $wgSquidServersNoPurge ) ||
146 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
151 * Forks processes to scan the originating IP for an open proxy server
152 * MemCached can be used to skip IPs that have already been scanned
154 function wfProxyCheck() {
155 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
156 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
159 if ( !$wgBlockOpenProxies ) {
167 if ( $wgUseMemCached ) {
168 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
169 $mcValue = $wgMemc->get( $mcKey );
177 $title = SpecialPage
::getTitleFor( 'Blockme' );
178 $iphash = md5( $ip . $wgProxyKey );
179 $url = $title->getFullURL( 'ip='.$iphash );
181 foreach ( $wgProxyPorts as $port ) {
182 $params = implode( ' ', array(
183 escapeshellarg( $wgProxyScriptPath ),
184 escapeshellarg( $ip ),
185 escapeshellarg( $port ),
186 escapeshellarg( $url )
188 exec( "php $params &>/dev/null &" );
191 if ( $wgUseMemCached ) {
192 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
198 * Convert a network specification in CIDR notation to an integer network and a number of bits
200 function wfParseCIDR( $range ) {
201 return IP
::parseCIDR( $range );
205 * Check if an IP address is in the local proxy list
207 function wfIsLocallyBlockedProxy( $ip ) {
209 $fname = 'wfIsLocallyBlockedProxy';
211 if ( !$wgProxyList ) {
214 wfProfileIn( $fname );
216 if ( !is_array( $wgProxyList ) ) {
217 # Load from the specified file
218 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
221 if ( !is_array( $wgProxyList ) ) {
223 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
225 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
226 # Old-style flipped proxy list
231 wfProfileOut( $fname );
236 * TODO: move this list to the database in a global IP info table incorporating
237 * trusted ISP proxies, blocked IP addresses and open proxies.
239 function wfIsAOLProxy( $ip ) {
260 '205.188.146.144/30',
264 static $parsedRanges;
265 if ( is_null( $parsedRanges ) ) {
266 $parsedRanges = array();
267 foreach ( $ranges as $range ) {
268 $parsedRanges[] = IP
::parseRange( $range );
272 $hex = IP
::toHex( $ip );
273 foreach ( $parsedRanges as $range ) {
274 if ( $hex >= $range[0] && $hex <= $range[1] ) {