main.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2014 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/tls"
  9. "errors"
  10. "flag"
  11. "fmt"
  12. "net/url"
  13. "os"
  14. "time"
  15. "github.com/syncthing/syncthing/lib/config"
  16. "github.com/syncthing/syncthing/lib/discover"
  17. "github.com/syncthing/syncthing/lib/events"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. )
  20. var timeout = 5 * time.Second
  21. func main() {
  22. var server string
  23. flag.StringVar(&server, "server", "", "Announce server (blank for default set)")
  24. flag.DurationVar(&timeout, "timeout", timeout, "Query timeout")
  25. flag.Usage = usage
  26. flag.Parse()
  27. if flag.NArg() != 1 {
  28. flag.Usage()
  29. os.Exit(64)
  30. }
  31. id, err := protocol.DeviceIDFromString(flag.Args()[0])
  32. if err != nil {
  33. fmt.Println(err)
  34. os.Exit(1)
  35. }
  36. if server != "" {
  37. checkServers(id, server)
  38. } else {
  39. checkServers(id, config.DefaultDiscoveryServers...)
  40. }
  41. }
  42. type checkResult struct {
  43. server string
  44. addresses []string
  45. error
  46. }
  47. func checkServers(deviceID protocol.DeviceID, servers ...string) {
  48. t0 := time.Now()
  49. resc := make(chan checkResult)
  50. for _, srv := range servers {
  51. srv := srv
  52. go func() {
  53. res := checkServer(deviceID, srv)
  54. res.server = srv
  55. resc <- res
  56. }()
  57. }
  58. for range servers {
  59. res := <-resc
  60. u, _ := url.Parse(res.server)
  61. fmt.Printf("%s (%v):\n", u.Host, time.Since(t0))
  62. if res.error != nil {
  63. fmt.Println(" " + res.error.Error())
  64. }
  65. for _, addr := range res.addresses {
  66. fmt.Println(" address:", addr)
  67. }
  68. }
  69. }
  70. func checkServer(deviceID protocol.DeviceID, server string) checkResult {
  71. disco, err := discover.NewGlobal(server, tls.Certificate{}, nil, events.NoopLogger)
  72. if err != nil {
  73. return checkResult{error: err}
  74. }
  75. res := make(chan checkResult, 1)
  76. time.AfterFunc(timeout, func() {
  77. res <- checkResult{error: errors.New("timeout")}
  78. })
  79. go func() {
  80. addresses, err := disco.Lookup(deviceID)
  81. res <- checkResult{addresses: addresses, error: err}
  82. }()
  83. return <-res
  84. }
  85. func usage() {
  86. fmt.Printf("Usage:\n\t%s [options] <device ID>\n\nOptions:\n", os.Args[0])
  87. flag.PrintDefaults()
  88. }