N
N
Nikolai Kovalevsky2017-08-05 21:36:14
PHP
Nikolai Kovalevsky, 2017-08-05 21:36:14

What is needed for a SOCKS proxy to work on a hosting?

Good evening. Faced such a problem, SOCKS proxies do not work on hosting via CURL. Everything is fine on the home server, but on the hosting the error "Failed to resolve "site" for SOCKS4 connect" occurs.
Actually, the code itself:

<?
$start = microtime(true);

$file = 'proxyList.txt';
$proxyList = [];
$fp = fopen ($file, "r+");
$proxy=fread($fp,filesize($file));
$proxys=explode(PHP_EOL,$proxy);
$mh = curl_multi_init();
print_r($proxys);
     foreach($proxys as $proxy){
        $proxy=explode(":",$proxy);
         echo $ip=$proxy[0];
         echo $port=$proxy[1];
         if(isset($proxy[2])){
             $type=$proxy[2];
         }
         if ( !$ip || !$port )
             continue;
         if(isset($proxy[2])){
             $proxy = $ip . ':' . $port. ':' . $type;
         }else{
             $proxy = $ip . ':' . $port;

         }
         $curlh[$proxy]= curl_init();
         curl_setopt($curlh[$proxy],CURLOPT_TIMEOUT, 10); 

         curl_setopt($curlh[$proxy], CURLOPT_URL, 'https://www.google.com');
         curl_setopt($curlh[$proxy], CURLOPT_FOLLOWLOCATION, true);

         curl_setopt($curlh[$proxy], CURLOPT_RETURNTRANSFER, true);

         curl_setopt($curlh[$proxy], CURLOPT_NOBODY, true);  
         curl_setopt($curlh[$proxy], CURLOPT_HEADER, true);

         curl_setopt($curlh[$proxy], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36');


         curl_setopt($curlh[$proxy], CURLOPT_PROXY, $ip . ":" . $port );
         if(isset($type)) {
             if(stripos($type,"SOCKS5")!==false){
                 curl_setopt ($curlh[$proxy], CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
             }
             if (stripos($type, "SOCKS4") !== false) {
                 curl_setopt($curlh[$proxy], CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
             }
             if (stripos($type, "HTTP") !== false) {
                 curl_setopt($curlh[$proxy], CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
             }
         }
         curl_setopt($curlh[$proxy], CURLOPT_HTTPPROXYTUNNEL, true);
         curl_multi_add_handle($mh, $curlh[$proxy]);
     }
$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running);
foreach ($curlh as $key => $value) {
    curl_multi_remove_handle($mh, $curlh[$key]);
    echo curl_error ($curlh[$key]) . "<br>";


}

foreach ($curlh as $key => $value) {
     $response[$key] = curl_multi_getcontent($curlh[$key]);
}
foreach($response as $key=> $out){
    if ( strpos($out, 'Forbidden') == true ){
        continue;}
    if ( strpos($out, '200 OK') == true ){
        array_push($proxyList,$key);}
}

if ( count($proxyList) ) {
    unlink($file);

    if ($fp = fopen ($file, "w")) {
        for ($i=0;$i<count($proxyList);$i++)
            fwrite ($fp, $proxyList[$i] . PHP_EOL);
        fclose ($fp);
    }
}


echo '<pre>';
print_r($proxyList);
echo '</pre>';

$time = microtime(true) - $start;
printf('Скрипт выполнялся %.4F сек.', $time);

Actually, the problem lies in the hosting. What exactly is needed to use a SOCKS proxy? What technologies, libraries? Thank you for attention.
PS I use proxies exclusively for collecting and analyzing information, no DDOS, data theft and other things.

UPD SOCKS5 doesn't work either, resulting in an error Failed to resolve "site" for SOCKS5 connect. HTTP proxies work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolai Kovalevsky, 2017-08-06
@Silert

The host helped me sort out the problem. According to him, using a similar method, I sent DNS requests through a proxy, which is why the error occurred. The correct solution was to replace CURLPROXY_SOCKS5 with CURLPROXY_SOCKS5_HOSTNAME, and CURLPROXY_SOCKS4 with CURLPROXY_SOCKS4A, which helped.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question