Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

87 linhas
1.4 KiB

  1. package main
  2. import (
  3. "bufio"
  4. "encoding/hex"
  5. "fmt"
  6. "log"
  7. "os"
  8. "strings"
  9. )
  10. func main() {
  11. file, err := os.Open("save.txt")
  12. if err != nil {
  13. log.Fatalf("Failed to open file: %s", err)
  14. }
  15. defer file.Close()
  16. scanner := bufio.NewScanner(file)
  17. for scanner.Scan() {
  18. line := scanner.Text()
  19. decodeBeacon(line)
  20. }
  21. }
  22. func decodeBeacon(beacon string) {
  23. beacon = strings.TrimSpace(beacon)
  24. if beacon == "" {
  25. return
  26. }
  27. // convert to bytes for faster operations
  28. b, err := hex.DecodeString(beacon)
  29. if err != nil {
  30. fmt.Println("invalid line: ", beacon)
  31. return
  32. }
  33. // remove flag bytes - they hold no structural information
  34. if len(b) > 1 && b[1] == 0x01 {
  35. l := int(b[0])
  36. if 1+l <= len(b) {
  37. b = b[1+l:]
  38. }
  39. }
  40. adBlockIndeces := parseADFast(b)
  41. for _, r := range adBlockIndeces {
  42. ad := b[r[0]:r[1]]
  43. if len(ad) >= 4 &&
  44. ad[1] == 0x16 &&
  45. ad[2] == 0xAA &&
  46. ad[3] == 0xFE {
  47. // fmt.Println("Eddystone:", hex.EncodeToString(b))
  48. return
  49. }
  50. if len(ad) >= 7 &&
  51. ad[1] == 0xFF &&
  52. ad[2] == 0x4C && ad[3] == 0x00 &&
  53. ad[4] == 0x02 && ad[5] == 0x15 {
  54. // fmt.Println("iBeacon:", hex.EncodeToString(b))
  55. return
  56. }
  57. }
  58. fmt.Println(hex.EncodeToString(b))
  59. }
  60. func parseADFast(b []byte) [][2]int {
  61. var res [][2]int
  62. i := 0
  63. for i < len(b) {
  64. l := int(b[i])
  65. if l == 0 || i+1+l > len(b) {
  66. break
  67. }
  68. res = append(res, [2]int{i, i + 1 + l})
  69. i += 1 + l
  70. }
  71. return res
  72. }