Files
certd/packages/ui/certd-server/src/modules/basic/service/plus-service.ts
T

101 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-08-24 01:05:06 +08:00
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
2024-08-23 11:35:34 +08:00
import { SysSettingsService } from '../../system/service/sys-settings-service.js';
2024-09-23 14:04:33 +08:00
import { SysInstallInfo, SysLicenseInfo } from '../../system/service/models.js';
import { AppKey, getPlusInfo, isPlus, verify } from '@certd/pipeline';
2024-08-23 11:35:34 +08:00
import * as crypto from 'crypto';
import { request } from '../../../utils/http.js';
import { logger } from '../../../utils/logger.js';
@Provide()
@Scope(ScopeEnum.Singleton)
export class PlusService {
@Inject()
sysSettingsService: SysSettingsService;
2024-08-24 01:05:06 +08:00
@Config('plus.server.baseUrl')
plusServerBaseUrl;
2024-08-23 11:35:34 +08:00
2024-08-25 15:34:00 +08:00
async requestWithoutSign(config: any): Promise<any> {
2024-08-24 01:05:06 +08:00
config.baseURL = this.plusServerBaseUrl;
2024-09-23 14:04:33 +08:00
config.method = config.method || 'POST';
2024-08-24 01:05:06 +08:00
return await request(config);
}
async request(config: any) {
2024-08-24 23:48:26 +08:00
if (!isPlus()) {
throw new Error('您还不是专业版,请先激活专业版');
}
2024-08-24 01:05:06 +08:00
const { url, data } = config;
2024-08-23 11:35:34 +08:00
const timestamps = Date.now();
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const sign = await this.sign(data, timestamps);
const requestHeader = {
subjectId: installInfo.siteId,
2024-08-25 01:55:34 +08:00
appKey: AppKey,
2024-08-23 11:35:34 +08:00
sign: sign,
timestamps: timestamps,
};
let requestHeaderStr = JSON.stringify(requestHeader);
requestHeaderStr = Buffer.from(requestHeaderStr).toString('base64');
const headers = {
'Content-Type': 'application/json',
'X-Plus-Subject': requestHeaderStr,
};
return await request({
url: url,
2024-08-24 01:05:06 +08:00
baseURL: this.plusServerBaseUrl,
2024-08-23 11:35:34 +08:00
method: 'POST',
data: data,
headers: headers,
});
}
async sign(body: any, timestamps: number) {
//content := fmt.Sprintf("%s.%d.%s", in.Params, in.Timestamps, secret)
const params = JSON.stringify(body);
const plusInfo = getPlusInfo();
const secret = plusInfo.secret;
2024-08-24 23:48:26 +08:00
if (!secret) {
const randomTime = Math.floor(Math.random() * 3 * 60 * 1000 + 30 * 1000);
setTimeout(() => {
process.exit();
}, randomTime);
return 'xxxxx';
}
2024-08-23 11:35:34 +08:00
const content = `${params}.${timestamps}.${secret}`;
// sha256
const sign = crypto.createHash('sha256').update(content).digest('base64');
logger.info('content:', content, 'sign:', sign);
return sign;
}
2024-08-24 01:05:06 +08:00
async active(formData: { code: any; appKey: string; subjectId: string }) {
return await this.requestWithoutSign({
url: '/activation/active',
method: 'post',
data: formData,
});
}
2024-09-23 14:04:33 +08:00
async updateLicense(siteId: string, license: string) {
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
if (!licenseInfo) {
licenseInfo = new SysLicenseInfo();
}
licenseInfo.license = license;
await this.sysSettingsService.saveSetting(licenseInfo);
const verifyRes = await verify({
subjectId: siteId,
license,
});
if (!verifyRes.isPlus) {
const message = verifyRes.message || '授权码校验失败';
logger.error(message);
throw new Error(message);
}
}
2024-08-23 11:35:34 +08:00
}