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)
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] ) && IP
::isPublic( $ipchain[$i +
1] ) ) {
101 $ip = $ipchain[$i +
1];
108 wfDebug( "IP: $ip\n" );
114 * Checks if an IP is a trusted proxy providor
115 * Useful to tell if X-Fowarded-For data is possibly bogus
116 * Squid cache servers for the site and AOL are whitelisted
120 function wfIsTrustedProxy( $ip ) {
121 global $wgSquidServers, $wgSquidServersNoPurge;
123 if ( in_array( $ip, $wgSquidServers ) ||
124 in_array( $ip, $wgSquidServersNoPurge )
130 wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
135 * Forks processes to scan the originating IP for an open proxy server
136 * MemCached can be used to skip IPs that have already been scanned
138 function wfProxyCheck() {
139 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
140 global $wgMemc, $wgProxyMemcExpiry;
143 if ( !$wgBlockOpenProxies ) {
150 $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
151 $mcValue = $wgMemc->get( $mcKey );
152 $skip = (bool)$mcValue;
156 $title = SpecialPage
::getTitleFor( 'Blockme' );
157 $iphash = md5( $ip . $wgProxyKey );
158 $url = $title->getFullURL( 'ip='.$iphash );
160 foreach ( $wgProxyPorts as $port ) {
161 $params = implode( ' ', array(
162 escapeshellarg( $wgProxyScriptPath ),
163 escapeshellarg( $ip ),
164 escapeshellarg( $port ),
165 escapeshellarg( $url )
167 exec( "php $params &>/dev/null &" );
170 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
175 * Convert a network specification in CIDR notation to an integer network and a number of bits
176 * @return array(string, int)
178 function wfParseCIDR( $range ) {
179 return IP
::parseCIDR( $range );
183 * Check if an IP address is in the local proxy list
186 function wfIsLocallyBlockedProxy( $ip ) {
188 $fname = 'wfIsLocallyBlockedProxy';
190 if ( !$wgProxyList ) {
193 wfProfileIn( $fname );
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( $fname );