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 * Returns the browser/OS data from the request header
36 * Note: headers are spoofable
39 function wfGetAgent() {
40 if( function_exists( 'apache_request_headers' ) ) {
41 // More reliable than $_SERVER due to case and -/_ folding
42 $set = apache_request_headers();
43 $index = 'User-Agent';
45 // Subject to spoofing with headers like X_Forwarded_For
47 $index = 'HTTP_USER_AGENT';
49 if( isset( $set[$index] ) ) {
57 * Work out the IP address based on various globals
58 * For trusted proxies, use the XFF client IP (first of the chain)
64 # Return cached result
65 if ( !empty( $wgIP ) ) {
69 /* collect the originating ips */
70 # Client connecting to this webserver
71 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
72 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
75 $ipchain = array( '127.0.0.1' );
79 # Append XFF on to $ipchain
80 $forwardedFor = wfGetForwardedFor();
81 if ( isset( $forwardedFor ) ) {
82 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
83 $xff = array_reverse( $xff );
84 $ipchain = array_merge( $ipchain, $xff );
87 # Step through XFF list and find the last address in the list which is a trusted server
88 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
89 foreach ( $ipchain as $i => $curIP ) {
90 $curIP = IP
::canonicalize( $curIP );
91 if ( wfIsTrustedProxy( $curIP ) ) {
92 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
93 $ip = $ipchain[$i +
1];
100 wfDebug( "IP: $ip\n" );
106 * Checks if an IP is a trusted proxy providor
107 * Useful to tell if X-Fowarded-For data is possibly bogus
108 * Squid cache servers for the site and AOL are whitelisted
112 function wfIsTrustedProxy( $ip ) {
113 global $wgSquidServers, $wgSquidServersNoPurge;
115 if ( in_array( $ip, $wgSquidServers ) ||
116 in_array( $ip, $wgSquidServersNoPurge ) ||
123 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
128 * Forks processes to scan the originating IP for an open proxy server
129 * MemCached can be used to skip IPs that have already been scanned
131 function wfProxyCheck() {
132 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
133 global $wgUseMemCached, $wgMemc, $wgProxyMemcExpiry;
136 if ( !$wgBlockOpenProxies ) {
144 if ( $wgUseMemCached ) {
145 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
146 $mcValue = $wgMemc->get( $mcKey );
154 $title = SpecialPage
::getTitleFor( 'Blockme' );
155 $iphash = md5( $ip . $wgProxyKey );
156 $url = $title->getFullURL( 'ip='.$iphash );
158 foreach ( $wgProxyPorts as $port ) {
159 $params = implode( ' ', array(
160 escapeshellarg( $wgProxyScriptPath ),
161 escapeshellarg( $ip ),
162 escapeshellarg( $port ),
163 escapeshellarg( $url )
165 exec( "php $params &>/dev/null &" );
168 if ( $wgUseMemCached ) {
169 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
175 * Convert a network specification in CIDR notation to an integer network and a number of bits
176 * @return array(string, int)
178 function wfParseCIDR( $range ) {
179 return IP
::parseCIDR( $range );
183 * Check if an IP address is in the local proxy list
186 function wfIsLocallyBlockedProxy( $ip ) {
188 $fname = 'wfIsLocallyBlockedProxy';
190 if ( !$wgProxyList ) {
193 wfProfileIn( $fname );
195 if ( !is_array( $wgProxyList ) ) {
196 # Load from the specified file
197 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
200 if ( !is_array( $wgProxyList ) ) {
202 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
204 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
205 # Old-style flipped proxy list
210 wfProfileOut( $fname );
215 * TODO: move this list to the database in a global IP info table incorporating
216 * trusted ISP proxies, blocked IP addresses and open proxies.
219 function wfIsAOLProxy( $ip ) {
240 '205.188.146.144/30',
244 static $parsedRanges;
245 if ( is_null( $parsedRanges ) ) {
246 $parsedRanges = array();
247 foreach ( $ranges as $range ) {
248 $parsedRanges[] = IP
::parseRange( $range );
252 $hex = IP
::toHex( $ip );
253 foreach ( $parsedRanges as $range ) {
254 if ( $hex >= $range[0] && $hex <= $range[1] ) {