继续初始化测试前保存启动链路与页面迁移检查点
This commit is contained in:
1 parent
34d1fb88e5
commit
199d727ecc
20 files changed
+891
-67
No files matched your search
@@ -0,0 +1,26 @@
|
||||
// 首次初始化只提交验证令牌,不接受浏览器指定敏感凭据引用或服务端路径。
|
||||
export function bootstrapPayload(legacy, databaseToken, environment) {
|
||||
const notification = {};
|
||||
for (const [key, value] of Object.entries(legacy)) {
|
||||
if (key.startsWith("email_") && key !== "email_http_credential_ref") {
|
||||
notification[key.slice(6)] = value;
|
||||
}
|
||||
}
|
||||
notification.recipients = legacy.recipients;
|
||||
return {
|
||||
database_verification_token: databaseToken,
|
||||
dns_verification_token: legacy.dns_verification_token,
|
||||
admin_username: legacy.admin_username,
|
||||
admin_email: legacy.admin_email,
|
||||
admin_password: legacy.admin_password,
|
||||
server_port: legacy.server_port,
|
||||
web_hostname: legacy.web_hostname,
|
||||
web_public_ip: legacy.web_public_ip,
|
||||
web_enabled: legacy.web_enabled,
|
||||
https_mode: legacy.https_mode,
|
||||
acme_environment: environment,
|
||||
propagation_timeout_secs: legacy.propagation_timeout_secs,
|
||||
propagation_polling_interval_secs: legacy.propagation_polling_interval_secs,
|
||||
notification,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import { computed, onUnmounted, ref, watch } from "vue";
|
||||
|
||||
export function useDatabaseSetup(clock) {
|
||||
const databaseForm = ref({ driver: "sqlite", host: "", port: 3306, database: "scert", username: "", password: "", root_certificate: "" });
|
||||
const databaseTesting = ref(false);
|
||||
const databaseError = ref("");
|
||||
const databaseVerification = ref(null);
|
||||
let version = 0;
|
||||
const databaseVerified = computed(() => {
|
||||
clock.value;
|
||||
return databaseVerification.value?.verified === true &&
|
||||
!!databaseVerification.value?.verification_token &&
|
||||
new Date(databaseVerification.value.expires_at).getTime() > Date.now();
|
||||
});
|
||||
function invalidateDatabase() {
|
||||
version += 1;
|
||||
databaseVerification.value = null;
|
||||
databaseTesting.value = false;
|
||||
databaseError.value = "";
|
||||
}
|
||||
watch(() => databaseForm.value.driver, (driver) => {
|
||||
databaseForm.value.port = driver === "postgres" ? 5432 : 3306;
|
||||
databaseForm.value.root_certificate = "";
|
||||
}, { flush: "sync" });
|
||||
watch(databaseForm, invalidateDatabase, { deep: true, flush: "sync" });
|
||||
async function testDatabase() {
|
||||
invalidateDatabase();
|
||||
const current = version;
|
||||
databaseTesting.value = true;
|
||||
const form = databaseForm.value;
|
||||
const payload = form.driver === "sqlite" ? { driver: "sqlite" } : {
|
||||
...form, port: Number(form.port), tls_mode: form.driver === "mysql" ? "true" : "verify-full",
|
||||
};
|
||||
try {
|
||||
const response = await fetch("/api/v1/bootstrap/database/test", {
|
||||
method: "POST", credentials: "same-origin",
|
||||
headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload),
|
||||
});
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (current !== version) return;
|
||||
if (!response.ok || !result.verified || !result.verification_token) {
|
||||
throw new Error(result.error || "数据库连接和写权限验证失败。");
|
||||
}
|
||||
databaseVerification.value = result;
|
||||
} catch (err) {
|
||||
if (current === version) databaseError.value = err.message || "数据库验证失败。";
|
||||
} finally {
|
||||
if (current === version) databaseTesting.value = false;
|
||||
}
|
||||
}
|
||||
onUnmounted(invalidateDatabase);
|
||||
return { databaseForm, databaseTesting, databaseError, databaseVerification, databaseVerified, testDatabase, invalidateDatabase };
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<script setup>
|
||||
defineProps({
|
||||
form: { type: Object, required: true },
|
||||
testing: Boolean,
|
||||
verified: Boolean,
|
||||
error: { type: String, default: "" },
|
||||
});
|
||||
const emit = defineEmits(["test"]);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="database-setup" aria-label="数据库存储配置">
|
||||
<div class="setup-section-heading">
|
||||
<strong>数据库存储</strong>
|
||||
<span>选择一种存储方式,验证连接和写权限后继续。不会清空已有数据。</span>
|
||||
</div>
|
||||
<div class="setup-grid two-columns">
|
||||
<label>
|
||||
<span>数据库类型</span>
|
||||
<select v-model="form.driver" :disabled="testing">
|
||||
<option value="sqlite">SQLite(本机文件,无需额外服务)</option>
|
||||
<option value="mysql">MySQL</option>
|
||||
<option value="postgres">PostgreSQL</option>
|
||||
</select>
|
||||
</label>
|
||||
<template v-if="form.driver !== 'sqlite'">
|
||||
<label><span>数据库主机</span><input v-model.trim="form.host" autocomplete="off" placeholder="db.example.com" /></label>
|
||||
<label><span>数据库端口</span><input v-model.number="form.port" type="number" min="1" max="65535" /></label>
|
||||
<label><span>数据库名称</span><input v-model.trim="form.database" autocomplete="off" /></label>
|
||||
<label><span>数据库用户名</span><input v-model.trim="form.username" autocomplete="off" /></label>
|
||||
<label><span>数据库密码</span><input v-model="form.password" type="password" autocomplete="new-password" /></label>
|
||||
<label v-if="form.driver === 'postgres'">
|
||||
<span>根证书路径(服务器绝对路径,可选)</span>
|
||||
<input v-model.trim="form.root_certificate" placeholder="使用系统信任库时留空" />
|
||||
</label>
|
||||
</template>
|
||||
</div>
|
||||
<p v-if="form.driver !== 'sqlite'">外部数据库必须启用 TLS 并校验证书和主机名,不支持跳过验证。请使用专属数据库与最小权限账号。</p>
|
||||
<div class="dns-test-actions">
|
||||
<button type="button" class="dns-test-button" :disabled="testing" @click="emit('test')">
|
||||
{{ testing ? "正在验证数据库,请等待(最长约 20 秒)……" : "验证数据库" }}
|
||||
</button>
|
||||
<span v-if="verified" role="status">数据库连接及写权限已验证。</span>
|
||||
</div>
|
||||
<p v-if="error" class="inline-error" role="alert">{{ error }}</p>
|
||||
</section>
|
||||
</template>
|
||||
@@ -1,4 +1,6 @@
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
import { useDatabaseSetup } from "./DatabaseSetup.js";
|
||||
import { bootstrapPayload } from "./BootstrapPayload.js";
|
||||
|
||||
const defaultDNSVerificationTimeoutSeconds = 60;
|
||||
const aliyunDNSVerificationTimeoutSeconds = 6;
|
||||
@@ -26,6 +28,8 @@ const commonEmailDomains = [
|
||||
|
||||
export function useSetupWizard(props, emit) {
|
||||
const scertLogo = "/Scert.png";
|
||||
const isBootstrap = props.status.mode === "bootstrap";
|
||||
const apiBase = isBootstrap ? "/api/v1/bootstrap" : "/api/v1/setup";
|
||||
|
||||
const submitting = ref(false);
|
||||
const error = ref("");
|
||||
@@ -44,6 +48,8 @@ export function useSetupWizard(props, emit) {
|
||||
const dnsTestDomain = ref(props.status.web_hostname || "");
|
||||
const dnsVerification = ref(null);
|
||||
const verificationClock = ref(Date.now());
|
||||
const database = useDatabaseSetup(verificationClock);
|
||||
const completed = ref(false);
|
||||
const currentStep = ref(1);
|
||||
const notificationAdvancedVisible = ref(false);
|
||||
const selectedEmailDomain = ref("");
|
||||
@@ -96,6 +102,7 @@ export function useSetupWizard(props, emit) {
|
||||
web_public_ip: props.status.web_public_ip || "",
|
||||
web_enabled: props.status.web_enabled !== false,
|
||||
https_mode: props.status.https_mode || "direct",
|
||||
acme_environment: props.status.acme_environment || "staging",
|
||||
});
|
||||
|
||||
const targetLabel = computed(() =>
|
||||
@@ -146,8 +153,8 @@ export function useSetupWizard(props, emit) {
|
||||
const dnsVerificationValid = computed(() => {
|
||||
verificationClock.value;
|
||||
if (
|
||||
!dnsVerification.value?.verification_token ||
|
||||
!dnsVerification.value?.credential_ref
|
||||
dnsVerification.value?.verified !== true || !dnsVerification.value?.verification_token ||
|
||||
(!isBootstrap && !dnsVerification.value?.credential_ref)
|
||||
)
|
||||
return false;
|
||||
return new Date(dnsVerification.value.expires_at).getTime() > Date.now();
|
||||
@@ -377,7 +384,7 @@ export function useSetupWizard(props, emit) {
|
||||
dnsCatalogLoading.value = true;
|
||||
dnsCatalogError.value = "";
|
||||
try {
|
||||
const response = await fetch("/api/v1/setup/dns/providers");
|
||||
const response = await fetch(`${apiBase}/dns/providers`);
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (!response.ok)
|
||||
throw new Error(result.error || "读取 DNS 服务商列表失败");
|
||||
@@ -465,7 +472,7 @@ export function useSetupWizard(props, emit) {
|
||||
dnsTesting.value = true;
|
||||
startDNSVerificationTimer(requestVersion, provider.id);
|
||||
try {
|
||||
const response = await fetch("/api/v1/setup/dns/test", {
|
||||
const response = await fetch(`${apiBase}/dns/test`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
@@ -535,6 +542,10 @@ export function useSetupWizard(props, emit) {
|
||||
error.value = `请填写${targetLabel.value}。`;
|
||||
return false;
|
||||
}
|
||||
if (isBootstrap && !database.databaseVerified.value) {
|
||||
error.value = "请先验证所选数据库的连接和写权限。";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (currentStep.value === 2) {
|
||||
@@ -572,10 +583,10 @@ export function useSetupWizard(props, emit) {
|
||||
if (
|
||||
form.value.email_http_enabled &&
|
||||
form.value.email_http_auth_mode !== "none" &&
|
||||
!form.value.email_http_credential_ref.trim() &&
|
||||
(isBootstrap || !form.value.email_http_credential_ref.trim()) &&
|
||||
!emailCredentials.value.trim()
|
||||
) {
|
||||
error.value = "启用 HTTP 通知认证后必须填写明文凭据或已有凭据引用。";
|
||||
error.value = "启用 HTTP 通知认证后必须填写有效凭据。";
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
@@ -674,11 +685,12 @@ export function useSetupWizard(props, emit) {
|
||||
}
|
||||
|
||||
function handleSubmit() {
|
||||
if (submitting.value || completed.value) return;
|
||||
if (currentStep.value < 4) {
|
||||
nextStep();
|
||||
return;
|
||||
}
|
||||
submit();
|
||||
return submit();
|
||||
}
|
||||
|
||||
function recipients() {
|
||||
@@ -703,6 +715,10 @@ export function useSetupWizard(props, emit) {
|
||||
async function submit() {
|
||||
error.value = "";
|
||||
resultMessage.value = "";
|
||||
if (isBootstrap && !database.databaseVerified.value) {
|
||||
error.value = "数据库验证结果无效或已过期,请重新验证。";
|
||||
return;
|
||||
}
|
||||
if (
|
||||
form.value.admin_password.length < 8 ||
|
||||
form.value.admin_password.length > 12
|
||||
@@ -755,13 +771,17 @@ export function useSetupWizard(props, emit) {
|
||||
error.value = `请填写${targetLabel.value}。`;
|
||||
return;
|
||||
}
|
||||
if (isBootstrap && hostname && hostname.toLowerCase().replace(/\.$/, "") !== dnsTestDomain.value.trim().toLowerCase().replace(/\.$/, "")) {
|
||||
error.value = "DNS 验证域名必须与管理页面域名一致,请重新验证。";
|
||||
return;
|
||||
}
|
||||
|
||||
submitting.value = true;
|
||||
try {
|
||||
const response = await fetch("/api/v1/setup", {
|
||||
const response = await fetch(isBootstrap ? `${apiBase}/complete` : apiBase, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
body: JSON.stringify((isBootstrap ? (payload) => bootstrapPayload(payload, database.databaseVerification.value.verification_token, form.value.acme_environment) : (payload) => payload)({
|
||||
admin_username: form.value.admin_username,
|
||||
admin_email: form.value.admin_email,
|
||||
admin_password: form.value.admin_password,
|
||||
@@ -809,14 +829,20 @@ export function useSetupWizard(props, emit) {
|
||||
web_public_ip: publicIP,
|
||||
web_enabled: form.value.web_enabled,
|
||||
https_mode: form.value.https_mode,
|
||||
}),
|
||||
})),
|
||||
});
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new Error(result.error || "保存初始化配置失败");
|
||||
resultMessage.value = result.message || "初始化配置已保存。";
|
||||
completed.value = true;
|
||||
resultMessage.value = isBootstrap ? `初始化配置已保存,正在切换至 ${result.management_url}。请稍候;IP 自签名证书或测试证书需要人工核对。` : result.message || "初始化配置已保存。";
|
||||
emit("initialized", result);
|
||||
} catch (err) {
|
||||
error.value = err.message || "保存初始化配置失败";
|
||||
if (isBootstrap) {
|
||||
// 服务端可能已领取令牌;失败重试必须重新验证,不能复用已消费状态。
|
||||
database.invalidateDatabase();
|
||||
invalidateDNSVerification();
|
||||
}
|
||||
} finally {
|
||||
submitting.value = false;
|
||||
}
|
||||
@@ -834,6 +860,10 @@ export function useSetupWizard(props, emit) {
|
||||
watch(dnsCredentials, invalidateDNSVerification, { deep: true, flush: "sync" });
|
||||
watch(dnsTestDomain, invalidateDNSVerification, { flush: "sync" });
|
||||
|
||||
watch(() => [targetType.value, form.value.web_hostname], ([target, hostname]) => {
|
||||
if (isBootstrap && target === "hostname") dnsTestDomain.value = hostname;
|
||||
}, { flush: "sync" });
|
||||
|
||||
onMounted(() => {
|
||||
loadDNSProviders();
|
||||
emailAutofillTimer = window.setInterval(() => {
|
||||
@@ -851,6 +881,9 @@ export function useSetupWizard(props, emit) {
|
||||
});
|
||||
|
||||
return {
|
||||
isBootstrap,
|
||||
completed,
|
||||
...database,
|
||||
scertLogo,
|
||||
submitting,
|
||||
error,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script setup>
|
||||
import { useSetupWizard } from "./SetupWizard.js";
|
||||
import AccessShell from "../components/AccessShell/AccessShell.vue";
|
||||
import DatabaseSetup from "./DatabaseSetup.vue";
|
||||
|
||||
const props = defineProps({
|
||||
status: {
|
||||
@@ -12,6 +13,13 @@ const props = defineProps({
|
||||
const emit = defineEmits(["initialized"]);
|
||||
|
||||
const {
|
||||
isBootstrap,
|
||||
completed,
|
||||
databaseForm,
|
||||
databaseTesting,
|
||||
databaseError,
|
||||
databaseVerified,
|
||||
testDatabase,
|
||||
scertLogo,
|
||||
submitting,
|
||||
error,
|
||||
@@ -38,6 +46,8 @@ const {
|
||||
selectedEmailDomain,
|
||||
emailLocalPart,
|
||||
emailInputElement,
|
||||
emailAutofillElement,
|
||||
handleEmailAutofill,
|
||||
targetLabel,
|
||||
resetURLPreview,
|
||||
emailCredentialLabel,
|
||||
@@ -81,7 +91,7 @@ const {
|
||||
<p class="setup-eyebrow">初始化向导</p>
|
||||
<h2>基础配置</h2>
|
||||
</div>
|
||||
<span class="local-badge">仅限本机</span>
|
||||
<span class="local-badge">{{ isBootstrap ? "受保护的 HTTPS 初始化" : "仅限本机" }}</span>
|
||||
</header>
|
||||
|
||||
<nav class="setup-progress" aria-label="初始化步骤">
|
||||
@@ -115,7 +125,7 @@ const {
|
||||
@submit.prevent="handleSubmit"
|
||||
>
|
||||
<div class="setup-step-grid">
|
||||
<fieldset v-if="currentStep === 1" class="setup-step-card">
|
||||
<fieldset v-if="currentStep === 1" class="setup-step-card" :disabled="submitting || completed">
|
||||
<legend><span>01</span>超级管理员</legend>
|
||||
<div class="setup-grid two-columns">
|
||||
<label
|
||||
@@ -264,6 +274,14 @@ const {
|
||||
/>
|
||||
</label>
|
||||
</div>
|
||||
<DatabaseSetup
|
||||
v-if="isBootstrap"
|
||||
:form="databaseForm"
|
||||
:testing="databaseTesting"
|
||||
:verified="databaseVerified"
|
||||
:error="databaseError"
|
||||
@test="testDatabase"
|
||||
/>
|
||||
<div class="provider-description reset-preview">
|
||||
<strong>密码重置地址预览</strong>
|
||||
<span>{{ resetURLPreview }}</span>
|
||||
@@ -513,7 +531,7 @@ const {
|
||||
placeholder="/api/login"
|
||||
/>
|
||||
</label>
|
||||
<label>
|
||||
<label v-if="!isBootstrap">
|
||||
<span>已有凭据引用(可选)</span>
|
||||
<input
|
||||
id="email-http-credential-ref"
|
||||
@@ -524,7 +542,7 @@ const {
|
||||
</div>
|
||||
|
||||
<div v-else class="notification-row notification-auth-row">
|
||||
<label>
|
||||
<label v-if="!isBootstrap">
|
||||
<span>已有凭据引用(可选)</span>
|
||||
<input
|
||||
id="email-http-credential-ref"
|
||||
@@ -532,7 +550,7 @@ const {
|
||||
placeholder="已有安全凭据引用"
|
||||
/>
|
||||
</label>
|
||||
<div class="notification-auth-note">
|
||||
<div v-if="form.email_http_auth_mode === 'none'" class="notification-auth-note">
|
||||
当前接口不启用认证,不需要填写凭据。
|
||||
</div>
|
||||
</div>
|
||||
@@ -865,9 +883,18 @@ const {
|
||||
id="certificate-root"
|
||||
name="certificate_root"
|
||||
v-model.trim="form.certificate_root"
|
||||
:readonly="isBootstrap"
|
||||
required
|
||||
/>
|
||||
</label>
|
||||
<label v-if="isBootstrap">
|
||||
<span>证书签发环境</span>
|
||||
<select v-model="form.acme_environment">
|
||||
<option value="staging">测试环境(证书不受公共信任)</option>
|
||||
<option value="production">正式环境(实际申请受信任域名证书)</option>
|
||||
</select>
|
||||
<small>IP 管理入口使用自签名证书,不会因选择正式环境而自动受信任。</small>
|
||||
</label>
|
||||
<label>
|
||||
<span>HTTPS 模式</span>
|
||||
<select
|
||||
@@ -887,6 +914,7 @@ const {
|
||||
id="web-enabled"
|
||||
name="web_enabled"
|
||||
v-model="form.web_enabled"
|
||||
:disabled="isBootstrap"
|
||||
type="checkbox"
|
||||
/>
|
||||
<span>
|
||||
@@ -901,17 +929,18 @@ const {
|
||||
<p v-if="resultMessage" class="setup-success">{{ resultMessage }}</p>
|
||||
<footer>
|
||||
<span
|
||||
>初始化程序将自动配置本机防火墙;云安全组只需放行主服务端口。配置完成前仅保存在内存中。</span
|
||||
>请放行初始化及正式服务端口。验证凭据加密保存;完成后关闭临时入口并切换正式管理地址。</span
|
||||
>
|
||||
<div class="wizard-actions">
|
||||
<button
|
||||
v-if="currentStep > 1"
|
||||
class="back-button"
|
||||
type="button"
|
||||
:disabled="submitting || completed"
|
||||
@click="previousStep"
|
||||
>
|
||||
上一步</button
|
||||
><button type="submit" :disabled="submitting || dnsTesting">
|
||||
><button type="submit" :disabled="submitting || dnsTesting || databaseTesting || completed">
|
||||
{{
|
||||
currentStep < 4
|
||||
? "下一步"
|
||||
|
||||
Reference in new issue
Block a user