animeic 2 years ago
parent
commit
ed81173bb9
2 changed files with 54 additions and 13 deletions
  1. 53 12
      boxcost/api/mac-cpu.go
  2. 1 1
      boxcost/api/router.go

+ 53 - 12
boxcost/api/mac-cpu.go

@@ -1,34 +1,75 @@
 package api
 
 import (
+	"box-cost/conf"
+	"crypto/hmac"
+	"crypto/sha256"
+	"fmt"
+	"net/http"
+	"time"
+
 	"github.com/gin-gonic/gin"
 	"github.com/shirou/gopsutil/net"
 )
 
 func Mac(r *GinRouter) {
 	r.GET("/mac/info", MacInfo)
+	r.POST("/get/code", GetCode)
 }
 
-func MacInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	// diskInfo, _ := disk.Partitions(false)
-	// fmt.Printf("%+v\n", diskInfo)
-
-	// netInfo, _ := net.Interfaces()
-	// fmt.Printf("%+v\n", netInfo)
-
-	// cpuInfo, _ := cpu.Info()
-	// fmt.Printf("%+v\n", cpuInfo)
+type DeviceInfo struct {
+	DeviceId   string
+	CreateTime time.Time
+	AppName    string
+	AppVersion string
+}
 
-	// memInfo, _ := mem.VirtualMemory()
-	// fmt.Printf("%+v\n", memInfo)
+const SECRET_KEY = "DFDFEXVEG"
 
+func MacInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	fmt.Println(conf.AppConfig.Name)
 	interfaces, err := net.Interfaces()
 	if err != nil {
 		return nil, err
 	}
 	macAddr := getRealMacAddr(interfaces)
+	deviceInfo := &DeviceInfo{
+		DeviceId:   UtilMd5(macAddr),
+		AppName:    "Toolchain",
+		AppVersion: "v1.0.0",
+		CreateTime: time.Now(),
+	}
+
+	return deviceInfo, nil
+}
+
+func GetCode(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	deviceId := c.Query("deviceId")
+	if len(deviceId) < 1 {
+		fmt.Println("deviceId 为空")
+		c.String(http.StatusOK, "err")
+		return nil, nil
+	}
+	key := []byte(SECRET_KEY)
+	message := []byte(deviceId) // 待签名的消息
+
+	// 创建一个 HMAC-SHA256 实例
+	h := hmac.New(sha256.New, key)
+
+	// 写入消息
+	h.Write(message)
+
+	// 计算签名
+	signature := h.Sum(nil)
+	fmt.Printf("%x\n", signature)
+	// fmt.Println(len("86068c837d554c8aba4f997b51be6bc209b68cdad7c5a4a0adccbfc289adf4a2"))
+	// fmt.Println(len("63e208dc7452ab74932b9e37"))
+	// fmt.Println(len(UtilMd5(mac)))
+
+	// 输出签名结果
+	c.String(http.StatusOK, fmt.Sprintf("%x", signature))
+	return nil, nil
 
-	return UtilMd5(macAddr), nil
 }
 
 // b0:6e:bf:c5:32:30

+ 1 - 1
boxcost/api/router.go

@@ -58,7 +58,7 @@ func RegRouters(svc *Service) {
 	// 成品采购管理
 	Product(boxcost)
 
-	// Mac(boxcost)
+	Mac(boxcost)
 
 	boxcost.GET("/apk/version", func(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		out, err := repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{