12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- 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)
- }
- type DeviceInfo struct {
- DeviceId string
- CreateTime time.Time
- AppName string
- AppVersion string
- }
- 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
- }
- // b0:6e:bf:c5:32:30
- func getRealMacAddr(netInterfaces []net.InterfaceStat) string {
- loindex := 0
- for _, netInterface := range netInterfaces {
- if netInterface.Name == "lo" {
- loindex = netInterface.Index
- break
- }
- }
- if len(netInterfaces) < loindex+1 {
- return ""
- }
- return netInterfaces[loindex].HardwareAddr
- }
|