修改mqtt初始化认证问题
This commit is contained in:
parent
06be64d5b4
commit
93bae27c2a
|
@ -12,9 +12,9 @@ MYSQL_CONFIG:
|
|||
OPTIONS:
|
||||
clean: true # true: 清除会话, false: 保留会话
|
||||
connectTimeout: 60 * 60 #超时时间
|
||||
clientId: 'HCC_JmfyUdkLgkBM2tP2'
|
||||
username: 'HCC_JmfyUdkLgkBM2tP2'
|
||||
password: 'vKlbtcTbFLzhtBH9'
|
||||
clientId: 'HCC_ELQbLUKLrRMZeI9d'
|
||||
username: 'HCC_ELQbLUKLrRMZeI9d'
|
||||
password: 'UBHKeh7p5keWffYq'
|
||||
port: 60036
|
||||
mqttUrl: 'tcp://103.222.189.9:60039/mqtt'
|
||||
ipBoxUrl: 'https://103.39.222.134:65531'
|
||||
|
|
18
Dockerfile
18
Dockerfile
|
@ -4,25 +4,23 @@ FROM node:18-alpine
|
|||
ENV TZ=Asia/Shanghai
|
||||
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
|
||||
|
||||
# 创建工作目录
|
||||
RUN mkdir -p /app
|
||||
|
||||
# 指定工作目录
|
||||
WORKDIR /app
|
||||
|
||||
# 复制当前代码到/app工作目录
|
||||
COPY . ./
|
||||
|
||||
# npm 源,选用国内镜像源以提高下载速度
|
||||
RUN npm config set registry https://registry.npm.taobao.org/
|
||||
|
||||
# 使用 npm ci 安装应用依赖
|
||||
# 使用通配符来确保 package.json 和 package-lock.json 被复制
|
||||
COPY package.json /app/package.json
|
||||
RUN rm -rf /app/package-lock.json
|
||||
RUN cd /app && rm -rf /app/node_modules && npm ci
|
||||
|
||||
# 安装应用依赖
|
||||
RUN pwd &&cd /app && ls && npm install --force
|
||||
|
||||
# 复制当前代码到/app工作目录
|
||||
COPY . .
|
||||
|
||||
# 打包
|
||||
RUN cd /app && rm -rf /app/dist && npm run build
|
||||
RUN npm run build
|
||||
|
||||
# 启动服务
|
||||
# "start:prod": "cross-env NODE_ENV=production node ./dist/src/main.js",
|
||||
|
|
|
@ -10,6 +10,7 @@ import { PlatformModule } from './platform/platform.module';
|
|||
import { OpenDataModule } from './open-data/open-data.module';
|
||||
import { TypeOrmModule, TypeOrmModuleAsyncOptions } from '@nestjs/typeorm';
|
||||
import { getConfig } from './utils';
|
||||
// import { PostInterceptor } from './common/interceptors/post.interceptors';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
|
|
@ -293,9 +293,9 @@ class ClientMapper {
|
|||
left join app_status_infos ai on ai.app_id = app.app_id
|
||||
`;
|
||||
if (mpIdOrName) {
|
||||
arr.push(mpIdOrName, mpIdOrName);
|
||||
arr.push(`%${mpIdOrName}%`, `%${mpIdOrName}%`);
|
||||
bool = true;
|
||||
sql += `where (mp_id = ? or app.name = ?) `;
|
||||
sql += `where (mp_id like ? or app.name like ?) `;
|
||||
}
|
||||
if (type) {
|
||||
// mpIdOrName存在,需要二次拼接
|
||||
|
@ -330,8 +330,8 @@ class ClientMapper {
|
|||
let sql =
|
||||
'select id,address_detail as address,auth_time,online_status as status from szja_hcc_info';
|
||||
if (hccId && hccId != '') {
|
||||
sql += ` where id = ?`;
|
||||
arr.push(hccId);
|
||||
sql += ` where id like ?`;
|
||||
arr.push(`%${hccId}%`);
|
||||
}
|
||||
const countArr = arr;
|
||||
arr.push((pageNo - 1) * pageSize, pageSize);
|
||||
|
@ -353,8 +353,8 @@ class ClientMapper {
|
|||
`;
|
||||
// hcc需要做表关联
|
||||
if (hccId) {
|
||||
sql += ` left join szja_user_home uh on uh.user_id = i.id where uh.hcc_id = ?`;
|
||||
arr.push(hccId);
|
||||
sql += ` left join szja_user_home uh on uh.user_id = i.id where uh.hcc_id like ?`;
|
||||
arr.push(`%${hccId}%`);
|
||||
}
|
||||
if (userId) {
|
||||
bool = true;
|
||||
|
@ -363,11 +363,11 @@ class ClientMapper {
|
|||
}
|
||||
if (mobile) {
|
||||
if (bool) {
|
||||
sql += ` and mobile = ?`;
|
||||
sql += ` and mobile like ?`;
|
||||
} else {
|
||||
sql += ` where mobile = ?`;
|
||||
sql += ` where mobile like ?`;
|
||||
}
|
||||
arr.push(mobile);
|
||||
arr.push(`%${mobile}%`);
|
||||
bool = true;
|
||||
}
|
||||
const countArr = arr;
|
||||
|
|
22
src/common/interceptors/post.interceptors.ts
Normal file
22
src/common/interceptors/post.interceptors.ts
Normal file
|
@ -0,0 +1,22 @@
|
|||
import {
|
||||
CallHandler,
|
||||
ExecutionContext,
|
||||
HttpStatus,
|
||||
Injectable,
|
||||
NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
@Injectable()
|
||||
export class PostInterceptor implements NestInterceptor {
|
||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
const request = context.switchToHttp().getRequest<Request>();
|
||||
const response = context.switchToHttp().getResponse<Response>();
|
||||
|
||||
if (request.method === 'POST') {
|
||||
if (response.status === 201)
|
||||
context.switchToHttp().getResponse().status(HttpStatus.OK);
|
||||
}
|
||||
return next.handle();
|
||||
}
|
||||
}
|
|
@ -8,11 +8,11 @@ import {
|
|||
HttpException,
|
||||
HttpStatus,
|
||||
Req,
|
||||
HttpCode,
|
||||
} from '@nestjs/common';
|
||||
import { HccService } from './hcc.service';
|
||||
import { IMqttAuth, IHccAuth } from './hcc.types';
|
||||
// import { BusinessException } from 'src/common/exceptions/business.exception';
|
||||
import { BUSINESS_ERROR_CODE } from 'src/common/exceptions/business.error.codes';
|
||||
|
||||
@Controller('hcc')
|
||||
export class HccController {
|
||||
|
@ -21,6 +21,7 @@ export class HccController {
|
|||
* mqtt认证
|
||||
* @param mqttAuthBody
|
||||
*/
|
||||
@HttpCode(200)
|
||||
@Post('/api/v1/mqttAuth')
|
||||
mqttAuth(@Body() mqttAuthBody: IMqttAuth) {
|
||||
return this.hccService.mqttAuth(mqttAuthBody);
|
||||
|
|
|
@ -16,7 +16,24 @@ export class HccService {
|
|||
async mqttAuth(mqttAuthBody: IMqttAuth) {
|
||||
const { clientid, username, password } = mqttAuthBody;
|
||||
const data = await HccMapper.getPasswdById(clientid, username);
|
||||
if (data?.length && password == data[0].mqtt_passwd) {
|
||||
const optclientId = globalConfig.options.clientId;
|
||||
const optPassword = globalConfig.options.password;
|
||||
console.log('optclientId+optPassword:', optclientId, optPassword);
|
||||
console.log(
|
||||
'判断:',
|
||||
optclientId,
|
||||
clientid,
|
||||
username,
|
||||
optPassword,
|
||||
password,
|
||||
);
|
||||
|
||||
if (
|
||||
(data?.length && password == data[0].mqtt_passwd) ||
|
||||
((optclientId == clientid || username == optclientId) &&
|
||||
optPassword == password)
|
||||
) {
|
||||
console.log('认证成功');
|
||||
return {
|
||||
result: 'allow', // 可选 "allow" | "deny" | "ignore"
|
||||
is_superuser: true, // 可选 true | false,该项为空时默认为 false
|
||||
|
|
|
@ -3,31 +3,36 @@ import * as mqtt from 'mqtt';
|
|||
import { getConfig } from '../utils/index';
|
||||
import * as stringRandom from 'string-random';
|
||||
// import * as crypto from 'crypto'
|
||||
// import aesDecrypt from './encrypt'
|
||||
import Status from '../utils/status';
|
||||
import { encrypt } from '../utils/encrypt';
|
||||
import mqttService from './mqtt.service';
|
||||
import { MpService } from '../mp/mp.service';
|
||||
import global from '../utils/global';
|
||||
import globalConfig from '../utils/global';
|
||||
import { SecureService } from '../secure/secure.service';
|
||||
import hccMapper from 'src/hcc/hcc.mapper';
|
||||
import mqttMapper from './mqtt.mapper';
|
||||
import { v4 } from 'uuid';
|
||||
// import { v4 } from 'uuid';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const crypto = require('crypto');
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const moment = require('moment');
|
||||
moment.locale('zh_CN');
|
||||
|
||||
// 地方服务平台生成连接地址
|
||||
// getConfig().options.clientId = `IOT_ASP_${stringRandom(16)}`
|
||||
// getConfig().options.key = stringRandom(16)
|
||||
// getConfig().options.password = aesDecrypt(getConfig().options.clientId, getConfig().options.key)
|
||||
// globalConfig.options = getConfig().OPTIONS;
|
||||
globalConfig.options.clientId = `IOT_ASP_${stringRandom(16)}`;
|
||||
globalConfig.options.username = globalConfig.options.clientId;
|
||||
const key = crypto.randomBytes(16).toString('hex');
|
||||
globalConfig.options.password = encrypt(globalConfig.options.clientId, key);
|
||||
console.log('mqtt配置', JSON.stringify(globalConfig.options));
|
||||
// console.log(JSON.stringify(getConfig().OPTIONS));
|
||||
|
||||
const client = mqtt.connect(getConfig().mqttUrl, globalConfig.options);
|
||||
// const client = mqtt.connect(getConfig().mqttUrl, getConfig().options);
|
||||
const client = mqtt.connect(getConfig().mqttUrl, getConfig().options);
|
||||
|
||||
console.log('开始连接mqtt');
|
||||
|
||||
client.on('connect', async () => {
|
||||
//初始化日志数据
|
||||
await mqttMapper.addLog();
|
||||
console.log(`成功连接到服务器[${getConfig().mqttUrl}]`);
|
||||
});
|
||||
|
||||
|
@ -40,8 +45,7 @@ client.on('message', async (topic, mes) => {
|
|||
data: JSON.parse(mes.toString()),
|
||||
};
|
||||
console.log('reqData:', JSON.stringify(reqData));
|
||||
const { seId, keyAlias, message, pass } = reqData.data;
|
||||
// if (!pass) {
|
||||
const { seId, keyAlias, message } = reqData.data;
|
||||
if (!seId || !keyAlias) {
|
||||
console.log('mqtt接受消息,缺少参数');
|
||||
return;
|
||||
|
@ -61,7 +65,6 @@ client.on('message', async (topic, mes) => {
|
|||
return;
|
||||
}
|
||||
console.log('mqtt解密之后获取的数据:', JSON.stringify(data));
|
||||
// }
|
||||
// const { cmd, reqId, payload } = reqData.data;
|
||||
// const { cmd, reqId, payload } = data; //原始代码需要复原
|
||||
const { cmd, reqId, payload } = message;
|
||||
|
@ -180,7 +183,7 @@ client.on('close', async () => {
|
|||
console.log('已断开');
|
||||
});
|
||||
|
||||
client.subscribe(['1/req/#', '1/rsp/#', '$SYS/brokers/+/clients/+/+']);
|
||||
client.subscribe(['1/req/#', '1/rsp/#']);
|
||||
|
||||
// 查询家庭数据
|
||||
setInterval(async () => {
|
||||
|
@ -208,7 +211,7 @@ setInterval(async () => {
|
|||
// await mqttMapper.updateLog(1);
|
||||
client.publish(`2/req/${hccId}`, JSON.stringify({ message: info }), () => {
|
||||
// 记录reqid,放入全局缓存
|
||||
global.reqIdMap.set(reqId, null);
|
||||
globalConfig.reqIdMap.set(reqId, null);
|
||||
// console.log('通知成功');
|
||||
});
|
||||
}, 500);
|
||||
|
|
|
@ -7,6 +7,13 @@ const globalConfig = {
|
|||
expireTime: 0,
|
||||
token: '',
|
||||
},
|
||||
options: {
|
||||
clean: true, // true: 清除会话, false: 保留会话
|
||||
connectTimeout: 60 * 60, //超时时间
|
||||
clientId: 'HCC_JmfyUdkLgkBM2tP2',
|
||||
username: 'HCC_JmfyUdkLgkBM2tP2',
|
||||
password: 'vKlbtcTbFLzhtBH9',
|
||||
},
|
||||
};
|
||||
|
||||
export default globalConfig;
|
||||
|
|
Loading…
Reference in New Issue
Block a user