196 lines
17 KiB
TypeScript
196 lines
17 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { Check, CheckCircle2, ChevronLeft, ChevronRight, Database, KeyRound, Server, ShieldCheck, UserRound } from 'lucide-react';
|
|
import { api, type DatabaseSetup, type InstallSetup, type PanelSetup } from '../api';
|
|
import { demoRuntimeVersions } from '../data';
|
|
import type { Runtime, RuntimeCapabilities, RuntimeCatalog } from '../types';
|
|
import { Brand } from './Brand';
|
|
|
|
const steps = [
|
|
{ label: '安装验证', icon: KeyRound },
|
|
{ label: '数据库', icon: Database },
|
|
{ label: '服务器', icon: Server },
|
|
{ label: '管理员', icon: UserRound },
|
|
];
|
|
const runtimes: Runtime[] = ['PHP', 'Node.js', 'Python', 'Go', 'Java'];
|
|
const runtimeStatusText = { available: '可用', not_installed: '未安装', probe_failed: '探测失败' } as const;
|
|
const demoRuntimeCapabilities = capabilitiesFromVersions(demoRuntimeVersions);
|
|
|
|
export function InstallWizard({ preview = false }: { preview?: boolean }) {
|
|
const [step, setStep] = useState(0);
|
|
const [controllerOnly,setControllerOnly]=useState(false);
|
|
const [token, setToken] = useState('');
|
|
const [database, setDatabase] = useState<DatabaseSetup>({ host: '127.0.0.1', port: 3306, name: 'sea_vhost', user: 'sea_vhost', password: '' });
|
|
const [panel, setPanel] = useState<PanelSetup>({ url: 'https://127.0.0.1:8888', apiKey: '', tlsInsecure: true });
|
|
const [rootDomain, setRootDomain] = useState('vhost.ser888.cn');
|
|
const [managementDomain, setManagementDomain] = useState('host.ser888.cn');
|
|
const [ftpDomain, setFTPDomain] = useState('host.ser888.cn');
|
|
const [domainApproval, setDomainApproval] = useState(true);
|
|
const [admin, setAdmin] = useState({ email: '', name: '平台管理员', password: '', confirm: '' });
|
|
const [databaseTested, setDatabaseTested] = useState(false);
|
|
const [panelTested, setPanelTested] = useState(false);
|
|
const [panelVersions, setPanelVersions] = useState<RuntimeCatalog | null>(null);
|
|
const [panelCapabilities, setPanelCapabilities] = useState<RuntimeCapabilities | null>(null);
|
|
const [busy, setBusy] = useState('');
|
|
const [error, setError] = useState('');
|
|
const [installed, setInstalled] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (!installed || preview) return;
|
|
const timer = window.setInterval(() => {
|
|
api.setupStatus().then(({ required }) => {
|
|
if (!required) window.location.reload();
|
|
}).catch(() => undefined);
|
|
}, 1200);
|
|
return () => window.clearInterval(timer);
|
|
}, [installed, preview]);
|
|
|
|
useEffect(()=>{if(!preview)void api.setupStatus().then(r=>{if(r.controllerOnly)setControllerOnly(true)}).catch(()=>undefined)},[preview]);
|
|
|
|
function goNext() {
|
|
setError('');
|
|
if (step === 0 && token.trim().length < 24) return setError('请输入安装脚本生成的完整一次性令牌');
|
|
if (step === 1 && !databaseTested) return setError('请先完成数据库连接测试');
|
|
if (step === 2 && !controllerOnly && (!panelTested || !validDomain(rootDomain) || !validDomain(managementDomain) || !validDomain(ftpDomain) || !domainsCompatible(rootDomain, managementDomain, ftpDomain))) return setError(!panelTested ? '请先完成宝塔 API 连接测试' : '控制台域名必须独立;FTP 证书来源不能覆盖用户根域');
|
|
if(step===2&&controllerOnly&&!validDomain(managementDomain))return setError('请输入有效的控制台域名');
|
|
setStep((current) => Math.min(3, current + 1));
|
|
}
|
|
|
|
async function testDatabase() {
|
|
setBusy('database'); setError(''); setDatabaseTested(false);
|
|
try {
|
|
if (!preview) await api.testDatabase(token.trim(), database);
|
|
setDatabaseTested(true);
|
|
} catch (reason) { setError(message(reason, '数据库连接失败')); }
|
|
finally { setBusy(''); }
|
|
}
|
|
|
|
async function testPanel() {
|
|
setBusy('panel'); setError(''); setPanelTested(false); setPanelVersions(null); setPanelCapabilities(null);
|
|
try {
|
|
const result = preview ? { versions: demoRuntimeVersions, runtimes: demoRuntimeCapabilities } : await api.testPanel(token.trim(), panel);
|
|
setPanelVersions(result.versions);
|
|
setPanelCapabilities(result.runtimes ?? capabilitiesFromVersions(result.versions));
|
|
setPanelTested(true);
|
|
} catch (reason) { setError(message(reason, '宝塔 API 连接失败')); }
|
|
finally { setBusy(''); }
|
|
}
|
|
|
|
async function install() {
|
|
setError('');
|
|
if (!admin.email.includes('@')) return setError('请输入有效的管理员邮箱');
|
|
if (admin.name.trim().length < 2) return setError('管理员名称至少需要 2 个字符');
|
|
if (admin.password.length < 12) return setError('管理员密码至少需要 12 个字符');
|
|
if (admin.password !== admin.confirm) return setError('两次输入的管理员密码不一致');
|
|
const input: InstallSetup = {
|
|
controllerOnly,database, panel, rootDomain: rootDomain.trim().toLowerCase(), managementDomain: managementDomain.trim().toLowerCase(), ftpDomain: ftpDomain.trim().toLowerCase(), domainApproval,
|
|
adminEmail: admin.email.trim(), adminName: admin.name.trim(), adminPassword: admin.password,
|
|
};
|
|
setBusy('install');
|
|
try {
|
|
if (!preview) await api.install(token.trim(), input);
|
|
setInstalled(true);
|
|
} catch (reason) { setError(message(reason, '安装失败')); }
|
|
finally { setBusy(''); }
|
|
}
|
|
|
|
if (installed) {
|
|
return <main className="setup-page"><section className="setup-success">
|
|
<div className="setup-success-icon"><CheckCircle2 /></div>
|
|
<h1>Sea vHost 已安装完成</h1>
|
|
<p>{preview ? '这是安装成功状态的预览。' : '配置已安全保存,服务正在切换到运行模式。稍后将自动进入登录页面。'}</p>
|
|
{!preview ? <div className="setup-restarting"><span />正在启动服务…</div> : null}
|
|
{!preview ? <button className="secondary setup-refresh" onClick={() => window.location.reload()}>手动刷新</button> : null}
|
|
</section></main>;
|
|
}
|
|
|
|
return <main className="setup-page">
|
|
<header className="setup-header"><Brand /><span><ShieldCheck size={16} />安全安装向导</span></header>
|
|
<div className="setup-layout">
|
|
<aside className="setup-steps" aria-label="安装进度">
|
|
<div><small>SEA VHOST SETUP</small><h1>开始安装</h1><p>大约需要 3 分钟</p></div>
|
|
<ol>{steps.map(({ label, icon: Icon }, index) => <li key={label} className={index === step ? 'active' : index < step ? 'done' : ''}>
|
|
<i>{index < step ? <Check size={15} /> : <Icon size={16} />}</i><span><small>步骤 {index + 1}</small>{label}</span>
|
|
</li>)}</ol>
|
|
<p className="setup-side-note"><ShieldCheck size={16} />安装令牌、数据库密码和 API 密钥不会保存在浏览器中。</p>
|
|
</aside>
|
|
<section className="setup-card">
|
|
<div className="setup-progress"><i style={{ width: `${((step + 1) / steps.length) * 100}%` }} /></div>
|
|
{step === 0 ? <>
|
|
<div className="setup-title"><span><KeyRound /></span><div><h2>验证安装权限</h2><p>仅服务器管理员可以执行首次安装。</p></div></div>
|
|
<div className="setup-info"><ShieldCheck size={18} /><p><b>在哪里找到令牌?</b><span>在服务器执行安装脚本后,终端会显示一次性安装令牌。也可由 root 查看 <code>/etc/sea-vhost/sea-vhost.env</code>。</span></p></div>
|
|
<label className="setup-field"><span>一次性安装令牌 <i>*</i></span><input type="password" autoComplete="off" value={token} onChange={(event) => { setToken(event.target.value); setDatabaseTested(false); setPanelTested(false); }} placeholder="粘贴 SEA_SETUP_TOKEN" /><small>令牌仅随本次请求发送,安装完成后所有安装写接口立即失效。</small></label>
|
|
</> : null}
|
|
{step === 1 ? <>
|
|
<div className="setup-title"><span><Database /></span><div><h2>连接 MySQL</h2><p>请先在宝塔中创建一个空数据库和专用账号。</p></div></div>
|
|
<div className="setup-grid two">
|
|
<label className="setup-field"><span>主机</span><input value={database.host} onChange={(e) => { setDatabase({ ...database, host: e.target.value }); setDatabaseTested(false); }} /></label>
|
|
<label className="setup-field"><span>端口</span><input type="number" value={database.port} onChange={(e) => { setDatabase({ ...database, port: Number(e.target.value) }); setDatabaseTested(false); }} /></label>
|
|
<label className="setup-field"><span>数据库名</span><input value={database.name} onChange={(e) => { setDatabase({ ...database, name: e.target.value }); setDatabaseTested(false); }} /></label>
|
|
<label className="setup-field"><span>用户名</span><input value={database.user} onChange={(e) => { setDatabase({ ...database, user: e.target.value }); setDatabaseTested(false); }} /></label>
|
|
</div>
|
|
<label className="setup-field"><span>数据库密码 <i>*</i></span><input type="password" autoComplete="new-password" value={database.password} onChange={(e) => { setDatabase({ ...database, password: e.target.value }); setDatabaseTested(false); }} /></label>
|
|
<div className="connection-test"><button className="secondary" onClick={testDatabase} disabled={busy !== ''}>{busy === 'database' ? '正在连接…' : '测试数据库连接'}</button>{databaseTested ? <span><CheckCircle2 size={17} />连接成功</span> : <small>仅允许本机 127.0.0.1 / localhost</small>}</div>
|
|
</> : null}
|
|
{step === 2 ? <>
|
|
<label className="setup-field"><span>部署模式</span><select aria-label="部署模式" value={controllerOnly?'controller':'combined'} onChange={e=>setControllerOnly(e.target.value==='controller')}><option value="combined">主控与本机托管(宝塔服务器)</option><option value="controller">独立主控(只调度远程被控)</option></select></label>{controllerOnly?<><div className="setup-title"><span><Server/></span><div><h2>独立主控</h2><p>平台数据仅保存在主控;安装后在节点管理中添加被控。</p></div></div><label className="setup-field"><span>管理控制台域名</span><input value={managementDomain} onChange={e=>setManagementDomain(e.target.value)} required/></label></>:<><div className="setup-title"><span><Server /></span><div><h2>连接宝塔与域名</h2><p>通过专用面板代理检查宝塔,并验证 root 隔离代理。</p></div></div>
|
|
<label className="setup-field"><span>宝塔面板地址 <i>*</i></span><input value={panel.url} onChange={(e) => { setPanel({ ...panel, url: e.target.value }); setPanelTested(false); setPanelVersions(null); setPanelCapabilities(null); }} placeholder="https://127.0.0.1:8888" /></label>
|
|
<label className="setup-field"><span>宝塔 API 密钥 <i>*</i></span><input type="password" autoComplete="new-password" value={panel.apiKey} onChange={(e) => { setPanel({ ...panel, apiKey: e.target.value }); setPanelTested(false); setPanelVersions(null); setPanelCapabilities(null); }} /></label>
|
|
<label className="setup-check"><input type="checkbox" checked={panel.tlsInsecure} onChange={(e) => { setPanel({ ...panel, tlsInsecure: e.target.checked }); setPanelTested(false); setPanelVersions(null); setPanelCapabilities(null); }} /><span><b>允许宝塔本机自签名证书</b><small>只对回环地址生效;不会放宽外部 HTTPS 校验。</small></span></label>
|
|
<div className="connection-test"><button className="secondary" onClick={testPanel} disabled={busy !== ''}>{busy === 'panel' ? '正在检查…' : '检查服务器组件'}</button>{panelTested ? <span><CheckCircle2 size={17} />面板代理与隔离代理均正常</span> : null}</div>
|
|
{panelVersions && panelCapabilities ? <div className="setup-runtime-inventory" aria-label="运行环境能力">{runtimes.map((runtime) => {
|
|
const capability = panelCapabilities[runtime];
|
|
const versions = panelVersions[runtime] ?? [];
|
|
return <div key={runtime} className={capability.status} title={capability.detail}><b>{runtime}</b><span>{versions.length ? versions.join('、') : runtimeStatusText[capability.status]}</span><em>{capability.policy === 'free_core' ? '基础功能' : '语言项目'}</em></div>;
|
|
})}</div> : null}
|
|
<hr />
|
|
<label className="setup-field"><span>用户三级域名根域 <i>*</i></span><input value={rootDomain} onChange={(e) => setRootDomain(e.target.value)} placeholder="vhost.ser888.cn" /><small>请提前将 <code>*.{rootDomain || 'vhost.ser888.cn'}</code> 解析至本机。</small></label>
|
|
<div className="setup-grid two">
|
|
<label className="setup-field"><span>管理控制台域名 <i>*</i></span><input value={managementDomain} onChange={(e) => setManagementDomain(e.target.value)} placeholder="host.ser888.cn" /><small>控制台只接受此 Host 的请求。</small></label>
|
|
<label className="setup-field"><span>FTP 证书来源域名 <i>*</i></span><input value={ftpDomain} onChange={(e) => setFTPDomain(e.target.value)} placeholder="host.ser888.cn" /><small>Pure-FTPd 复用此域名证书;客户端直接连接各虚拟主机自己的域名,可与管理域名相同。</small></label>
|
|
</div>
|
|
<label className="setup-check"><input type="checkbox" checked={domainApproval} onChange={(e) => setDomainApproval(e.target.checked)} /><span><b>用户自绑定域名需要管理员审核</b><small>默认开启,安装后可在管理控制台修改。</small></span></label>
|
|
</>}</> : null}
|
|
{step === 3 ? <>
|
|
<div className="setup-title"><span><UserRound /></span><div><h2>创建首个管理员</h2><p>该账号拥有全部用户与系统设置权限。</p></div></div>
|
|
<div className="setup-grid two">
|
|
<label className="setup-field"><span>管理员名称</span><input value={admin.name} onChange={(e) => setAdmin({ ...admin, name: e.target.value })} /></label>
|
|
<label className="setup-field"><span>管理员邮箱</span><input type="email" autoComplete="username" value={admin.email} onChange={(e) => setAdmin({ ...admin, email: e.target.value })} placeholder="admin@ser888.cn" /></label>
|
|
<label className="setup-field"><span>密码</span><input type="password" autoComplete="new-password" value={admin.password} onChange={(e) => setAdmin({ ...admin, password: e.target.value })} /></label>
|
|
<label className="setup-field"><span>确认密码</span><input type="password" autoComplete="new-password" value={admin.confirm} onChange={(e) => setAdmin({ ...admin, confirm: e.target.value })} /></label>
|
|
</div>
|
|
<div className="setup-review"><h3>安装摘要</h3><dl><div><dt>数据库</dt><dd>{database.user}@{database.host}:{database.port}/{database.name}</dd></div><div><dt>宝塔面板</dt><dd>{panel.url}</dd></div><div><dt>管理域名</dt><dd>{managementDomain}</dd></div><div><dt>FTP 证书</dt><dd>{ftpDomain}</dd></div><div><dt>FTP 连接</dt><dd>复用各虚拟主机域名</dd></div><div><dt>三级域名</dt><dd>*.{rootDomain}</dd></div><div><dt>自绑定审核</dt><dd>{domainApproval ? '需要审核' : '自动通过'}</dd></div></dl></div>
|
|
</> : null}
|
|
{error ? <p className="setup-error" role="alert">{error}</p> : null}
|
|
<footer className="setup-actions">
|
|
{step > 0 ? <button className="secondary" onClick={() => { setError(''); setStep((current) => current - 1); }} disabled={busy !== ''}><ChevronLeft size={16} />上一步</button> : <span />}
|
|
{step < 3 ? <button className="primary" onClick={goNext}>继续<ChevronRight size={16} /></button> : <button className="primary install-button" onClick={install} disabled={busy !== ''}>{busy === 'install' ? '正在初始化…' : '确认并完成安装'}<Check size={16} /></button>}
|
|
</footer>
|
|
</section>
|
|
</div>
|
|
</main>;
|
|
}
|
|
|
|
function validDomain(value: string) {
|
|
return /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/i.test(value.trim().replace(/\.$/, ''));
|
|
}
|
|
|
|
function domainsCompatible(rootValue: string, managementValue: string, ftpValue: string) {
|
|
const [root, management, ftp] = [rootValue, managementValue, ftpValue].map((value) => value.trim().toLowerCase().replace(/\.$/, ''));
|
|
const overlaps = (first: string, second: string) => first === second || first.endsWith(`.${second}`) || second.endsWith(`.${first}`);
|
|
if (overlaps(root, management) || root === ftp || root.endsWith(`.${ftp}`)) return false;
|
|
if (!ftp.endsWith(`.${root}`)) return true;
|
|
return !/^sv[0-9a-f]{10}$/.test(ftp.slice(0, -(root.length + 1)));
|
|
}
|
|
|
|
function message(reason: unknown, fallback: string) {
|
|
return reason instanceof Error ? reason.message : fallback;
|
|
}
|
|
|
|
function capabilitiesFromVersions(versions: RuntimeCatalog): RuntimeCapabilities {
|
|
return Object.fromEntries(runtimes.map((runtime) => [runtime, {
|
|
status: versions[runtime]?.length ? 'available' : 'not_installed',
|
|
versions: versions[runtime] ?? [],
|
|
policy: runtime === 'PHP' ? 'free_core' : 'free_project',
|
|
}])) as RuntimeCapabilities;
|
|
}
|