手动添加域名

This commit is contained in:
root
2026-07-06 10:34:34 +08:00
parent c3c58198ab
commit 7efb3e6eb5
11 changed files with 728 additions and 141 deletions
+62 -10
View File
@@ -29,27 +29,33 @@ class DomainProvisioningService
/**
* 添加域名:本地入库 + 可选创建 Cloudflare Zone
*
* @return array{ok:bool,error?:string,hostname?:string,id?:int,nameservers?:array,cf_status?:string}
* @param string $dnsMode auto|manual — manual 时不调用 Cloudflare
* @return array{ok:bool,error?:string,hostname?:string,id?:int,nameservers?:array,cf_status?:string,dns_mode?:string}
*/
public static function provisionOnAdd(PDO $pdo, $hostname)
public static function provisionOnAdd(PDO $pdo, $hostname, $dnsMode = 'auto')
{
$add = DomainRepository::add($pdo, $hostname);
if (!$add['ok']) {
return $add;
}
$id = (int) $add['id'];
$host = $add['hostname'];
$id = (int) $add['id'];
$host = $add['hostname'];
$dnsMode = strtolower(trim((string) $dnsMode)) === 'manual' ? 'manual' : 'auto';
if (!CloudflareConfig::isEnabled()) {
if ($dnsMode === 'manual' || !CloudflareConfig::isEnabled()) {
$status = $dnsMode === 'manual' ? 'manual' : 'local';
DomainRepository::updateCloudflareMeta($pdo, $id, [
'cf_status' => 'local',
'cf_status' => $status,
'cf_error' => '',
]);
return [
'ok' => true,
'id' => $id,
'hostname' => $host,
'cf_status' => 'local',
'ok' => true,
'id' => $id,
'hostname' => $host,
'cf_status' => $status,
'dns_mode' => $dnsMode,
'nameservers' => [],
];
}
@@ -84,10 +90,52 @@ class DomainProvisioningService
'id' => $id,
'hostname' => $host,
'cf_status' => 'pending_ns',
'dns_mode' => 'auto',
'nameservers' => $ns,
];
}
/**
* 批量添加域名
*
* @return array{ok:bool,error?:string,added?:array,failed?:array,dns_mode?:string}
*/
public static function provisionBatch(PDO $pdo, $hostnameText, $dnsMode = 'auto')
{
$hosts = DomainRepository::parseHostnameBatch($hostnameText);
if ($hosts === []) {
return ['ok' => false, 'error' => '请至少输入一个有效域名(每行一个)'];
}
$dnsMode = strtolower(trim((string) $dnsMode)) === 'manual' ? 'manual' : 'auto';
$added = [];
$failed = [];
foreach ($hosts as $host) {
$res = self::provisionOnAdd($pdo, $host, $dnsMode);
if (!empty($res['ok'])) {
$added[] = $res;
} else {
$failed[] = [
'hostname' => $host,
'error' => $res['error'] ?? '添加失败',
];
}
}
if ($added === []) {
$firstErr = $failed[0]['error'] ?? '添加失败';
return ['ok' => false, 'error' => $firstErr, 'added' => [], 'failed' => $failed, 'dns_mode' => $dnsMode];
}
return [
'ok' => true,
'added' => $added,
'failed' => $failed,
'dns_mode' => $dnsMode,
];
}
/**
* 手动检测 NS 是否生效,生效后配置 A 记录与 SSL
*
@@ -100,6 +148,10 @@ class DomainProvisioningService
return ['ok' => false, 'message' => '域名不存在'];
}
if (($row['cf_status'] ?? '') === 'manual') {
return ['ok' => false, 'message' => '该域名为手动解析模式,无需 Cloudflare 检测'];
}
if (!CloudflareConfig::isEnabled()) {
return ['ok' => false, 'message' => '未配置 Cloudflare API,无法检测'];
}