id.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. // todo 应用注册逻辑 && 写入proc_conf层维护
  96. var id_string string
  97. fmt.Println(os.Getenv("JAVA"))
  98. if os.Getenv("JAVA") == "1" {
  99. id_string = "3365853273187618"
  100. } else {
  101. id_string = "5410049101545798"
  102. }
  103. fmt.Println("APPID:", id_string)
  104. id, err := strconv.ParseInt(id_string, 10, 64)
  105. if err != nil {
  106. return 0, HashByte{}
  107. }
  108. APP_ID_INT64 = id
  109. var charArray HashByte
  110. hexStringToBPFBytes(id_string, &charArray)
  111. APP_ID_BYTE = charArray
  112. return id, charArray
  113. }
  114. //func GetHostID2() uint64 {
  115. // if HOST_ID_UINT64 != 0 {
  116. // return HOST_ID_UINT64
  117. // }
  118. // uuid := MachineID()
  119. // hash := make([]byte, HASH_SIZE)
  120. // customHash(uuid, hash, HASH_SIZE)
  121. //
  122. // fmt.Print("Hashed string: ")
  123. // for i := 0; i < HASH_SIZE; i++ {
  124. // fmt.Printf("%d", hash[i])
  125. // }
  126. // hashInt := byteArrayToInt(hash)
  127. // fmt.Println("HashByte as integer:", hashInt)
  128. // str := strconv.FormatInt(int64(hashInt), 10)
  129. //
  130. // // 将 hash 转换为字符串(可以使用 Base64 或其他编码)
  131. // fmt.Println("HashByte as string:", str)
  132. // HOST_ID_UINT64 = hashInt
  133. // os.Exit(1)
  134. // return hashInt
  135. //}
  136. func MachineID() string {
  137. for _, p := range []string{"sys/devices/virtual/dmi/id/product_uuid", "etc/machine-id", "var/lib/dbus/machine-id"} {
  138. payload, err := os.ReadFile(path.Join("/proc/1/root", p))
  139. if err != nil {
  140. continue
  141. }
  142. id := strings.TrimSpace(strings.Replace(string(payload), "-", "", -1))
  143. return id
  144. }
  145. return ""
  146. }
  147. func customHash(str string, hash []byte, size int) {
  148. var intHash uint64 = 0
  149. for i := 0; i < len(str); i++ {
  150. intHash = (intHash * 31) + uint64(str[i])
  151. }
  152. // Convert intHash to 16 bytes
  153. for i := 0; i < size; i++ {
  154. hash[i] = byte(intHash % 10)
  155. intHash /= 10
  156. }
  157. if hash[0] == 0 {
  158. hash[0] = 1
  159. }
  160. }
  161. func byteArrayToInt(hash []byte) uint64 {
  162. var result uint64
  163. for _, b := range hash {
  164. result = result*10 + uint64(b)
  165. }
  166. return result
  167. }
  168. func hexdec(hexStr string) int {
  169. dec, _ := strconv.ParseInt(hexStr, 16, 64)
  170. return int(dec)
  171. }
  172. func md5ToDec(str string) string {
  173. strCode := ""
  174. for i := 0; i < 16; i++ {
  175. strCode += strconv.Itoa(int(math.Floor(float64(hexdec(string(str[i]))) / 16.0 * 10)))
  176. if i == 0 && strings.TrimSpace(strCode) == "0" {
  177. strCode = "1" + strCode
  178. }
  179. }
  180. return strCode
  181. }
  182. //func hexStringToBPFBytes(str string, size int) []byte {
  183. // out := make([]byte, size/2)
  184. // for i := 0; i < size/2; i++ {
  185. // ch0 := str[2*i]
  186. // ch1 := str[2*i+1]
  187. // nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  188. // nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  189. // out[i] = (nib0 << 4) | nib1
  190. // }
  191. // return out
  192. //}
  193. func hexStringToBPFBytes(str string, out *HashByte) {
  194. for i := 0; i < len(str)/2; i++ {
  195. ch0 := str[2*i]
  196. ch1 := str[2*i+1]
  197. nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  198. nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  199. (*out)[i] = (nib0 << 4) | nib1
  200. }
  201. }
  202. // hex 表示十六进制字符
  203. var hex = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
  204. // bytesToHexString 将字节数组转换为十六进制字符串
  205. func BytesToHexString(pin HashByte) string {
  206. size := len(pin)
  207. out := make([]byte, size*2)
  208. pout := 0
  209. for i := 0; i < size; i++ {
  210. out[pout] = hex[(pin[i]>>4)&0xF]
  211. pout++
  212. out[pout] = hex[pin[i]&0xF]
  213. pout++
  214. }
  215. return string(out)
  216. }