mac-cpu.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package api
  2. import (
  3. "box-cost/conf"
  4. "box-cost/db/repo"
  5. "box-cost/log"
  6. "crypto/hmac"
  7. "crypto/sha256"
  8. "fmt"
  9. "net/http"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. "github.com/shirou/gopsutil/net"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. )
  15. func Mac(r *GinRouter) {
  16. r.GET("/mac/info", MacInfo)
  17. r.POST("/get/code", GetCode)
  18. }
  19. type DeviceInfo struct {
  20. DeviceId string
  21. CreateTime time.Time
  22. AppName string
  23. AppVersion string
  24. }
  25. const SECRET_KEY = "DFDFEXVEG"
  26. func MacInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  27. fmt.Println(conf.AppConfig.Name)
  28. interfaces, err := net.Interfaces()
  29. if err != nil {
  30. return nil, err
  31. }
  32. macAddr := getRealMacAddr(interfaces)
  33. deviceInfo := &DeviceInfo{
  34. DeviceId: UtilMd5(macAddr),
  35. AppName: "Toolchain",
  36. AppVersion: "v1.0.0",
  37. CreateTime: time.Now(),
  38. }
  39. return deviceInfo, nil
  40. }
  41. func GetCode(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  42. deviceId := c.Query("deviceId")
  43. if len(deviceId) < 1 {
  44. fmt.Println("deviceId 为空")
  45. c.String(http.StatusOK, "err")
  46. return nil, nil
  47. }
  48. key := []byte(SECRET_KEY)
  49. message := []byte(deviceId) // 待签名的消息
  50. // 创建一个 HMAC-SHA256 实例
  51. h := hmac.New(sha256.New, key)
  52. // 写入消息
  53. h.Write(message)
  54. // 计算签名
  55. signature := h.Sum(nil)
  56. fmt.Printf("%x\n", signature)
  57. // fmt.Println(len("86068c837d554c8aba4f997b51be6bc209b68cdad7c5a4a0adccbfc289adf4a2"))
  58. // fmt.Println(len("63e208dc7452ab74932b9e37"))
  59. // fmt.Println(len(UtilMd5(mac)))
  60. appName := c.Query("appName")
  61. appVersion := c.Query("appVersion")
  62. // create_time := c.Query("createTime")
  63. // deviceId设置唯一key
  64. type RegCode struct {
  65. Id primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
  66. AppName string `bson:"appName,omitempty" json:"appName"`
  67. AppVersion string `bson:"appVersion,omitempty" json:"appVersion"`
  68. DeviceId string `bson:"deviceId,omitempty" json:"deviceId"`
  69. Key string `bson:"key,omitempty" json:"key"`
  70. Code string `bson:"code,omitempty" json:"code"`
  71. CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
  72. UpdateTIme time.Time `bson:"updateTIme,omitempty" json:"updateTIme"`
  73. ExpireTime time.Time `bson:"expireTime,omitempty" json:"expireTime"`
  74. Status int `bson:"status,omitempty" json:"status"`
  75. }
  76. _, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), "reg-code", &RegCode{
  77. AppName: appName,
  78. AppVersion: appVersion,
  79. DeviceId: deviceId,
  80. Key: SECRET_KEY,
  81. Code: fmt.Sprintf("%x", signature),
  82. CreateTime: time.Now(),
  83. UpdateTIme: time.Now(),
  84. ExpireTime: time.Now().AddDate(0, 0, 3),
  85. Status: 1,
  86. })
  87. if err != nil {
  88. log.Error(err)
  89. return nil, err
  90. }
  91. // 输出签名结果
  92. c.String(http.StatusOK, fmt.Sprintf("%x", signature))
  93. return nil, nil
  94. }
  95. // b0:6e:bf:c5:32:30
  96. func getRealMacAddr(netInterfaces []net.InterfaceStat) string {
  97. loindex := 0
  98. for _, netInterface := range netInterfaces {
  99. if netInterface.Name == "lo" {
  100. loindex = netInterface.Index
  101. break
  102. }
  103. }
  104. if len(netInterfaces) < loindex+1 {
  105. return ""
  106. }
  107. return netInterfaces[loindex].HardwareAddr
  108. }