/** * Run the server. * * @return void */ public function run() { $this->_server->considerRemoteAddress(true); $this->_server->connectAndWait(); while (true) { $buffer = $this->_server->read(1024); if (empty($buffer)) { continue; } // Skip header. $handle = substr($buffer, 12); $domain = null; // QNAME. for ($i = 0, $m = strlen($handle); $i < $m; ++$i) { if (0 === ($length = ord($handle[$i]))) { break; } if (null !== $domain) { $domain .= '.'; } $domain .= substr($handle, $i + 1, $length); $i += $length; } // QTYPE. $i += 2; $qtype = (int) (string) ord($handle[$i]) + (int) (string) ord($handle[$i + 1]); $type = array_search($qtype, static::$_types) ?: $qtype; // QCLASS. $i += 2; $qclass = (int) (string) ord($handle[$i]); $class = array_search($qclass, static::$_classes) ?: $qclass; $ips = $this->_on->fire('query', new Core\Event\Bucket(['domain' => $domain, 'type' => $type, 'class' => $class])); $ip = null; if (false === $ips[0]) { $this->_server->writeAll($buffer[0] . $buffer[1] . pack('C', 1 << 7 | 1) . pack('C', 0 | 3) . pack('n', 0) . pack('n', 0) . pack('n', 0) . pack('n', 0)); continue; } foreach (explode('.', $ips[0]) as $foo) { $ip .= pack('C', $foo); } $this->_server->writeAll($buffer[0] . $buffer[1] . pack('C', 1 << 7 | 1) . pack('C', 0) . $buffer[4] . $buffer[5] . pack('n', 1) . pack('n', 0) . pack('n', 0) . $handle . pack('CC', 192, 12) . pack('n', $qtype) . pack('n', $qclass) . pack('N', 60) . pack('n', 4) . $ip); } $this->_server->disconnect(); return; }
protected function _case_check($uri, $expect) { $this->given($this->constant->STREAM_PF_INET6 = true, $this->function->stream_get_transports = ['tcp', 'file'])->when($result = new SUT($uri))->then->integer($result->getAddressType())->isEqualTo($expect['type'])->string($result->getTransport())->isEqualTo($expect['transport'])->string($result->getAddress())->isEqualTo($expect['address'])->integer($result->getPort())->isEqualTo($expect['port']); }
/** * Compute an HTTP GET method. * * @param Request $request HTTP request. * @param Response $response HTTP response. * @return bool * @throws Exception\Dav\Exception */ function httpGet(Request $request, Response $response) { if (System\Collection::NAME . '/' . Node::NAME !== $request->getPath()) { return; } $queries = $request->getQueryParameters(); if (isset($queries['test']) && 'mail' === $queries['test'] && isset($queries['payload'])) { $payload = @json_decode($queries['payload']); if (!$payload || !isset($payload->transport) || !isset($payload->username) || !isset($payload->password)) { throw new Exception\Dav\Exception('Payload is corrupted.'); } Mail\Message::setDefaultTransport(new Mail\Transport\Smtp(new Socket\Client('tcp://' . $payload->transport), $payload->username, $payload->password)); $message = new Mail\Message(); $message['from'] = 'sabre/katana <' . $payload->username . '>'; $message['to'] = $payload->username; $message['subject'] = 'Test mail from sabre/katana'; $message->addContent(new Mail\Content\Text('Hey!' . "\n\n" . 'If you receive this email, it means that your ' . 'sabre/katana server is able to send emails! ' . "\n\n" . 'Congratulations :-).')); $message->send(); $response->setHeader('Content-Type', 'application/json'); $response->setBody(json_encode(true)); return false; } $configuration = ['database' => ['dsn' => $this->configuration->database->dsn, 'username' => $this->configuration->database->username], 'mail' => ['address' => '', 'port' => '', 'username' => '', 'password' => '']]; if (isset($this->configuration->mail)) { $socket = new Socket('tcp://' . $this->configuration->mail->transport); $configuration['mail']['address'] = $socket->getAddress(); $configuration['mail']['port'] = $socket->hasPort() ? $socket->getPort() : 587; $configuration['mail']['username'] = $this->configuration->mail->username; $configuration['mail']['password'] = $this->configuration->mail->password; unset($socket); } $response->setHeader('Content-Type', 'application/json'); $response->setBody(json_encode($configuration)); return false; }
/** * Constructor * * @param string $uri Socket URI. * @param boolean $secured Whether the connection is secured. */ public function __construct($uri, $secured = false) { parent::__construct($uri); $this->_secured = $secured; return; }