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
|
|
|
|
|
* Checks first for "X-Forwarded-For", then "Client-ip"
|
|
|
|
|
* Note: headers are spoofable
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2006-06-01 08:19:02 +00:00
|
|
|
function wfGetForwardedFor() {
|
|
|
|
|
if( function_exists( 'apache_request_headers' ) ) {
|
|
|
|
|
// More reliable than $_SERVER due to case and -/_ folding
|
2008-03-08 16:22:05 +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 ( 'X-Forwarded-For' );
|
|
|
|
|
$index2 = strtoupper ( 'Client-ip' );
|
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
|
|
|
$index2 = 'CLIENT-IP';
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-02-12 01:02:35 +00:00
|
|
|
#Try a couple of headers
|
|
|
|
|
if( isset( $set[$index] ) ) {
|
|
|
|
|
return $set[$index];
|
|
|
|
|
} else if( isset( $set[$index2] ) ) {
|
|
|
|
|
return $set[$index2];
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-23 18:06:37 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the browser/OS data from the request header
|
|
|
|
|
* Note: headers are spoofable
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2007-02-12 01:02:35 +00:00
|
|
|
function wfGetAgent() {
|
|
|
|
|
if( function_exists( 'apache_request_headers' ) ) {
|
|
|
|
|
// More reliable than $_SERVER due to case and -/_ folding
|
2008-03-08 16:22:05 +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)
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2006-06-01 08:19:02 +00:00
|
|
|
function wfGetIP() {
|
2009-06-12 09:34:49 +00:00
|
|
|
global $wgIP, $wgUsePrivateIPs, $wgCommandLineMode;
|
2005-09-05 02:22:20 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
# Return cached result
|
|
|
|
|
if ( !empty( $wgIP ) ) {
|
|
|
|
|
return $wgIP;
|
|
|
|
|
}
|
2005-03-31 09:10:25 +00:00
|
|
|
|
2009-06-12 09:17:21 +00:00
|
|
|
$ipchain = array();
|
|
|
|
|
$ip = false;
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
if( $ip ) {
|
|
|
|
|
$ipchain[] = $ip;
|
2006-06-01 08:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-25 16:24:44 +00:00
|
|
|
# Append XFF on to $ipchain
|
|
|
|
|
$forwardedFor = wfGetForwardedFor();
|
|
|
|
|
if ( isset( $forwardedFor ) ) {
|
|
|
|
|
$xff = array_map( 'trim', explode( ',', $forwardedFor ) );
|
|
|
|
|
$xff = array_reverse( $xff );
|
|
|
|
|
$ipchain = array_merge( $ipchain, $xff );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-11-25 16:24:44 +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 ) {
|
2006-11-29 08:08:57 +00:00
|
|
|
$curIP = IP::canonicalize( $curIP );
|
2006-11-25 16:24:44 +00:00
|
|
|
if ( wfIsTrustedProxy( $curIP ) ) {
|
2008-11-20 23:30:08 +00:00
|
|
|
if ( isset( $ipchain[$i + 1] ) ) {
|
|
|
|
|
if( $wgUsePrivateIPs || IP::isPublic( $ipchain[$i + 1 ] ) ) {
|
|
|
|
|
$ip = $ipchain[$i + 1];
|
|
|
|
|
}
|
2005-03-31 09:10:25 +00:00
|
|
|
}
|
2006-11-25 16:24:44 +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
|
|
|
|
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" );
|
|
|
|
|
$wgIP = $ip;
|
|
|
|
|
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;
|
|
|
|
|
|
2008-04-14 07:45:50 +00:00
|
|
|
if ( in_array( $ip, $wgSquidServers ) ||
|
2008-08-24 06:45:07 +00:00
|
|
|
in_array( $ip, $wgSquidServersNoPurge )
|
2006-11-25 16:24:44 +00:00
|
|
|
) {
|
|
|
|
|
$trusted = true;
|
|
|
|
|
} else {
|
|
|
|
|
$trusted = false;
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2006-06-01 07:22:49 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
/**
|
|
|
|
|
* Convert a network specification in CIDR notation to an integer network and a number of bits
|
2007-04-23 18:06:37 +00:00
|
|
|
* @return array(string, int)
|
2006-06-01 08:19:02 +00:00
|
|
|
*/
|
|
|
|
|
function wfParseCIDR( $range ) {
|
2006-09-24 06:11:21 +00:00
|
|
|
return IP::parseCIDR( $range );
|
2006-06-01 08:19:02 +00:00
|
|
|
}
|
2005-12-01 10:37:47 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
/**
|
|
|
|
|
* Check if an IP address is in the local proxy list
|
2007-04-23 18:06:37 +00:00
|
|
|
* @return bool
|
2006-06-01 08:19:02 +00:00
|
|
|
*/
|
|
|
|
|
function wfIsLocallyBlockedProxy( $ip ) {
|
|
|
|
|
global $wgProxyList;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
if ( !$wgProxyList ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2009-11-07 14:23:09 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
if ( !is_array( $wgProxyList ) ) {
|
|
|
|
|
# Load from the specified file
|
|
|
|
|
$wgProxyList = array_map( 'trim', file( $wgProxyList ) );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2006-06-01 08:19:02 +00:00
|
|
|
if ( !is_array( $wgProxyList ) ) {
|
|
|
|
|
$ret = false;
|
|
|
|
|
} elseif ( array_search( $ip, $wgProxyList ) !== false ) {
|
|
|
|
|
$ret = true;
|
|
|
|
|
} elseif ( array_key_exists( $ip, $wgProxyList ) ) {
|
|
|
|
|
# Old-style flipped proxy list
|
|
|
|
|
$ret = true;
|
|
|
|
|
} else {
|
|
|
|
|
$ret = false;
|
2005-12-06 13:24:47 +00:00
|
|
|
}
|
2009-11-07 14:23:09 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-06-01 08:19:02 +00:00
|
|
|
return $ret;
|
2005-12-06 13:24:47 +00:00
|
|
|
}
|
2006-06-01 08:19:02 +00:00
|
|
|
|