import { REDIS_KEY, SDK_37_ADDR, SDK_37_CONST } from '../consts'; import { request37 } from './httpUtil'; import { nowSeconds } from './timeUtil'; import { LoginValidataReturn37 } from '../domain/sdk'; import * as crypto from 'crypto' // 通用加密方法 export function md5(str: string, treatStr?: (str: string) => string) { if(treatStr) str = treatStr(str); console.log('*****str', str) return crypto.createHash('md5').update(str, 'utf8').digest("hex"); }; export function getMd5ObjSign(obj: any, joinMark: string, treatStr?: (str: string) => string) { const str = []; Object.keys(obj).sort().forEach((key) => { if (key == 'sign') { } else { str.push(key + '=' + obj[key]); } }); if (str.length == 0) { console.error('check obj', obj); } var strs = str.join(joinMark); return md5(strs, treatStr); } export async function checkMd5Sign(obj: any, treatStr?: (str: string) => string) { if (getMd5ObjSign(obj, '', treatStr) === obj.sign) { return true; } console.warn('correct sign:', getMd5ObjSign(obj, '', treatStr), obj.sign); return false; } /************** 37sdk **************/ export function get37Md5Sign(body: any, key: string) { return getMd5ObjSign(body, '&', (str) => `${str}&${key}`); } export async function loginValidate37(clientId: string, pst: string) { let result: LoginValidataReturn37 = await request37(SDK_37_ADDR.LOGIN, { clientid: clientId, pid: SDK_37_CONST.PID, game_id: SDK_37_CONST.GAME_ID, pst, time: nowSeconds(), c_game_id: SDK_37_CONST.FX_C_GAME_ID }, SDK_37_CONST.LOGIN_KEY); if (result && result.code !== 1) { console.error(result.msg); } return result; } export async function loginValidata(channelType: string, clientId: string, pst: string) { if(channelType == '37') { return await loginValidate37(clientId, pst); } else { return false; } } export function getChannelId(channelType: string, uid: number|string) { return `${channelType}: ${uid}`; } /********* 厚土防沉迷 *********/ export function getVidObjSign(body: any) { return getMd5ObjSign(body, '', treatVid); } export function checkVidObjSign(obj: any) { return checkMd5Sign(obj, treatVid); } function treatVid(str) { return 'vId' + str; } export function getRedisSubChannel(key: REDIS_KEY, env: string) { return `${key}:${env}`; }