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 if( function_exists( 'apache_request_headers' ) ) {
16 // More reliable than $_SERVER due to case and -/_ folding
18 foreach ( apache_request_headers() as $tempName => $tempValue ) {
19 $set[ strtoupper( $tempName ) ] = $tempValue;
21 $index = strtoupper ( 'X-Forwarded-For' );
22 $index2 = strtoupper ( 'Client-ip' );
24 // Subject to spoofing with headers like X_Forwarded_For
26 $index = 'HTTP_X_FORWARDED_FOR';
27 $index2 = 'CLIENT-IP';
30 #Try a couple of headers
31 if( isset( $set[$index] ) ) {
33 } else if( isset( $set[$index2] ) ) {
41 * Returns the browser/OS data from the request header
42 * Note: headers are spoofable
45 function wfGetAgent() {
46 if( function_exists( 'apache_request_headers' ) ) {
47 // More reliable than $_SERVER due to case and -/_ folding
49 foreach ( apache_request_headers() as $tempName => $tempValue ) {
50 $set[ strtoupper( $tempName ) ] = $tempValue;
52 $index = strtoupper ( 'User-Agent' );
54 // Subject to spoofing with headers like X_Forwarded_For
56 $index = 'HTTP_USER_AGENT';
58 if( isset( $set[$index] ) ) {
66 * Work out the IP address based on various globals
67 * For trusted proxies, use the XFF client IP (first of the chain)
71 global $wgIP, $wgUsePrivateIPs, $wgCommandLineMode;
73 # Return cached result
74 if ( !empty( $wgIP ) ) {
81 /* collect the originating ips */
82 # Client connecting to this webserver
83 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
84 $ip = IP
::canonicalize( $_SERVER['REMOTE_ADDR'] );
85 } elseif( $wgCommandLineMode ) {
92 # Append XFF on to $ipchain
93 $forwardedFor = wfGetForwardedFor();
94 if ( isset( $forwardedFor ) ) {
95 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
96 $xff = array_reverse( $xff );
97 $ipchain = array_merge( $ipchain, $xff );
100 # Step through XFF list and find the last address in the list which is a trusted server
101 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
102 foreach ( $ipchain as $i => $curIP ) {
103 $curIP = IP
::canonicalize( $curIP );
104 if ( wfIsTrustedProxy( $curIP ) ) {
105 if ( isset( $ipchain[$i +
1] ) ) {
106 if( $wgUsePrivateIPs || IP
::isPublic( $ipchain[$i +
1 ] ) ) {
107 $ip = $ipchain[$i +
1];
116 throw new MWException( "Unable to determine IP" );
119 wfDebug( "IP: $ip\n" );
125 * Checks if an IP is a trusted proxy providor
126 * Useful to tell if X-Fowarded-For data is possibly bogus
127 * Squid cache servers for the site and AOL are whitelisted
131 function wfIsTrustedProxy( $ip ) {
132 global $wgSquidServers, $wgSquidServersNoPurge;
134 if ( in_array( $ip, $wgSquidServers ) ||
135 in_array( $ip, $wgSquidServersNoPurge )
141 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
146 * Forks processes to scan the originating IP for an open proxy server
147 * MemCached can be used to skip IPs that have already been scanned
149 function wfProxyCheck() {
150 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
151 global $wgMemc, $wgProxyMemcExpiry;
154 if ( !$wgBlockOpenProxies ) {
161 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
162 $mcValue = $wgMemc->get( $mcKey );
163 $skip = (bool)$mcValue;
167 $title = SpecialPage
::getTitleFor( 'Blockme' );
168 $iphash = md5( $ip . $wgProxyKey );
169 $url = $title->getFullURL( 'ip='.$iphash );
171 foreach ( $wgProxyPorts as $port ) {
172 $params = implode( ' ', array(
173 escapeshellarg( $wgProxyScriptPath ),
174 escapeshellarg( $ip ),
175 escapeshellarg( $port ),
176 escapeshellarg( $url )
178 exec( "php $params >" . wfGetNull() . " 2>&1 &" );
181 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
186 * Convert a network specification in CIDR notation to an integer network and a number of bits
187 * @return array(string, int)
189 function wfParseCIDR( $range ) {
190 return IP
::parseCIDR( $range );
194 * Check if an IP address is in the local proxy list
197 function wfIsLocallyBlockedProxy( $ip ) {
200 if ( !$wgProxyList ) {
203 wfProfileIn( __METHOD__
);
205 if ( !is_array( $wgProxyList ) ) {
206 # Load from the specified file
207 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
210 if ( !is_array( $wgProxyList ) ) {
212 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
214 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
215 # Old-style flipped proxy list
220 wfProfileOut( __METHOD__
);