main.go 2.8 KB

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