syncthing/cmd/stfinddevice/main.go

110 lines
2.2 KiB
Go
Raw Permalink Normal View History

2014-12-25 23:12:12 +00:00
// Copyright (C) 2014 The Syncthing Authors.
//
2015-03-07 20:36:35 +00:00
// 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,
// You can obtain one at https://mozilla.org/MPL/2.0/.
2014-12-25 23:12:12 +00:00
package main
import (
"context"
"crypto/tls"
"errors"
2014-12-25 23:12:12 +00:00
"flag"
"fmt"
"net/url"
2014-12-25 23:12:12 +00:00
"os"
"time"
2014-12-25 23:12:12 +00:00
"github.com/syncthing/syncthing/lib/config"
2015-08-06 09:29:25 +00:00
"github.com/syncthing/syncthing/lib/discover"
"github.com/syncthing/syncthing/lib/events"
2015-09-22 17:38:46 +00:00
"github.com/syncthing/syncthing/lib/protocol"
2014-12-25 23:12:12 +00:00
)
var timeout = 5 * time.Second
2014-12-25 23:12:12 +00:00
func main() {
2014-12-25 23:12:12 +00:00
var server string
flag.StringVar(&server, "server", "", "Announce server (blank for default set)")
flag.DurationVar(&timeout, "timeout", timeout, "Query timeout")
flag.Usage = usage
2014-12-25 23:12:12 +00:00
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
2014-12-25 23:12:12 +00:00
os.Exit(64)
}
id, err := protocol.DeviceIDFromString(flag.Args()[0])
if err != nil {
fmt.Println(err)
2014-12-25 23:12:12 +00:00
os.Exit(1)
}
if server != "" {
checkServers(id, server)
} else {
checkServers(id, config.DefaultDiscoveryServers...)
}
}
type checkResult struct {
server string
addresses []string
error
}
func checkServers(deviceID protocol.DeviceID, servers ...string) {
t0 := time.Now()
resc := make(chan checkResult)
for _, srv := range servers {
srv := srv
go func() {
res := checkServer(deviceID, srv)
res.server = srv
resc <- res
}()
}
for range servers {
res := <-resc
u, _ := url.Parse(res.server)
fmt.Printf("%s (%v):\n", u.Host, time.Since(t0))
if res.error != nil {
fmt.Println(" " + res.error.Error())
}
for _, addr := range res.addresses {
fmt.Println(" address:", addr)
}
}
}
func checkServer(deviceID protocol.DeviceID, server string) checkResult {
disco, err := discover.NewGlobal(server, tls.Certificate{}, nil, events.NoopLogger)
if err != nil {
return checkResult{error: err}
2014-12-25 23:12:12 +00:00
}
res := make(chan checkResult, 1)
time.AfterFunc(timeout, func() {
res <- checkResult{error: errors.New("timeout")}
})
go func() {
addresses, err := disco.Lookup(context.Background(), deviceID)
res <- checkResult{addresses: addresses, error: err}
}()
return <-res
}
func usage() {
fmt.Printf("Usage:\n\t%s [options] <device ID>\n\nOptions:\n", os.Args[0])
flag.PrintDefaults()
2014-12-25 23:12:12 +00:00
}