public function doExecute(Manager $args) { $email = $args->get("email"); (yield \Amp\resolve($this->checkEmail($email))); $server = \Kelunik\AcmeClient\resolveServer($args->get("server")); $keyFile = \Kelunik\AcmeClient\serverToKeyname($server); $path = "accounts/{$keyFile}.pem"; $bits = 4096; $keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage"))); $this->climate->br(); try { $keyPair = (yield $keyStore->get($path)); $this->climate->whisper(" Using existing private key ..."); } catch (KeyStoreException $e) { $this->climate->whisper(" No private key found, generating new one ..."); $keyPair = (new OpenSSLKeyGenerator())->generate($bits); $keyPair = (yield $keyStore->put($path, $keyPair)); $this->climate->whisper(" Generated new private key with {$bits} bits."); } $acme = $this->acmeFactory->build($server, $keyPair); $this->climate->whisper(" Registering with " . substr($server, 8) . " ..."); /** @var Registration $registration */ $registration = (yield $acme->register($email)); $this->climate->info(" Registration successful. Contacts: " . implode(", ", $registration->getContact())); $this->climate->br(); (yield new CoroutineResult(0)); }
private function doExecute(Manager $args) { if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { if (posix_geteuid() !== 0) { $processUser = posix_getpwnam(posix_geteuid()); $currentUsername = $processUser["name"]; $user = $args->get("user") ?: $currentUsername; if ($currentUsername !== $user) { throw new AcmeException("Running this script with --user only works as root!"); } } else { $user = $args->get("user") ?: "www-data"; } } $domains = array_map("trim", explode(":", str_replace([",", ";"], ":", $args->get("domains")))); (yield \Amp\resolve($this->checkDnsRecords($domains))); $docRoots = explode(PATH_SEPARATOR, str_replace("\\", "/", $args->get("path"))); $docRoots = array_map(function ($root) { return rtrim($root, "/"); }, $docRoots); if (count($domains) < count($docRoots)) { throw new AcmeException("Specified more document roots than domains."); } if (count($domains) > count($docRoots)) { $docRoots = array_merge($docRoots, array_fill(count($docRoots), count($domains) - count($docRoots), end($docRoots))); } $keyStore = new KeyStore(\Kelunik\AcmeClient\normalizePath($args->get("storage"))); $server = \Kelunik\AcmeClient\resolveServer($args->get("server")); $keyFile = \Kelunik\AcmeClient\serverToKeyname($server); try { $keyPair = (yield $keyStore->get("accounts/{$keyFile}.pem")); } catch (KeyStoreException $e) { throw new AcmeException("Account key not found, did you run 'bin/acme setup'?", 0, $e); } $this->climate->br(); $acme = $this->acmeFactory->build($server, $keyPair); $errors = []; $domainChunks = array_chunk($domains, 10, true); foreach ($domainChunks as $domainChunk) { $promises = []; foreach ($domainChunk as $i => $domain) { $promises[] = \Amp\resolve($this->solveChallenge($acme, $keyPair, $domain, $docRoots[$i])); } list($chunkErrors) = (yield \Amp\any($promises)); $errors += $chunkErrors; } if (!empty($errors)) { foreach ($errors as $error) { $this->climate->error($error->getMessage()); } throw new AcmeException("Issuance failed, not all challenges could be solved."); } $path = "certs/" . $keyFile . "/" . reset($domains) . "/key.pem"; $bits = $args->get("bits"); try { $keyPair = (yield $keyStore->get($path)); } catch (KeyStoreException $e) { $keyPair = (new OpenSSLKeyGenerator())->generate($bits); $keyPair = (yield $keyStore->put($path, $keyPair)); } $this->climate->br(); $this->climate->whisper(" Requesting certificate ..."); $location = (yield $acme->requestCertificate($keyPair, $domains)); $certificates = (yield $acme->pollForCertificate($location)); $path = \Kelunik\AcmeClient\normalizePath($args->get("storage")) . "/certs/" . $keyFile; $certificateStore = new CertificateStore($path); (yield $certificateStore->put($certificates)); $this->climate->info(" Successfully issued certificate."); $this->climate->info(" See {$path}/" . reset($domains)); $this->climate->br(); (yield new CoroutineResult(0)); }