feat: export client activation files and add Windows local launcher
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
2026-10-12 10:13:39 +08:00
parent 099878dbba
commit fad1fba710
11 changed files with 298 additions and 2 deletions

View File

@@ -0,0 +1,27 @@
import { afterEach, expect, it, vi } from 'vitest'
import { buildLicenseFile, downloadLicenseFile } from './license-file'
const credentials = { instance_id: 'test-instance', license_key: 'test-license', secret: 'test-secret' }
afterEach(() => { vi.unstubAllGlobals(); vi.restoreAllMocks(); vi.useRealTimers() })
it('exports versioned activation credentials with normalized deployment root', () => {
expect(buildLicenseFile(credentials, ' https://license.yihuiyong.cn/ ')).toEqual({
format: 'yhy-license-activation', version: 1, api_base_url: 'https://license.yihuiyong.cn', ...credentials,
})
expect(buildLicenseFile(credentials, 'http://127.0.0.1:18081').api_base_url).toBe('http://127.0.0.1:18081')
})
it.each(['http://example.com', 'https://user:pass@example.com', 'https://example.com/api', 'https://example.com/?secret=x', 'https://example.com/#x', 'javascript:alert(1)', 'invalid'])('rejects invalid center %s', address => {
expect(() => buildLicenseFile(credentials, address)).toThrow()
})
it('downloads JSON and releases the temporary object URL', () => {
vi.useFakeTimers()
const create = vi.fn(() => 'blob:test'), revoke = vi.fn()
// Preserve URL parsing while stubbing browser download APIs absent in jsdom.
vi.stubGlobal('URL', class extends URL { static createObjectURL = create; static revokeObjectURL = revoke })
const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function(this: HTMLAnchorElement) {
expect(this.download).toBe('license-test-instance.license.json')
expect(this.href).toBe('blob:test')
})
downloadLicenseFile(credentials, 'https://license.yihuiyong.cn')
expect(create).toHaveBeenCalledWith(expect.any(Blob)); expect(click).toHaveBeenCalledOnce()
expect(document.querySelector('a')).toBeNull()
vi.runAllTimers(); expect(revoke).toHaveBeenCalledWith('blob:test')
})

View File

@@ -0,0 +1,34 @@
export type ActivationCredentials = { instance_id: string; license_key: string; secret: string }
// This is a credential delivery file, not a signed license certificate or lease.
export function buildLicenseFile(credentials: ActivationCredentials, baseURL: string) {
const url = new URL(baseURL.trim())
const loopback = ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname)
if ((url.protocol !== 'https:' && !(url.protocol === 'http:' && loopback)) ||
url.username || url.password || url.search || url.hash || url.pathname !== '/') {
throw new Error('授权中心必须是 HTTPS 根地址;仅本机开发允许 HTTP 回环地址。')
}
return {
format: 'yhy-license-activation',
version: 1,
api_base_url: url.origin,
instance_id: credentials.instance_id,
license_key: credentials.license_key,
secret: credentials.secret,
}
}
export function downloadLicenseFile(credentials: ActivationCredentials, baseURL: string) {
const content = JSON.stringify(buildLicenseFile(credentials, baseURL), null, 2) + '\n'
const blob = new Blob([content], { type: 'application/json;charset=utf-8' })
const url = URL.createObjectURL(blob)
const anchor = document.createElement('a')
anchor.href = url
anchor.download = `license-${credentials.instance_id}.license.json`
document.body.appendChild(anchor)
try { anchor.click() } finally {
anchor.remove()
// Release only after the browser has processed the download navigation.
setTimeout(() => URL.revokeObjectURL(url), 1000)
}
}

View File

@@ -10,7 +10,7 @@ it('creates and displays first-return credentials, without another creation entr
await w.get('button').trigger('click');await flushPromises()
expect(api.provisionLicenseInstance).toHaveBeenCalledWith(7)
expect(w.text()).toContain('test-license');expect(w.text()).toContain('test-instance');expect(w.text()).toContain('test-secret');expect(w.emitted('created')).toHaveLength(1)
expect(w.find('button').exists()).toBe(false);w.unmount()
expect(w.findAll('button').map(b=>b.text())).toEqual(['导出授权文件']);w.unmount()
const reopened=mount(Panel,{props:{licenseId:7,hasInstance:true,ready:true},global:{plugins:[ElementPlus]}})
expect(reopened.get('button').attributes('disabled')).toBeDefined();expect(reopened.text()).not.toContain('test-secret');reopened.unmount()
})

View File

@@ -2,9 +2,17 @@
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'
import { getLicense, provisionLicenseInstance } from '../api'
import { downloadLicenseFile } from '../license-file'
const props = defineProps<{licenseId:number;hasInstance:boolean;ready:boolean}>()
const emit = defineEmits<{created:[]}>()
const busy = ref(false), error = ref('')
const exportError = ref(''), baseURL = ref('https://license.yihuiyong.cn')
function exportFile() {
if (!credentials.value) return
exportError.value = ''
try { downloadLicenseFile(credentials.value, baseURL.value) }
catch (e) { exportError.value = e instanceof Error ? e.message : '导出失败,请重试' }
}
const credentials = ref<{instance_id:string;license_key:string;secret:string}|null>(null)
async function create() {
if(busy.value || props.hasInstance || !props.ready || credentials.value || error.value) return
@@ -29,6 +37,11 @@ async function create() {
<template v-if="credentials">
<p role="status">实例已创建。请立即安全保存下列凭据,关闭窗口后不再显示激活密钥。</p>
<dl class="license-detail"><dt>实例 ID(instance_id)</dt><dd class="license-code">{{credentials.instance_id}}</dd><dt>授权码(license_key)</dt><dd class="license-code">{{credentials.license_key}}</dd><dt>激活密钥(secret)</dt><dd class="license-code">{{credentials.secret}}</dd></dl>
<label for="license-export-base-url">授权文件中的授权中心地址</label>
<el-input id="license-export-base-url" v-model="baseURL" placeholder="https://license.yihuiyong.cn" />
<el-button type="primary" @click="exportFile">导出授权文件</el-button>
<p v-if="exportError" class="error" role="alert">{{ exportError }}</p>
<p>导出的 .license.json 含激活密钥,请安全交付。客户端需支持本文件格式,导入后在线激活并获取运行租约;文件本身不代表授权有效。关闭此窗口后无法再次导出包含密钥的文件。</p>
<p>默认授权中心:https://license.yihuiyong.cn;私有部署填写实际 HTTPS 根地址。密钥仅首次展示,但客户端激活后仍需安全保存用于心跳和下载;不要放入日志、截图或普通工单。生产验签公钥应由开发团队预置。</p>
</template>
</section>