id.go 3.1 KB

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