123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package routers
- import (
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mysql"
- "net/http"
- "strings"
- )
- //goland:noinspection GoUnreachableCode
- func Gininit(HttpRort, Mysql string) {
- r := gin.Default()
- //"mqtt:UQ7sPD8YZwCF2Zg!@tcp(192.168.0.88:3306)/mqtt?charset=utf8mb4&loc=Local&parseTime=True"
- db, err := gorm.Open("mysql", Mysql)
- defer db.Close()
- if err != nil {
- panic(any("mysql Err!"))
- }
- // 开启 Logger, 以展示详细的日志
- db.LogMode(true)
- // 1.创建路由
- // 2.绑定路由规则,执行的函数
- // gin.Context,封装了request和response
- r.POST("/CreateAuth", func(c *gin.Context) {
- // 生产批次(数字:u32)
- // 出库经办人编号(数字:u32)
- // 产品防伪码(数字:u32)
- // 产品生产时间(unix)
- // 产品入库时间(unix)
- // 产品出库时间(unix)
- // SN码(文本:<=16个字符)
- // mqtt域名/IP(文本:<=32个字符)
- // mqtt端口(数字:1~65534)
- // mqtt用户名(文本:<=32个字符)
- // mqtt密码(文本:<=32个字符)
- // mqtt保活时间(数字:30~120)
- // mqtt消息质量(数字:0~2)
- //sn := ""
- sn := c.PostForm("sn")
- GetRandstringI := 0
- if len(sn) < 10 {
- for true {
- sn = WeekByDate() + GetRandstring(10, "0123456789123456789", int64(GetRandstringI))
- count := 0
- db.Table("mqtt_user").Where("username = ?", sn).Count(&count)
- println("count:", count)
- if count == 0 {
- break
- }
- GetRandstringI += 1
- }
- } else {
- count := 0
- db.Table("mqtt_user").Where("username = ?", sn).Count(&count)
- println("count:", count)
- if count != 0 {
- c.JSON(http.StatusOK, gin.H{"status": 201, "smg": "sn 重复!"})
- return
- }
- }
- psaa := GetRandstring(16, "", 1000)
- psaa_sha256 := Sha256("salt" + psaa)
- model := c.PostForm("model")
- sver := c.PostForm("sver")
- hver := c.PostForm("hver")
- imei := c.PostForm("imei")
- iccid := c.PostForm("iccid")
- 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 + "');")
- if db_.Error != nil {
- c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
- return
- }
- 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 + "');")
- if db_.Error != nil {
- c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
- return
- }
- c.JSON(http.StatusOK, gin.H{"status": 200, "sn": sn, "psaa": psaa})
- return
- })
- r.GET("/CreateAuth_2", func(c *gin.Context) {
- // 生产批次(数字:u32)
- // 出库经办人编号(数字:u32)
- // 产品防伪码(数字:u32)
- // 产品生产时间(unix)
- // 产品入库时间(unix)
- // 产品出库时间(unix)
- // SN码(文本:<=16个字符)
- // mqtt域名/IP(文本:<=32个字符)
- // mqtt端口(数字:1~65534)
- // mqtt用户名(文本:<=32个字符)
- // mqtt密码(文本:<=32个字符)
- // mqtt保活时间(数字:30~120)
- // mqtt消息质量(数字:0~2)
- //sn := ""
- sn := c.Query("sn")
- if len(sn) < 10 {
- c.JSON(http.StatusOK, gin.H{"status": 201, "msg": "sn == null"})
- return
- }
- //count := 0
- db_ := db.Exec("DELETE FROM mqtt_user WHERE username = ?;", sn)
- db_ = db.Exec("DELETE FROM mqtt_user WHERE username = ?;", sn+"_s")
- //
- //db.Debug().Table("mqtt_user").Where("username = ?", sn).Count(&count)
- //println("count:",count)
- //if count != 0 {
- // c.JSON(http.StatusOK, gin.H{"status": 201,"smg": "sn 重复!"})
- // return
- //}
- psaa := GetRandstring(16, "", 1000)
- psaa_sha256 := Sha256("salt" + psaa)
- model := c.Query("model")
- sver := c.Query("sver")
- hver := c.Query("hver")
- imei := c.Query("imei")
- iccid := c.Query("iccid")
- 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 + "');")
- if db_.Error != nil {
- c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
- return
- }
- 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 + "');")
- if db_.Error != nil {
- c.JSON(http.StatusOK, gin.H{"status": 201, "msg": db_.Error})
- return
- }
- 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})
- return
- })
- r.POST("/MqttAcl", func(c *gin.Context) {
- //${clientid} — 客户端的 ID。
- //${username} — 客户端登录是用的用户名。
- //${peerhost} — 客户端的源 IP 地址。
- //${proto_name} — 客户端使用的协议名称。例如 MQTT,CoAP 等。
- //${mountpoint} — 网关监听器的挂载点(主题前缀)。
- //${action} — 当前执行的动作请求,例如 publish,subscribe。
- //${topic} — 当前请求想要发布或订阅的主题(或主题过滤器)
- username := c.PostForm("username")
- clientid := c.PostForm("clientid")
- peerhost := c.PostForm("peerhost")
- mountpoint := c.PostForm("mountpoint")
- topic := c.PostForm("topic")
- action := c.PostForm("action")
- fmt.Println("username:", username)
- fmt.Println("clientid:", clientid)
- fmt.Println("peerhost:", peerhost)
- fmt.Println("mountpoint:", mountpoint)
- fmt.Println("topic:", topic)
- fmt.Println("action:", action)
- if username == "admin" {
- c.JSON(200, gin.H{"result": "allow"})
- return
- }
- if username == "test" {
- c.JSON(200, gin.H{"result": "allow"})
- return
- }
- //
- if username != clientid {
- fmt.Println("username != clientid")
- c.JSON(200, gin.H{"result": "deny"})
- return
- }
- //topic /sub/SN /pub/SN
- topic_list := strings.Split(topic, "/")
- if len(topic_list) < 3 {
- fmt.Println("len(topic_list) < 3", len(topic_list))
- c.JSON(200, gin.H{"result": "deny"})
- return
- }
- Clientid_list := strings.Split(username+"_s", "_")
- username = Clientid_list[0]
- if topic_list[2] != username {
- fmt.Println("topic_list[2] != username", topic_list[2])
- c.JSON(200, gin.H{"result": "deny"})
- return
- }
- c.JSON(200, gin.H{"result": "allow"})
- return
- })
- r.Run(":" + HttpRort)
- }
|