加密:接口加密解密
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user