Compare commits

...

3 Commits

Author SHA1 Message Date
whyour 06723407d2 更新版本 v2.13.8 2022-08-11 19:41:59 +08:00
whyour db8dfb32cf 修改生成系统token逻辑 2022-08-11 19:37:14 +08:00
whyour a7117e4442 修改内置token获取方式 2022-08-11 13:13:37 +08:00
7 changed files with 43 additions and 63 deletions
+19 -1
View File
@@ -3,12 +3,30 @@ import _ from 'lodash';
import SystemService from '../services/system';
import ScheduleService from '../services/schedule';
import SubscriptionService from '../services/subscription';
import config from '../config';
import { fileExist } from '../config/util';
export default async () => {
const systemService = Container.get(SystemService);
const scheduleService = Container.get(ScheduleService);
const subscriptionService = Container.get(SubscriptionService);
// 生成内置token
let tokenCommand = `ts-node-transpile-only ${config.rootPath}/back/token.ts`;
const tokenFile = `${config.rootPath}/static/build/token.js`;
if (await fileExist(tokenFile)) {
tokenCommand = `node ${tokenFile}`;
}
const cron = {
id: 'token',
name: '生成token',
command: tokenCommand,
};
scheduleService.createIntervalTask(cron as any, {
days: 28,
runImmediately: true,
});
// 运行删除日志任务
const data = await systemService.getLogRemoveFrequency();
if (data && data.info && data.info.frequency) {
@@ -17,7 +35,7 @@ export default async () => {
name: '删除日志',
command: `ql rmlog ${data.info.frequency}`,
};
await scheduleService.createIntervalTask(cron, {
scheduleService.createIntervalTask(cron, {
days: data.info.frequency,
runImmediately: true,
});
+9 -18
View File
@@ -145,7 +145,7 @@ export default class OpenService {
}
}
public async findSystemToken(): Promise<{
public async generateSystemToken(): Promise<{
value: string;
expiration: number;
}> {
@@ -158,22 +158,13 @@ export default class OpenService {
scopes: ['crons', 'system'],
} as App);
}
const nowTime = Math.round(Date.now() / 1000);
let token;
if (
!systemApp.tokens ||
!systemApp.tokens.length ||
nowTime > [...systemApp.tokens].pop()!.expiration
) {
const authToken = await this.authToken({
client_id: systemApp.client_id,
client_secret: systemApp.client_secret,
});
token = authToken.data;
token.value = token.token;
} else {
token = [...systemApp.tokens].pop();
}
return token;
const { data } = await this.authToken({
client_id: systemApp.client_id,
client_secret: systemApp.client_secret,
});
return {
...data,
value: data.token,
};
}
}
+1 -1
View File
@@ -169,7 +169,7 @@ export default class ScheduleService {
);
const job = new LongIntervalJob(
{ ...schedule, runImmediately: false },
{ runImmediately: false, ...schedule },
task,
_id,
);
+8 -30
View File
@@ -10,41 +10,19 @@ const tokenFile = path.join(config.configPath, 'token.json');
async function getToken() {
try {
const data = await readFile();
const nowTime = Math.round(Date.now() / 1000);
if (data.value && data.expiration > nowTime) {
console.log(data.value);
} else {
Container.set('logger', LoggerInstance);
const openService = Container.get(OpenService);
const appToken = await openService.findSystemToken();
console.log(appToken.value);
await writeFile({
value: appToken.value,
expiration: appToken.expiration,
});
}
Container.set('logger', LoggerInstance);
const openService = Container.get(OpenService);
const appToken = await openService.generateSystemToken();
console.log(appToken.value);
await writeFile({
value: appToken.value,
expiration: appToken.expiration,
});
} catch (error) {
console.log(error);
}
}
async function readFile() {
return new Promise<any>((resolve, reject) => {
fs.readFile(
path.join(config.configPath, 'token.json'),
{ encoding: 'utf8' },
(err, data) => {
if (err) {
resolve({});
} else {
resolve(JSON.parse(data));
}
},
);
});
}
async function writeFile(data: any) {
return new Promise<void>((resolve, reject) => {
fs.writeFile(tokenFile, JSON.stringify(data), { encoding: 'utf8' }, () => {
+1 -6
View File
@@ -1,12 +1,7 @@
#!/usr/bin/env bash
get_token() {
local tokenFile="$dir_static/build/token.js"
if [[ ! -f "$tokenFile" ]]; then
token=$(ts-node-transpile-only "$dir_root/back/token.ts")
else
token=$(node "$tokenFile")
fi
token=$(cat $file_auth_token | jq -r .value)
}
add_cron_api() {
+1
View File
@@ -24,6 +24,7 @@ file_sharecode=$dir_config/sharecode.sh
file_config_user=$dir_config/config.sh
file_auth_sample=$dir_sample/auth.sample.json
file_auth_user=$dir_config/auth.json
file_auth_token=$dir_config/token.json
file_extra_shell=$dir_config/extra.sh
file_task_before=$dir_config/task_before.sh
file_task_after=$dir_config/task_after.sh
+4 -7
View File
@@ -1,8 +1,5 @@
export const version = '2.13.7';
export const changeLogLink = 'https://t.me/jiao_long/322';
export const changeLog = `2.13.7 版本说明
1. 修复typescript脚本引用sendNotify文件,感谢 charmingYouYou
2. 修复订阅管理搜索
3. 修复环境变量\n转义
4. repo命令后缀参数改为竖线分割
export const version = '2.13.8';
export const changeLogLink = 'https://t.me/jiao_long/323';
export const changeLog = `2.13.8 版本说明
1. 修改系统token访问逻辑,加快任务启动速度
`;