id.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. type ID_STRING string
  38. func (id ID_STRING) ToString() string {
  39. return string(id)
  40. }
  41. func (id ID_STRING) ToInt64() (int64, error) {
  42. intID, err := strconv.ParseInt(id.ToString(), 10, 64)
  43. if err != nil {
  44. return 0, err
  45. }
  46. return intID, nil
  47. }
  48. func (id ID_STRING) ToHashByte() HashByte {
  49. var charArray HashByte
  50. hexStringToBPFBytes(id.ToString(), &charArray)
  51. return charArray
  52. }
  53. func init() {
  54. }
  55. var (
  56. APP_ID_INT64 int64
  57. HOST_ID_INT64 int64
  58. INSTANCE_ID_INT64 int64
  59. APP_ID_BYTE HashByte
  60. HOST_ID_BYTE HashByte
  61. INSTANCE_ID_BYTE HashByte
  62. )
  63. func BuildInt64ID(str string) ID_STRING {
  64. srcCode := md5.Sum([]byte(str))
  65. code := fmt.Sprintf("%x", srcCode)
  66. id_string := md5ToDec(code)
  67. return ID_STRING(id_string)
  68. //
  69. //id, err := strconv.ParseInt(id_string, 10, 64)
  70. //if err != nil {
  71. // return 0, HashByte{}, ""
  72. //}
  73. ////INSTANCE_ID_INT64 = id
  74. //var charArray HashByte
  75. //hexStringToBPFBytes(id_string, &charArray)
  76. ////INSTANCE_ID_BYTE = charArray
  77. //return id, charArray, id_string
  78. }
  79. func BuildInt64ID2(str string) (int64, HashByte, string) {
  80. srcCode := md5.Sum([]byte(str))
  81. code := fmt.Sprintf("%x", srcCode)
  82. id_string := md5ToDec(code)
  83. fmt.Println(id_string)
  84. id, err := strconv.ParseInt(id_string, 10, 64)
  85. if err != nil {
  86. return 0, HashByte{}, ""
  87. }
  88. //INSTANCE_ID_INT64 = id
  89. var charArray HashByte
  90. hexStringToBPFBytes(id_string, &charArray)
  91. //INSTANCE_ID_BYTE = charArray
  92. return id, charArray, id_string
  93. }
  94. func GetHostID() (int64, HashByte) {
  95. if HOST_ID_INT64 != 0 {
  96. return HOST_ID_INT64, HOST_ID_BYTE
  97. }
  98. uuid := MachineID()
  99. str := fmt.Sprintf("%s:%s", "110", uuid)
  100. srcCode := md5.Sum([]byte(str))
  101. code := fmt.Sprintf("%x", srcCode)
  102. host_id_string := md5ToDec(code)
  103. id, err := strconv.ParseInt(host_id_string, 10, 64)
  104. if err != nil {
  105. return 0, HashByte{}
  106. }
  107. HOST_ID_INT64 = id
  108. var charArray HashByte
  109. hexStringToBPFBytes(host_id_string, &charArray)
  110. HOST_ID_BYTE = charArray
  111. // 将rune切片复制到char数组中
  112. //for i := 0; i < HASH_SIZE; i++ {
  113. // charArray[i] = []byte(host_id_string)[i]
  114. //}
  115. return id, charArray
  116. }
  117. func GetAppID() (int64, HashByte) {
  118. if APP_ID_INT64 != 0 {
  119. return APP_ID_INT64, APP_ID_BYTE
  120. }
  121. //str := fmt.Sprintf("%s:%s", "110", uuid)
  122. //srcCode := md5.Sum([]byte(str))
  123. //code := fmt.Sprintf("%x", srcCode)
  124. //id_string := md5ToDec(code)
  125. // todo 应用注册逻辑 && 写入proc_conf层维护
  126. var id_string string
  127. fmt.Println(os.Getenv("JAVA"))
  128. if os.Getenv("JAVA") == "1" {
  129. id_string = "3365853273187618"
  130. } else {
  131. id_string = "5410049101545798"
  132. }
  133. id, err := strconv.ParseInt(id_string, 10, 64)
  134. if err != nil {
  135. return 0, HashByte{}
  136. }
  137. APP_ID_INT64 = id
  138. var charArray HashByte
  139. hexStringToBPFBytes(id_string, &charArray)
  140. APP_ID_BYTE = charArray
  141. return id, charArray
  142. }
  143. //func GetHostID2() uint64 {
  144. // if HOST_ID_UINT64 != 0 {
  145. // return HOST_ID_UINT64
  146. // }
  147. // uuid := MachineID()
  148. // hash := make([]byte, HASH_SIZE)
  149. // customHash(uuid, hash, HASH_SIZE)
  150. //
  151. // fmt.Print("Hashed string: ")
  152. // for i := 0; i < HASH_SIZE; i++ {
  153. // fmt.Printf("%d", hash[i])
  154. // }
  155. // hashInt := byteArrayToInt(hash)
  156. // fmt.Println("HashByte as integer:", hashInt)
  157. // str := strconv.FormatInt(int64(hashInt), 10)
  158. //
  159. // // 将 hash 转换为字符串(可以使用 Base64 或其他编码)
  160. // fmt.Println("HashByte as string:", str)
  161. // HOST_ID_UINT64 = hashInt
  162. // os.Exit(1)
  163. // return hashInt
  164. //}
  165. func MachineID() string {
  166. for _, p := range []string{"sys/devices/virtual/dmi/id/product_uuid", "etc/machine-id", "var/lib/dbus/machine-id"} {
  167. payload, err := os.ReadFile(path.Join("/proc/1/root", p))
  168. if err != nil {
  169. continue
  170. }
  171. id := strings.TrimSpace(strings.Replace(string(payload), "-", "", -1))
  172. return id
  173. }
  174. return ""
  175. }
  176. func customHash(str string, hash []byte, size int) {
  177. var intHash uint64 = 0
  178. for i := 0; i < len(str); i++ {
  179. intHash = (intHash * 31) + uint64(str[i])
  180. }
  181. // Convert intHash to 16 bytes
  182. for i := 0; i < size; i++ {
  183. hash[i] = byte(intHash % 10)
  184. intHash /= 10
  185. }
  186. if hash[0] == 0 {
  187. hash[0] = 1
  188. }
  189. }
  190. func byteArrayToInt(hash []byte) uint64 {
  191. var result uint64
  192. for _, b := range hash {
  193. result = result*10 + uint64(b)
  194. }
  195. return result
  196. }
  197. func hexdec(hexStr string) int {
  198. dec, _ := strconv.ParseInt(hexStr, 16, 64)
  199. return int(dec)
  200. }
  201. func md5ToDec(str string) string {
  202. strCode := ""
  203. for i := 0; i < 16; i++ {
  204. strCode += strconv.Itoa(int(math.Floor(float64(hexdec(string(str[i]))) / 16.0 * 10)))
  205. if i == 0 && strings.TrimSpace(strCode) == "0" {
  206. strCode = "1" + strCode
  207. }
  208. }
  209. return strCode
  210. }
  211. //func hexStringToBPFBytes(str string, size int) []byte {
  212. // out := make([]byte, size/2)
  213. // for i := 0; i < size/2; i++ {
  214. // ch0 := str[2*i]
  215. // ch1 := str[2*i+1]
  216. // nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  217. // nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  218. // out[i] = (nib0 << 4) | nib1
  219. // }
  220. // return out
  221. //}
  222. func hexStringToBPFBytes(str string, out *HashByte) {
  223. for i := 0; i < len(str)/2; i++ {
  224. ch0 := str[2*i]
  225. ch1 := str[2*i+1]
  226. nib0 := (ch0 & 0x0F) + (ch0 >> 6) | ((ch0 >> 3) & 0x08)
  227. nib1 := (ch1 & 0x0F) + (ch1 >> 6) | ((ch1 >> 3) & 0x08)
  228. (*out)[i] = (nib0 << 4) | nib1
  229. }
  230. }
  231. // hex 表示十六进制字符
  232. var hex = []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
  233. // bytesToHexString 将字节数组转换为十六进制字符串
  234. func BytesToHexString(pin HashByte) string {
  235. size := len(pin)
  236. out := make([]byte, size*2)
  237. pout := 0
  238. for i := 0; i < size; i++ {
  239. out[pout] = hex[(pin[i]>>4)&0xF]
  240. pout++
  241. out[pout] = hex[pin[i]&0xF]
  242. pout++
  243. }
  244. return string(out)
  245. }