搜索
首页php教程PHP源码基于Tor socks5的http消息类

<script>ec(2);</script>

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;
    }
}

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具