3 * Functions for dealing with proxies
8 * Extracts the XFF string from the request header
9 * Checks first for "X-Forwarded-For", then "Client-ip"
10 * Note: headers are spoofable
13 function wfGetForwardedFor() {
14 if( function_exists( 'apache_request_headers' ) ) {
15 // More reliable than $_SERVER due to case and -/_ folding
17 foreach ( apache_request_headers() as $tempName => $tempValue ) {
18 $set[ strtoupper( $tempName ) ] = $tempValue;
20 $index = strtoupper ( 'X-Forwarded-For' );
21 $index2 = strtoupper ( 'Client-ip' );
23 // Subject to spoofing with headers like X_Forwarded_For
25 $index = 'HTTP_X_FORWARDED_FOR';
26 $index2 = 'CLIENT-IP';
29 #Try a couple of headers
30 if( isset( $set[$index] ) ) {
32 } else if( isset( $set[$index2] ) ) {
40 * Returns the browser/OS data from the request header
41 * Note: headers are spoofable
44 function wfGetAgent() {
45 if( function_exists( 'apache_request_headers' ) ) {
46 // More reliable than $_SERVER due to case and -/_ folding
48 foreach ( apache_request_headers() as $tempName => $tempValue ) {
49 $set[ strtoupper( $tempName ) ] = $tempValue;
51 $index = strtoupper ( 'User-Agent' );
53 // Subject to spoofing with headers like X_Forwarded_For
55 $index = 'HTTP_USER_AGENT';
57 if( isset( $set[$index] ) ) {
65 * Work out the IP address based on various globals
66 * For trusted proxies, use the XFF client IP (first of the chain)
70 global $wgIP, $wgUsePrivateIPs;
72 # Return cached result
73 if ( !empty( $wgIP ) ) {
77 /* collect the originating ips */
78 # Client connecting to this webserver
79 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
80 $ipchain = array( IP
::canonicalize( $_SERVER['REMOTE_ADDR'] ) );
83 $ipchain = array( '127.0.0.1' );
87 # Append XFF on to $ipchain
88 $forwardedFor = wfGetForwardedFor();
89 if ( isset( $forwardedFor ) ) {
90 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
91 $xff = array_reverse( $xff );
92 $ipchain = array_merge( $ipchain, $xff );
95 # Step through XFF list and find the last address in the list which is a trusted server
96 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
97 foreach ( $ipchain as $i => $curIP ) {
98 $curIP = IP
::canonicalize( $curIP );
99 if ( wfIsTrustedProxy( $curIP ) ) {
100 if ( isset( $ipchain[$i +
1] ) ) {
101 if( $wgUsePrivateIPs || IP
::isPublic( $ipchain[$i +
1 ] ) ) {
102 $ip = $ipchain[$i +
1];
110 wfDebug( "IP: $ip\n" );
116 * Checks if an IP is a trusted proxy providor
117 * Useful to tell if X-Fowarded-For data is possibly bogus
118 * Squid cache servers for the site and AOL are whitelisted
122 function wfIsTrustedProxy( $ip ) {
123 global $wgSquidServers, $wgSquidServersNoPurge;
125 if ( in_array( $ip, $wgSquidServers ) ||
126 in_array( $ip, $wgSquidServersNoPurge )
132 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
137 * Forks processes to scan the originating IP for an open proxy server
138 * MemCached can be used to skip IPs that have already been scanned
140 function wfProxyCheck() {
141 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
142 global $wgMemc, $wgProxyMemcExpiry;
145 if ( !$wgBlockOpenProxies ) {
152 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
153 $mcValue = $wgMemc->get( $mcKey );
154 $skip = (bool)$mcValue;
158 $title = SpecialPage
::getTitleFor( 'Blockme' );
159 $iphash = md5( $ip . $wgProxyKey );
160 $url = $title->getFullURL( 'ip='.$iphash );
162 foreach ( $wgProxyPorts as $port ) {
163 $params = implode( ' ', array(
164 escapeshellarg( $wgProxyScriptPath ),
165 escapeshellarg( $ip ),
166 escapeshellarg( $port ),
167 escapeshellarg( $url )
169 exec( "php $params &>/dev/null &" );
172 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
177 * Convert a network specification in CIDR notation to an integer network and a number of bits
178 * @return array(string, int)
180 function wfParseCIDR( $range ) {
181 return IP
::parseCIDR( $range );
185 * Check if an IP address is in the local proxy list
188 function wfIsLocallyBlockedProxy( $ip ) {
190 $fname = 'wfIsLocallyBlockedProxy';
192 if ( !$wgProxyList ) {
195 wfProfileIn( $fname );
197 if ( !is_array( $wgProxyList ) ) {
198 # Load from the specified file
199 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
202 if ( !is_array( $wgProxyList ) ) {
204 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
206 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
207 # Old-style flipped proxy list
212 wfProfileOut( $fname );