XMPP A practice of communication between gtalk with XMPP over TCP

<?php
/*
 *
 *
 *
 *
 *
 */

/**
 * See @p StartLine.php @p for details
 */
include_once dirname(dirname(__FILE__)) . "/xml/StartLine.php";

/**
 * See @p Element.php @p for details
 */
include_once dirname(dirname(__FILE__)) . "/xml/Element.php";

/**
 * See @p Tag.php @p for details
 */
include_once dirname(dirname(__FILE__)) . "/aml/Tag.php";

/**
 * See @p SSocket.php @p for details
 */
include_once dirname(dirname(__FILE__)) . "/net/SSocket.php";

/**
 * See @p SocketContext.php @p for details
 */
include_once dirname(__FILE__) . "/SocketContext.php";

/**
 * See @p SSocketHandler @p for details
 */
include_once dirname(__FILE__) . "/SSocketHandler.php";

/**
 *
 *
 *
 *
 */
class XMPPEndpoint {

    /*
     *
     *
     *
     *
     */
    var $targetAddress;

    /*
     *
     *
     *
     *
     */
    var $targetPort;

    /*
     *
     *
     *
     *
     *
     */
    var $socket;

    /*
     *
     *
     *
     *
     *
     *
     */
    var $socketContext;

    /*
     *
     *
     *
     *
     *
     */
    var $socketHandler;

    /*
     *
     *
     *
     *
     */
    var $loginID;

    /*
     *
     *
     *
     *
     *
     */
    function __construct($targetAddress, $targetPort) {
        echo get_class($this)."#__construct(".get_class($targetAddress).", ".get_class($targetPort).")\n";
        $this->targetAddress = $targetAddress;
        $this->targetPort = $targetPort;
        $this->init();
    }

    /*
     *
     *
     *
     *
     */
    function init() {
        $this->socket = new SSocket("tcp://{$this->targetAddress}:{$this->targetPort}");
    }

    /*
     *
     *
     *
     *
     *
     *
     */
    function connect() {
        $this->socket->connect($this->targetAddress, $this->targetPort);
        //$sock = $this->socket->getSocket();
       
        $this->socketContext = $this->createSocketContext();
    }

   
    function reconnect() {
        $this->init();
        $this->connect();
    }

    /*
     *
     *
     *
     *
     *
     *
     *
     */
    function process() {
        $cursor = $this->socketHandler->getCursor();
        while (true) {
            $firstChild = $cursor->getFirstChild();
            //echo "&&&&&&&&&&&&&&&&&&&&&&&&:\n";
            //var_dump($cursor);
            //stream:features
            if ($firstChild != null) {
                $name = $firstChild->getName();
                $name = strtolower($name);
                if ($name == "stream:features") {
                    break;
                }
            }
            echo "...\n";
            try {
                $this->socketContext->notify();
            } catch (Exception $e) {
                echo "Failure: " . $e->getMessage() . "\n";
                return false;
            }
        }
        return true;
    }

    /*
     *
     *
     *
     *
     *
     *
     */
    function toStream() {
        $bf = '<stream:stream to="gmail.com" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
        //$bf = '<xstream to="gmail.com" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
        //$bf = '<stream:stream>';
        $outputStream = $this->socket->getOutputStream();
        $outputStream->write($bf);
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Failure: " . $e->getMessage() . "\n";
            return false;
        }
        return true;
    }

    /*
     *
     *
     *
     *
     *
     *
     */
    function openStream() {

        /*
        if (! ($socketContext instanceof SocketContext)) {
            throw new Exception("Illegal argument ".get_class($socket));
        }
        $socket = $socketContext->getSocket();
        */
        echo "Open stream...\n";
        $b = $this->toStream();
        if ($b) {
            echo "Success to open stream...Ok\n";
        }
        $this->process();
        return $b;
    }

    /*
     *
     *
     *
     *
     *
     *
     *
     */
    function createSocketContext($socket = null) {
        if ($socket == null) {
            $socket = $this->socket;
        }
        if ($socket instanceof SSocket) {
            $socket = $socket->getSocket();
        }
        $socketContext = new SocketContext($socket);
        $this->socketHandler = new SSocketHandler($socket);
        $socketContext->addObserver($this->socketHandler);
        return $socketContext;
    }

    /*
     *
     *
     *
     *
     *
     *
     */
    function startTLS() {
        /*
        if (! ($socketContext instanceof SocketContext)) {
            throw new Exception("Illegal argument ".get_class($socket));
        }
        $socket = $socketContext->getSocket();
        */
        echo "Starting TLS...\n";
        $bf = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>";

        $outputStream = $this->socket->getOutputStream();
        $outputStream->write($bf);
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail: " . $e->getMessage() . "\n";
            return false;
        }
        return true;
    }

    /*
     *
     *
     *
     *
     *
     *
     *
     */
    function getAuthenticateRequest() {
        /*
        if (! ($socketContext instanceof SocketContext)) {
            throw new Exception("Illegal argument ".get_class($socket));
        }
        $socket = $socketContext->getSocket();
        */
        $this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, true);
        $b = $this->toStream();
        $this->process();
        echo "Attempting Auth...\n";

        $auth = base64_encode("\x00"."*********"."\x00"."*********");
        $bf = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>".$auth."</auth>";

        $outputStream = $this->socket->getOutputStream();
        $outputStream->write($bf);
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
        return true;
    }


    function setBind() {
        /*
        if (! ($socketContext instanceof SocketContext)) {
            throw new Exception("Illegal argument ".get_class($socket));
        }
        $socket = $socketContext->getSocket();
        */
        //$this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, false);
        //$this->reconnect();
        $b = $this->toStream();
        $this->process();
        echo "Attempting bind...\n";

        $bf = '<iq xmlns="jabber:client" type="set" id="1"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>gtalk</resource></bind></iq>';

        $outputStream = $this->socket->getOutputStream();
        $outputStream->write($bf);
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
        /*
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
        */
        $cursor = $this->socketHandler->getCurrentNode();
        $this->loginID = $cursor->getText();
        echo "****************************************************************\n";
        //var_dump($cursor);
        echo "****************************************************************\n";
        return true;
    }

    /*
     * <iq xmlns='jabber:client' type='set' id='2'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>
     *
     *
     *
     *
     *
     */
    function openSession() {
        /*
        if (! ($socketContext instanceof SocketContext)) {
            throw new Exception("Illegal argument ".get_class($socket));
        }
        $socket = $socketContext->getSocket();
        */
        //$this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, false);
        //$this->reconnect();
        //$b = $this->toStream();
        //$this->process();
        echo "Open session...\n";

        $bf = "<iq xmlns='jabber:client' type='set' id='2'><session xmlns='urn:ietf:params:xml:ns:xmpp-session' /></iq>";

        $outputStream = $this->socket->getOutputStream();
        $outputStream->write($bf);
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
       
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
        /*
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
       
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
        */
        return true;
    }

    /*
     * <message from='luoix0423@gmail.com/xmpphp81E902FF' to='atm.luo@gmail.com' type='chat'><body>abcdefg!</body></message>
     *
     *
     *
     *
     */
    function messaging() {
        /*
        if (! ($socketContext instanceof SocketContext)) {
            throw new Exception("Illegal argument ".get_class($socket));
        }
        $socket = $socketContext->getSocket();
        */
        //$this->socket->crypto(STREAM_CRYPTO_METHOD_TLS_CLIENT, false);
        //$this->reconnect();
        //$b = $this->toStream();
        //$this->process();
        echo "Open session...\n";
        echo "Auth-ID: ", $this->loginID, "\n";
        $bf = "<message from='".$this->loginID."' to='atm.luo@gmail.com' type='chat'><body>Hello, This is ".$this->loginID."!</body></message>";

        $outputStream = $this->socket->getOutputStream();
        $outputStream->write($bf);
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
       
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
       
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
        /*
        try {
            $this->socketContext->notify();
        } catch (Exception $e) {
            echo "Fail to request authentication: " . $e->getMessage() . "\n";
            return false;
        }
        */
        return true;
    }

    /*
    //
    $bf = "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'><required /></starttls>";
    $b = socket_write($socket, $bf);
    if ($b == false) {
        $error_code = socket_last_error();
        $message = socket_strerror($error_code);
        echo "Error occurs while writing data: ", $bf, "\n";
    }
    echo "Ok to write data: ", $bf, "\n";

    try {
        $socketContext->notify();
    } catch (Exception $e) {
        echo "Exception: " . $e->getMessage() . "\n";
    }

    //$bf = socket_read($socket, 1024);
    //echo "Ok to read data: ", $bf, "\n";

    //

    $bf = '<stream:stream to="gmail.com" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
    $b = socket_write($socket, $bf);
    if ($b == false) {
        $error_code = socket_last_error();
        $message = socket_strerror($error_code);
        echo "Error occurs while writing data: ", $bf, "\n";
    }
    echo "Ok to write data: ", $bf, "\n";

    try {
        $socketContext->notify();
    } catch (Exception $e) {
        echo "Exception: " . $e->getMessage() . "\n";
    }

    //$bf = socket_read($socket, 1024);
    //echo "Ok to read data: ", $bf, "\n";

    //
    $bf = "<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>AGx1b2l4MDQyMwBqeGNvY28xMTI4</auth>";
    $b = socket_write($socket, $bf);
    if ($b == false) {
        $error_code = socket_last_error();
        $message = socket_strerror($error_code);
        echo "Error occurs while writing data: ", $bf, "\n";
    }
    echo "Ok to write data: ", $bf, "\n";

    try {
        $socketContext->notify();
    } catch (Exception $e) {
        echo "Exception: " . $e->getMessage() . "\n";
    }

    //$bf = socket_read($socket, 1024);
    //echo "Ok to read data: ", $bf, "\n";
    */
}
/*
function connect($targetAddress, $targetPort) {
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if (! $socket) {
        $error_code = socket_last_error();
        $message = socket_strerror($error_code);
        echo "Error occurs while create socket... Error, error code: ".$error_code.", error message: ".$message."\n";
        return false;
    }
    echo "Socket create... "."Ok\n";
    echo "---------", "---------", "\n";
   
    //
    $b = socket_bind($socket, "localhost", 1192);
    if (! $b) {
        $error_code = socket_last_error();
        $message = socket_strerror($error_code);
        echo "Failure to bind socket to localhost:", 1192, "... Failure, error code: ".$error_code.", error message: ".$message."\n";
    }
    echo "Bind socket to localhost:", 1192, "... Ok\n";
    //
   
    echo "Connecting to ".$targetAddress.":".$targetPort."...\n";
    $b = socket_connect($socket, $targetAddress, $targetPort);
    if ($b == false) {
        $error_code = socket_last_error();
        $message = socket_strerror($error_code);
        echo "Failure to connect $targetAddress:", $targetPort, "... Failure, error code: ".$error_code.", error message: ".$message."\n";
        return false;
    }
    return $socket;
}
*/

/**
 *
 *
 *
 *
 *
 */
class GTalk {

    /*
    // talk.google.com:5222
    // talk.google.com:5223
    *
    *
    *
    *
    */
    var $targetAddress = "talk.google.com";

    /*
     *
     *
     *
     *
     *
     */
    var $targetPort = 5222;
   
    /*
     *
     *
     *
     *
     *
     *
     */
    var $endpoint;

    /*
     *
     *
     *
     *
     *
     *
     */
    function __construct() {
        $this->init();
    }

    /*
     *
     *
     *
     *
     *
     *
     */
    function init() {
        $this->endpoint = new XMPPEndpoint($this->targetAddress, $this->targetPort);
        $this->endpoint->connect();
    }

    /*
     *
     *
     *
     *
     *
     *
     */
    function start() {
       
        $this->endpoint->openStream();
        $this->endpoint->startTLS();
        $this->endpoint->getAuthenticateRequest();
        $this->endpoint->setBind();
        $this->endpoint->openSession();
        $this->endpoint->messaging();
    }
}

/*
 *
 *
 *
 *
 *
 *
 */
$gtalk = new GTalk();
$gtalk->start();


?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值