|
@@ -100,11 +100,16 @@ func Run_MqttServer() {
|
|
if err != nil {
|
|
if err != nil {
|
|
logs.Println("MqttServer", "订阅消息 [cli.Subscribe]", "")
|
|
logs.Println("MqttServer", "订阅消息 [cli.Subscribe]", "")
|
|
logs.Println("err!!!!!! Run_MqttServer:", "连接MQTT失败:", err)
|
|
logs.Println("err!!!!!! Run_MqttServer:", "连接MQTT失败:", err)
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
fmt.Println("MQTT ok!")
|
|
fmt.Println("MQTT ok!")
|
|
|
|
|
|
|
|
+ go MQTT_Gin_Run() // 初始化 MQTT ACL
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func MQTT_Gin_Run() {
|
|
|
|
+
|
|
// 创建一个Gin路由器
|
|
// 创建一个Gin路由器
|
|
r := gin.Default()
|
|
r := gin.Default()
|
|
|
|
|
|
@@ -117,16 +122,16 @@ func Run_MqttServer() {
|
|
}
|
|
}
|
|
if err := c.Bind(&json); err != nil {
|
|
if err := c.Bind(&json); err != nil {
|
|
c.JSON(200, gin.H{
|
|
c.JSON(200, gin.H{
|
|
- "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
- "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
|
|
|
|
+ "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
+ "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
})
|
|
})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
// 保证 Clientid唯一,否则会 下线
|
|
// 保证 Clientid唯一,否则会 下线
|
|
if json.Clientid != json.Username {
|
|
if json.Clientid != json.Username {
|
|
c.JSON(200, gin.H{
|
|
c.JSON(200, gin.H{
|
|
- "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
- "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
|
|
|
|
+ "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
+ "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
})
|
|
})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -136,42 +141,42 @@ func Run_MqttServer() {
|
|
// 判断是否存在
|
|
// 判断是否存在
|
|
if !Device_r.Read_Tidy() {
|
|
if !Device_r.Read_Tidy() {
|
|
c.JSON(200, gin.H{
|
|
c.JSON(200, gin.H{
|
|
- "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
- "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
|
|
|
|
+ "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
+ "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
})
|
|
})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
// 判断密码是否正确
|
|
// 判断密码是否正确
|
|
if Device_r.T_password != json.Password {
|
|
if Device_r.T_password != json.Password {
|
|
c.JSON(200, gin.H{
|
|
c.JSON(200, gin.H{
|
|
- "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
- "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
|
|
|
|
+ "result": "deny", // 认证结果 可选 "allow" | "deny" | "ignore"
|
|
|
|
+ "is_superuser": false, // 超级用户 可选 true | false,该项为空时默认为 false
|
|
})
|
|
})
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
c.JSON(200, gin.H{
|
|
c.JSON(200, gin.H{
|
|
- "result": "allow", // 可选 "allow" | "deny" | "ignore"
|
|
|
|
- "is_superuser": false, // 可选 true | false,该项为空时默认为 false
|
|
|
|
|
|
+ "result": "allow", // 可选 "allow" | "deny" | "ignore"
|
|
|
|
+ "is_superuser": false, // 可选 true | false,该项为空时默认为 false
|
|
})
|
|
})
|
|
})
|
|
})
|
|
r.POST("/Acl", func(c *gin.Context) {
|
|
r.POST("/Acl", func(c *gin.Context) {
|
|
logs.Println("Acl!!")
|
|
logs.Println("Acl!!")
|
|
var json struct {
|
|
var json struct {
|
|
- Clientid string `json:"clientid"`//${clientid} — 客户端的 ID。
|
|
|
|
- Username string `json:"username"`//${username} — 客户端登录是用的用户名。
|
|
|
|
- Peerhost string `json:"peerhost"`//${peerhost} — 客户端的源 IP 地址。
|
|
|
|
- Proto_name string `json:"proto_name"`//${proto_name} — 客户端使用的协议名称。例如 MQTT,CoAP 等。
|
|
|
|
- Mountpoint string `json:"mountpoint"`//${mountpoint} — 网关监听器的挂载点(主题前缀)。
|
|
|
|
- Action string `json:"action"`//${action} — 当前执行的动作请求,例如 publish,subscribe。
|
|
|
|
- Topic string `json:"topic"`//${topic} — 当前请求想要发布或订阅的主题(或主题过滤器)
|
|
|
|
|
|
+ Clientid string `json:"clientid"` //${clientid} — 客户端的 ID。
|
|
|
|
+ Username string `json:"username"` //${username} — 客户端登录是用的用户名。
|
|
|
|
+ Peerhost string `json:"peerhost"` //${peerhost} — 客户端的源 IP 地址。
|
|
|
|
+ Proto_name string `json:"proto_name"` //${proto_name} — 客户端使用的协议名称。例如 MQTT,CoAP 等。
|
|
|
|
+ Mountpoint string `json:"mountpoint"` //${mountpoint} — 网关监听器的挂载点(主题前缀)。
|
|
|
|
+ Action string `json:"action"` //${action} — 当前执行的动作请求,例如 publish,subscribe。
|
|
|
|
+ Topic string `json:"topic"` //${topic} — 当前请求想要发布或订阅的主题(或主题过滤器)
|
|
}
|
|
}
|
|
if err := c.Bind(&json); err != nil {
|
|
if err := c.Bind(&json); err != nil {
|
|
c.JSON(200, gin.H{
|
|
c.JSON(200, gin.H{
|
|
"result": "deny", // 可选 "allow" | "deny" | "ignore"
|
|
"result": "deny", // 可选 "allow" | "deny" | "ignore"
|
|
})
|
|
})
|
|
}
|
|
}
|
|
- fmt.Println("json:",json)
|
|
|
|
|
|
+ fmt.Println("json:", json)
|
|
|
|
|
|
if json.Username == "admin" {
|
|
if json.Username == "admin" {
|
|
c.JSON(200, gin.H{"result": "allow"})
|
|
c.JSON(200, gin.H{"result": "allow"})
|
|
@@ -197,10 +202,9 @@ func Run_MqttServer() {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
//topic /sub/SN /pub/SN
|
|
//topic /sub/SN /pub/SN
|
|
- topic_list := strings.Split(json.Topic,"/")
|
|
|
|
- for _,v := range topic_list{
|
|
|
|
|
|
+ topic_list := strings.Split(json.Topic, "/")
|
|
|
|
+ for _, v := range topic_list {
|
|
if len(v) < 7 {
|
|
if len(v) < 7 {
|
|
continue // 不是有效SN
|
|
continue // 不是有效SN
|
|
}
|
|
}
|
|
@@ -210,12 +214,11 @@ func Run_MqttServer() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- logs.Println("MQTT Acl E!topic_list:",topic_list)
|
|
|
|
|
|
+ logs.Println("MQTT Acl E!topic_list:", topic_list)
|
|
|
|
|
|
c.JSON(200, gin.H{"result": "deny"})
|
|
c.JSON(200, gin.H{"result": "deny"})
|
|
return
|
|
return
|
|
|
|
|
|
-
|
|
|
|
//
|
|
//
|
|
//Clientid_list := strings.Split(username+"_s", "_")
|
|
//Clientid_list := strings.Split(username+"_s", "_")
|
|
//username = Clientid_list[0]
|
|
//username = Clientid_list[0]
|
|
@@ -273,15 +276,15 @@ func messagePubHandler(topicName string, message []byte) {
|
|
// 设备协议
|
|
// 设备协议
|
|
ProductProt_r := Product.ProductProt{Id: ProductType_r.T_prot}
|
|
ProductProt_r := Product.ProductProt{Id: ProductType_r.T_prot}
|
|
if !ProductProt_r.Read() {
|
|
if !ProductProt_r.Read() {
|
|
- logs.Println("MqttServer", Device_r.T_sn+"|"+Device_r.T_ProductID+"-"+ fmt.Sprintf("%d",ProductType_r.T_prot)+" 设备协议找不到!")
|
|
|
|
|
|
+ logs.Println("MqttServer", Device_r.T_sn+"|"+Device_r.T_ProductID+"-"+fmt.Sprintf("%d", ProductType_r.T_prot)+" 设备协议找不到!")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
// 根据库的存放路径加载库
|
|
// 根据库的存放路径加载库
|
|
- p, err := plugin.Open(conf.Analysis_Dir + ProductProt_r.T_analysis)
|
|
|
|
|
|
+ p, err := plugin.Open(conf.Analysis_Dir + ProductProt_r.T_analysis + ".so")
|
|
if err != nil {
|
|
if err != nil {
|
|
println(err)
|
|
println(err)
|
|
- panic(any(err))
|
|
|
|
|
|
+ logs.PrintlnError("打开 SO 失败:", err)
|
|
}
|
|
}
|
|
|
|
|
|
if topicName[len(topicName)-6:] == "_reply" {
|
|
if topicName[len(topicName)-6:] == "_reply" {
|