使用php的curl类,检测socket5代理服务器可用性


设计初衷当然是用php curl bind不一个sokcet5去访问一个随机页面(公网)这个页面的功能就是通过一个md5来验证sokcet5服务器是否真的可用
A服务器通过 socket5发出http://xxxx/a.php?t=randkey
B服务器,echo md5($_GET['t'] . mykey);
A服务器拿到content以后也用相同算法算一个$resut看看是不是等于B服务器的echo值,如果一样,就是success 不一样就是failed

  1. 第一个文件curl.inc.php
  2. <?php
  3. class CurlRequest
  4. {
  5.     private $ch=0;
  6.     /**
  7.      * Init curl session
  8.      *
  9.      * $params = array(‘url’ => ”,
  10.      *                    ’host’ => ”,
  11.      *                   ‘header’ => ”,
  12.      *                   ‘method’ => ”,
  13.      *                   ‘referer’ => ”,
  14.      *                   ‘cookie’ => ”,
  15.      *                   ‘post_fields’ => ”,
  16.      *                    ['login' => '',]
  17.      *                    ['password' => '',]
  18.      *                   ‘timeout’ => 0
  19.      *                   );
  20.      */
  21.     public function init($params)
  22.     {
  23.                                 if($this->ch==0)
  24.                         $this->ch = curl_init();
  25.         $user_agent = ‘Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.0.9) Gecko/20061206 Firefox/1.5.0.9′;
  26.         $header = array(
  27.         "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
  28.         "Accept-Language: ru-ru,ru;q=0.7,en-us;q=0.5,en;q=0.3",
  29.         "Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7",
  30.         "Keep-Alive: 300");
  31.         if (isset($params[‘host’]) && $params[‘host’])      $header[]="Host: ".$params[‘host’];
  32.         if (isset($params[‘header’]) && $params[‘header’]) $header[]=$params[‘header’];
  33.  
  34.         @curl_setopt ( $this -> ch , CURLOPT_RETURNTRANSFER , 1 );
  35.         @curl_setopt ( $this -> ch , CURLOPT_VERBOSE , 1 );
  36.         @curl_setopt ( $this -> ch , CURLOPT_HEADER , 1 );
  37.         if(strlen($params[‘proxy’])>0) curl_setopt($this->ch, CURLOPT_PROXY,$params[‘proxy’]);
  38.         if(strlen($params[‘proxyport’])>0) curl_setopt($this->ch, CURLOPT_PROXYPORT, $params[‘proxyprot’]);  
  39.         curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);    
  40.         if ($params[‘method’] == "HEAD") @curl_setopt($this -> ch,CURLOPT_NOBODY,1);
  41.         @curl_setopt ( $this -> ch, CURLOPT_FOLLOWLOCATION, 1);
  42.         @curl_setopt ( $this -> ch , CURLOPT_HTTPHEADER, $header );
  43.         if ($params[‘referer’])    @curl_setopt ($this -> ch , CURLOPT_REFERER, $params[‘referer’] );
  44.         @curl_setopt ( $this -> ch , CURLOPT_USERAGENT, $user_agent);
  45.         if ($params[‘cookie’])    @curl_setopt ($this -> ch , CURLOPT_COOKIE, $params[‘cookie’]);
  46.  
  47.         if ( $params[‘method’] == "POST" )
  48.         {
  49.             curl_setopt( $this -> ch, CURLOPT_POST, true );
  50.             curl_setopt( $this -> ch, CURLOPT_POSTFIELDS, $params[‘post_fields’] );
  51.         }
  52.         @curl_setopt( $this -> ch, CURLOPT_URL, $params[‘url’]);
  53.         @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYPEER, 0 );
  54.         @curl_setopt ( $this -> ch , CURLOPT_SSL_VERIFYHOST, 0 );
  55.         if (isset($params[‘login’]) & isset($params[‘password’]))
  56.             @curl_setopt($this -> ch , CURLOPT_USERPWD,$params[‘login’].‘:’.$params[‘password’]);
  57.         @curl_setopt ( $this -> ch , CURLOPT_TIMEOUT, $params[‘timeout’]);
  58.     }
  59.  
  60.     /**
  61.      * Make curl request
  62.      *
  63.      * @return array  ’header’,'body’,'curl_error’,'http_code’,'last_url’
  64.      */
  65.     public function exec()
  66.     {
  67.         $response = curl_exec($this->ch);
  68.         $error = curl_error($this->ch);
  69.         $result = array( ‘header’ => ,
  70.                          ‘body’ => ,
  71.                          ‘curl_error’ => ,
  72.                          ‘http_code’ => ,
  73.                          ‘last_url’ => );
  74.         if ( $error != "" )
  75.         {
  76.             $result[‘curl_error’] = $error;
  77.             return $result;
  78.         }
  79.  
  80.         $header_size = curl_getinfo($this->ch,CURLINFO_HEADER_SIZE);
  81.         $result[‘header’] = $this->pass_header(substr($response, 0, $header_size));
  82.         $result[‘body’] = substr( $response, $header_size );
  83.         $result[‘http_code’] = curl_getinfo($this -> ch,CURLINFO_HTTP_CODE);
  84.         $result[‘last_url’] = curl_getinfo($this -> ch,CURLINFO_EFFECTIVE_URL);
  85.                                 $result[‘last_sent’]=curl_getinfo($this->ch,CURLINFO_HEADER_OUT );
  86.         return $result;
  87.     }
  88.     function __destruct(){
  89.         @curl_close($this->ch);
  90.     }
  91.         public function pass_header($header)
  92.         {
  93.                 $result=array();
  94.                 $varHader=explode("\r\n", $header);
  95.                 if(count($varHader)>0)
  96.                         {
  97.                                 for($i=0;$i<count($varHader);$i++)
  98.                                         {
  99.                                         $varresult=explode(":",$varHader[$i]);
  100.                                         if(is_array($varresult) && isset($varresult[1]))
  101.                                                 $result[$varresult[0]]=$varresult[1];
  102.                                         }
  103.                         }
  104.                 return $result;
  105.         }
  106. }
  107. ?>
  108. 第二个文件
  109. <?php
  110. include ‘curl.inc.php’;
  111. $GLOBALS[‘curl’]=new CurlRequest;
  112. function verify($proxy,$prot)
  113. {
  114. $key=rand(’1000000′,’9999999′);
  115. $params = array(‘url’ => "http://xxxxx.com/a.php?md=$key",   //自己找个服务器放个程序!
  116.                        ‘host’ => ‘help.dhgate.com’,
  117.                        ‘proxy’ => "$proxy:$prot",
  118.                        ‘proxyport’=>"$prot",
  119.                        ‘timeout’ =>10
  120.                        );
  121. $GLOBALS[‘curl’]->init($params);
  122. $result=$GLOBALS[‘curl’]->exec();
  123. if(trim($result[‘body’])==md5($key.‘my’))
  124.         return true;
  125. else
  126.         return false;
  127. }
  128. echo verify(‘localhost’,’7777′);
  129. ?>
  130. 目标服务器的那个程序。。。
  131. <?php
  132. echo md5($_GET[‘md’].‘my’);
  133. ?>

verify 函数返回的true false标志socket5服务器是否可用!
curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5); 这行话,是专门给socket5用的你要验证http proxy的也一样,删了这行就行了!

  1. No comments yet.
(will not be published)
  1. No trackbacks yet.