3 * Functions for dealing with proxies
8 function getForwardedFor() {
9 if( function_exists( 'apache_request_headers' ) ) {
10 // More reliable than $_SERVER due to case and -/_ folding
11 $set = apache_request_headers();
12 $index = 'X-Forwarded-For';
14 // Subject to spoofing with headers like X_Forwarded_For
16 $index = 'HTTP_X_FORWARDED_FOR';
18 if( isset( $set[$index] ) ) {
25 /** Work out the IP address based on various globals */
27 global $wgSquidServers, $wgSquidServersNoPurge, $wgIP;
29 # Return cached result
30 if ( !empty( $wgIP ) ) {
34 /* collect the originating ips */
35 # Client connecting to this webserver
36 if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
37 $ipchain = array( $_SERVER['REMOTE_ADDR'] );
40 $ipchain = array( '127.0.0.1' );
44 # Get list of trusted proxies
45 # Flipped for quicker access
46 $trustedProxies = array_flip( array_merge( $wgSquidServers, $wgSquidServersNoPurge ) );
47 if ( count( $trustedProxies ) ) {
48 # Append XFF on to $ipchain
49 $forwardedFor = ProxyTools
::getForwardedFor();
50 if ( isset( $forwardedFor ) ) {
51 $xff = array_map( 'trim', explode( ',', $forwardedFor ) );
52 $xff = array_reverse( $xff );
53 $ipchain = array_merge( $ipchain, $xff );
55 # Step through XFF list and find the last address in the list which is a trusted server
56 # Set $ip to the IP address given by that trusted server, unless the address is not sensible (e.g. private)
57 foreach ( $ipchain as $i => $curIP ) {
58 if ( array_key_exists( $curIP, $trustedProxies ) ) {
59 if ( isset( $ipchain[$i +
1] ) && ProxyTools
::isIPPublic( $ipchain[$i +
1] ) ) {
60 $ip = $ipchain[$i +
1];
68 wfDebug( "IP: $ip\n" );
74 * Given an IP address in dotted-quad notation, returns an unsigned integer.
75 * Like ip2long() except that it actually works and has a consistent error return value.
77 function IP2Unsigned( $ip ) {
79 if ( $n == -1 ||
$n === false ) { # Return value on error depends on PHP version
88 * Return a zero-padded hexadecimal representation of an IP address
90 function IP2Hex( $ip ) {
91 $n = ProxyTools
::IP2Unsigned( $ip );
93 $n = sprintf( '%08X', $n );
99 * Determine if an IP address really is an IP address, and if it is public,
100 * i.e. not RFC 1918 or similar
102 function isIPPublic( $ip ) {
103 $n = ProxyTools
::IP2Unsigned( $ip );
108 // ip2long accepts incomplete addresses, as well as some addresses
109 // followed by garbage characters. Check that it's really valid.
110 if( $ip != long2ip( $n ) ) {
114 static $privateRanges = false;
115 if ( !$privateRanges ) {
116 $privateRanges = array(
117 array( '10.0.0.0', '10.255.255.255' ), # RFC 1918 (private)
118 array( '172.16.0.0', '172.31.255.255' ), # "
119 array( '192.168.0.0', '192.168.255.255' ), # "
120 array( '0.0.0.0', '0.255.255.255' ), # this network
121 array( '127.0.0.0', '127.255.255.255' ), # loopback
125 foreach ( $privateRanges as $r ) {
126 $start = ProxyTools
::IP2Unsigned( $r[0] );
127 $end = ProxyTools
::IP2Unsigned( $r[1] );
128 if ( $n >= $start && $n <= $end ) {
136 * Forks processes to scan the originating IP for an open proxy server
137 * MemCached can be used to skip IPs that have already been scanned
139 function proxyCheck() {
140 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
141 global $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
144 if ( !$wgBlockOpenProxies ) {
148 $ip = ProxyTools
::getIP();
152 if ( $wgUseMemCached ) {
153 $mcKey = "$wgDBname:proxy:ip:$ip";
154 $mcValue = $wgMemc->get( $mcKey );
162 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
163 $iphash = md5( $ip . $wgProxyKey );
164 $url = $title->getFullURL( 'ip='.$iphash );
166 foreach ( $wgProxyPorts as $port ) {
167 $params = implode( ' ', array(
168 escapeshellarg( $wgProxyScriptPath ),
169 escapeshellarg( $ip ),
170 escapeshellarg( $port ),
171 escapeshellarg( $url )
173 exec( "php $params &>/dev/null &" );
176 if ( $wgUseMemCached ) {
177 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
183 * Convert a network specification in CIDR notation to an integer network and a number of bits
185 function parseCIDR( $range ) {
186 $parts = explode( '/', $range, 2 );
187 if ( count( $parts ) != 2 ) {
188 return array( false, false );
190 $network = ProxyTools
::IP2Unsigned( $parts[0] );
191 if ( $network !== false && is_numeric( $parts[1] ) && $parts[1] >= 0 && $parts[1] <= 32 ) {
197 return array( $network, $bits );
201 * Check if an IP address is in the local proxy list
203 function isLocallyBlockedProxy( $ip ) {
205 $fname = 'ProxyTools::isLocallyBlockedProxy';
207 if ( !$wgProxyList ) {
210 wfProfileIn( $fname );
212 if ( !is_array( $wgProxyList ) ) {
213 # Load from the specified file
214 $wgProxyList = array_map( 'trim', file( $wgProxyList ) );
217 if ( !is_array( $wgProxyList ) ) {
219 } elseif ( array_search( $ip, $wgProxyList ) !== false ) {
221 } elseif ( array_key_exists( $ip, $wgProxyList ) ) {
222 # Old-style flipped proxy list
227 wfProfileOut( $fname );