chore: sdk

This commit is contained in:
xiaojunnuo
2026-05-14 18:31:25 +08:00
parent f8f51adf88
commit 639756dfcd
11 changed files with 874 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
<?php
class CertdClient
{
private string $keyId;
private string $keySecret;
private string $baseUrl;
private bool $encrypt;
private string $signType;
public function __construct(string $keyId, string $keySecret, array $options = [])
{
if ($keyId === '') {
throw new InvalidArgumentException('keyId is required');
}
if ($keySecret === '') {
throw new InvalidArgumentException('keySecret is required');
}
$this->keyId = $keyId;
$this->keySecret = $keySecret;
$this->baseUrl = rtrim($options['baseUrl'] ?? 'http://127.0.0.1:7001', '/');
$this->encrypt = $options['encrypt'] ?? false;
$this->signType = $options['signType'] ?? 'md5';
}
public function getSign(string $content): string
{
if ($this->signType !== 'md5') {
throw new InvalidArgumentException("Unsupported signType: {$this->signType}");
}
return md5($content . $this->keySecret);
}
public function getToken(?bool $encrypt = null): string
{
$content = json_encode([
'keyId' => $this->keyId,
't' => time(),
'encrypt' => $encrypt ?? $this->encrypt,
'signType' => $this->signType,
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$sign = $this->getSign($content);
return base64_encode($content) . '.' . base64_encode($sign);
}
public function request(string $path, array $body = [], ?bool $encrypt = null): string
{
if (!function_exists('curl_init')) {
throw new RuntimeException('PHP curl extension is required');
}
$payload = json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$headers = [
'Content-Type: application/json',
'x-certd-token: ' . $this->getToken($encrypt),
];
$ch = curl_init($this->baseUrl . $path);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 60,
]);
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException(curl_error($ch));
}
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode < 200 || $statusCode >= 300) {
throw new RuntimeException("HTTP {$statusCode}: {$response}");
}
return $response;
}
public function getCert(array $params): string
{
return $this->request('/api/v1/cert/get', $params);
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
require_once __DIR__ . '/CertdClient.php';
function require_env(string $name): string
{
$value = getenv($name);
if ($value === false || $value === '') {
throw new RuntimeException("Missing environment variable: {$name}");
}
return $value;
}
function bool_env(string $name, bool $default = false): bool
{
$value = getenv($name);
if ($value === false || $value === '') {
return $default;
}
return in_array(strtolower($value), ['1', 'true', 'yes', 'y'], true);
}
try {
$client = new CertdClient(require_env('CERTD_KEY_ID'), require_env('CERTD_KEY_SECRET'), [
'baseUrl' => getenv('CERTD_BASE_URL') ?: 'http://127.0.0.1:7001',
'encrypt' => bool_env('CERTD_ENCRYPT'),
]);
$params = [
'autoApply' => bool_env('CERTD_AUTO_APPLY'),
];
if (getenv('CERTD_CERT_ID')) {
if (!ctype_digit(getenv('CERTD_CERT_ID'))) {
throw new RuntimeException('CERTD_CERT_ID must be a positive integer');
}
$params['certId'] = intval(getenv('CERTD_CERT_ID'));
}
if (getenv('CERTD_DOMAINS')) {
$params['domains'] = getenv('CERTD_DOMAINS');
}
if (getenv('CERTD_FORMAT')) {
$params['format'] = getenv('CERTD_FORMAT');
}
if (empty($params['certId']) && empty($params['domains'])) {
throw new RuntimeException('Set CERTD_CERT_ID or CERTD_DOMAINS');
}
echo $client->getCert($params) . PHP_EOL;
} catch (Throwable $e) {
fwrite(STDERR, $e->getMessage() . PHP_EOL);
exit(1);
}