main.go 2.9 KB

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