MQTT 協議學習:001-搭建MQTT通信環境,並抓包測試


--- title: protocol-app-mqtt-1-setup-mqtt-debug-env date: 2020-02-03 11:22:51 categories: tags: - mqtt - protocol - env ---

背景

目的:了解MQTT 通信的有關概念與流程;方便推算某些數據與文檔描述是否一致。

為了能夠在保證學習質量的前提下,降低配置環境的門檻,我們將服務器搭建在windwos中,實行內網間的MQTT協議訪問。

搭建Linux平台下的 MQTT 環境可以參考:《arm linux 移植 MQTT》 、或者根據mosquitto的readme文檔,這一講主要是為了快速入門。

HOST: Windows10-x64
MQTT: mosquitto v1.6.8-x64(mosquitto 官網下載比較慢,點擊這里進行下載)

mosquitto 自帶了MQTT服務器與MQTT客戶端的命令行工具,對於習慣了在Linux命令行下工作學習的我來說,使用起來非常方便😀。

網絡分析工具 :WireShark v3.2.1-x64

搭建MQTT 服務

下載mosquitto_v1.6.8-x64

注意,以管理員權限安裝、路徑不要帶有空格

修改配置文件
編輯 Mosquitto安裝路徑下的 mosquitto.conf文件,在任何位置插入下面文本

#設置不允許匿名登錄(調試可以不加,否則下文需要新建用戶)
allow_anonymous false
#設置賬戶密碼文件位置
password_file c:/mosquitto/pwfile

創建用戶
1)打開cmd
2)進入mosquito安裝目錄:cd c:\mosquitto (如果將mosquito添加進環境變量,此步可以省略)
3)輸入以下命令(保證 pwfile的路徑和上面的配置一致。)

創建多個用戶是為了測試 信息互不干擾的情況。

# 創建第1個用戶,輸入密碼時界面是不會顯示的,直接輸入后回車就可以,需要連續輸入兩次。(下同)
mosquitto_passwd -c c:/mosquitto/pwfile admin (使用-c 參數會導致清空密碼文件)

# 創建第2個用戶
mosquitto_passwd    c:/mosquitto/pwfile user     (不使用-c 表示追加用戶,不影響舊用戶)

# 示例
#C:\Users\schips>cd c:\mosquitto
#c:\mosquitto>mosquitto_passwd -c c:/mosquitto/pwfile admin
#Password:
#Reenter password:
#c:\mosquitto>mosquitto_passwd c:/mosquitto/pwfile user
#Password:
#Reenter password:

啟動服務
1)打開cmd
2)進入mosquito安裝目錄:cd c:\mosquitto (如果將mosquito添加進環境變量,此步可以省略)
3)輸入以下命令(保證 pwfile的路徑和上面的配置一致。)

mosquitto.exe -c mosquitto.conf

#或者使其在windows下后台運行。
start /b mosquitto.exe -c mosquitto.conf

# 示例
#c:\Users\schips>cd c:\mosquitto
#c:\mosquitto>start /b mosquitto.exe -c mosquitto.conf

測試 mqtt

先訂閱一個主題
開啟一個cmd命令行窗口

mosquitto_sub -u admin -P root -t 'topic' -v
# -u 后面跟着 賬號, -p 指定密碼(當allow_anonymous 為true時,可以不指定)
# -t 指定一個主題 (這個參數可以指定多次,至少指定一次)
# -v 代表 實時顯示收到的消息

后發布一個主題消息
另外開啟一個cmd命令行窗口

mosquitto_pub -u admin -P root -t 'topic' -m 'test'

效果
訂閱者能夠收到發布者發來的信息,那么證明測試是成功的。

簡單抓包測試

1)打開Wireshark,選擇你的網卡,添加以下過濾條件,並點擊開始捕獲。

tcp.port==1883

2)打開終端,輸入以下命令,發布一條消息。MQTT快速入門

mosquitto_pub -d -u admin -P root -t 'topic' -m 'test' 

# -d 代表調試模式,以顯示詳細的連接以及數據收發過程。

Client mosq-CEBchWY2M3FxBeiHS8 sending CONNECT
Client mosq-CEBchWY2M3FxBeiHS8 received CONNACK (0)
Client mosq-CEBchWY2M3FxBeiHS8 sending PUBLISH (d0, q0, r0, m1, ''topic'', ... (6 bytes))
Client mosq-CEBchWY2M3FxBeiHS8 sending DISCONNECT

3)進入Wireshark捕獲窗口,發現捕獲到了一些TCP和MQTT協議,如下:

4)查看其中一條MQTT消息,比如Connect Command,點擊這一行。查看MQTT消息內容。其字節碼為

MQ Telemetry Transport Protocol, Connect Command
    Header Flags: 0x10, Message Type: Connect Command
        0001 .... = Message Type: Connect Command (1)
        .... 0000 = Reserved: 0
    Msg Len: 55
    Protocol Name Length: 4
    Protocol Name: MQTT
    Version: MQTT v3.1.1 (4)
    Connect Flags: 0xc6, User Name Flag, Password Flag, QoS Level: At most once delivery (Fire and Forget), Will Flag, Clean Session Flag
        1... .... = User Name Flag: Set
        .1.. .... = Password Flag: Set
        ..0. .... = Will Retain: Not set
        ...0 0... = QoS Level: At most once delivery (Fire and Forget) (0)
        .... .1.. = Will Flag: Set
        .... ..1. = Clean Session Flag: Set
        .... ...0 = (Reserved): Not set
    Keep Alive: 60
    Client ID Length: 23
    Client ID: mosq-UmzxCNey7s4rXXl05L
    Will Topic Length: 3
    Will Topic: 123
    Will Message Length: 0
    Will Message: 
    User Name Length: 5
    User Name: admin
    Password Length: 4
    Password: root

...
0040   10 37 00 04 4d 51 54 54 04 c6 00 3c 00 17 6d 6f   .7..MQTT...<..mo
0050   73 71 2d 55 6d 7a 78 43 4e 65 79 37 73 34 72 58   sq-UmzxCNey7s4rX
0060   58 6c 30 35 4c 00 03 31 32 33 00 00 00 05 61 64   Xl05L..123....ad
0070   6d 69 6e 00 04 72 6f 6f 74                        min..root

現在不了解這些東西是什么含義,沒關系,不要慌。明白了抓包以后,接下來的章節可以經常參考本文的內容,不懂就抓包。

在抓包協議中看到的 Message Identifier在本系列文章中 指的是 Packet Identifier,兩者沒有區別。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM