init.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package routers
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/jinzhu/gorm"
  6. _ "github.com/jinzhu/gorm/dialects/mysql"
  7. "net/http"
  8. "strings"
  9. )
  10. //goland:noinspection GoUnreachableCode
  11. func Gininit(HttpRort, Mysql string) {
  12. r := gin.Default()
  13. //"mqtt:UQ7sPD8YZwCF2Zg!@tcp(192.168.0.88:3306)/mqtt?charset=utf8mb4&loc=Local&parseTime=True"
  14. db, err := gorm.Open("mysql", Mysql)
  15. defer db.Close()
  16. if err != nil {
  17. panic(any("mysql Err!"))
  18. }
  19. // 开启 Logger, 以展示详细的日志
  20. db.LogMode(true)
  21. // 1.创建路由
  22. // 2.绑定路由规则,执行的函数
  23. // gin.Context,封装了request和response
  24. r.POST("/CreateAuth", func(c *gin.Context) {
  25. // 生产批次(数字:u32)
  26. // 出库经办人编号(数字:u32)
  27. // 产品防伪码(数字:u32)
  28. // 产品生产时间(unix)
  29. // 产品入库时间(unix)
  30. // 产品出库时间(unix)
  31. // SN码(文本:<=16个字符)
  32. // mqtt域名/IP(文本:<=32个字符)
  33. // mqtt端口(数字:1~65534)
  34. // mqtt用户名(文本:<=32个字符)
  35. // mqtt密码(文本:<=32个字符)
  36. // mqtt保活时间(数字:30~120)
  37. // mqtt消息质量(数字:0~2)
  38. //sn := ""
  39. sn := c.PostForm("sn")
  40. GetRandstringI := 0
  41. if len(sn) < 10 {
  42. for true {
  43. sn = WeekByDate() + GetRandstring(10, "0123456789123456789", int64(GetRandstringI))
  44. count := 0
  45. db.Table("mqtt_user").Where("username = ?", sn).Count(&count)
  46. println("count:", count)
  47. if count == 0 {
  48. break
  49. }
  50. GetRandstringI += 1
  51. }
  52. } else {
  53. count := 0
  54. db.Table("mqtt_user").Where("username = ?", sn).Count(&count)
  55. println("count:", count)
  56. if count != 0 {
  57. c.JSON(http.StatusOK, gin.H{"status": 201, "smg": "sn 重复!"})
  58. return
  59. }
  60. }
  61. psaa := GetRandstring(16, "", 1000)
  62. psaa_sha256 := Sha256("salt" + psaa)
  63. model := c.PostForm("model")
  64. sver := c.PostForm("sver")
  65. hver := c.PostForm("hver")
  66. imei := c.PostForm("imei")
  67. iccid := c.PostForm("iccid")
  68. db_ := db.Exec(" INSERT INTO mqtt_user(username, password_hash, salt, is_superuser, model, sver, hver, imei, iccid) VALUES ('" + sn + "', '" + psaa_sha256 + "', 'salt', 0, '" + model + "', '" + sver + "', '" + hver + "', '" + imei + "', '" + iccid + "');")
  69. if db_.Error != nil {
  70. c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
  71. return
  72. }
  73. db_ = db.Exec(" INSERT INTO mqtt_user(username, password_hash, salt, is_superuser, model, sver, hver, imei, iccid) VALUES ('" + sn + "_s', '" + psaa_sha256 + "', 'salt', 0, '" + model + "', '" + sver + "', '" + hver + "', '" + imei + "', '" + iccid + "');")
  74. if db_.Error != nil {
  75. c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
  76. return
  77. }
  78. c.JSON(http.StatusOK, gin.H{"status": 200, "sn": sn, "psaa": psaa})
  79. return
  80. })
  81. r.GET("/CreateAuth_2", func(c *gin.Context) {
  82. // 生产批次(数字:u32)
  83. // 出库经办人编号(数字:u32)
  84. // 产品防伪码(数字:u32)
  85. // 产品生产时间(unix)
  86. // 产品入库时间(unix)
  87. // 产品出库时间(unix)
  88. // SN码(文本:<=16个字符)
  89. // mqtt域名/IP(文本:<=32个字符)
  90. // mqtt端口(数字:1~65534)
  91. // mqtt用户名(文本:<=32个字符)
  92. // mqtt密码(文本:<=32个字符)
  93. // mqtt保活时间(数字:30~120)
  94. // mqtt消息质量(数字:0~2)
  95. //sn := ""
  96. sn := c.Query("sn")
  97. if len(sn) < 10 {
  98. c.JSON(http.StatusOK, gin.H{"status": 201, "msg": "sn == null"})
  99. return
  100. }
  101. //count := 0
  102. db_ := db.Exec("DELETE FROM mqtt_user WHERE username = ?;", sn)
  103. db_ = db.Exec("DELETE FROM mqtt_user WHERE username = ?;", sn+"_s")
  104. //
  105. //db.Debug().Table("mqtt_user").Where("username = ?", sn).Count(&count)
  106. //println("count:",count)
  107. //if count != 0 {
  108. // c.JSON(http.StatusOK, gin.H{"status": 201,"smg": "sn 重复!"})
  109. // return
  110. //}
  111. psaa := GetRandstring(16, "", 1000)
  112. psaa_sha256 := Sha256("salt" + psaa)
  113. model := c.Query("model")
  114. sver := c.Query("sver")
  115. hver := c.Query("hver")
  116. imei := c.Query("imei")
  117. iccid := c.Query("iccid")
  118. db_ = db.Exec(" INSERT INTO mqtt_user(username, password_hash, salt, is_superuser, model, sver, hver, imei, iccid) VALUES ('" + sn + "', '" + psaa_sha256 + "', 'salt', 0, '" + model + "', '" + sver + "', '" + hver + "', '" + imei + "', '" + iccid + "');")
  119. if db_.Error != nil {
  120. c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
  121. return
  122. }
  123. db_ = db.Exec(" INSERT INTO mqtt_user(username, password_hash, salt, is_superuser, model, sver, hver, imei, iccid) VALUES ('" + sn + "_s', '" + psaa_sha256 + "', 'salt', 0, '" + model + "', '" + sver + "', '" + hver + "', '" + imei + "', '" + iccid + "');")
  124. if db_.Error != nil {
  125. c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
  126. return
  127. }
  128. c.JSON(http.StatusOK, gin.H{"status": 200, "sn": sn, "user": sn, "pass": psaa, "mqtt1": "mqttjxit.coldbaozhida.com", "mqtt2": "mqttlodr.coldbaozhida.com", "mqtt3": "mqttyuht.coldbaozhida.com", "mqtt4": "", "mqtt5": "", "port": 1883})
  129. return
  130. })
  131. r.POST("/MqttAcl", func(c *gin.Context) {
  132. //${clientid} — 客户端的 ID。
  133. //${username} — 客户端登录是用的用户名。
  134. //${peerhost} — 客户端的源 IP 地址。
  135. //${proto_name} — 客户端使用的协议名称。例如 MQTT,CoAP 等。
  136. //${mountpoint} — 网关监听器的挂载点(主题前缀)。
  137. //${action} — 当前执行的动作请求,例如 publish,subscribe。
  138. //${topic} — 当前请求想要发布或订阅的主题(或主题过滤器)
  139. username := c.PostForm("username")
  140. clientid := c.PostForm("clientid")
  141. peerhost := c.PostForm("peerhost")
  142. mountpoint := c.PostForm("mountpoint")
  143. topic := c.PostForm("topic")
  144. action := c.PostForm("action")
  145. fmt.Println("username:", username)
  146. fmt.Println("clientid:", clientid)
  147. fmt.Println("peerhost:", peerhost)
  148. fmt.Println("mountpoint:", mountpoint)
  149. fmt.Println("topic:", topic)
  150. fmt.Println("action:", action)
  151. if username == "admin" {
  152. c.JSON(200, gin.H{"result": "allow"})
  153. return
  154. }
  155. if username == "test" {
  156. c.JSON(200, gin.H{"result": "allow"})
  157. return
  158. }
  159. //
  160. if username != clientid {
  161. fmt.Println("username != clientid")
  162. c.JSON(200, gin.H{"result": "deny"})
  163. return
  164. }
  165. //topic /sub/SN /pub/SN
  166. topic_list := strings.Split(topic, "/")
  167. if len(topic_list) < 3 {
  168. fmt.Println("len(topic_list) < 3", len(topic_list))
  169. c.JSON(200, gin.H{"result": "deny"})
  170. return
  171. }
  172. Clientid_list := strings.Split(username+"_s", "_")
  173. username = Clientid_list[0]
  174. if topic_list[2] != username {
  175. fmt.Println("topic_list[2] != username", topic_list[2])
  176. c.JSON(200, gin.H{"result": "deny"})
  177. return
  178. }
  179. c.JSON(200, gin.H{"result": "allow"})
  180. return
  181. })
  182. r.Run(":" + HttpRort)
  183. }