main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "crypto/rand"
  9. "encoding/binary"
  10. "flag"
  11. "log"
  12. "strings"
  13. "time"
  14. "github.com/syncthing/syncthing/lib/beacon"
  15. "github.com/syncthing/syncthing/lib/discover"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. )
  18. var (
  19. all = false // print all packets, not just first from each device/source
  20. fake = false // send fake packets to lure out other devices faster
  21. mc = "[ff12::8384]:21027"
  22. bc = 21027
  23. )
  24. var (
  25. // Static prefix that we use when generating fake device IDs, so that we
  26. // can recognize them ourselves. Also makes the device ID start with
  27. // "STPROBE-" which is humanly recognizable.
  28. randomPrefix = []byte{148, 223, 23, 4, 148}
  29. // Our random, fake, device ID that we use when sending announcements.
  30. myID = randomDeviceID()
  31. )
  32. func main() {
  33. flag.BoolVar(&all, "all", all, "Print all received announcements (not only first)")
  34. flag.BoolVar(&fake, "fake", fake, "Send fake announcements")
  35. flag.StringVar(&mc, "mc", mc, "IPv6 multicast address")
  36. flag.IntVar(&bc, "bc", bc, "IPv4 broadcast port number")
  37. flag.Parse()
  38. if fake {
  39. log.Println("My ID:", myID)
  40. }
  41. runbeacon(beacon.NewMulticast(mc), fake)
  42. runbeacon(beacon.NewBroadcast(bc), fake)
  43. select {}
  44. }
  45. func runbeacon(bc beacon.Interface, fake bool) {
  46. go bc.Serve()
  47. go recv(bc)
  48. if fake {
  49. go send(bc)
  50. }
  51. }
  52. // receives and prints discovery announcements
  53. func recv(bc beacon.Interface) {
  54. seen := make(map[string]bool)
  55. for {
  56. data, src := bc.Recv()
  57. if m := binary.BigEndian.Uint32(data); m != discover.Magic {
  58. log.Printf("Incorrect magic %x in announcement from %v", m, src)
  59. continue
  60. }
  61. var ann discover.Announce
  62. ann.Unmarshal(data[4:])
  63. if ann.ID == myID {
  64. // This is one of our own fake packets, don't print it.
  65. continue
  66. }
  67. // Print announcement details for the first packet from a given
  68. // device ID and source address, or if -all was given.
  69. key := ann.ID.String() + src.String()
  70. if all || !seen[key] {
  71. log.Printf("Announcement from %v\n", src)
  72. log.Printf(" %v at %s\n", ann.ID, strings.Join(ann.Addresses, ", "))
  73. seen[key] = true
  74. }
  75. }
  76. }
  77. // sends fake discovery announcements once every second
  78. func send(bc beacon.Interface) {
  79. ann := discover.Announce{
  80. ID: myID,
  81. Addresses: []string{"tcp://fake.example.com:12345"},
  82. }
  83. bs, _ := ann.Marshal()
  84. for {
  85. bc.Send(bs)
  86. time.Sleep(time.Second)
  87. }
  88. }
  89. // returns a random but recognizable device ID
  90. func randomDeviceID() protocol.DeviceID {
  91. var id protocol.DeviceID
  92. copy(id[:], randomPrefix)
  93. rand.Read(id[len(randomPrefix):])
  94. return id
  95. }