- 插件执行超时默认延长至 6 小时,支持插件单独配置超时时间 - 新增插件根目录迁移逻辑,解决旧版权限问题 - 新增多个服务端实体、DTO、配置模块等基础代码 - 新增插件 UI 模板及相关静态资源
108 lines
3.7 KiB
TypeScript
108 lines
3.7 KiB
TypeScript
import { CoolController, BaseController } from '@cool-midway/core';
|
||
import { Get, Headers, Inject, Provide, Query } from '@midwayjs/core';
|
||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||
import { Repository } from 'typeorm';
|
||
import { PluginEntity, AccountPluginEntity } from '../../entity/plugin';
|
||
import { CenterAuthService, ApiError } from '../../service/auth';
|
||
import { pubkeyPem, signBlob } from '../../service/crypto';
|
||
import { LICENSE_TTL_DAYS, fmtNow } from '../../comm/conf';
|
||
|
||
/**
|
||
* 客户端票据接口(对齐旧 FastAPI apis/plugins.py /api/me/*)
|
||
* - /api/me/pubkey:下发中心机公钥(Ed25519 PEM)
|
||
* - /api/me/license:签发离线授权票据(插件授权快照,Ed25519 签名)
|
||
* - /api/me/control-list:签发管控清单(revoked/disabled/forcedUpdates,Ed25519 签名)
|
||
*/
|
||
@Provide()
|
||
@CoolController('/api')
|
||
export class ApiMeController extends BaseController {
|
||
@Inject()
|
||
centerAuthService: CenterAuthService;
|
||
|
||
@InjectEntityModel(PluginEntity)
|
||
pluginRepo: Repository<PluginEntity>;
|
||
|
||
@InjectEntityModel(AccountPluginEntity)
|
||
accountPluginRepo: Repository<AccountPluginEntity>;
|
||
|
||
private expiresAtEpoch(): number {
|
||
return Math.floor(Date.now() / 1000) + LICENSE_TTL_DAYS * 24 * 3600;
|
||
}
|
||
|
||
/** GET /api/me/pubkey */
|
||
@Get('/me/pubkey')
|
||
async pubkey() {
|
||
return { pub: pubkeyPem(), algo: 'ed25519' };
|
||
}
|
||
|
||
/** GET /api/me/license?account=&device= */
|
||
@Get('/me/license')
|
||
async license(
|
||
@Query('account') account: string = '',
|
||
@Query('device') device: string = '',
|
||
@Headers('X-API-Token') xApiToken: string = ''
|
||
) {
|
||
this.centerAuthService.checkApiToken(xApiToken);
|
||
if (!(account || '').trim()) throw new ApiError(400, 'account 不能为空');
|
||
if (await this.centerAuthService.accountDisabled(account.trim())) {
|
||
throw new ApiError(403, '账号已停用,无法获取授权');
|
||
}
|
||
const rows = await this.accountPluginRepo
|
||
.createQueryBuilder('g')
|
||
.innerJoin(PluginEntity, 'p', 'p.id = g.pluginId')
|
||
.select([
|
||
'g.pluginId AS pluginId',
|
||
'g.canDownload AS canDownload',
|
||
'g.canRun AS canRun',
|
||
'p.name AS name',
|
||
'p.status AS status',
|
||
'p.latestVersion AS latestVersion',
|
||
])
|
||
.where('g.account = :account', { account: account.trim() })
|
||
.getRawMany();
|
||
const plugins = rows.map(r => ({
|
||
pluginId: r.pluginId,
|
||
name: r.name,
|
||
canDownload: !!Number(r.canDownload),
|
||
canRun: !!Number(r.canRun),
|
||
status: r.status,
|
||
latestVersion: r.latestVersion,
|
||
}));
|
||
const issuedAt = fmtNow();
|
||
const expiresAt = this.expiresAtEpoch();
|
||
const blob = signBlob({
|
||
userId: account.trim(),
|
||
deviceId: device || '',
|
||
issuedAt,
|
||
expiresAt,
|
||
plugins,
|
||
});
|
||
return { ticket: blob, plugins, issuedAt, expiresAt };
|
||
}
|
||
|
||
/** GET /api/me/control-list */
|
||
@Get('/me/control-list')
|
||
async controlList(@Headers('X-API-Token') xApiToken: string = '') {
|
||
this.centerAuthService.checkApiToken(xApiToken);
|
||
const revoked: string[] = [];
|
||
const disabled: string[] = [];
|
||
const forced: any[] = [];
|
||
const rows = await this.pluginRepo.find();
|
||
for (const r of rows) {
|
||
if (r.status === 'revoked') revoked.push(r.id);
|
||
else if (r.status === 'disabled') disabled.push(r.id);
|
||
if (r.forceMinVersion && r.forceDeadline) {
|
||
forced.push({ pluginId: r.id, minVersion: r.forceMinVersion, deadline: r.forceDeadline });
|
||
}
|
||
}
|
||
const payload = {
|
||
revokedPlugins: revoked,
|
||
disabledPlugins: disabled,
|
||
forcedUpdates: forced,
|
||
updatedAt: fmtNow(),
|
||
expiresAt: this.expiresAtEpoch(),
|
||
};
|
||
return { ticket: signBlob(payload), control: payload };
|
||
}
|
||
}
|