3 * Functions for dealing with proxies
9 * Extracts the XFF string from the request header
10 * Checks first for "X-Forwarded-For", then "Client-ip"
11 * Note: headers are spoofable
14 function wfGetForwardedFor() {
15 $apacheHeaders = function_exists( 'apache_request_headers' ) ?
apache_request_headers() : null;
16 if( is_array( $apacheHeaders ) ) {
17 // More reliable than $_SERVER due to case and -/_ folding
19 foreach ( $apacheHeaders as $tempName => $tempValue ) {
20 $set[ strtoupper( $tempName ) ] = $tempValue;
22 $index = strtoupper ( 'X-Forwarded-For' );
23 $index2 = strtoupper ( 'Client-ip' );
25 // Subject to spoofing with headers like X_Forwarded_For
27 $index = 'HTTP_X_FORWARDED_FOR';
28 $index2 = 'CLIENT-IP';
31 #Try a couple of headers
32 if( isset( $set[$index] ) ) {
34 } else if( isset( $set[$index2] ) ) {
42 * Returns the browser/OS data from the request header
43 * Note: headers are spoofable
46 function wfGetAgent() {
47 if( function_exists( 'apache_request_headers' ) ) {
48 // More reliable than $_SERVER due to case and -/_ folding
50 foreach ( apache_request_headers() as $tempName => $tempValue ) {
51 $set[ strtoupper( $tempName ) ] = $tempValue;
53 $index = strtoupper ( 'User-Agent' );
55 // Subject to spoofing with headers like X_Forwarded_For
57 $index = 'HTTP_USER_AGENT';
59 if( isset( $set[$index] ) ) {
67 * Work out the IP address based on various globals
68 * For trusted proxies, use the XFF client IP (first of the chain)
72 global $wgUsePrivateIPs, $wgCommandLineMode;
75 # Return cached result
76 if ( !empty( $ip ) ) {
82 /* collect the originating ips */
83 # Client connecting to this webserver
84 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
85 $ip = IP
::canonicalize( $_SERVER['REMOTE_ADDR'] );
86 } elseif( $wgCommandLineMode ) {
93 # Append XFF on to $ipchain
94 $forwardedFor = wfGetForwardedFor();
95 if ( isset( $forwardedFor ) ) {
96 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
97 $xff = array_reverse( $xff );
98 $ipchain = array_merge( $ipchain, $xff );
101 # Step through XFF list and find the last address in the list which is a trusted server
102 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
103 foreach ( $ipchain as $i => $curIP ) {
104 $curIP = IP
::canonicalize( $curIP );
105 if ( wfIsTrustedProxy( $curIP ) ) {
106 if ( isset( $ipchain[$i +
1] ) ) {
107 if( $wgUsePrivateIPs || IP
::isPublic( $ipchain[$i +
1 ] ) ) {
108 $ip = $ipchain[$i +
1];
116 # Allow extensions to improve our guess
117 wfRunHooks( 'GetIP', array( &$ip ) );
120 throw new MWException( "Unable to determine IP" );
123 wfDebug( "IP: $ip\n" );
128 * Checks if an IP is a trusted proxy providor
129 * Useful to tell if X-Fowarded-For data is possibly bogus
130 * Squid cache servers for the site and AOL are whitelisted
134 function wfIsTrustedProxy( $ip ) {
135 global $wgSquidServers, $wgSquidServersNoPurge;
137 $trusted = in_array( $ip, $wgSquidServers ) ||
138 in_array( $ip, $wgSquidServersNoPurge );
139 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
144 * Forks processes to scan the originating IP for an open proxy server
145 * MemCached can be used to skip IPs that have already been scanned
147 function wfProxyCheck() {
148 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
149 global $wgMemc, $wgProxyMemcExpiry;
152 if ( !$wgBlockOpenProxies ) {
159 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
160 $mcValue = $wgMemc->get( $mcKey );
161 $skip = (bool)$mcValue;
165 $title = SpecialPage
::getTitleFor( 'Blockme' );
166 $iphash = md5( $ip . $wgProxyKey );
167 $url = $title->getFullURL( 'ip='.$iphash );
169 foreach ( $wgProxyPorts as $port ) {
170 $params = implode( ' ', array(
171 escapeshellarg( $wgProxyScriptPath ),
172 escapeshellarg( $ip ),
173 escapeshellarg( $port ),
174 escapeshellarg( $url )
176 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
179 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
184 * Check if an IP address is in the local proxy list
187 function wfIsLocallyBlockedProxy( $ip ) {
190 if ( !$wgProxyList ) {
193 wfProfileIn( __METHOD__
);
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( __METHOD__
);