id.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. . "github.com/coroot/coroot-node-agent/utils/modelse"
  6. "math"
  7. "os"
  8. "path"
  9. "strconv"
  10. "strings"
  11. )
  12. func init() {
  13. }
  14. var (
  15. APP_ID_INT64 int64
  16. HOST_ID_INT64 int64
  17. ACCOUNT_ID_INT int
  18. INSTANCE_ID_INT64 int64
  19. APP_ID_BYTE HashByte
  20. HOST_ID_BYTE HashByte
  21. INSTANCE_ID_BYTE HashByte
  22. )
  23. func BuildInt64ID(str string) ID_STRING {
  24. srcCode := md5.Sum([]byte(str))
  25. code := fmt.Sprintf("%x", srcCode)
  26. id_string := md5ToDec(code)
  27. return ID_STRING(id_string)
  28. }
  29. func hexStringToBPFBytes(str string, out *HashByte) {
  30. for i := 0; i < len(str)/2; i++ {
  31. ch0 := str[2*i]
  32. ch1 := str[2*i+1]
  33. nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  34. nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  35. (*out)[i] = (nib0 << 4) | nib1
  36. }
  37. }
  38. // todo account id
  39. func GetAccountID() int {
  40. if ACCOUNT_ID_INT != 0 {
  41. return ACCOUNT_ID_INT
  42. }
  43. ACCOUNT_ID_INT = 110
  44. return ACCOUNT_ID_INT
  45. }
  46. // todo HostId
  47. func GetHostID() (int64, HashByte) {
  48. if HOST_ID_INT64 != 0 {
  49. return HOST_ID_INT64, HOST_ID_BYTE
  50. }
  51. uuid := MachineID()
  52. // todo AccountId
  53. str := fmt.Sprintf("%d:%s", GetAccountID(), uuid)
  54. srcCode := md5.Sum([]byte(str))
  55. code := fmt.Sprintf("%x", srcCode)
  56. host_id_string := md5ToDec(code)
  57. id, err := strconv.ParseInt(host_id_string, 10, 64)
  58. if err != nil {
  59. return 0, HashByte{}
  60. }
  61. HOST_ID_INT64 = id
  62. var charArray HashByte
  63. hexStringToBPFBytes(host_id_string, &charArray)
  64. HOST_ID_BYTE = charArray
  65. // 将rune切片复制到char数组中
  66. //for i := 0; i < HASH_SIZE; i++ {
  67. // charArray[i] = []byte(host_id_string)[i]
  68. //}
  69. return id, charArray
  70. }
  71. func GetIntHostID() int64 {
  72. id, _ := GetHostID()
  73. return id
  74. }
  75. func MachineID() string {
  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. return id
  83. }
  84. return ""
  85. }
  86. func hexdec(hexStr string) int {
  87. dec, _ := strconv.ParseInt(hexStr, 16, 64)
  88. return int(dec)
  89. }
  90. func md5ToDec(str string) string {
  91. strCode := ""
  92. for i := 0; i < 16; i++ {
  93. strCode += strconv.Itoa(int(math.Floor(float64(hexdec(string(str[i]))) / 16.0 * 10)))
  94. if i == 0 && strings.TrimSpace(strCode) == "0" {
  95. strCode = "1" + strCode
  96. }
  97. }
  98. return strCode
  99. }