id.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. "math"
  6. "math/big"
  7. "os"
  8. "path"
  9. "strconv"
  10. "strings"
  11. . "github.com/coroot/coroot-node-agent/utils/modelse"
  12. )
  13. func SaveNodeInfo(n *NodeInfoT) {
  14. if n != nil {
  15. NODE_INFO = n
  16. }
  17. }
  18. func BuildInt64ID(str string) ID_STRING {
  19. //srcCode := md5.Sum([]byte(str))
  20. //code := fmt.Sprintf("%x", srcCode)
  21. //id_string := md5ToDec(code)
  22. //return ID_STRING(id_string)
  23. return ID_STRING(hashTo16DigitString(str))
  24. }
  25. func hexStringToBPFBytes(str string, out *HashByte) {
  26. for i := 0; i < len(str)/2; i++ {
  27. ch0 := str[2*i]
  28. ch1 := str[2*i+1]
  29. nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  30. nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  31. (*out)[i] = (nib0 << 4) | nib1
  32. }
  33. }
  34. // GetAccountID 获取账户ID,如果未注册则返回0
  35. func GetAccountID() int {
  36. if NODE_INFO != nil && NODE_INFO.AccountID != 0 {
  37. return NODE_INFO.AccountID
  38. }
  39. // 如果未注册,返回0,等待注册完成后更新
  40. return 0
  41. }
  42. // GetSys
  43. func GetSysTag() string {
  44. if NODE_INFO != nil {
  45. return NODE_INFO.SysTag
  46. }
  47. return ""
  48. }
  49. // GetSys
  50. func GetSystemUUID() string {
  51. if NODE_INFO != nil {
  52. return NODE_INFO.SystemUUID
  53. }
  54. return ""
  55. }
  56. func GetHostIP() string {
  57. if NODE_INFO != nil {
  58. return NODE_INFO.HostIp
  59. }
  60. return ""
  61. }
  62. func GetHostID() int64 {
  63. if NODE_INFO != nil && NODE_INFO.HostID != 0 {
  64. return NODE_INFO.HostID
  65. }
  66. uuid := SystemUUID()
  67. // todo AccountId
  68. str := fmt.Sprintf("%d:%s", GetAccountID(), uuid)
  69. srcCode := md5.Sum([]byte(str))
  70. code := fmt.Sprintf("%x", srcCode)
  71. host_id_string := md5ToDec(code)
  72. id, err := strconv.ParseInt(host_id_string, 10, 64)
  73. if err != nil {
  74. return 0
  75. }
  76. NODE_INFO.HostID = id
  77. //var charArray HashByte
  78. //hexStringToBPFBytes(host_id_string, &charArray)
  79. //HOST_ID_BYTE = charArray
  80. // 将rune切片复制到char数组中
  81. //for i := 0; i < HASH_SIZE; i++ {
  82. // charArray[i] = []byte(host_id_string)[i]
  83. //}
  84. return id
  85. }
  86. func GetHostIDBPFString() HashByte {
  87. host_id_string := strconv.FormatInt(GetHostID(), 10)
  88. var charArray HashByte
  89. hexStringToBPFBytes(host_id_string, &charArray)
  90. return charArray
  91. }
  92. func SystemUUID() string {
  93. if NODE_INFO != nil && NODE_INFO.SystemUUID != "" {
  94. return NODE_INFO.SystemUUID
  95. }
  96. for _, p := range []string{"sys/devices/virtual/dmi/id/product_uuid", "etc/machine-id", "var/lib/dbus/machine-id"} {
  97. payload, err := os.ReadFile(path.Join("/proc/1/root", p))
  98. if err != nil {
  99. continue
  100. }
  101. id := strings.TrimSpace(strings.Replace(string(payload), "\n", "", -1))
  102. NODE_INFO.SystemUUID = id
  103. return id
  104. }
  105. return ""
  106. }
  107. func hexdec(hexStr string) int {
  108. dec, _ := strconv.ParseInt(hexStr, 16, 64)
  109. return int(dec)
  110. }
  111. func md5ToDec(str string) string {
  112. strCode := ""
  113. for i := 0; i < 16; i++ {
  114. strCode += strconv.Itoa(int(math.Floor(float64(hexdec(string(str[i]))) / 16.0 * 10)))
  115. if i == 0 && strings.TrimSpace(strCode) == "0" {
  116. strCode = "1" + strCode
  117. }
  118. }
  119. return strCode
  120. }
  121. func hashTo16DigitString(appName string) string {
  122. // 计算 MD5 哈希
  123. hash := md5.Sum([]byte(appName))
  124. // 将哈希值转换为一个大整数
  125. bigInt := new(big.Int).SetBytes(hash[:])
  126. // 转换为十进制字符串
  127. intStr := bigInt.String()
  128. // 如果结果少于 16 位,补零
  129. if len(intStr) < 16 {
  130. intStr = fmt.Sprintf("%016s", intStr) // 左侧补零
  131. // 如果第一个字符是 0,将其替换为 1
  132. if intStr[0] == '0' {
  133. intStr = "1" + intStr[1:]
  134. }
  135. }
  136. // 截取前 16 位
  137. return intStr[:16]
  138. }