id.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. . "github.com/coroot/coroot-node-agent/utils/modelse"
  6. "math"
  7. "math/big"
  8. "os"
  9. "path"
  10. "strconv"
  11. "strings"
  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. // todo account id
  35. func GetAccountID() int {
  36. if NODE_INFO != nil && NODE_INFO.AccountID != 0 {
  37. return NODE_INFO.AccountID
  38. }
  39. NODE_INFO.AccountID = 110
  40. return NODE_INFO.AccountID
  41. }
  42. func GetHostID() int64 {
  43. if NODE_INFO != nil && NODE_INFO.HostID != 0 {
  44. return NODE_INFO.HostID
  45. }
  46. uuid := SystemUUID()
  47. // todo AccountId
  48. str := fmt.Sprintf("%d:%s", GetAccountID(), uuid)
  49. srcCode := md5.Sum([]byte(str))
  50. code := fmt.Sprintf("%x", srcCode)
  51. host_id_string := md5ToDec(code)
  52. id, err := strconv.ParseInt(host_id_string, 10, 64)
  53. if err != nil {
  54. return 0
  55. }
  56. NODE_INFO.HostID = id
  57. //var charArray HashByte
  58. //hexStringToBPFBytes(host_id_string, &charArray)
  59. //HOST_ID_BYTE = charArray
  60. // 将rune切片复制到char数组中
  61. //for i := 0; i < HASH_SIZE; i++ {
  62. // charArray[i] = []byte(host_id_string)[i]
  63. //}
  64. return id
  65. }
  66. func GetHostIDBPFString() HashByte {
  67. host_id_string := strconv.FormatInt(GetHostID(), 10)
  68. var charArray HashByte
  69. hexStringToBPFBytes(host_id_string, &charArray)
  70. return charArray
  71. }
  72. func SystemUUID() string {
  73. if NODE_INFO != nil && NODE_INFO.SystemUUID != "" {
  74. return NODE_INFO.SystemUUID
  75. }
  76. for _, p := range []string{"sys/devices/virtual/dmi/id/product_uuid", "etc/machine-id", "var/lib/dbus/machine-id"} {
  77. payload, err := os.ReadFile(path.Join("/proc/1/root", p))
  78. if err != nil {
  79. continue
  80. }
  81. id := strings.TrimSpace(strings.Replace(string(payload), "\n", "", -1))
  82. NODE_INFO.SystemUUID = id
  83. return id
  84. }
  85. return ""
  86. }
  87. func hexdec(hexStr string) int {
  88. dec, _ := strconv.ParseInt(hexStr, 16, 64)
  89. return int(dec)
  90. }
  91. func md5ToDec(str string) string {
  92. strCode := ""
  93. for i := 0; i < 16; i++ {
  94. strCode += strconv.Itoa(int(math.Floor(float64(hexdec(string(str[i]))) / 16.0 * 10)))
  95. if i == 0 && strings.TrimSpace(strCode) == "0" {
  96. strCode = "1" + strCode
  97. }
  98. }
  99. return strCode
  100. }
  101. func hashTo16DigitString(appName string) string {
  102. // 计算 MD5 哈希
  103. hash := md5.Sum([]byte(appName))
  104. // 将哈希值转换为一个大整数
  105. bigInt := new(big.Int).SetBytes(hash[:])
  106. // 转换为十进制字符串
  107. intStr := bigInt.String()
  108. // 如果结果少于 16 位,补零
  109. if len(intStr) < 16 {
  110. intStr = fmt.Sprintf("%016s", intStr) // 左侧补零
  111. // 如果第一个字符是 0,将其替换为 1
  112. if intStr[0] == '0' {
  113. intStr = "1" + intStr[1:]
  114. }
  115. }
  116. // 截取前 16 位
  117. return intStr[:16]
  118. }