加密:接口加密解密

This commit is contained in:
luying
2022-06-16 19:14:05 +08:00
parent 144f3ed0b0
commit 469e393f5e
18 changed files with 833 additions and 162 deletions

View File

@@ -15,6 +15,8 @@ export const STATUS = {
ACCESS_BUSY: { code: 12, simStr: '您的点击过于频繁,服务器受不了了' },
ONLINE_USER_MAX: { code: 13, simStr: '服务器繁忙,请稍后再试' },
FUNCTION_CLOSE: { code: 14, simStr: '抱歉,该功能暂时关闭' },
TIMESTAMP_ERR: { code: 15, simStr: '请求时间参数错误' },
DUPLICATE_ACCESS: { code: 16, simStr: '随机请求参数重复' },
GLOBAL_ERR: { code: 1003, simStr: '服务器内部错误' },
UPDATE_INFO_ERR: {code: 1004, simStr: '热更新配置错误'},
// http请求

View File

@@ -35,6 +35,10 @@ export interface DicServerConst {
readonly CHECK_WORD: number;
// 是否可以支付
readonly CAN_PAY: number;
// 请求timestamp差距
readonly TIME_STAMP_OVER: number;
// 请求参数过期时间
readonly ACCESS_CODE_EXPIRE_TIME: number;
}
export const dicServerConst: DicServerConst = {} as DicServerConst;

117
shared/pubUtils/sysUtil.ts Normal file
View File

@@ -0,0 +1,117 @@
import { ENCRYPT_IV, ENCRYPT_KEY } from '../consts';
import { WhiteListModel } from '../db/RegionWhiteList';
const fs = require('fs');
const path = require('path');
import { aesDecrypt, aesEncrypt } from './util';
const crypto = require('crypto');
const isJSON = require('koa-is-json');
const privateKey = fs.readFileSync(path.resolve(__dirname, `../resource/privateKey`));
const publicKey = fs.readFileSync(path.resolve(__dirname, `../resource/publicKey`)); // 发推送加密的秘钥和privateKey不是一对
export async function checkWhiteList(env: string, ip: string, uid: number, serverId: number) {
if(ip) {
let result = await WhiteListModel.checkIp(env, ip);
if(!!result) return true;
}
if(uid && serverId) {
let result = await WhiteListModel.checkUid(env, serverId, uid);
if(!!result) return true;
}
return false
}
export class MsgEncrypt {
private k: string = ENCRYPT_KEY;
private v: string = ENCRYPT_IV;
private encodeK: string = '';
private encodeV: string = '';
constructor(data: { k?: string, v?: string, encodeK?: string, encodeV?: string }) {
if(data.k && data.v) {
this.encodeAndSetKv(data.k, data.v);
}
if(data.encodeK && data.encodeV) {
this.decodeAndSetKv(data.encodeK, data.encodeV);
}
}
public decryptMsg(data: string) {
if(!data) return false
try {
const decodeStr = aesDecrypt(data, this.k, this.v);
console.log('decoded str:', decodeStr);
let body = JSON.parse(decodeStr);
return body
} catch(e) {
console.error(e);
return false;
}
}
public encryptMsg(json: Object) {
if(!isJSON(json)) return false;
try {
const encodeStr = aesEncrypt(JSON.stringify(json), this.k, this.v);
// console.log('encode str:', encodeStr);
return encodeStr;
} catch(e) {
console.error(e);
return false;
}
}
public decodeAndSetKv(requestK: string, requestV: string) {
if(requestK) {
this.encodeK = requestK;
this.k = this.privateDecrypt(Buffer.from(requestK, 'base64'));
}
if(requestV) {
this.encodeV = requestV;
this.v = this.privateDecrypt(Buffer.from(requestV, 'base64'));
}
return this.getKv();
}
public getKv() {
return { aesKey: this.k, aesIV: this.v }
}
public getEncodeKv() {
return { aesKey: this.encodeK, aesIV: this.encodeV }
}
private privateDecrypt(encryptMsg: Buffer) {
const decryptMsg = crypto.privateDecrypt(
{ key: privateKey, padding: crypto.constants.RSA_PKCS1_PADDING },
encryptMsg
);
return decryptMsg.toString();
}
private getRsaEncodedData(original: string) {
const encryptMsg = crypto.publicEncrypt(
{ key: publicKey, padding: crypto.constants.RSA_PKCS1_PADDING },
Buffer.from(original)
);
return encryptMsg.toString('base64');
}
private encodeAndSetKv(k: string, v: string) {
if(k) {
this.k = k;
this.encodeK = this.getRsaEncodedData(k);
}
if(v) {
this.v = v;
this.encodeV = this.getRsaEncodedData(v);
}
}
}

View File

@@ -10,7 +10,6 @@ import { ABI_STAGE, REFRESH_TIME, ROBOT_SYS_TYPE, ITEM_CHANGE_REASON, WAR_TYPE,
import { findIndex } from 'underscore';
import { getZeroPointD } from './timeUtil';
import { Floor } from '../domain/activityField/gachaField';
import { WhiteListModel } from '../db/RegionWhiteList';
import { RewardInter } from './interface';
import { gameData } from './data';
const randomName = require("chinese-random-name");
@@ -621,17 +620,6 @@ export function splitString(dataString: string, key: string) {
return numberArray;
}
export async function checkWhiteList(env: string, ip: string, uid: number, serverId: number) {
if(ip) {
let result = await WhiteListModel.checkIp(env, ip);
if(!!result) return true;
}
if(uid && serverId) {
let result = await WhiteListModel.checkUid(env, serverId, uid);
if(!!result) return true;
}
return false
}
export function isTimestamp(time: number, len = 10) {
if(!isNumber(time)) return false;

View File

@@ -16,6 +16,8 @@
{ "id": 8, "desc": "捐赠", "route": "guild.donateHandler.donate", "param": {}, "interval": 200 },
{ "id": 9, "desc": "碾压镇念塔", "route": "battle.towerBattleHandler.skipTower", "param": {}, "interval": 200 }
],
"TIME_STAMP_OVER": 900000,
"ACCESS_CODE_EXPIRE_TIME": 900000,
"DEBUG_PAY": 0,
"DEBUG_TIME": 1,
"CHECK_WORD": 1,

View File

@@ -0,0 +1,6 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDiU143FYs5+uJlIn/AK6gKhOEl
WydR/7OJ1MUSWFJz9yL/acIyNBQ4Ji/0adDaIB01pQ9eqCYFP0Z9SgGMcpST/ejU
LEUbjls7GTwutxoVVe9PUDBkRY601RVAEjXaZmWfZZ7028uWrqfJmeEo3voKhscO
/vCOKII2TM1xC+FMGwIDAQAB
-----END PUBLIC KEY-----