up
This commit is contained in:
@@ -9,6 +9,8 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"meshtastic_mqtt_server/mqtpp"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -90,7 +92,7 @@ func parseArgs() (*config, error) {
|
|||||||
if cfg.qos < 0 || cfg.qos > 2 {
|
if cfg.qos < 0 || cfg.qos > 2 {
|
||||||
return nil, fmt.Errorf("invalid qos %d: must be 0, 1, or 2", cfg.qos)
|
return nil, fmt.Errorf("invalid qos %d: must be 0, 1, or 2", cfg.qos)
|
||||||
}
|
}
|
||||||
key, err := expandPSK(cfg.psk)
|
key, err := mqtpp.ExpandPSK(cfg.psk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -139,7 +141,7 @@ func run(cfg *config) error {
|
|||||||
// handleMessage 返回 MQTT 消息回调,把原始 payload 交给 MQTTPP 处理后按类型输出。
|
// handleMessage 返回 MQTT 消息回调,把原始 payload 交给 MQTTPP 处理后按类型输出。
|
||||||
func handleMessage(key []byte) mqtt.MessageHandler {
|
func handleMessage(key []byte) mqtt.MessageHandler {
|
||||||
return func(_ mqtt.Client, msg mqtt.Message) {
|
return func(_ mqtt.Client, msg mqtt.Message) {
|
||||||
valid, _, decodedJSON := MQTTPP(msg.Topic(), msg.Payload(), key)
|
valid, _, decodedJSON := mqtpp.MQTTPP(msg.Topic(), msg.Payload(), key)
|
||||||
if !valid || len(decodedJSON) == 0 {
|
if !valid || len(decodedJSON) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -158,7 +160,7 @@ func handleMessage(key []byte) mqtt.MessageHandler {
|
|||||||
|
|
||||||
// printJSON 将记录编码为 JSON 后按数据包类型着色输出。
|
// printJSON 将记录编码为 JSON 后按数据包类型着色输出。
|
||||||
func printJSON(record map[string]any) {
|
func printJSON(record map[string]any) {
|
||||||
printJSONBytes(record, mustJSON(record))
|
printJSONBytes(record, mqtpp.MustJSON(record))
|
||||||
}
|
}
|
||||||
|
|
||||||
// printJSONBytes 使用已编码好的 JSON 文本,并根据记录 type 选择控制台颜色。
|
// printJSONBytes 使用已编码好的 JSON 文本,并根据记录 type 选择控制台颜色。
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package mqtpp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/aes"
|
"crypto/aes"
|
||||||
@@ -89,17 +89,17 @@ func MQTTPP(topic string, raw []byte, key []byte) (bool, []byte, []byte) {
|
|||||||
|
|
||||||
env, err := parseServiceEnvelope(raw)
|
env, err := parseServiceEnvelope(raw)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, raw, mustJSON(map[string]any{"topic": topic, "error": "protobuf decode failed: " + err.Error(), "payload_len": len(raw)})
|
return true, raw, MustJSON(map[string]any{"topic": topic, "error": "protobuf decode failed: " + err.Error(), "payload_len": len(raw)})
|
||||||
}
|
}
|
||||||
record, err := describePacket(topic, env, key)
|
record, err := describePacket(topic, env, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return true, raw, mustJSON(map[string]any{"topic": topic, "error": err.Error(), "payload_len": len(raw)})
|
return true, raw, MustJSON(map[string]any{"topic": topic, "error": err.Error(), "payload_len": len(raw)})
|
||||||
}
|
}
|
||||||
return true, raw, mustJSON(record)
|
return true, raw, MustJSON(record)
|
||||||
}
|
}
|
||||||
|
|
||||||
// expandPSK 展开 Base64 PSK,兼容 Meshtastic 默认索引 PSK 和短 key 补零规则。
|
// ExpandPSK 展开 Base64 PSK,兼容 Meshtastic 默认索引 PSK 和短 key 补零规则。
|
||||||
func expandPSK(pskBase64 string) ([]byte, error) {
|
func ExpandPSK(pskBase64 string) ([]byte, error) {
|
||||||
psk, err := base64.StdEncoding.DecodeString(pskBase64)
|
psk, err := base64.StdEncoding.DecodeString(pskBase64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid psk: %w", err)
|
return nil, fmt.Errorf("invalid psk: %w", err)
|
||||||
@@ -131,8 +131,8 @@ func isCompliantMQTTPacket(_ []byte) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// mustJSON 将记录编码成 JSON;编码失败时返回包含错误信息的 JSON。
|
// MustJSON 将记录编码成 JSON;编码失败时返回包含错误信息的 JSON。
|
||||||
func mustJSON(record map[string]any) []byte {
|
func MustJSON(record map[string]any) []byte {
|
||||||
text, err := json.Marshal(record)
|
text, err := json.Marshal(record)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
text, _ = json.Marshal(map[string]any{"error": err.Error()})
|
text, _ = json.Marshal(map[string]any{"error": err.Error()})
|
||||||
Reference in New Issue
Block a user