mosquitto

还是4台机器,128,129,130,131。128是桥接中心,这次准备把129设置为需要证书才能连接的。

基于ca证书的桥接

生成证书

129主机,生成CA证书后启动服务,生成证书参考前面写的。

配置

用winscp连接128和129,然后把129的ca证书,放到128的 /etc/mosquitto/ca/ca129/

128桥接配置

# vi /etc/mosquitto/mosquitto.conf...#connection bridge129
#address 192.168.193.129:1883
#cleansession true
#topic # both 0
#notifications true
#notification_topic notifications129
#remote_username lxy129
#remote_password lxy129# 129安全接口配置
connection bridge129SSL
address 192.168.193.129:8883
cleansession true
topic # both 0
notifications true
notification_topic notifications129SSL
remote_username lxy129
remote_password lxy129
bridge_cafile /etc/mosquitto/ca/ca129/ca.crt
bridge_keyfile /etc/mosquitto/ca/ca129/client.key
bridge_certfile /etc/mosquitto/ca/ca129/client.crt
bridge_tls_version tlsv1# 130
connection bridge130
address 192.168.193.130:1883
cleansession true
topic # both 0
notifications true
notification_topic notifications130# 131
connection bridge131
address 192.168.193.131:1883
cleansession true
topic # both 0
notifications true
notification_topic notifications131...

启动

此时启动128的mosquitto,会要求输入两个 PEM pass。

root@ubuntu:/etc/mosquitto/ca# mosquitto -c /etc/mosquitto/mosquitto.conf
Enter PEM pass phrase:  # 因为128主机也是有配置证书访问的,输入128服务端的密码
Enter PEM pass phrase:  # 这个就是访问129客户端的证书密码

在129发送消息,好长一串。

root@ubuntu:~# mosquitto_pub -h 192.168.193.129 -p 8883 -t "ssl/topic/129" --tls-version tlsv1 --cafile /etc/mosquitto/ca/ca.crt --cert /etc/mosquitto/ca/client.crt --key /etc/mosquitto/ca/client.key -m "i'm 129, msg to 129, port 8883" -u lxy129 -P lxy129
Enter PEM pass phrase:
root@ubuntu:~#

128,129,130,131都监听话题 #,都能收到消息。

# 128
root@ubuntu:/etc/mosquitto/certs# mosquitto_sub -t "#" -u lxy128 -P lxy128 -v
ssl/topic/129 i'm 129, msg to 129, port 8883# 129
root@ubuntu:~# mosquitto_sub -t "#" -u lxy129 -P lxy129 -v
ssl/topic/129 i'm 129, msg to 129, port 8883# 130
root@ubuntu:~# mosquitto_sub -t "#" -v
ssl/topic/129 i'm 129, msg to 129, port 8883# 131
root@ubuntu:~# mosquitto_sub -t "#" -v
ssl/topic/129 i'm 129, msg to 129, port 8883

129发布的消息,发布到128(桥接中心)是通过SSL传输的,报文截下来是加密的,但是128发送到130和131都是明文发送。

这点根据配置文件也可以看出来,对于130和131都是“裸奔”的消息。

# vi /etc/mosquitto/mosquitto.conf...# 129安全接口配置
connection bridge129SSL
address 192.168.193.129:8883
cleansession true
topic # both 0
notifications true
notification_topic notifications129SSL
remote_username lxy129
remote_password lxy129
bridge_cafile /etc/mosquitto/ca/ca129/ca.crt
bridge_keyfile /etc/mosquitto/ca/ca129/client.key
bridge_certfile /etc/mosquitto/ca/ca129/client.crt
bridge_tls_version tlsv1# 130
connection bridge130
address 192.168.193.130:1883
cleansession true
topic # both 0
notifications true
notification_topic notifications130# 131
connection bridge131
address 192.168.193.131:1883
cleansession true
topic # both 0
notifications true
notification_topic notifications131...

消息死循环一

另外,如果把下面这个bridge129的注释放开。即和129主机桥接了两个端口1883和8883,那么就会出现消息的无限发送,128,129,130,131的订阅都被消息无限刷屏

# vi /etc/mosquitto/mosquitto.conf...# 129 1883端口
connection bridge129
address 192.168.193.129:1883
cleansession true
topic # both 0
notifications true
notification_topic notifications129
remote_username lxy129
remote_password lxy129# 129安全接口配置
connection bridge129SSL
address 192.168.193.129:8883
cleansession true
topic # both 0
notifications true
notification_topic notifications129SSL
remote_username lxy129
remote_password lxy129
bridge_cafile /etc/mosquitto/ca/ca129/ca.crt
bridge_keyfile /etc/mosquitto/ca/ca129/client.key
bridge_certfile /etc/mosquitto/ca/ca129/client.crt
bridge_tls_version tlsv1...

我自己的推断与测试,当我只配置一个的时候,如下是开放1883端口,注释8883端口,注册的情况是这样的:

当我发送消息的时候,是这样的:

截取的报文也是符合这样的。129发送一份给本地订阅者,然后发送一份给桥接中心(可以认为也是一个订阅者)。 然后128作为桥接中心,向130,131这两个订阅者发送该消息

当把两个注释都放开,就是和129主机的1883和8883端口都配置了桥接,那么现在注册的情况如下

那么现在,情况是这样的:由于128和129之间配置了两个桥接,分别是1883和8883端口,那么mosquitto把它们认为是两个不同的订阅,而1883和8883端口订阅的话题都是 # ,就是订阅所有话题。

当我在129上发送一个消息给1883端口。

mosquitto_pub -t "topic/nossl/129" -m "xxxx"

此消息第一步如红色所示,会发送给本地的一个sub和128主机。

当128主机收到该消息时,它会向所有订阅该主题的订阅者发送该消息。所以,130和131是能够收到消息的,但同时因为8883端口也是订阅所有话题的,(mosquitto知道给它发送消息的是1883端口,并且不会再向1883端口发送这个消息,这个我们从前面只配置1883端口的时候截取的报文也可以知道),但它会向8883端口发送该消息, 如下绿色那条msg。

接下来连锁反应就来了,129作为订阅者(128和129互为订阅者,因为配置中 topic # both 0),收到那个绿色的msg之后,它会向符合该消息和话题订阅者发送绿色的msg。

注意,此时129发送该绿色msg(我把颜色改成了蓝色,和原来发送红色消息路径一样), 最后128又会有一条msg(紫色表示)发送给129。

然后就死循环了,解决方法:

  • 1、对一台主机只配置一个桥接,这样妥妥的没问题。
  • 2、小心合理配置要订阅的topic,如果订阅的话题不是#, 对一台主机的两个桥接配置,不要让订阅的topic有交集(这样就等于堵死从128->129的8883端口的那条绿色的消息)
# vi /etc/mosquitto/mosquitto.conf...connection bridge129
address 192.168.193.129:1883
cleansession true
# 订阅 topic/nossl/#
topic topic/nossl/# both 0
notifications true
notification_topic notifications129
remote_username lxy129
remote_password lxy129connection bridge129SSL
address 192.168.193.129:8883
cleansession true
# 订阅 topic/ssl/#
topic topic/ssl/# both 0
notifications false
notification_topic notifications129SSL
remote_username lxy129
remote_password lxy129
bridge_cafile /etc/mosquitto/ca/ca129/ca.crt
bridge_keyfile /etc/mosquitto/ca/ca129/client.key
bridge_certfile /etc/mosquitto/ca/ca129/client.crt
bridge_tls_version tlsv1...
  • 3、和解决方法二类似的道理,我们可以控制订阅的方向,同时订阅in,或者同时订阅out。这样数据只会从128->129或者只会从129->128,就不会出现循环消息。
# vi /etc/mosquitto/mosquitto.conf...
connection bridge129
address 192.168.193.129:1883
cleansession true
# 订阅in
topic # in 0
notifications true
notification_topic notifications129
remote_username lxy129
remote_password lxy129connection bridge129SSL
address 192.168.193.129:8883
cleansession true
# 订阅in
topic # in 0
notifications false
notification_topic notifications129SSL
remote_username lxy129
remote_password lxy129
bridge_cafile /etc/mosquitto/ca/ca129/ca.crt
bridge_keyfile /etc/mosquitto/ca/ca129/client.key
bridge_certfile /etc/mosquitto/ca/ca129/client.crt
bridge_tls_version tlsv1...

消息死循环二

还有另一种情况,就是配置了多个桥接中心,之前我还特别好奇的配置了两个桥接中心,结果也是消息死循环。这是一幅草稿图。。。。

128中的桥接配置

address 192.168.139.129:1883
topic # both 0address 192.168.139.130:1883
topic # both 0

129中的桥接配置

address 192.168.139.128:1883
topic # both 0address 192.168.139.130:1883
topic # both 0

和前面出现的问题都是同一个原因

128发送一条消息msg,会发送到129和130

当129收到来自128的,又会将该消息发送给128和130

然后也出现了死循环

2018-11-15-mqtt-mosquitto系列11之配置基于ca证书的桥接相关推荐

  1. MQTT mosquitto

    #背景 Mosquitto是一个开源(BSD许可证)的消息代理,实现MQTT(消息队列遥测传输)协议版本3.1. MQTT(MQ Telemetry Transport),消息队列遥测传输协议,轻量级 ...

  2. Interview:算法岗位面试—11.15下午上海某航天***公司(国企)技术面之工业机器视觉认知、计算机视觉算法的理解、目标检测相关项目案例

    ML岗位面试:11.15下午上海某航天***公司(国企)技术面之工业机器视觉认知.计算机视觉算法的理解.目标检测相关项目案例 Interview:算法岗位面试-11.15下午上海某航天***公司(国企 ...

  3. kubernetes系列11—PV和PVC详解

    kubernetes系列11-PV和PVC详解 原文:kubernetes系列11-PV和PVC详解 本文收录在容器技术学习系列文章总目录 1.认识PV/PVC/StorageClass 1.1 介绍 ...

  4. 狂神学习系列11:SpringBoot

    狂神学习系列11:SpringBoot 声明: 本文章是基于狂神的课程所编写,本人才疏学浅,内容仅作参考 项目和markdown文件资料: 07_SpringBoot: 基于狂神说SpringBoot ...

  5. python步骤解析 给定一个整数数组和目标值_给定一个整数数组和一个目标值,找出数组中和为目标值的两个数 例如给定nums = [2,7,11,15],target = 9...

    python解决方案 nums = [1,2,3,4,5,6] #假如这是给定的数组 target = 9 #假如这是给定的目标值 num_list = [] #用来装结果的容器 def run(nu ...

  6. SAP PM 初级系列11 - 为维修工单触发采购申请

    SAP PM 初级系列11 - 为维修工单触发采购申请 执行事务代码IW32,输入维修工单号,进入维修工单的修改界面,进入components选项卡,可以为维修工单增加备品备件(non-stock i ...

  7. SAP PM 入门系列11 - 一个维护通知单只能创建一个维护订单?

    SAP PM 入门系列11 - 一个维护通知单只能创建一个维护订单? 在SAP系统里,执行事务代码IW34,输入Notification号码100314924,以及Order type ZM03,试图 ...

  8. c语言数据结构线性表LA和LB,数据结构(C语言版)设有线性表LA(3,5,8,110)和LB(2,6,8,9,11,15,20)求新集合?...

    数据结构(C语言版)设有线性表LA(3,5,8,110)和LB(2,6,8,9,11,15,20)求新集合? 数据结构(C语言版)设有线性表LA(3,5,8,110)和LB(2,6,8,9,11,15 ...

  9. Python3算法基础练习:编程100例(11 ~ 15)

    往期练习: Python3算法基础练习:编程100例(1~5) Python3算法基础练习:编程100例(6 ~ 10) Python3算法基础练习:编程100例(11 ~ 15) 11.兔子问题 1 ...

  10. IntelliJ IDEA 2018.2 发布,支持 Java 11

    转载自   IntelliJ IDEA 2018.2 发布,支持 Java 11 近期,JetBrains发布了其旗舰产品IntelliJ IDEA的2018.2版本.新版本支持即将推出的Java 1 ...

最新文章

  1. [Spring MVC] - @ModelAttribute使用
  2. hashcode是什么意思_面试官:说一下HashMap原理,为什么会产生死循环
  3. mysql简单命令行操作以及环境变量的配置
  4. 顶部固定不动 下部分滚动_彻底解决背景跟随弹窗滚动问题
  5. Tableau Desktop 安装与破解
  6. 2022软件库iAPP源码前端/后端程序整套独立后台
  7. javaSocket网络编程
  8. java mail pom_集成JavaMail
  9. 能让你「情商暴涨」的6个聊天小技巧
  10. 乐扣309:最佳购买股票时机含冷冻期
  11. 用API网关把API管起来
  12. 2022蓝桥杯B组—积木画——递推算法
  13. 微信无法连接服务器1-500,GIF表情超过500kb无法添加到微信的解决方法
  14. 微信小程序开发 从创建到使用
  15. Hero项目分析(基于Angular架构)
  16. 7年沉淀之作--滴滴Logi日志服务套件
  17. 相识到爱的不可自拔的过程(学年总结)
  18. MLDonkey命令手册
  19. 解决谷歌翻译不能用的方法
  20. git status的用法

热门文章

  1. .net / .net core excel转pdf
  2. Hello December
  3. C语言read和write函数解析
  4. 七日杀服务器技能点修改,七日杀技能点修改方法 七日杀技能点怎么调
  5. 读《Ideal MHD》(1)-磁流体力学方程组推导
  6. Echarts柱状图,实现不同系列,柱体之间的部分重叠效果
  7. C/C++游戏项目详细教程:《黄金矿工》
  8. java将日期转化为周几_关于java中时间和周几转换的小小工具
  9. 17.半拗(小拗)可救可不救
  10. 【CPU微架构】分支预测(二)常用分支预测算法