From b52e0519445438aab31922122774f4e097bc4f07 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Sat, 23 Oct 2021 17:20:21 +0800 Subject: [PATCH] fix: several issues --- encryption365.php | 68 ++++++++++++++++++++++++++++++++++++++--------- install.sh | 4 ++- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/encryption365.php b/encryption365.php index 1f1530e..77fcce0 100644 --- a/encryption365.php +++ b/encryption365.php @@ -411,7 +411,7 @@ class Certificate { } private static function waitIssued($vendorId) { // 等待证书签发 - $maxTime = 20; + $maxTime = 40; $interval = 30; for ($i = 0; $i < 20; $i++) { Output::line('Let\'s wait ' . $interval . ' seconds and query certificate status...'); @@ -420,7 +420,7 @@ class Certificate { if ($detail['result'] !== "success") { Output::str('Fail to get certificate details => '); Output::line($detail['message'], 'red'); - continue; + break; } if ($detail['cert_status'] === 'issued_active') { Output::line('Certificate issue success', 'green'); @@ -442,6 +442,12 @@ class Certificate { Output::line('Issue certificate time out, you may validate again...', 'red'); return false; } + self::saveCert($host, $certInfo); + return true; + } + + public static function saveCert($host, $certInfo) { + $info = Storage::getInfo($host); $cert = $certInfo['cert_code']; $caCert = $certInfo['ca_code']; $startTime = $certInfo['created_at']; @@ -460,7 +466,6 @@ class Certificate { Output::line($startTime, 'sky-blue'); Output::str('Expire time: '); Output::line($endTime, 'sky-blue'); - return true; } public static function createCert($productId, $domains, $isEcc = false) { // 创建新的证书订单 @@ -480,7 +485,8 @@ class Certificate { } $vendorId = (string)$orderRlt['trustocean_id']; foreach ($orderRlt['dcv_info'] as $domain => $dcvInfo) { - $verifyLink = $dcvInfo['http_verifylink']; + preg_match('/^http:\/\/[\S]*?(\/[\S]+.txt)/', $dcvInfo['http_verifylink'], $match); + $verifyLink = 'http://{domain}' . $match[1]; $verifyContent = $dcvInfo['http_filecontent']; break; } @@ -546,7 +552,8 @@ class Certificate { } $vendorId = (string)$orderRlt['trustocean_id']; foreach ($orderRlt['dcv_info'] as $domain => $dcvInfo) { - $verifyLink = $dcvInfo['http_verifylink']; + preg_match('/^http:\/\/[\S]*?(\/[\S]+.txt)/', $dcvInfo['http_verifylink'], $match); + $verifyLink = 'http://{domain}' . $match[1]; $verifyContent = $dcvInfo['http_filecontent']; break; } @@ -666,17 +673,17 @@ class LoginCtr { $result = Encryption365::clientLogin($email, $passwd); if ($result['result'] !== 'success') { Output::str('Fail to login: '); - Output::str($result['message'], 'red'); + Output::line($result['message'], 'red'); return; } - Output::str('Login success' . PHP_EOL, 'green'); + Output::line('Login success', 'green'); Output::line('Account status: ' . $result['status']); Output::line('Login time: ' . $result['created_at']); Storage::setClientInfo($email, $result['client_id'], $result['access_token']); } } -class listCtr { +class ListCtr { public static function list() { // 列出所有证书 $list = Storage::getHostList(); if (count($list) === 0) { @@ -689,14 +696,22 @@ class listCtr { Output::str($info['host'] . "\t", 'yellow'); Output::str($info['vendorId'] . "\t", 'sky-blue'); Output::str(implode('/', $info['domains']) . "\t", 'purple'); - Output::str($info['createTime'] . "\t", 'blue'); - Output::str($info['expireTime'] . "\t", 'blue'); + if (isset($info['createTime'])) { + Output::str($info['createTime'] . "\t", 'blue'); + } else { + Output::str('-' . "\t\t", 'blue'); + } + if (isset($info['expireTime'])) { + Output::str($info['expireTime'] . "\t", 'blue'); + } else { + Output::str('-' . "\t\t", 'blue'); + } Output::line(''); } } } -class issueCtr { +class IssueCtr { public static function entry($params) { if (count($params) === 0) { Output::line('You must specify encryption method.', 'red'); @@ -726,7 +741,8 @@ class issueCtr { Output::line('Illegal product ID.', 'red'); return; } - Certificate::createNewOrder($productId, $domains, $isEcc); + Storage::addHost($domains[0]); + Certificate::createCert($productId, $domains, $isEcc); } private function isHost($host) { // 判断host是否合法 @@ -775,6 +791,31 @@ class ReverifyCtr { } } +class FlashCtr { + public static function entry($params) { + if (count($params) === 0) { + Output::line('Host must be specified.', 'red'); + return; + } + if (count($params) > 1) { + Output::line('Too many parameters to flash certificate.', 'red'); + return; + } + $host = $params[0]; + $info = Storage::getInfo($host); + $detail = Encryption365::certDetails($info['vendorId']); + if ($detail['result'] !== "success") { + Output::str('Fail to flash certificate => '); + Output::line($detail['message'], 'red'); + } + Output::str('Certificate status: '); + Output::line($detail['cert_status'], 'sky-blue'); + if ($detail['cert_status'] === 'issued_active') { + Certificate::saveCert($host, $detail); + } + } +} + class RenewCtr { public static function entry($params) { if (count($params) === 0) { @@ -921,6 +962,9 @@ function main($argv) { // 脚本入口 case 'reverify': ReverifyCtr::entry($params); break; + case 'flash': + FlashCtr::entry($params); + break; case 'renew': RenewCtr::entry($params); break; diff --git a/install.sh b/install.sh index 8876856..5edf437 100755 --- a/install.sh +++ b/install.sh @@ -28,7 +28,9 @@ echo -e "\033[32m OK\033[0m\n" echo -e "\033[33mPlease ensure that the following modules are exist\033[0m" echo -e "\033[36mphp / php-cli / php-fpm / php-json / php-openssl / php-mbstring\033[0m\n" -echo "0 * * * * encryption365 autorenew" >> /var/spool/cron/root +if [ ! -n `crontab -l | grep -o encryption365` ]; then + echo "0 * * * * encryption365 autorenew" >> /var/spool/cron/root +fi echo -e "\033[33mYou can use \"\033[0m\033[36mencryption365 help\033[0m\033[33m\" command for details.\033[0m\n" echo -e "\033[32mInstall OK\033[0m"