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
16 foreach ( apache_request_headers() as $tempName => $tempValue ) {
17 $set[ strtoupper( $tempName ) ] = $tempValue;
19 $index = strtoupper ( 'X-Forwarded-For' );
20 $index2 = strtoupper ( 'Client-ip' );
22 // Subject to spoofing with headers like X_Forwarded_For
24 $index = 'HTTP_X_FORWARDED_FOR';
25 $index2 = 'CLIENT-IP';
28 #Try a couple of headers
29 if( isset( $set[$index] ) ) {
31 } else if( isset( $set[$index2] ) ) {
39 * Returns the browser/OS data from the request header
40 * Note: headers are spoofable
43 function wfGetAgent() {
44 if( function_exists( 'apache_request_headers' ) ) {
45 // More reliable than $_SERVER due to case and -/_ folding
47 foreach ( apache_request_headers() as $tempName => $tempValue ) {
48 $set[ strtoupper( $tempName ) ] = $tempValue;
50 $index = strtoupper ( 'User-Agent' );
52 // Subject to spoofing with headers like X_Forwarded_For
54 $index = 'HTTP_USER_AGENT';
56 if( isset( $set[$index] ) ) {
64 * Work out the IP address based on various globals
65 * For trusted proxies, use the XFF client IP (first of the chain)
71 # Return cached result
72 if ( !empty( $wgIP ) ) {
76 /* collect the originating ips */
77 # Client connecting to this webserver
78 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
79 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
82 $ipchain = array( '127.0.0.1' );
86 # Append XFF on to $ipchain
87 $forwardedFor = wfGetForwardedFor();
88 if ( isset( $forwardedFor ) ) {
89 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
90 $xff = array_reverse( $xff );
91 $ipchain = array_merge( $ipchain, $xff );
94 # Step through XFF list and find the last address in the list which is a trusted server
95 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
96 foreach ( $ipchain as $i => $curIP ) {
97 $curIP = IP
::canonicalize( $curIP );
98 if ( wfIsTrustedProxy( $curIP ) ) {
99 if ( isset( $ipchain[$i +
1] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
100 $ip = $ipchain[$i +
1];
107 wfDebug( "IP: $ip\n" );
113 * Checks if an IP is a trusted proxy providor
114 * Useful to tell if X-Fowarded-For data is possibly bogus
115 * Squid cache servers for the site and AOL are whitelisted
119 function wfIsTrustedProxy( $ip ) {
120 global $wgSquidServers, $wgSquidServersNoPurge;
122 if ( in_array( $ip, $wgSquidServers ) ||
123 in_array( $ip, $wgSquidServersNoPurge ) ||
130 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
135 * Forks processes to scan the originating IP for an open proxy server
136 * MemCached can be used to skip IPs that have already been scanned
138 function wfProxyCheck() {
139 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
140 global $wgMemc, $wgProxyMemcExpiry;
143 if ( !$wgBlockOpenProxies ) {
150 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
151 $mcValue = $wgMemc->get( $mcKey );
152 $skip = (bool)$mcValue;
156 $title = SpecialPage
::getTitleFor( 'Blockme' );
157 $iphash = md5( $ip . $wgProxyKey );
158 $url = $title->getFullURL( 'ip='.$iphash );
160 foreach ( $wgProxyPorts as $port ) {
161 $params = implode( ' ', array(
162 escapeshellarg( $wgProxyScriptPath ),
163 escapeshellarg( $ip ),
164 escapeshellarg( $port ),
165 escapeshellarg( $url )
167 exec( "php $params &>/dev/null &" );
170 $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] ) {