id.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package utils
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. "math"
  6. "os"
  7. "path"
  8. "strconv"
  9. "strings"
  10. )
  11. const (
  12. HASH_SIZE_8 = 8
  13. HASH_SIZE_16 = 16
  14. )
  15. // apm_span_context
  16. type ApmSpanContextT struct {
  17. type_from [1]byte
  18. sample [1]byte
  19. host_id [8]byte
  20. app_id [8]byte
  21. instance_id [8]byte
  22. trace_id [16]byte
  23. assumed_app_id [8]byte
  24. span_id [8]byte
  25. }
  26. type HashByte [HASH_SIZE_8]byte
  27. type HashByte16 [HASH_SIZE_16]byte
  28. type Id_T struct {
  29. AppID ID
  30. HostID ID
  31. InstanceID ID
  32. }
  33. type ID struct {
  34. IntVal int64
  35. HashtVal HashByte
  36. }
  37. func init() {
  38. }
  39. var (
  40. APP_ID_INT64 int64
  41. HOST_ID_INT64 int64
  42. INSTANCE_ID_INT64 int64
  43. APP_ID_BYTE HashByte
  44. HOST_ID_BYTE HashByte
  45. INSTANCE_ID_BYTE HashByte
  46. )
  47. func SetInsID(str string) (int64, HashByte) {
  48. srcCode := md5.Sum([]byte(str))
  49. code := fmt.Sprintf("%x", srcCode)
  50. id_string := md5ToDec(code)
  51. fmt.Println(id_string)
  52. id, err := strconv.ParseInt(id_string, 10, 64)
  53. if err != nil {
  54. return 0, HashByte{}
  55. }
  56. INSTANCE_ID_INT64 = id
  57. var charArray HashByte
  58. hexStringToBPFBytes(id_string, &charArray)
  59. INSTANCE_ID_BYTE = charArray
  60. return id, charArray
  61. }
  62. func GetHostID() (int64, HashByte) {
  63. if HOST_ID_INT64 != 0 {
  64. return HOST_ID_INT64, HOST_ID_BYTE
  65. }
  66. uuid := MachineID()
  67. str := fmt.Sprintf("%s:%s", "110", uuid)
  68. srcCode := md5.Sum([]byte(str))
  69. code := fmt.Sprintf("%x", srcCode)
  70. host_id_string := md5ToDec(code)
  71. fmt.Println(host_id_string)
  72. id, err := strconv.ParseInt(host_id_string, 10, 64)
  73. if err != nil {
  74. return 0, HashByte{}
  75. }
  76. fmt.Println(host_id_string)
  77. HOST_ID_INT64 = id
  78. var charArray HashByte
  79. hexStringToBPFBytes(host_id_string, &charArray)
  80. HOST_ID_BYTE = charArray
  81. // 将rune切片复制到char数组中
  82. //for i := 0; i < HASH_SIZE; i++ {
  83. // charArray[i] = []byte(host_id_string)[i]
  84. //}
  85. return id, charArray
  86. }
  87. func GetAppID() (int64, HashByte) {
  88. if APP_ID_INT64 != 0 {
  89. return APP_ID_INT64, APP_ID_BYTE
  90. }
  91. //str := fmt.Sprintf("%s:%s", "110", uuid)
  92. //srcCode := md5.Sum([]byte(str))
  93. //code := fmt.Sprintf("%x", srcCode)
  94. //id_string := md5ToDec(code)
  95. id_string := "5410049101545798"
  96. fmt.Println(id_string)
  97. id, err := strconv.ParseInt(id_string, 10, 64)
  98. if err != nil {
  99. return 0, HashByte{}
  100. }
  101. APP_ID_INT64 = id
  102. var charArray HashByte
  103. hexStringToBPFBytes(id_string, &charArray)
  104. APP_ID_BYTE = charArray
  105. return id, charArray
  106. }
  107. //func GetHostID2() uint64 {
  108. // if HOST_ID_UINT64 != 0 {
  109. // return HOST_ID_UINT64
  110. // }
  111. // uuid := MachineID()
  112. // hash := make([]byte, HASH_SIZE)
  113. // customHash(uuid, hash, HASH_SIZE)
  114. //
  115. // fmt.Print("Hashed string: ")
  116. // for i := 0; i < HASH_SIZE; i++ {
  117. // fmt.Printf("%d", hash[i])
  118. // }
  119. // hashInt := byteArrayToInt(hash)
  120. // fmt.Println("HashByte as integer:", hashInt)
  121. // str := strconv.FormatInt(int64(hashInt), 10)
  122. //
  123. // // 将 hash 转换为字符串(可以使用 Base64 或其他编码)
  124. // fmt.Println("HashByte as string:", str)
  125. // HOST_ID_UINT64 = hashInt
  126. // os.Exit(1)
  127. // return hashInt
  128. //}
  129. func MachineID() string {
  130. for _, p := range []string{"sys/devices/virtual/dmi/id/product_uuid", "etc/machine-id", "var/lib/dbus/machine-id"} {
  131. payload, err := os.ReadFile(path.Join("/proc/1/root", p))
  132. if err != nil {
  133. continue
  134. }
  135. id := strings.TrimSpace(strings.Replace(string(payload), "-", "", -1))
  136. return id
  137. }
  138. return ""
  139. }
  140. func customHash(str string, hash []byte, size int) {
  141. var intHash uint64 = 0
  142. for i := 0; i < len(str); i++ {
  143. intHash = (intHash * 31) + uint64(str[i])
  144. }
  145. // Convert intHash to 16 bytes
  146. for i := 0; i < size; i++ {
  147. hash[i] = byte(intHash % 10)
  148. intHash /= 10
  149. }
  150. if hash[0] == 0 {
  151. hash[0] = 1
  152. }
  153. }
  154. func byteArrayToInt(hash []byte) uint64 {
  155. var result uint64
  156. for _, b := range hash {
  157. result = result*10 + uint64(b)
  158. }
  159. return result
  160. }
  161. func hexdec(hexStr string) int {
  162. dec, _ := strconv.ParseInt(hexStr, 16, 64)
  163. return int(dec)
  164. }
  165. func md5ToDec(str string) string {
  166. strCode := ""
  167. for i := 0; i < 16; i++ {
  168. strCode += strconv.Itoa(int(math.Floor(float64(hexdec(string(str[i]))) / 16.0 * 10)))
  169. if i == 0 && strings.TrimSpace(strCode) == "0" {
  170. strCode = "1" + strCode
  171. }
  172. }
  173. return strCode
  174. }
  175. //func hexStringToBPFBytes(str string, size int) []byte {
  176. // out := make([]byte, size/2)
  177. // for i := 0; i < size/2; i++ {
  178. // ch0 := str[2*i]
  179. // ch1 := str[2*i+1]
  180. // nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  181. // nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  182. // out[i] = (nib0 << 4) | nib1
  183. // }
  184. // return out
  185. //}
  186. func hexStringToBPFBytes(str string, out *HashByte) {
  187. for i := 0; i < len(str)/2; i++ {
  188. ch0 := str[2*i]
  189. ch1 := str[2*i+1]
  190. nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  191. nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  192. (*out)[i] = (nib0 << 4) | nib1
  193. }
  194. }
  195. // hex 表示十六进制字符
  196. var hex = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
  197. // bytesToHexString 将字节数组转换为十六进制字符串
  198. func BytesToHexString(pin HashByte) string {
  199. size := len(pin)
  200. out := make([]byte, size*2)
  201. pout := 0
  202. for i := 0; i < size; i++ {
  203. out[pout] = hex[(pin[i]>>4)&0xF]
  204. pout++
  205. out[pout] = hex[pin[i]&0xF]
  206. pout++
  207. }
  208. return string(out)
  209. }