加密:接口加密解密

This commit is contained in:
luying
2022-07-26 20:47:01 +08:00
parent 144f3ed0b0
commit 469e393f5e
18 changed files with 833 additions and 162 deletions
-1
View File
@@ -32,7 +32,6 @@ import { SurveyRecModel } from "../db/SurveyRec";
import { getPopUpShopData } from "./activity/popUpShopService";
import { TaskPassData } from "../domain/activityField/taskPassField";
import { ActivityGroupModel } from "../db/ActivityGroup";
import { ActivityInRemote } from "../domain/activityField/activityField";
import { redisClient } from "./redisService";
// —————————————— 跑马灯 —————————————— //
+28 -4
View File
@@ -1,9 +1,10 @@
import { Channel, pinus } from "pinus";
import { CHANNEL_PREFIX, PUSH_BATCH, PUSH_INTERVAL, PUSH_ROUTE, STATUS } from "../consts";
import { resResult } from "../pubUtils/util";
import { genCode, resResult } from "../pubUtils/util";
import { getCityChannelSid, getGuildChannelSid, getWorldChannelSid, groupRoomId } from "./chatService";
import { getAllOnlineRoles, getRoleOnlineInfo } from "./redisService";
import { errlogger, infologger } from '../util/logger';
import { MsgEncrypt } from "../pubUtils/sysUtil";
export async function sendMessageToAllWithSuc(route: string, data: any, filterCb?: ({ lv, topLineupCe }) => boolean) {
await sendMessageToAll(route, resResult(STATUS.SUCCESS, data), filterCb);
@@ -84,14 +85,14 @@ export async function sendMessageToUsersWithSuc(route: string, data: any, uids:
// 推送给个人的方法收束于这个函数
export async function sendMessageToUsers(route: string, data: any, uids: { uid: string, sid: string }[]) {
if(uids.length > 0) {
pinus.app.get('channelService').pushMessageByUids(route, data, uids);
pinus.app.get('channelService').pushMessageByUids(route, encryptMsg(data), uids);
infologger.debug(`pushMessage route: ${route} data: ${JSON.stringify(data)} members: ${uids.map(obj => obj.uid).join()}`);
}
}
export async function sendMessageToTeam(teamCode: string, route: string, data: any) {
const channel = pinus.app.get('channelService').getChannel(teamCode);
sendMessageToChannel(channel, route, resResult(STATUS.SUCCESS, data));
sendMessageToChannel(channel, route, resResult(STATUS.SUCCESS, encryptMsg(data)));
}
/**
@@ -153,4 +154,27 @@ export function removeFromTeamChannel(teamCode: string, roleId: string) {
if (users.indexOf(roleId) !== -1) {
channel.removeMember(roleId);
}
}
}
function getKvFromMemory() {
let k = pinus.app.get('aeaKey');
let v = pinus.app.get('aesIV');
let kvRefTime = pinus.app.get('kvRefTime');
let now = new Date().setMinutes(0, 0, 0);
if(!kvRefTime || now - kvRefTime >= 60 * 60 * 1000) {
k = genCode(24);
v = genCode(16);
pinus.app.set('aeaKey', k);
pinus.app.set('aesIV', v);
pinus.app.set('kvRefTime', now);
}
return { k, v }
}
function encryptMsg(json: any) {
let kv = getKvFromMemory();
let msgEncrypt = new MsgEncrypt(kv);
let { aesKey, aesIV } = msgEncrypt.getEncodeKv();
let data = msgEncrypt.encryptMsg(json);
return { data, k: aesKey, v: aesIV }
}
+2
View File
@@ -337,6 +337,7 @@ export async function roleLeave(roleId: string) {
await redisClient().hdelAsync(REDIS_KEY.ONLINE_CNT, roleId);
await redisClient().hdelAsync(REDIS_KEY.ONLINE_USERS, roleId);
await redisClient().hdelAsync(REDIS_KEY.USER_CODE, role.userCode);
await redisClient().hdelAsync(REDIS_KEY.ONLINE_CNT, roleId);
}
return role;
}
@@ -365,6 +366,7 @@ export async function getRoleOnlineInfo(roleId: string) {
try {
let result = str.split('|');
let [userCode, sid, createTime, serverId, lv, topLineupCe] = result;
return {
isOnline: true, userCode, sid,
createtime: parseInt(createTime),