Пример #1
0
 /** @test */
 public function gettingEncryptedStuffFromGoogleShouldWork()
 {
     $loop = new StreamSelectLoop();
     $factory = new Factory();
     $dns = $factory->create('8.8.8.8', $loop);
     $connected = false;
     $response = null;
     $secureConnector = new SecureConnector(new Connector($loop, $dns), $loop);
     $secureConnector->create('google.com', 443)->then(function ($conn) use(&$connected) {
         $connected = true;
         $conn->write("GET / HTTP/1.0\r\n\r\n");
         return BufferedSink::createPromise($conn);
     })->then(function ($data) use(&$response) {
         $response = $data;
     });
     $loop->run();
     $this->assertTrue($connected);
     $this->assertRegExp('#^HTTP/1\\.0#', $response);
 }
Пример #2
0
 private function createImapstream(&$imapaccount)
 {
     echo 'createImapstream';
     //$loop = \React\EventLoop\Factory::create();
     $dnsResolverFactory = new Factory();
     $dns = $dnsResolverFactory->createCached('8.8.8.8', $this->loop);
     $imapconnector = new Connector($this->loop, $dns);
     /** @var Mailaccount $mailaccount */
     $mailaccount = $imapaccount['mailaccount'];
     $imapport = $mailaccount->getImapport() ? $mailaccount->getImapport() : 993;
     //$imapport=143;
     if ($imapport == 993) {
         //secure
         $secureConnector = new SecureConnector($imapconnector, $this->loop);
         $conn = $secureConnector->create($mailaccount->getImapserver(), $imapport);
     } else {
         $conn = $imapconnector->create($mailaccount->getImapserver(), $imapport);
     }
     echo $mailaccount->getImapserver() . $imapport;
     $conn->then(function (\React\Stream\Stream $imapstream) use(&$imapaccount) {
         echo 'Jojo';
         $uid = uniqid();
         echo $imapaccount['mailaccount']->getImapserver() . ': ' . $uid;
         $imapaccount['imapstream'] = $imapstream;
         $login = $imapaccount['mailaccount']->getImapusername();
         $password = $imapaccount['mailaccount']->getImappassword();
         $imapstream->write($uid . " LOGIN {$login} {$password}\r\n");
         $status = 'LOGIN';
         $imapstream->on('data', function ($data) use($uid, &$status, &$imapstream, &$imapaccount) {
             echo $imapaccount['mailaccount']->getImapserver() . ': ' . $data;
             $dataexpl = explode("\r\n", $data);
             foreach ($dataexpl as $dexpl) {
                 if (preg_match("/^" . $uid . " OK/", $dexpl)) {
                     //login OK:
                     if ($status == 'LOGIN') {
                         $imapstream->write($uid . " SELECT " . $imapaccount['mailaccount']->getImappathprefix() . "\r\n");
                         $status = 'SELECT';
                         echo "SEND: SELECT {$status}\r\n";
                     } else {
                         if ($status == 'SELECT') {
                             $imapstream->write($uid . " IDLE\r\n");
                             $status = 'IDLE';
                             echo "SEND: IDLE {$status}\r\n";
                         }
                     }
                 }
                 if ($status == 'IDLE') {
                     if (preg_match("/^\\* (\\d+) RECENT/", $dexpl, $countrecent)) {
                         //login OK:
                         $countrecent = $countrecent[1];
                         echo 'RECENT:' . $countrecent;
                         $this->notifychanges($imapaccount, ['mailaccount_id' => $imapaccount['mailaccount']->getId(), 'recent' => $countrecent]);
                     }
                     if (preg_match("/^\\* (\\d+) EXISTS/", $dexpl, $countexists)) {
                         //login OK:
                         $countexists = $countexists[1];
                         echo 'EXISTS' . $countexists;
                         $this->notifychanges($imapaccount, ['mailaccount_id' => $imapaccount['mailaccount']->getId(), 'exists' => $countexists]);
                     }
                 }
             }
         });
         $imapstream->on('end', function () use($uid, &$status, &$imapstream, &$imapaccount) {
             echo 'END!!!!!';
             $this->createImapstream($imapaccount);
         });
     }, function (\Exception $error) use(&$imapaccount) {
         echo "Call Error: \n";
         dump($error->getMessage());
         echo "trying again...\n";
         $this->createImapstream($imapaccount);
     });
     return $imapaccount;
 }
Пример #3
0
 public function testSecureConnectorInvalidPlaintextIsNotSsl()
 {
     if (!function_exists('stream_socket_enable_crypto')) {
         $this->markTestSkipped('Required function does not exist in your environment (HHVM?)');
     }
     $ssl = new SecureConnector($this->client, $this->loop);
     $this->assertRejectPromise($ssl->create('www.google.com', 80));
 }
Пример #4
0
 public function connect()
 {
     $connector = new Connector($this->loop, $this->resolver);
     switch ($this->uri->scheme) {
         case 'ws':
             $port = isset($this->uri->port) ? $this->uri->port : self::PORT_DEFAULT_HTTP;
             break;
         case 'wss':
             $port = isset($this->uri->port) ? $this->uri->port : self::PORT_DEFAULT_HTTPS;
             //Upgrade the connector
             $connector = new SecureConnector($connector, $this->loop);
             break;
         default:
             throw new \InvalidArgumentException(sprintf('Invalid scheme [%s]', $this->uri->scheme));
     }
     $that = $this;
     $connector->create($this->uri->host, $port)->then(function (DuplexStreamInterface $stream) use($that) {
         $that->transport = new $that->protocol($that, $stream);
         $that->transport->upgrade();
     });
     $this->setState(self::STATE_CONNECTING);
 }
Пример #5
0
<?php

use React\Stream\Stream;
use Clue\React\Socks\Client;
use React\SocketClient\TcpConnector;
use React\SocketClient\SecureConnector;
include_once __DIR__ . '/../vendor/autoload.php';
$first = isset($argv[1]) ? $argv[1] : 9050;
$second = isset($argv[2]) ? $argv[2] : $first;
$loop = React\EventLoop\Factory::create();
// https via the proxy chain  "foo -> bar -> target"
// please note how the client uses bar (not foo!), which in turn then uses foo
// this creates a TCP/IP connection to foo, which then connects to bar, which then connects to the target
$foo = new Client('127.0.0.1:' . $first, new TcpConnector($loop));
$bar = new Client('127.0.0.1:' . $second, $foo);
$ssl = new SecureConnector($bar, $loop);
echo 'Demo SOCKS client connecting to SOCKS proxy server chain 127.0.0.1:' . $first . ' and 127.0.0.1:' . $second . PHP_EOL;
echo 'Not already running a SOCKS server? Try this: ssh -D ' . $first . ' localhost' . PHP_EOL;
$ssl->create('www.google.com', 443)->then(function (Stream $stream) {
    echo 'connected' . PHP_EOL;
    $stream->write("GET / HTTP/1.0\r\n\r\n");
    $stream->on('data', function ($data) {
        echo $data;
    });
}, 'printf');
$loop->run();