Saltar al contenido
Codifíca.me | Desarrollo web | Programación

Cómo detectar que un usuario accede desde un PROXY

29 septiembre, 2016
sistemasdistribuidos

Hoy os traigo un script que nos vale para detectar si un usuario accede a una web a través de un proxy.

No tengo nada en contra de utilizar proxys, de hecho es un servicio web que nos es útil que para muchas cosas, sobre todo en el mundo del SEO y programación.

No obstante muchas veces nos es útil reconocer si un usuario accede a nuestra web a través de un servicio proxy.

sistemasdistribuidos

Este script realizado en PHP que utiliza la librería CURL nos es válido para reconocer si un usuario está enmascarando su IP con un proxy.

Os copio-pego el script

<?php
// This script is released into the public domain, see http://creativecommons.org/licenses/publicdomain/
 
function curl_func($url, $timeout=15){
	$curl = curl_init($url);
	curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	$page = curl_exec($curl);
	curl_close($curl);
	return $page;
}
 
$cache = 'proxylist.txt';
$maxage = 432000;
 
// If the script was called remotely we need to return the proxy list.
if ($_SERVER['REMOTE_ADDR']){
	// Send the proper headers so the browser can cache this page.
	if ($mtime = @filemtime($cache)){
		if ($_SERVER['HTTP_IF_MODIFIED_SINCE'] && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == gmdate('D, d M Y H:i:s', $mtime).' GMT'){
			header('HTTP/1.1 304 Not Modified');
			exit();
		}
		header('Last-Modified: '.gmdate('D, d M Y H:i:s', $mtime).' GMT');
		header('ETag: "'.md5($mtime.$_SERVER['SCRIPT_FILENAME']).'"');
		header('Expires: '.gmdate('D, d M Y H:i:s', $mtime + $maxage).' GMT');	
	}
	header('Content-Type: text/plain');
	if ($mtime < time() - $maxage){
		// We're re-caching the proxy list. Update the file modification time to play nice with multiple instances of this script.
		if (file_exists($cache)){
			touch($cache);
		} else {
			@file_put_contents($cache, null) or exit("Cannot write to $cache");
		}
		// Launch a separate process to re-cache the proxy list.
		exec("php {$_SERVER['SCRIPT_FILENAME']} > /dev/null &");
	}
	exit(file_get_contents($cache));
}
 
// If the script was called locally we need to update the cache. Get all the anonymous proxy list pages.
$pnum = 0;
while ($pnum <= $maxpnum){
	$page .= curl_func("http://www.xroxy.com/proxylist.php?type=Anonymous&pnum=$pnum");
	preg_match_all('/pnum=(\d+)/', $page, $matches);	
	$link = array_pop($matches[1]);
	$maxpnum >= $link or $maxpnum = $link;
	$pnum++;
}
 
// Match the IP and port.
preg_match_all('/&host=((?:[0-9]{1,3}\.){3}[0-9]{1,3})&port=(\d+)/', $page, $matches, PREG_SET_ORDER);
 
// Check if the proxy is accepting connections and if so, add it to the list
foreach($matches as $row){
	if ($fp = @fsockopen($row[1], $row[2], $errno, $errstr, 5)){
			fclose($fp);
			$text .= "{$row[1]}:{$row[2]}\n";
	}
}
 
file_put_contents($cache, $text);
?>

Mencionar que la web del creador del script es la siguiente:
shroomery.org