giovedì 9 gennaio 2014

Multithreaded Proxy Check and Rotation with CURL and PHP (ITALIANO, ENGLISH)

*** ITALIAN *** (ENGLISH below)

L'esigenza nasce dal moltiplicare gli indirizzi IP in proprio possesso per compiere operazioni che richiedano l'utilizzo di IP multipli. La necessità del multithread è dovuta alla proverbiale lentezza delle operazioni di triangolazione dei proxy, spesso intasati e basati su hardware desueto, che in questo caso viene mitigata dal multithread parallel processing. Il multithread è una funzione di CURL e ci dà la possibilità di compiere operazioni in parallelo grazie al codice che sto scrivendo e testando nel corso della mia attività di amministratore di sistema.

Se qualcuno ingenuamente dovesse credere di poter usare questo meccanismo per frodare, mascherare le proprie operazioni e altre attività lecite o illecite che siano, dovrà poi scoprire a proprie spese che essendo le liste proxy di pubblico dominio, gli IP verranno bloccati da quasi tutti i siti. Infatti, oltre ai grandi players come Google Facebook etc, i siti che si appoggiano a servizi gratuiti di proxy blocking sono praticamente tutti quelli che hanno alle spalle una piattaforma di blog (Wordpress), di CMS (Drupal, Joomla, etc), di e-commerce (Zen-cart). Senza considerare che per fidarsi dell'anonimato totale di un proxy ci vuole davvero una bella dose di ingenuità mista a fiducia nel prossimo… Infatti, sebbene il mio script implementi un controllo azione per azione dell'IP con cui viene svolta ogni singola operazione (per questioni legate alla coerenza di sessione), la traccia del vostro IP potrebbe essere nascosta negli headers HTTP, che vengono forgiati dal proxy stesso in modo da lasciare comunque una traccia. Ebbene si, nessuno vorrà andare in galera a causa della vostra irresistibile simpatia, essere inserito nelle banlist offerte gratuitamente da SORBS a causa della vostro fascino da canaglia, o comunque ricevere richieste di logs, tabulati, spiegazioni dalla Polizia nemmeno quella dello Zimbabwe. Chi conosce i mercati ICT africani, asiatici o latino-americani sa bene a cosa mi riferisco.


*** ENGLISH ***

The concept was born from the need to multiply your IP addresses and perform operations which require multiple IP addresses. The need for multithread starts from the well known slowness of the bouncing operations performed by the proxies, often congested and based on old hardware, which in this case is leveraged by multithread parallel processing. Multithread is a CURL feature and enables the operations to be performed in parallel with the following code I'm writing and testing during my sysadmin jobs.

If some noob should think to use this code to scum or mask his operations or other legal or illegal activities, will then discover at his expenses that the IPs will be blocked by almost all sites, since public proxy lists are public domain. Infact, other than the big players like Google or Facebook, also all the websites which take benefit from free proxy blocking services are almost all the ones with known platforms like blog (Wordpress), CMS (Drupal, Joomla, etc), e-commerce (Zen-cart). Please also consider that trusting a total anonymate declaration by a public proxy requires an high dose of noobness mixed to trust… Infact my script implements an IP check for every performed action (for session coerence matters), but your real IP track could be hidden insude the HTTP headers, which are forged by the proxy itself in order to leave a track in any case. Infact nobody wants to be jailed because of your irresistible smartness, be inserted into the banlists freely offered by SORBS because of your charm, or tipically be flooded by log requests, explainations, etc even from Zimbabwe Police. Whoever knows the ICT markets in Africa, Asia or Latin America knows well what I'm referring to.



PHP SCRIPT CODE

$threads = 10; //NUMBER OF CONCURRENT OPERATIONS
$timeout = 60; //SECONDS
$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'; //IT'S BETTER TO UNIFORM TO AN UNIQUE USER AGENT SINCE THE SERVER COULD HAVE A CHECK IMPLEMENTED
/*-------------------------------------------------------*/
//CURL SETUP
$session = curl_init();
curl_setopt($session, CURLOPT_HEADER, 1);
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1 ); //1 TO PUT IT IN A VARIABLE AND MANAGE IT, 0 FOR OUTPUT ONLY
curl_setopt($session, CURLOPT_HTTPGET, 1 );
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true ); //NEEDED FOR 302 REDIRECTION
curl_setopt($session, CURLOPT_USERAGENT, $agent );
/*-------------------------------------------------------*/
//PROXY50-50 BUT OTHERS MAY BE ADDED
curl_setopt($session, CURLOPT_URL, 'http://proxy50-50.blogspot.it' );
$output = curl_exec($session);
preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\&port=\d{2,5}/', $output, $list); //REGEX PARSE IP AND PORT
foreach ($list as $proxy) $proxy_list[] = str_replace("&port=",":", $proxy);
/*-------------------------------------------------------*/
work_checker($proxy_list[0], $threads);
/*-------------------------------------------------------*/
function work_checker($array_ips,$thread) {
global $timeout;
$multi = curl_multi_init();
$ips = array_chunk($array_ips,$thread);
$total = 0;
$time1 = time();
foreach($ips as $ip) {
for($i=0;$i<=count($ip)-1;$i++) {
$curl[$i] = curl_init();
curl_setopt($curl[$i],CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl[$i],CURLOPT_URL,$ip[$i]);
curl_setopt($curl[$i],CURLOPT_TIMEOUT,$timeout);
curl_multi_add_handle($multi,$curl[$i]);
} do {
curl_multi_exec($multi,$active);
usleep(11); //WAIT IN MILLISECONDS
}while( $active > 0 ); 
    foreach($curl as $cid => $cend) {
$info = curl_getinfo($cend);
curl_multi_remove_handle($multi,$cend);
if($info['http_code'] != 0) {
$total++;
        //HERE YOU COULD UPDATE THE MYSQL DB echo "Proxy works -> ".$ip[$cid]."<br />";
}
}
}
$time2 = time();
}