This commit is contained in:
1 parent
dd9d659cb7
commit
9dc861d560
41 files changed
+1408
-198
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import { createHash, createPrivateKey, createPublicKey, X509Certificate } from 'node:crypto'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { createSecureContext } from 'node:tls'
|
||||
import { readDevelopmentRuntime } from '../deploy/runtime-config.mjs'
|
||||
|
||||
// 开发 HTTPS 也读取同一部署配置;整套材料校验后再替换,不关闭现有连接。
|
||||
export function readDevelopmentTLS(root) {
|
||||
try {
|
||||
return readTLSMaterial(readDevelopmentRuntime(root))
|
||||
} catch (error) {
|
||||
try {
|
||||
return readTLSMaterial(readDevelopmentRuntime(root, { previous: true }))
|
||||
} catch {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function readTLSMaterial(runtime) {
|
||||
const cert = readFileSync(runtime.certificatePath)
|
||||
const key = readFileSync(runtime.privateKeyPath)
|
||||
const certificate = new X509Certificate(cert)
|
||||
const now = Date.now()
|
||||
if (now < Date.parse(certificate.validFrom) || now >= Date.parse(certificate.validTo)) {
|
||||
throw new Error('开发证书尚未生效或已经过期')
|
||||
}
|
||||
const publicKey = createPublicKey(createPrivateKey(key))
|
||||
if (!certificate.publicKey.equals(publicKey) || !certificate.checkHost(new URL(runtime.origin).hostname)) {
|
||||
throw new Error('开发证书与私钥或 Web 域名不匹配')
|
||||
}
|
||||
createSecureContext({ cert, key, minVersion: 'TLSv1.2' })
|
||||
const fingerprint = createHash('sha256').update(cert).update(key).digest('hex')
|
||||
return { runtime, options: { cert, key, minVersion: 'TLSv1.2' }, fingerprint, expiresAt: Date.parse(certificate.validTo) }
|
||||
}
|
||||
|
||||
export function developmentTLSReload(initial, root) {
|
||||
return {
|
||||
name: 'smail-development-tls-reload',
|
||||
apply: 'serve',
|
||||
configureServer(server) {
|
||||
let active = initial
|
||||
let warned = false
|
||||
let expired = false
|
||||
const timer = setInterval(() => {
|
||||
try {
|
||||
const next = readDevelopmentTLS(root)
|
||||
if (expired || next.fingerprint !== active.fingerprint) {
|
||||
server.httpServer.setSecureContext(next.options)
|
||||
active = next
|
||||
expired = false
|
||||
}
|
||||
warned = false
|
||||
} catch {
|
||||
if (!expired && Date.now() >= active.expiresAt) {
|
||||
// 不再给新握手提供过期证书;已经建立的连接不关闭。
|
||||
try {
|
||||
server.httpServer.setSecureContext({ minVersion: 'TLSv1.2' })
|
||||
expired = true
|
||||
} catch {
|
||||
server.config.logger.error('无法停用过期开发证书,请重启前端服务')
|
||||
}
|
||||
}
|
||||
if (!warned) server.config.logger.warn('证书热加载失败,暂时保留上一套有效证书,请检查 yunx-test/ssl')
|
||||
warned = true
|
||||
}
|
||||
}, 1000)
|
||||
timer.unref()
|
||||
server.httpServer.once('close', () => clearInterval(timer))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,14 +181,14 @@ const sslGroups = [
|
||||
field('tls.certificateRef', '证书文件', 'text', {
|
||||
wide: true,
|
||||
show: externalManagedCertificate,
|
||||
placeholder: 'data/ssl/example.com/fullchain.pem',
|
||||
hint: '填写后端服务器上的 PEM 证书路径。自定义模式支持绝对路径或相对运行根目录的路径,保存后显示导入后的受管路径;Web 服务器管理模式填写已有受管证书的相对路径。'
|
||||
placeholder: 'ssl/example.com.crt',
|
||||
hint: '直接使用服务器上的证书文件,支持相对部署目录的路径或绝对路径,不生成副本。'
|
||||
}),
|
||||
field('tls.privateKeyRef', '私钥文件', 'text', {
|
||||
wide: true,
|
||||
show: externalManagedCertificate,
|
||||
placeholder: 'data/ssl/example.com/privkey.pem',
|
||||
hint: '填写与证书匹配的 PEM 私钥文件路径,不是私钥内容。文件必须可被后端运行用户读取;自定义模式保存后显示受管副本的路径。'
|
||||
placeholder: 'ssl/example.com.key',
|
||||
hint: '填写配套私钥的文件路径,不是私钥内容;须允许运行用户读取。'
|
||||
})
|
||||
])
|
||||
]
|
||||
@@ -376,8 +376,8 @@ export function validateFormConfig(section, config) {
|
||||
for (const name of ['certificateRef', 'privateKeyRef']) {
|
||||
if (!tls[name]) continue
|
||||
const managedReference = controlled(tls[name]) && tls[name].startsWith(`${directory}/`)
|
||||
const externalReference = management === 'custom' && absoluteCertificatePath(tls[name])
|
||||
if (!managedReference && !externalReference) errors[`tls.${name}`] = '文件引用必须是受管存储引用或自定义服务器绝对路径'
|
||||
const externalReference = management !== 'project' && (absoluteCertificatePath(tls[name]) || controlled(tls[name]))
|
||||
if (!managedReference && !externalReference) errors[`tls.${name}`] = '请填写有效的证书文件路径,不允许上级目录跳转'
|
||||
}
|
||||
if (!!tls.certificateRef !== !!tls.privateKeyRef) errors['tls.privateKeyRef'] = '证书与私钥引用必须成对填写'
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
/* eslint-env node */
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { createServer as createHTTPServer } from 'node:http'
|
||||
import { fileURLToPath, URL } from 'node:url'
|
||||
import { defineConfig, loadEnv } from 'vite'
|
||||
@@ -7,7 +6,7 @@ import AutoImport from 'unplugin-auto-import/vite'
|
||||
import Components from 'unplugin-vue-components/vite'
|
||||
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import { readDevelopmentRuntime } from '../deploy/runtime-config.mjs'
|
||||
import { readDevelopmentTLS, developmentTLSReload } from './dev-tls.js'
|
||||
|
||||
const developmentHTTPRedirect = (frontendOrigin, host = '127.0.0.1', port = 80) => ({
|
||||
name: 'smail-development-http-redirect',
|
||||
@@ -35,13 +34,15 @@ const developmentHTTPRedirect = (frontendOrigin, host = '127.0.0.1', port = 80)
|
||||
export default defineConfig(({ mode, command }) => {
|
||||
const env = loadEnv(mode, process.cwd(), '')
|
||||
const backendUrl = (env.VITE_DEV_BACKEND_URL || 'http://127.0.0.1:5201').replace(/\/$/, '')
|
||||
const runtime = command === 'serve' ? readDevelopmentRuntime() : undefined
|
||||
const developmentTLS = command === 'serve' ? readDevelopmentTLS() : undefined
|
||||
const runtime = developmentTLS?.runtime
|
||||
const backendOrigin = runtime?.origin
|
||||
const frontendOrigin = runtime?.origin
|
||||
|
||||
return {
|
||||
plugins: [
|
||||
developmentHTTPRedirect(frontendOrigin),
|
||||
command === 'serve' && developmentTLSReload(developmentTLS),
|
||||
vue(),
|
||||
AutoImport({
|
||||
resolvers: [ElementPlusResolver()],
|
||||
@@ -60,10 +61,7 @@ export default defineConfig(({ mode, command }) => {
|
||||
port: 443,
|
||||
strictPort: true,
|
||||
// 生产构建不依赖本机开发证书,只有开发服务器启动时才读取私钥。
|
||||
https: command === 'serve' ? {
|
||||
cert: readFileSync(runtime.certificatePath),
|
||||
key: readFileSync(runtime.privateKeyPath)
|
||||
} : undefined,
|
||||
https: developmentTLS?.options,
|
||||
cors: true,
|
||||
proxy: {
|
||||
"/api": {
|
||||
|
||||
Reference in new issue
Block a user