2005-03-31 09:10:25 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Functions for dealing with proxies
|
2010-08-14 17:42:40 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
2005-03-31 09:10:25 +00:00
|
|
|
*/
|
|
|
|
|
|
2007-04-23 18:06:37 +00:00
|
|
|
/**
|
|
|
|
|
* Extracts the XFF string from the request header
|
|
|
|
|
* Note: headers are spoofable
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2006-06-01 08:19:02 +00:00
|
|
|
function wfGetForwardedFor() {
|
2011-04-11 16:49:36 +00:00
|
|
|
$apacheHeaders = function_exists( 'apache_request_headers' ) ? apache_request_headers() : null;
|
|
|
|
|
if( is_array( $apacheHeaders ) ) {
|
2006-06-01 08:19:02 +00:00
|
|
|
// More reliable than $_SERVER due to case and -/_ folding
|
2011-04-25 21:25:45 +00:00
|
|
|
$set = array();
|
2011-04-11 16:49:36 +00:00
|
|
|
foreach ( $apacheHeaders as $tempName => $tempValue ) {
|
2008-03-08 16:22:05 +00:00
|
|
|
$set[ strtoupper( $tempName ) ] = $tempValue;
|
2008-03-07 22:12:34 +00:00
|
|
|
}
|
|
|
|
|
$index = strtoupper ( 'X-Forwarded-For' );
|
2006-06-01 08:19:02 +00:00
|
|
|
} else {
|
|
|
|
|
// Subject to spoofing with headers like X_Forwarded_For
|
|
|
|
|
$set = $_SERVER;
|
|
|
|
|
$index = 'HTTP_X_FORWARDED_FOR';
|
2007-02-12 01:02:35 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-08-15 05:25:56 +00:00
|
|
|
#Try to see if XFF is set
|
2007-02-12 01:02:35 +00:00
|
|
|
if( isset( $set[$index] ) ) {
|
|
|
|
|
return $set[$index];
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-23 18:06:37 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the browser/OS data from the request header
|
|
|
|
|
* Note: headers are spoofable
|
2011-06-03 10:41:53 +00:00
|
|
|
*
|
2011-07-18 23:01:08 +00:00
|
|
|
* @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead.
|
2007-04-23 18:06:37 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2007-02-12 01:02:35 +00:00
|
|
|
function wfGetAgent() {
|
2011-06-03 10:41:53 +00:00
|
|
|
wfDeprecated( __FUNCTION__ );
|
2007-02-12 01:02:35 +00:00
|
|
|
if( function_exists( 'apache_request_headers' ) ) {
|
|
|
|
|
// More reliable than $_SERVER due to case and -/_ folding
|
2011-04-25 21:25:45 +00:00
|
|
|
$set = array();
|
2008-03-07 22:12:34 +00:00
|
|
|
foreach ( apache_request_headers() as $tempName => $tempValue ) {
|
2008-03-08 16:22:05 +00:00
|
|
|
$set[ strtoupper( $tempName ) ] = $tempValue;
|
2008-03-07 22:12:34 +00:00
|
|
|
}
|
|
|
|
|
$index = strtoupper ( 'User-Agent' );
|
2007-02-12 01:02:35 +00:00
|
|
|
} else {
|
|
|
|
|
// Subject to spoofing with headers like X_Forwarded_For
|
|
|
|
|
$set = $_SERVER;
|
|
|
|
|
$index = 'HTTP_USER_AGENT';
|
2006-01-07 21:44:10 +00:00
|
|
|
}
|
2006-06-01 08:19:02 +00:00
|
|
|
if( isset( $set[$index] ) ) {
|
|
|
|
|
return $set[$index];
|
|
|
|
|
} else {
|
2007-03-09 22:57:25 +00:00
|
|
|
return '';
|
2006-06-01 08:19:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 21:44:10 +00:00
|
|
|
|
2007-04-23 18:06:37 +00:00
|
|
|
/**
|
|
|
|
|
* Work out the IP address based on various globals
|
|
|
|
|
* For trusted proxies, use the XFF client IP (first of the chain)
|
2011-08-16 14:15:07 +00:00
|
|
|
* @param $reset boolean Used to reset the internal static variable
|
|
|
|
|
* tracking the IP address. Set to anything non empty to reset it, for
|
|
|
|
|
* example: wfGetIP( 'reset' ); (default: false).
|
2007-04-23 18:06:37 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-08-16 14:15:07 +00:00
|
|
|
function wfGetIP( $reset = false ) {
|
2010-09-11 13:45:51 +00:00
|
|
|
global $wgUsePrivateIPs, $wgCommandLineMode;
|
|
|
|
|
static $ip = false;
|
2005-09-05 02:22:20 +00:00
|
|
|
|
2011-08-16 14:15:07 +00:00
|
|
|
if( $reset ) {
|
|
|
|
|
$ip = false;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
# Return cached result
|
2010-09-11 13:45:51 +00:00
|
|
|
if ( !empty( $ip ) ) {
|
|
|
|
|
return $ip;
|
2006-06-01 08:19:02 +00:00
|
|
|
}
|
2005-03-31 09:10:25 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
/* collect the originating ips */
|
|
|
|
|
# Client connecting to this webserver
|
2009-06-12 01:34:44 +00:00
|
|
|
if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
|
|
|
|
|
$ip = IP::canonicalize( $_SERVER['REMOTE_ADDR'] );
|
2009-06-12 09:34:49 +00:00
|
|
|
} elseif( $wgCommandLineMode ) {
|
|
|
|
|
$ip = '127.0.0.1';
|
2009-06-12 01:34:44 +00:00
|
|
|
}
|
2006-06-01 08:19:02 +00:00
|
|
|
|
2011-06-03 10:55:38 +00:00
|
|
|
# Append XFF
|
2006-11-25 16:24:44 +00:00
|
|
|
$forwardedFor = wfGetForwardedFor();
|
2011-06-03 10:55:38 +00:00
|
|
|
if ( $forwardedFor !== null ) {
|
|
|
|
|
$ipchain = array_map( 'trim', explode( ',', $forwardedFor ) );
|
|
|
|
|
$ipchain = array_reverse( $ipchain );
|
|
|
|
|
if ( $ip ) {
|
|
|
|
|
array_unshift( $ipchain, $ip );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-06-03 10:55:38 +00:00
|
|
|
# Step through XFF list and find the last address in the list which is a trusted server
|
|
|
|
|
# Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
|
|
|
|
|
foreach ( $ipchain as $i => $curIP ) {
|
|
|
|
|
$curIP = IP::canonicalize( $curIP );
|
|
|
|
|
if ( wfIsTrustedProxy( $curIP ) ) {
|
|
|
|
|
if ( isset( $ipchain[$i + 1] ) ) {
|
|
|
|
|
if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
|
|
|
|
|
$ip = $ipchain[$i + 1];
|
|
|
|
|
}
|
2008-11-20 23:30:08 +00:00
|
|
|
}
|
2011-06-03 10:55:38 +00:00
|
|
|
} else {
|
|
|
|
|
break;
|
2005-03-31 09:10:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-06-01 08:19:02 +00:00
|
|
|
}
|
2005-03-31 09:10:25 +00:00
|
|
|
|
2010-09-11 13:45:51 +00:00
|
|
|
# Allow extensions to improve our guess
|
|
|
|
|
wfRunHooks( 'GetIP', array( &$ip ) );
|
|
|
|
|
|
2009-06-12 09:17:21 +00:00
|
|
|
if( !$ip ) {
|
2009-06-12 01:34:44 +00:00
|
|
|
throw new MWException( "Unable to determine IP" );
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
wfDebug( "IP: $ip\n" );
|
|
|
|
|
return $ip;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-23 18:06:37 +00:00
|
|
|
/**
|
|
|
|
|
* Checks if an IP is a trusted proxy providor
|
|
|
|
|
* Useful to tell if X-Fowarded-For data is possibly bogus
|
|
|
|
|
* Squid cache servers for the site and AOL are whitelisted
|
2009-12-11 22:19:37 +00:00
|
|
|
* @param $ip String
|
2007-04-23 18:06:37 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2006-11-25 16:24:44 +00:00
|
|
|
function wfIsTrustedProxy( $ip ) {
|
|
|
|
|
global $wgSquidServers, $wgSquidServersNoPurge;
|
|
|
|
|
|
2011-04-25 21:25:45 +00:00
|
|
|
$trusted = in_array( $ip, $wgSquidServers ) ||
|
|
|
|
|
in_array( $ip, $wgSquidServersNoPurge );
|
2006-11-25 16:24:44 +00:00
|
|
|
wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
|
|
|
|
|
return $trusted;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
/**
|
|
|
|
|
* Forks processes to scan the originating IP for an open proxy server
|
|
|
|
|
* MemCached can be used to skip IPs that have already been scanned
|
|
|
|
|
*/
|
|
|
|
|
function wfProxyCheck() {
|
|
|
|
|
global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
|
2008-03-01 15:08:49 +00:00
|
|
|
global $wgMemc, $wgProxyMemcExpiry;
|
2008-09-03 02:28:41 +00:00
|
|
|
global $wgProxyKey;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
if ( !$wgBlockOpenProxies ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-09-05 02:22:20 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
$ip = wfGetIP();
|
2006-06-01 07:22:49 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
# Get MemCached key
|
2008-03-01 15:08:49 +00:00
|
|
|
$mcKey = wfMemcKey( 'proxy', 'ip', $ip );
|
|
|
|
|
$mcValue = $wgMemc->get( $mcKey );
|
|
|
|
|
$skip = (bool)$mcValue;
|
2005-09-05 02:22:20 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
# Fork the processes
|
|
|
|
|
if ( !$skip ) {
|
2006-10-30 06:25:31 +00:00
|
|
|
$title = SpecialPage::getTitleFor( 'Blockme' );
|
2008-09-03 02:28:41 +00:00
|
|
|
$iphash = md5( $ip . $wgProxyKey );
|
2006-06-01 08:19:02 +00:00
|
|
|
$url = $title->getFullURL( 'ip='.$iphash );
|
|
|
|
|
|
|
|
|
|
foreach ( $wgProxyPorts as $port ) {
|
|
|
|
|
$params = implode( ' ', array(
|
|
|
|
|
escapeshellarg( $wgProxyScriptPath ),
|
|
|
|
|
escapeshellarg( $ip ),
|
|
|
|
|
escapeshellarg( $port ),
|
|
|
|
|
escapeshellarg( $url )
|
|
|
|
|
));
|
2009-08-25 15:47:46 +00:00
|
|
|
exec( "php $params >" . wfGetNull() . " 2>&1 &" );
|
2006-06-01 08:19:02 +00:00
|
|
|
}
|
|
|
|
|
# Set MemCached key
|
2008-03-01 15:08:49 +00:00
|
|
|
$wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
|
2005-12-01 10:37:47 +00:00
|
|
|
}
|
2006-06-01 08:19:02 +00:00
|
|
|
}
|