public function get($key) { if (!isset($this->data[$key])) { return When::reject(); } return When::resolve($this->data[$key]); }
protected function resolveHostname($host) { if (false !== filter_var($host, FILTER_VALIDATE_IP)) { return When::resolve($host); } return $this->resolver->resolve($host); }
/** @test */ public function resolveSouldProvideDefaultErrorbackToExecutor() { $executor = $this->createExecutorMock(); $executor->expects($this->once())->method('query')->with($this->anything(), $this->isInstanceOf('React\\Dns\\Query\\Query'))->will($this->returnCallback(function ($nameserver, $query) { return When::resolve(); })); $resolver = new Resolver('8.8.8.8:53', $executor); $resolver->resolve('igor.io')->then($this->expectCallableNever()); }
private function callQueryCallbackWithAddress($address) { return $this->returnCallback(function ($nameserver, $query) use($address) { $response = new Message(); $response->header->set('qr', 1); $response->questions[] = new Record($query->name, $query->type, $query->class); $response->answers[] = new Record($query->name, $query->type, $query->class, 3600, $address); return When::resolve($response); }); }
public function query($nameserver, Query $query) { $cachedRecords = $this->cache->lookup($query); if (count($cachedRecords)) { $cachedResponse = $this->buildResponse($query, $cachedRecords); return When::resolve($cachedResponse); } $cache = $this->cache; return $this->executor->query($nameserver, $query)->then(function ($response) use($cache, $query) { $cache->storeResponseMessage($query->currentTime, $response); return $response; }); }
public function parseEtcResolvConf($contents) { $nameservers = array(); $contents = preg_replace('/^#/', '', $contents); $lines = preg_split('/\\r?\\n/is', $contents); foreach ($lines as $line) { if (preg_match('/^nameserver (.+)/', $line, $match)) { $nameservers[] = $match[1]; } } $config = new Config(); $config->nameservers = $nameservers; return When::resolve($config); }
/** * @covers React\Dns\Query\RetryExecutor * @test */ public function queryShouldRetryQueryOnTimeout() { $response = $this->createStandardResponse(); $executor = $this->createExecutorMock(); $executor->expects($this->exactly(2))->method('query')->with('8.8.8.8', $this->isInstanceOf('React\\Dns\\Query\\Query'))->will($this->onConsecutiveCalls($this->returnCallback(function ($domain, $query) { return When::reject(new TimeoutException("timeout")); }), $this->returnCallback(function ($domain, $query) use($response) { return When::resolve($response); }))); $callback = $this->createCallableMock(); $callback->expects($this->once())->method('__invoke')->with($this->isInstanceOf('React\\Dns\\Model\\Message')); $errorback = $this->expectCallableNever(); $retryExecutor = new RetryExecutor($executor, 2); $query = new Query('igor.io', Message::TYPE_A, Message::CLASS_IN, 1345656451); $retryExecutor->query('8.8.8.8', $query)->then($callback, $errorback); }
public function setAuth($auth) { if (!is_callable($auth)) { throw new InvalidArgumentException('Given authenticator is not a valid callable'); } if ($this->protocolVersion !== null && $this->protocolVersion !== '5') { throw new UnexpectedValueException('Authentication requires SOCKS5. Consider using protocol version 5 or waive authentication'); } // wrap authentication callback in order to cast its return value to a promise $this->auth = function ($username, $password) use($auth) { $ret = call_user_func($auth, $username, $password); if ($ret instanceof PromiseInterface) { return $ret; } return $ret ? When::resolve() : When::reject(); }; }
/** * resolve given address via DNS if applicable * * Letting host names pass through will not break things, but it * requires a blocking resolution afterwards. So make sure to try to * resolve hostnames here. * * @param string $address * @return PromiseInterface * @todo use Resolver to perform async resolving */ private function resolve($address) { return When::resolve($address); }
private function resolve($host) { // return if it's already an IP or we want to resolve remotely (socks 4 only supports resolving locally) if (false !== filter_var($host, FILTER_VALIDATE_IP) || $this->protocolVersion !== '4' && !$this->resolveLocal) { return When::resolve($host); } return $this->resolver->resolve($host); }
private function resolve($host) { return When::resolve($host); }