Example #1
0
 public function testCancelConnectionDuringSessionWillCloseStream()
 {
     $stream = $this->getMockBuilder('React\\Stream\\Stream')->disableOriginalConstructor()->getMock();
     $stream->expects($this->once())->method('close');
     $promise = new Promise(function ($resolve) use($stream) {
         $resolve($stream);
     });
     $this->connector->expects($this->once())->method('create')->with('127.0.0.1', 1080)->willReturn($promise);
     $promise = $this->client->create('google.com', 80);
     $promise->cancel();
     $this->expectPromiseReject($promise);
 }
Example #2
0
 public function runPing($socket, App $app)
 {
     try {
         $parsed = $app->parseSocksSocket($socket);
     } catch (Exception $e) {
         echo 'error: invalid ping target: ' . $e->getMessage() . PHP_EOL;
         return;
     }
     // TODO: remove hack
     // resolver can not resolve 'localhost' ATM
     if ($parsed['host'] === 'localhost') {
         $parsed['host'] = '127.0.0.1';
     }
     $direct = new Connector($app->getLoop(), $app->getResolver());
     $via = new Client($parsed['host'] . ':' . $parsed['port'], $app->getLoop(), $direct, $app->getResolver());
     if (isset($parsed['protocolVersion'])) {
         try {
             $via->setProtocolVersion($parsed['protocolVersion']);
         } catch (Exception $e) {
             echo 'error: invalid protocol version: ' . $e->getMessage() . PHP_EOL;
             return;
         }
     }
     if (isset($parsed['user']) || isset($parsed['pass'])) {
         $parsed += array('user' => '', 'pass' => '');
         try {
             $via->setAuth($parsed['user'], $parsed['pass']);
         } catch (Exception $e) {
             echo 'error: invalid authentication info: ' . $e->getMessage() . PHP_EOL;
             return;
         }
     }
     try {
         $via->setResolveLocal(false);
     } catch (UnexpectedValueException $ignore) {
         // ignore in case it's not allowed (SOCKS4 client)
     }
     $this->pingEcho($via->createConnector(), 'www.google.com', 80);
 }
Example #3
0
 /**
  * P2P constructor.
  * @param Container $container
  */
 public function __construct(Container $container)
 {
     $this->loop = $container['loop'];
     $this->config = $container['config'];
     $this->peerStates = $container['p2p.states'];
     /** @var ConnectionParams $params */
     $params = $container['p2p.params'];
     $factory = new Factory($this->loop, Bitcoin::getNetwork());
     $dns = $factory->getDns();
     $messages = $factory->getMessages();
     if ((bool) $this->config->getItem('config', 'tor', false)) {
         $socks = new Client('127.0.0.1:9050', $this->loop);
         $socks->setResolveLocal(false);
         $this->connector = new Connector($messages, $params, $this->loop, $dns, $socks->createConnector());
     } else {
         $this->connector = new Connector($messages, $params, $this->loop, $dns);
     }
     $this->manager = new Manager($this->connector);
     $this->locator = new Locator($dns);
     // Setup listener if required
     if ($this->config->getItem('config', 'listen', '0')) {
         $listener = new Listener($params, $messages, new Server($this->loop), $this->loop);
         $this->manager->registerListener($listener);
     }
     /**
      * @var Peers $peersInbound
      * @var Peers $peersOutbound
      * @var DebugInterface $debug
      */
     $debug = $container['debug'];
     $peersInbound = $container['p2p.inbound'];
     $peersOutbound = $container['p2p.outbound'];
     $this->manager->on('outbound', function (Peer $peer) use($peersOutbound, $debug) {
         $addr = $peer->getRemoteAddress();
         $debug->log('p2p.outbound', ['peer' => ['ip' => $addr->getIp(), 'port' => $addr->getPort(), 'services' => $this->decodeServices($addr->getServices())]]);
         $this->setupPeer($peer);
         $peersOutbound->add($peer);
         $peer->on('close', [$this, 'onPeerClose']);
     });
     $this->manager->on('inbound', function (Peer $peer) use($peersInbound, $debug) {
         $addr = $peer->getRemoteAddress();
         $debug->log('p2p.inbound', ['peer' => ['ip' => $addr->getIp(), 'port' => $addr->getPort()]]);
         $this->setupPeer($peer);
         $peersInbound->add($peer);
     });
     $this->manager->on('outbound', [$this, 'onOutBoundPeer']);
     $this->manager->on('inbound', [$this, 'onInboundPeer']);
 }
Example #4
0
File: App.php Project: clue/psocksd
 public function createConnectionManager($socket)
 {
     if ($socket === 'reject') {
         $this->stdio->write('reject' . PHP_EOL);
         return new ConnectionManagerLabeled(new ConnectionManagerReject(), '-reject-');
     }
     $direct = new Connector($this->loop, $this->resolver);
     if ($socket === 'none') {
         $via = new ConnectionManagerLabeled($direct, '-direct-');
         $this->stdio->write('use direct connection to target' . PHP_EOL);
     } else {
         $parsed = $this->parseSocksSocket($socket);
         // TODO: remove hack
         // resolver can not resolve 'localhost' ATM
         if ($parsed['host'] === 'localhost') {
             $parsed['host'] = '127.0.0.1';
         }
         $via = new Client($parsed['host'] . ':' . $parsed['port'], $this->loop, $direct, $this->resolver);
         if (isset($parsed['protocolVersion'])) {
             try {
                 $via->setProtocolVersion($parsed['protocolVersion']);
             } catch (Exception $e) {
                 throw new Exception('invalid protocol version: ' . $e->getMessage());
             }
         }
         if (isset($parsed['user']) || isset($parsed['pass'])) {
             $parsed += array('user' => '', 'pass' => '');
             try {
                 $via->setAuth($parsed['user'], $parsed['pass']);
             } catch (Exception $e) {
                 throw new Exception('invalid authentication info: ' . $e->getMessage());
             }
         }
         $this->stdio->write('use ' . $this->reverseSocksSocket($parsed) . ' as next hop');
         try {
             $via->setResolveLocal(false);
             $this->stdio->write(' (resolve remotely)');
         } catch (UnexpectedValueException $ignore) {
             // ignore in case it's not allowed (SOCKS4 client)
             $this->stdio->write(' (resolve locally)');
         }
         $via = new ConnectionManagerLabeled($via->createConnector(), $this->reverseSocksSocket($parsed));
         $this->stdio->write(PHP_EOL);
     }
     return $via;
 }