mirror of https://github.com/certd/certd
Merge remote-tracking branch 'origin/v2-dev-cname-common' into v2-dev-cname-common
commit
0c645b6e66
|
@ -34,9 +34,10 @@
|
||||||
"license": "AGPL-3.0",
|
"license": "AGPL-3.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.7.7",
|
"axios": "^1.7.7",
|
||||||
"lodash-es": "^4.17.21"
|
"lodash-es": "^4.17.21",
|
||||||
|
"typescript": "^5.4.2"
|
||||||
},
|
},
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"packages/**"
|
"packages/**"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,35 @@
|
||||||
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||||
import { AppKey, PlusRequestService, verify } from '@certd/plus-core';
|
import { AppKey, PlusRequestService } from '@certd/plus-core';
|
||||||
import { logger } from '@certd/basic';
|
import { http, HttpRequestConfig, logger } from '@certd/basic';
|
||||||
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
|
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
|
||||||
|
import { merge } from 'lodash-es';
|
||||||
|
|
||||||
@Provide()
|
@Provide()
|
||||||
@Scope(ScopeEnum.Singleton)
|
@Scope(ScopeEnum.Singleton)
|
||||||
export class PlusService {
|
export class PlusService {
|
||||||
@Inject()
|
@Inject()
|
||||||
sysSettingsService: SysSettingsService;
|
sysSettingsService: SysSettingsService;
|
||||||
@Config('plus.server.baseUrls')
|
|
||||||
plusServerBaseUrls: string[];
|
plusRequestService: PlusRequestService;
|
||||||
|
|
||||||
async getPlusRequestService() {
|
async getPlusRequestService() {
|
||||||
const subjectId = await this.getSubjectId();
|
if (this.plusRequestService) {
|
||||||
return new PlusRequestService({
|
return this.plusRequestService;
|
||||||
plusServerBaseUrls: this.plusServerBaseUrls,
|
}
|
||||||
subjectId,
|
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
||||||
});
|
|
||||||
|
const subjectId = installInfo.siteId;
|
||||||
|
const bindUrl = installInfo.bindUrl;
|
||||||
|
const installTime = installInfo.installTime;
|
||||||
|
const saveLicense = async (license: string) => {
|
||||||
|
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
||||||
|
if (!licenseInfo) {
|
||||||
|
licenseInfo = new SysLicenseInfo();
|
||||||
|
}
|
||||||
|
licenseInfo.license = license;
|
||||||
|
await this.sysSettingsService.saveSetting(licenseInfo);
|
||||||
|
};
|
||||||
|
return new PlusRequestService({ subjectId, bindUrl, installTime, saveLicense });
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSubjectId() {
|
async getSubjectId() {
|
||||||
|
@ -24,82 +37,75 @@ export class PlusService {
|
||||||
return installInfo.siteId;
|
return installInfo.siteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async requestWithoutSign(config: any) {
|
async active(code: string) {
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
return await plusRequestService.requestWithoutSign(config);
|
return await plusRequestService.active(code);
|
||||||
}
|
|
||||||
async request(config: any) {
|
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
|
||||||
return await plusRequestService.request(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
async active(formData: { code: any; appKey: string; subjectId: string }) {
|
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
|
||||||
return await plusRequestService.requestWithoutSign({
|
|
||||||
url: '/activation/active',
|
|
||||||
method: 'post',
|
|
||||||
data: formData,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateLicense(license: string) {
|
async updateLicense(license: string) {
|
||||||
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
if (!licenseInfo) {
|
await plusRequestService.updateLicense({ license });
|
||||||
licenseInfo = new SysLicenseInfo();
|
|
||||||
}
|
|
||||||
licenseInfo.license = license;
|
|
||||||
await this.sysSettingsService.saveSetting(licenseInfo);
|
|
||||||
const verifyRes = await this.verify();
|
|
||||||
if (!verifyRes.isPlus) {
|
|
||||||
const message = verifyRes.message || '授权码校验失败';
|
|
||||||
logger.error(message);
|
|
||||||
throw new Error(message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
async verify() {
|
async verify() {
|
||||||
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
|
||||||
|
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
||||||
return await verify({
|
await plusRequestService.verify({ license: licenseInfo.license });
|
||||||
subjectId: plusRequestService.subjectId,
|
|
||||||
license: licenseInfo.license,
|
|
||||||
plusRequestService: plusRequestService,
|
|
||||||
bindUrl: installInfo?.bindUrl,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async bindUrl(subjectId: string, url: string) {
|
async bindUrl(url: string) {
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
return await plusRequestService.request({
|
return await plusRequestService.bindUrl(url);
|
||||||
url: '/activation/subject/urlBind',
|
|
||||||
data: {
|
|
||||||
subjectId,
|
|
||||||
appKey: AppKey,
|
|
||||||
url,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async register() {
|
async register() {
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
if (!licenseInfo.license) {
|
||||||
if (!licenseInfo?.license) {
|
await plusRequestService.register();
|
||||||
//还没有license,注册一个
|
logger.info('站点注册成功');
|
||||||
const res = await plusRequestService.requestWithoutSign({
|
|
||||||
url: '/activation/subject/register',
|
|
||||||
data: {
|
|
||||||
appKey: AppKey,
|
|
||||||
subjectId: installInfo.siteId,
|
|
||||||
installTime: installInfo.installTime,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (res.license) {
|
|
||||||
await this.updateLicense(res.license);
|
|
||||||
logger.info('站点注册成功');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async userPreBind(userId: number) {
|
||||||
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
|
await plusRequestService.requestWithoutSign({
|
||||||
|
url: '/activation/subject/preBind',
|
||||||
|
method: 'POST',
|
||||||
|
data: {
|
||||||
|
userId,
|
||||||
|
appKey: AppKey,
|
||||||
|
subjectId: this.getSubjectId(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendEmail(email: any) {
|
||||||
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
|
await plusRequestService.request({
|
||||||
|
url: '/activation/emailSend',
|
||||||
|
data: {
|
||||||
|
subject: email.subject,
|
||||||
|
text: email.content,
|
||||||
|
to: email.receivers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAccessToken() {
|
||||||
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
|
await this.register();
|
||||||
|
return await plusRequestService.getAccessToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
async requestWithToken(config: HttpRequestConfig) {
|
||||||
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
|
const token = await this.getAccessToken();
|
||||||
|
merge(config, {
|
||||||
|
baseURL: plusRequestService.getBaseURL(),
|
||||||
|
headers: {
|
||||||
|
Authorization: token,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return await http.request(config);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
# key: ./data/ssl/cert.key
|
||||||
|
# cert: ./data/ssl/cert.crt
|
||||||
|
#plus:
|
||||||
|
# server:
|
||||||
|
# baseUrl: 'http://127.0.0.1:11007'
|
||||||
|
|
||||||
|
#flyway:
|
||||||
|
# scriptDir: './db/migration-pg'
|
||||||
|
|
||||||
|
#typeorm:
|
||||||
|
# dataSource:
|
||||||
|
# default:
|
||||||
|
# type: postgres
|
||||||
|
# host: localhost
|
||||||
|
# port: 5433
|
||||||
|
# username: postgres
|
||||||
|
# password: root
|
||||||
|
# database: postgres
|
||||||
|
|
||||||
|
typeorm:
|
||||||
|
dataSource:
|
||||||
|
default:
|
||||||
|
database: './data/db-plus-dev.sqlite'
|
||||||
|
|
||||||
|
# plus server: 'http://127.0.0.1:11007'
|
||||||
|
|
||||||
|
account:
|
||||||
|
server:
|
||||||
|
baseUrl: 'http://127.0.0.1:1017/subject'
|
|
@ -10,6 +10,7 @@
|
||||||
"commdev": "cross-env NODE_ENV=commdev mwtsc --watch --run @midwayjs/mock/app",
|
"commdev": "cross-env NODE_ENV=commdev mwtsc --watch --run @midwayjs/mock/app",
|
||||||
"commpro": "cross-env NODE_ENV=commpro mwtsc --watch --run @midwayjs/mock/app",
|
"commpro": "cross-env NODE_ENV=commpro mwtsc --watch --run @midwayjs/mock/app",
|
||||||
"pgdev": "cross-env NODE_ENV=pgdev mwtsc --watch --run @midwayjs/mock/app",
|
"pgdev": "cross-env NODE_ENV=pgdev mwtsc --watch --run @midwayjs/mock/app",
|
||||||
|
"local-plus": "cross-env NODE_ENV=localplus mwtsc --watch --run @midwayjs/mock/app",
|
||||||
"pgpl": "cross-env NODE_ENV=pgpl mwtsc --watch --run @midwayjs/mock/app",
|
"pgpl": "cross-env NODE_ENV=pgpl mwtsc --watch --run @midwayjs/mock/app",
|
||||||
"dev-new": "cross-env NODE_ENV=devnew mwtsc --watch --run @midwayjs/mock/app",
|
"dev-new": "cross-env NODE_ENV=devnew mwtsc --watch --run @midwayjs/mock/app",
|
||||||
"rm-db-new": "rimraf ./data/db-new.sqlite",
|
"rm-db-new": "rimraf ./data/db-new.sqlite",
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
|
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
|
||||||
import { BaseController, PlusService } from '@certd/lib-server';
|
import { BaseController, PlusService, SysInstallInfo, SysSettingsService } from '@certd/lib-server';
|
||||||
import { AppKey } from '@certd/plus-core';
|
|
||||||
import { SysSettingsService } from '@certd/lib-server';
|
|
||||||
import { SysInstallInfo } from '@certd/lib-server';
|
|
||||||
|
|
||||||
export type PreBindUserReq = {
|
export type PreBindUserReq = {
|
||||||
userId: number;
|
userId: number;
|
||||||
|
@ -23,18 +20,8 @@ export class BasicController extends BaseController {
|
||||||
|
|
||||||
@Post('/preBindUser', { summary: 'sys:settings:edit' })
|
@Post('/preBindUser', { summary: 'sys:settings:edit' })
|
||||||
public async preBindUser(@Body(ALL) body: PreBindUserReq) {
|
public async preBindUser(@Body(ALL) body: PreBindUserReq) {
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
|
||||||
// 设置缓存内容
|
// 设置缓存内容
|
||||||
await this.plusService.requestWithoutSign({
|
await this.plusService.userPreBind(body.userId);
|
||||||
url: '/activation/subject/preBind',
|
|
||||||
method: 'POST',
|
|
||||||
data: {
|
|
||||||
userId: body.userId,
|
|
||||||
appKey: AppKey,
|
|
||||||
subjectId: installInfo.siteId,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return this.ok({});
|
return this.ok({});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
|
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
|
||||||
import { BaseController, PlusService, SysInstallInfo, SysSettingsService } from '@certd/lib-server';
|
import { BaseController, PlusService, SysInstallInfo, SysSettingsService } from '@certd/lib-server';
|
||||||
import { AppKey } from '@certd/plus-core';
|
|
||||||
import { logger } from '@certd/basic';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
|
@ -17,23 +15,8 @@ export class SysPlusController extends BaseController {
|
||||||
@Post('/active', { summary: 'sys:settings:edit' })
|
@Post('/active', { summary: 'sys:settings:edit' })
|
||||||
async active(@Body(ALL) body) {
|
async active(@Body(ALL) body) {
|
||||||
const { code } = body;
|
const { code } = body;
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
|
||||||
const siteId = installInfo.siteId;
|
|
||||||
const formData = {
|
|
||||||
appKey: AppKey,
|
|
||||||
code,
|
|
||||||
subjectId: siteId,
|
|
||||||
};
|
|
||||||
|
|
||||||
const res: any = await this.plusService.active(formData);
|
await this.plusService.active(code);
|
||||||
|
|
||||||
if (res.code > 0) {
|
|
||||||
logger.error('激活失败', res.message);
|
|
||||||
return this.fail(res.message, 1);
|
|
||||||
}
|
|
||||||
const license = res.data.license;
|
|
||||||
|
|
||||||
await this.plusService.updateLicense(license);
|
|
||||||
|
|
||||||
return this.ok(true);
|
return this.ok(true);
|
||||||
}
|
}
|
||||||
|
@ -42,7 +25,7 @@ export class SysPlusController extends BaseController {
|
||||||
const { url } = body;
|
const { url } = body;
|
||||||
|
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
||||||
await this.plusService.bindUrl(installInfo.siteId, url);
|
await this.plusService.bindUrl(url);
|
||||||
|
|
||||||
installInfo.bindUrl = url;
|
installInfo.bindUrl = url;
|
||||||
await this.sysSettingsService.saveSetting(installInfo);
|
await this.sysSettingsService.saveSetting(installInfo);
|
||||||
|
|
|
@ -49,14 +49,7 @@ export class EmailService implements IEmailService {
|
||||||
* receivers: string[];
|
* receivers: string[];
|
||||||
*/
|
*/
|
||||||
|
|
||||||
await this.plusService.request({
|
await this.plusService.sendEmail(email);
|
||||||
url: '/activation/emailSend',
|
|
||||||
data: {
|
|
||||||
subject: email.subject,
|
|
||||||
text: email.content,
|
|
||||||
to: email.receivers,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -32,7 +32,7 @@ export class CommonDnsProvider implements IDnsProvider {
|
||||||
|
|
||||||
await this.plusService.register();
|
await this.plusService.register();
|
||||||
|
|
||||||
const res = await this.plusService.requestWithoutSign({
|
const res = await this.plusService.requestWithToken({
|
||||||
url: '/activation/certd/cname/recordCreate',
|
url: '/activation/certd/cname/recordCreate',
|
||||||
data: {
|
data: {
|
||||||
subjectId: this.plusService.getSubjectId(),
|
subjectId: this.plusService.getSubjectId(),
|
||||||
|
@ -45,7 +45,7 @@ export class CommonDnsProvider implements IDnsProvider {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
async removeRecord(options: RemoveRecordOptions<any>) {
|
async removeRecord(options: RemoveRecordOptions<any>) {
|
||||||
const res = await this.plusService.requestWithoutSign({
|
const res = await this.plusService.requestWithToken({
|
||||||
url: '/activation/certd/cname/recordRemove',
|
url: '/activation/certd/cname/recordRemove',
|
||||||
data: {
|
data: {
|
||||||
subjectId: this.plusService.getSubjectId(),
|
subjectId: this.plusService.getSubjectId(),
|
||||||
|
|
Loading…
Reference in New Issue