const mosca = require('mosca');
const tls = require('tls');
var mqttconfig=require('../config/mqtt_config/mqtt_config').config;
//var dataParse = require('./dataParse');
var CAFILE = '../ssl-file/cert_mesh_mqtts_100y_sha256/ca.crt';
var SECURE_KEY = '../ssl-file/cert_mesh_mqtts_100y_sha256/server.key.insecure';
var SECURE_CERT = '../ssl-file/cert_mesh_mqtts_100y_sha256/server.crt';
var sslsetting = {
port: 1883,
logger:{
name:"secureExample",
level:40,
},
secure :{
port: 9883,
keyPath: SECURE_KEY,
certPath: SECURE_CERT,
caPaths: [CAFILE], //ca could have lots,so this place should use array.
requestCert: true,
}
};
var server = new mosca.Server(sslsetting); //connect client
var clientId;
//var packetMessage;
server.on('clientConnected', function(client) {
console.log('client connected ! cilent id :', client.id);
clientIdclientId=client.id;
});
server.on('published', function(packet,client)
{
});
server.on('message', function (topic, message) {
});
// Accepts the connection according to some conditions
var authenticate = function(client, username, password, callback) {
//console.log('client:',client.id);
//console.log('username:',username);
//console.log('password:',password);
callback(null, true);
}
// Accepts the Publish according to some conditions
var authorizePublish = function(client, topic, payload, callback) {
//console.log('pub:',client.id.indexOf("/server/xiaofu968343"), (client.id.indexOf("/server/xiaofu968343") == 0), (topic.indexOf("/server/") == 0));
callback(null, ((client.id.indexOf(mqttconfig.server_id) == 0) || (topic.indexOf("/server/") == 0)));
}
// Accepts the Subscribe according to some conditions
var authorizeSubscribe = function(client, topic, callback) {
//console.log('client:',client.id);
//console.log('client:',client.user);
//console.log('topic:',topic);
//callback(null, client.user == topic.split('/')[1]);
callback(null, true);
}
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Welcome to MESH server using MQTT!');
console.log('2018.5.19 14.50 ');
//server.authenticate = authenticate;
server.authorizePublish = authorizePublish;
//server.authorizeSubscribe = authorizeSubscribe;
}
mqtt雙向ssl認證
最新推荐文章于 2024-03-04 12:38:31 发布
本文介绍如何使用mosca库配置MQTT服务器,实现双SSL认证。通过设置CA证书、服务器密钥和证书,以及监听不同端口,确保客户端连接的安全性。同时,定义了认证、发布和订阅的授权策略。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Stable-Diffusion-3.5
图片生成
Stable-Diffusion
Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率
1万+

被折叠的 条评论
为什么被折叠?



