修改部分代码

This commit is contained in:
wuhan 2023-07-31 16:28:48 +08:00
parent fc2d26c7bc
commit 53337e72f2
4 changed files with 41 additions and 19 deletions

View File

@ -1,14 +1,14 @@
TEST_VALUE:
name: cookie
MYSQL_CONFIG:
type: "mysql"
# type: "mysql"
host: "192.168.10.4"
port: 3306
user: "root"
password: "123456"
database: "finclip"
entities: "mysql" # 这里的命名一定要跟 MongoDB 里面的配置命名区分开
synchronize: false
# entities: "mysql" # 这里的命名一定要跟 MongoDB 里面的配置命名区分开
# synchronize: false
OPTIONS:
clean: true # true: 清除会话, false: 保留会话
connectTimeout: 60 * 60 #超时时间

View File

@ -78,13 +78,15 @@ client.on('message', async (topic, mes) => {
const { bindList } = payload;
await mqttService.bindInfo(bindList, hccId);
info = await mqttService.encrypt(seId, keyAlias, res);
client.publish(`2/rsp/${hccId}`, info);
console.log('mqtt---cmd:1---返回数据:', info);
client.publish(`2/rsp/${hccId}`, JSON.stringify({ message: info }));
break;
case '2':
// 家庭解绑同步
await mqttService.unbind(payload);
info = await mqttService.encrypt(seId, keyAlias, res);
client.publish(`2/rsp/${hccId}`, info);
console.log('mqtt---cmd:2---返回数据:', info);
client.publish(`2/rsp/${hccId}`, JSON.stringify({ message: info }));
break;
case '3':
// 查询小程序列表
@ -93,7 +95,8 @@ client.on('message', async (topic, mes) => {
appInfos: data,
};
info = await mqttService.encrypt(seId, keyAlias, res);
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
console.log('mqtt---cmd:3---返回数据:', info);
client.publish(`2/rsp/${hccId}`, JSON.stringify({ message: info }));
break;
case '4':
// 家庭地址同步
@ -104,7 +107,8 @@ client.on('message', async (topic, mes) => {
hccId,
});
info = await mqttService.encrypt(seId, keyAlias, res);
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
console.log('mqtt---cmd:4---返回数据:', info);
client.publish(`2/rsp/${hccId}`, JSON.stringify({ message: info }));
break;
case '5':
// 查询应用服务授权列表
@ -113,7 +117,8 @@ client.on('message', async (topic, mes) => {
appInfos: data,
};
info = await mqttService.encrypt(seId, keyAlias, res);
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
console.log('mqtt---cmd:5---返回数据:', info);
client.publish(`2/rsp/${hccId}`, JSON.stringify({ message: info }));
break;
case '6':
// 家庭数据推送
@ -126,7 +131,7 @@ client.on('message', async (topic, mes) => {
console.log('家庭数据推送数据成功');
console.log('家庭数据推送数据成功');
info = await mqttService.encrypt(seId, keyAlias, res);
client.publish(`2/rsp/${hccId}`, JSON.stringify(res));
client.publish(`2/rsp/${hccId}`, JSON.stringify({ message: info }));
break;
default:
break;
@ -160,6 +165,8 @@ client.subscribe(['1/req/#', '1/rsp/#']);
setInterval(async () => {
console.log(`${new Date()}查询家庭数据`);
const reqId = stringRandom(16);
// info = await mqttService.encrypt(seId, keyAlias, res);
// 调用加密接口
const data = await new SecureService().encrypt({
seId: '',

View File

@ -1,6 +1,11 @@
import { Injectable } from '@nestjs/common';
import { AxiosResult } from 'src/utils/axios';
import { getConfig, secureDectyptBase64, secureHeader } from '../utils/index';
import {
getConfig,
secureDectyptBase64,
secureEncryptBase64,
secureHeader,
} from '../utils/index';
import Base from '../utils/base';
import globalConfig from '../utils/global';
@ -27,15 +32,15 @@ export class SecureService {
'Content-Type': 'application/json',
};
if (globalConfig.cookie[seId]) {
headers['cookie'] = [global.cookie];
headers['cookie'] = globalConfig.cookie[seId];
}
// 证书申请并下载
// 调用加密接口
const result = await AxiosResult(
`${getConfig().secureIp}/api/v1/se/key/encrypt`,
{
seId,
keyAlias,
data,
data: secureEncryptBase64(data),
algorithm,
iv,
},
@ -47,8 +52,8 @@ export class SecureService {
return;
}
// 取出header
globalConfig.cookie[seId] = result.headers['set-cookie'][0];
return result.data.data;
// globalConfig.cookie[seId] = result.headers['set-cookie'][0];
return result.data.data.encData;
}
/**
@ -69,7 +74,7 @@ export class SecureService {
'Content-Type': 'application/json',
};
if (globalConfig.cookie[seId]) {
headers['cookie'] = [global.cookie];
headers['cookie'] = globalConfig.cookie[seId];
}
const result = await AxiosResult(
`${getConfig().secureIp}/api/v1/se/key/decrypt`,
@ -88,7 +93,9 @@ export class SecureService {
// 错误的回应
return;
}
globalConfig.cookie[seId] = result.headers['set-cookie'][0];
return secureDectyptBase64(result.data.data.data);
if (result.headers['set-cookie']?.length) {
globalConfig.cookie[seId] = result.headers['set-cookie'][0];
}
return JSON.parse(secureDectyptBase64(result.data.data.data));
}
}

View File

@ -19,13 +19,21 @@ export const getConfig = () => {
return config;
};
// 安全模块,转码base64
// 安全模块请求头转码base64
export function secureHeader() {
const data = new Buffer(`${getConfig().title}:${getConfig().secret}`);
const base64Data = data.toString('base64');
return `Basic ${base64Data}`;
}
//转码base64
export function secureEncryptBase64(data) {
const base = Buffer.from(JSON.stringify(data));
const base64Data = base.toString('base64');
return base64Data;
}
// 解码base64
export function secureDectyptBase64(data) {
const base = new Buffer(data, 'base64');
return base.toString();