2016-04-17 18:47:38 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-04-17 18:47:38 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-11-17 12:19:04 +00:00
|
|
|
"context"
|
2016-04-17 18:47:38 +00:00
|
|
|
"crypto/rand"
|
2016-07-04 10:40:29 +00:00
|
|
|
"encoding/binary"
|
2016-04-17 18:47:38 +00:00
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/beacon"
|
|
|
|
"github.com/syncthing/syncthing/lib/discover"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
all = false // print all packets, not just first from each device/source
|
|
|
|
fake = false // send fake packets to lure out other devices faster
|
|
|
|
mc = "[ff12::8384]:21027"
|
|
|
|
bc = 21027
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Static prefix that we use when generating fake device IDs, so that we
|
|
|
|
// can recognize them ourselves. Also makes the device ID start with
|
2016-05-08 10:54:22 +00:00
|
|
|
// "STPROBE-" which is humanly recognizable.
|
2016-04-17 18:47:38 +00:00
|
|
|
randomPrefix = []byte{148, 223, 23, 4, 148}
|
|
|
|
|
|
|
|
// Our random, fake, device ID that we use when sending announcements.
|
|
|
|
myID = randomDeviceID()
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.BoolVar(&all, "all", all, "Print all received announcements (not only first)")
|
|
|
|
flag.BoolVar(&fake, "fake", fake, "Send fake announcements")
|
|
|
|
flag.StringVar(&mc, "mc", mc, "IPv6 multicast address")
|
|
|
|
flag.IntVar(&bc, "bc", bc, "IPv4 broadcast port number")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if fake {
|
2016-10-29 21:56:24 +00:00
|
|
|
log.Println("My ID:", myID)
|
2016-04-17 18:47:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
runbeacon(ctx, beacon.NewMulticast(mc), fake)
|
|
|
|
runbeacon(ctx, beacon.NewBroadcast(bc), fake)
|
2016-04-17 18:47:38 +00:00
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func runbeacon(ctx context.Context, bc beacon.Interface, fake bool) {
|
|
|
|
go bc.Serve(ctx)
|
2016-04-17 18:47:38 +00:00
|
|
|
go recv(bc)
|
|
|
|
if fake {
|
|
|
|
go send(bc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// receives and prints discovery announcements
|
|
|
|
func recv(bc beacon.Interface) {
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
for {
|
|
|
|
data, src := bc.Recv()
|
2016-07-04 10:40:29 +00:00
|
|
|
if m := binary.BigEndian.Uint32(data); m != discover.Magic {
|
|
|
|
log.Printf("Incorrect magic %x in announcement from %v", m, src)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-17 18:47:38 +00:00
|
|
|
var ann discover.Announce
|
2016-07-04 10:40:29 +00:00
|
|
|
ann.Unmarshal(data[4:])
|
2016-04-17 18:47:38 +00:00
|
|
|
|
2016-10-29 21:56:24 +00:00
|
|
|
if ann.ID == myID {
|
2016-04-17 18:47:38 +00:00
|
|
|
// This is one of our own fake packets, don't print it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print announcement details for the first packet from a given
|
|
|
|
// device ID and source address, or if -all was given.
|
2016-10-29 21:56:24 +00:00
|
|
|
key := ann.ID.String() + src.String()
|
2016-04-17 18:47:38 +00:00
|
|
|
if all || !seen[key] {
|
|
|
|
log.Printf("Announcement from %v\n", src)
|
2016-10-29 21:56:24 +00:00
|
|
|
log.Printf(" %v at %s\n", ann.ID, strings.Join(ann.Addresses, ", "))
|
2016-04-17 18:47:38 +00:00
|
|
|
seen[key] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sends fake discovery announcements once every second
|
|
|
|
func send(bc beacon.Interface) {
|
|
|
|
ann := discover.Announce{
|
2016-07-04 10:40:29 +00:00
|
|
|
ID: myID,
|
|
|
|
Addresses: []string{"tcp://fake.example.com:12345"},
|
2016-04-17 18:47:38 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
bs, _ := ann.Marshal()
|
2016-04-17 18:47:38 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
bc.Send(bs)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-08 10:54:22 +00:00
|
|
|
// returns a random but recognizable device ID
|
2016-10-29 21:56:24 +00:00
|
|
|
func randomDeviceID() protocol.DeviceID {
|
|
|
|
var id protocol.DeviceID
|
2016-04-17 18:47:38 +00:00
|
|
|
copy(id[:], randomPrefix)
|
|
|
|
rand.Read(id[len(randomPrefix):])
|
2016-10-29 21:56:24 +00:00
|
|
|
return id
|
2016-04-17 18:47:38 +00:00
|
|
|
}
|