From 4d3569f8983233a0d066dcdd2889ec8318fa77d2 Mon Sep 17 00:00:00 2001 From: stafall Date: Mon, 10 Jan 2022 16:46:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E8=BD=AC=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...21\345\211\215\345\207\206\345\244\207.md" | 8 +++ .../index.py" | 57 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 "\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" diff --git "a/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/\345\274\200\345\217\221\345\211\215\345\207\206\345\244\207.md" "b/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/\345\274\200\345\217\221\345\211\215\345\207\206\345\244\207.md" index e320b87..2615af4 100644 --- "a/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/\345\274\200\345\217\221\345\211\215\345\207\206\345\244\207.md" +++ "b/\345\233\275\346\260\221\345\267\245\344\275\234\350\277\233\345\261\225\345\221\250\346\212\245/\345\274\200\345\217\221\345\211\215\345\207\206\345\244\207.md" @@ -63,3 +63,11 @@ d. 用于双方数字签名的公钥 如设备device001发送信息,则发布主题应为 /device001/message +网关端脚本会识别数据格式对不对,如果不对的话也就在这里就拦截了,而且不会再转发到服务器上,并且会发送一个主题为/{deviceId}/error 的错误信息给设备 + +所以设备要主动订阅/{deviceId}/error 的消息内容 + +设想: + 这里是不是可以定制一个callback主题,让网关处理完消息之后及时发送回调给设备,这样设备也知道自己的消息到底发没发成功,如果没法成功的话在哪里出现了问题,提高交互体验。 +问题1: + 假设中间人在这里做了手脚,发送了这样的主题给设备,让设备误以为消息已经到达了服务器,实际上被拦截下来了,是不是存在着安全的问题。 \ No newline at end of file diff --git "a/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" "b/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" new file mode 100644 index 0000000..bef505c --- /dev/null +++ "b/\346\240\221\350\216\223\346\264\276\344\270\255\350\275\254\350\204\232\346\234\254/index.py" @@ -0,0 +1,57 @@ +import time +import paho.mqtt.client as mqtt +import json,requests + +HOST = '192.168.31.240' +PORT = 1883 + +def on_connect(client, userdata, flags, rc): + if rc == 0: + print('连接成功') + print('Connected with result code ' + str(rc)) + +def on_message(client, userdata, msg): + message = msg.payload.decode("utf-8") + print(msg.topic + "\n" + message) + topic_split = msg.topic.split("/") + deviceId = topic_split[1] + message_type = topic_split[2] + if not (message_type == "error"): # 判断消息不是error否则进入死循环 + print(validate_message(message, deviceId)) + +def validate_message(msg, deviceId): + keys = ['type', 'topic', 'payload', 'timestamp', 'SNNo'] + try: + json_data = json.loads(msg) + for key in keys: + json_data[key] + except Exception: + client.publish("/" + deviceId + "/error","data format wrong!", qos=0, retain=False) + return False + return True + +# def message_handle(msg, topic): +# try: +# after = json.loads(msg.decode()) +# deviceId = after['deviceId'] +# value = after['value'] +# data = { +# "deviceId": deviceId, +# "value": value, +# "title": topic +# } +# json_data = json.dumps(data) +# headers = { +# 'Content-Type': 'application/json' +# } +# requests.post('http://localhost:8000/addMessage',data=json_data,headers=headers) +# except Exception: +# print(Exception) + +client = mqtt.Client(client_id="server_script", protocol=3) +client.on_connect = on_connect +client.on_message = on_message +client.connect(host=HOST, port=PORT, keepalive=60) +time.sleep(1) +client.subscribe('#') +client.loop_forever() \ No newline at end of file -- Gitee