|
|
@@ -37,6 +37,31 @@ const options = {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+const whiteList = [
|
|
|
+ {
|
|
|
+ type: 'topic',
|
|
|
+ val: /^\$SYS/
|
|
|
+ }, {
|
|
|
+ type: 'topic',
|
|
|
+ eq: true,
|
|
|
+ val: willTopic
|
|
|
+ }, {
|
|
|
+ type: 'topic',
|
|
|
+ val: /^\d+\/\d+\/status/
|
|
|
+ }, {
|
|
|
+ val: /messageId/
|
|
|
+ }
|
|
|
+]
|
|
|
+
|
|
|
+function isWhite (item, topic, message) {
|
|
|
+ switch (item.type) {
|
|
|
+ case 'topic':
|
|
|
+ return item.eq ? topic === item.val : item.val.test(topic)
|
|
|
+ default:
|
|
|
+ return item.val.test(message)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
let mqttClient
|
|
|
function start () {
|
|
|
console.log('Connecting mqtt client...')
|
|
|
@@ -73,44 +98,36 @@ function start () {
|
|
|
mqttClient = null
|
|
|
})
|
|
|
|
|
|
- client.on('message', (topic, message, packet) => {
|
|
|
- console.log('MQTT topic')
|
|
|
+ client.on('message', (topic, message) => {
|
|
|
+ console.log('MQTT topic', topic)
|
|
|
|
|
|
- if (topic === willTopic) {
|
|
|
- console.log('lwt')
|
|
|
- message = utf8ArrayBufferToString(message)
|
|
|
- } else if (/^\$SYS/.test(topic)) {
|
|
|
- console.log('sys')
|
|
|
- message = utf8ArrayBufferToString(message)
|
|
|
- } else {
|
|
|
- if (typeof message !== 'string') {
|
|
|
- try {
|
|
|
- const s = utf8ArrayBufferToString(message)
|
|
|
- if (s) {
|
|
|
- if (/messageId/.test(s)) {
|
|
|
- console.log('json')
|
|
|
- message = s
|
|
|
+ if (typeof message !== 'string') {
|
|
|
+ try {
|
|
|
+ const s = utf8ArrayBufferToString(message)
|
|
|
+ if (s) {
|
|
|
+ if (whiteList.some(item => isWhite(item, topic, s))) {
|
|
|
+ console.log('white list')
|
|
|
+ message = s
|
|
|
+ } else {
|
|
|
+ const decodeStr = sm4.decrypt(message)
|
|
|
+ if (decodeStr) {
|
|
|
+ console.log('sm4')
|
|
|
+ message = decodeStr
|
|
|
} else {
|
|
|
- const decodeStr = sm4.decrypt(message)
|
|
|
- if (decodeStr) {
|
|
|
- console.log('sm4')
|
|
|
- message = decodeStr
|
|
|
- } else {
|
|
|
- console.log('unencrypted')
|
|
|
- message = s
|
|
|
- }
|
|
|
+ console.log('unencrypted')
|
|
|
+ message = s
|
|
|
}
|
|
|
- } else {
|
|
|
- console.log('null')
|
|
|
- message = s
|
|
|
}
|
|
|
- } catch (e) {
|
|
|
- console.warn(e)
|
|
|
- message = utf8ArrayBufferToString(message)
|
|
|
+ } else {
|
|
|
+ console.log('null')
|
|
|
+ message = s
|
|
|
}
|
|
|
- } else {
|
|
|
- console.log('char')
|
|
|
+ } catch (e) {
|
|
|
+ console.warn(e)
|
|
|
+ message = utf8ArrayBufferToString(message)
|
|
|
}
|
|
|
+ } else {
|
|
|
+ console.log('char')
|
|
|
}
|
|
|
|
|
|
console.log(message)
|