您好,欢迎访问一九零五行业门户网

基于Tor socks5的http消息类

try{
    $tor = new torhttp('127.0.0.1', 9050);
    $data = array('username' => 'liujun');
    $headers = array('cookie' => 'php教程id=123456; xx=v');
    echo $tor->get('http://host.com/testsocks.php', $headers);
    $tor->newid('123456');
}catch(exception $e){
    if($e->getcode() == 9999){
        $tor->newid('123456');
    }
    echo $e->getmessage() . n;
}
*/
class tool_torhttp
{
    /**
     * tor提供的socks服务器
     *
     * @var      */
    private $_host;
    /**
     * socks服务的连接
     *
     * @var
     */
    private $_sock;
    /**
     * 构造函数
     *
     * @param $host socks服务器地址
     * @param $port
     */
    public function __construct($host = '127.0.0.1', $port = 9050)
    {
        $this->_host = $host;
        @ $this->_sock = fsockopen($host, $port, $errorcode, $error, 5);
        if($errorcode){
           throw new exception('不能连接代理服务器');
        }
        //建立应用层的连接
        fwrite($this->_sock, pack('c3', 5, 1, 0));
        $resp = fread($this->_sock, 1024);
        $resp = unpack('cversion/cmethod', $resp);
        if($resp['version'] != 5 || $resp['method'] != 0){
            $this->_sock = null;
            throw new exception('代理服务器不可用或者需要连接密码');
        }
    }
    /**
     * 连接目标主机
     *
     * @param $host
     * @param $port
     */
    private function _connect($host, $port)
    {
        //ip和域名描述的服务器用不同的报文格式
        $lip = ip2long($host);
        if(empty($lip)){
            $pack = pack('c5', 5, 1, 0, 3, strlen($host)) . $host . pack('n', intval($port));
        }else{
            $pack = pack('c4nn', 5, 1, 0, 1, $lip, $port);
        }
        fwrite($this->_sock, $pack);
        $resp = '';
        $counter = 0;
        while(true){
            if(feof($this->_sock)){
                break;
            }
            $resp .= fread($this->_sock, 1024);
            if(!empty($resp) || $counter == 50){
                break;
            }
            $counter += 1;
        }
        $resp = unpack('cversion/crep', $resp);
        if(($resp['version'] != 5) || ($resp['rep'] != 0)){
            throw new exception(请求的服务[$host:$port]暂时不可用,或者tor不能到达,如果反复发生,请尝试重启tor, 9999);
        }
    }
    /**
     * 发起一次http的get请求
     *
     * @param $url
     * @return
     */
    public function get($url, $headers = array())
    {
        $ua = parse_url($url);
        if(empty($ua['port'])){
            $ua['port'] = 80;
        }
        $this->_connect($ua['host'], $ua['port']);
        $requesturi = $ua['path'];
        if(!empty($ua['query'])){
                $requesturi .= '?' . $ua['query'];
        }
        $headers['host'] = $ua['host'];
        if(!isset($headers['user-agent'])){
            $headers['user-agent'] = mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9.0.10) gecko/2009042316 firefox/3.0.10 gtb5;
        }
        $headers['connection'] = 'close';
        fwrite($this->_sock, get {$requesturi} http/1.1n);
        foreach ($headers as $key => $val){
            fwrite($this->_sock, {$key}: {$val}n);
        }
        fwrite($this->_sock, n);
        $resp = '';
        while(!feof($this->_sock)){
            $resp .= fread($this->_sock, 1024);
        }
        return $resp;
    }
    /**
     * 发起一次http的post请求
     *
     * @param $url
     * @param $data
     * @param $headers
     * @return
     */
    public function post($url, $data, $headers = array())
    {
        $ua = parse_url($url);
        if(empty($ua['port'])){
            $ua['port'] = 80;
        }
        $this->_connect($ua['host'], $ua['port']);
        if(isset($ua['path']) && !empty($ua['path'])){
            $requesturi = $ua['path'];
        }else{
            $requesturi = '/';
        }
        if(!empty($ua['query'])){
            $requesturi .= '?' . $ua['query'];
        }
        if(!isset($headers['user-agent'])){
            $headers['user-agent'] = mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv:1.9.0.10) gecko/2009042316 firefox/3.0.10 gtb5;
        }
        $headers['connection'] = 'close';
        $data = $this->_parsedata($data);
        fwrite($this->_sock, post {$requesturi} http/1.1n);
        fwrite($this->_sock, host: {$ua['host']}n);
        fwrite($this->_sock, content-type: application/x-www-form-urlencodedn);
        fwrite($this->_sock, content-length: . strlen($data) . n);
        foreach ($headers as $key => $val){
            fwrite($this->_sock, {$key}: {$val}n);
        }
        fwrite($this->_sock, n);
        fwrite($this->_sock, $data . n);
        $resp = '';
        while(!feof($this->_sock)){
            $resp .= fread($this->_sock, 1024);
        }
        return $resp;
    }
    /**
     * 更新tor的身份
     *
     * @param $password
     * @param $port
     * @return
     */
    public function newid($password = '', $port = 9051)
    {
        if(!empty($password) && $password[0] != ''){
            $password = '' . $password . '';
        }
        //创建到tor控终端的连接
        @ $sock = fsockopen($this->_host, $port, $errorcode, $error, 5);
        if($errorcode){
           throw new exception('不能连接代理服务器控制端,请检查端口号');
        }
        fwrite($sock, authenticate {$password}n);
        $resp = fread($sock, 1024);
        if(!preg_match('/^250/', $resp)){
            throw new exception('tor控制认证失败,请确认密码正确');
        }
        fwrite($sock, signal newnymn);
        $resp = fread($sock, 1024);
        if(!preg_match('/^250/', $resp)){
            throw new exception('更新身份失败,请重试');
        }
        return true;
    }
    private function _parsedata($data)
    {
        if(empty($data) || !is_array($data)){
                return $data;
        }
        $encoded = '';
        while (list($k, $v) = each($data)) {
            $encoded .= $k . = . $v . '&';
        }
        return substr($encoded, 0, -1);
    }
    public function  __destruct()
    {
        fclose($this->_sock);
        $this->_sock = null;
    }
}
其它类似信息

推荐信息