浏览代码

Add device finder utility

Audrius Butkevicius 11 年之前
父节点
当前提交
12eabb220d
共有 1 个文件被更改,包括 52 次插入0 次删除
  1. 52 0
      cmd/stfinddevice/main.go

+ 52 - 0
cmd/stfinddevice/main.go

@@ -0,0 +1,52 @@
+// Copyright (C) 2014 The Syncthing Authors.
+//
+// This program is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation, either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package main
+
+import (
+	"flag"
+	"log"
+	"os"
+
+	"github.com/syncthing/syncthing/internal/discover"
+	"github.com/syncthing/syncthing/internal/protocol"
+)
+
+func main() {
+	log.SetFlags(0)
+	log.SetOutput(os.Stdout)
+
+	var server string
+
+	flag.StringVar(&server, "server", "udp4://announce.syncthing.net:22026", "Announce server")
+	flag.Parse()
+
+	if len(flag.Args()) != 1 || server == "" {
+		log.Printf("Usage: %s [-server=\"udp4://announce.syncthing.net:22026\"] <device>", os.Args[0])
+		os.Exit(64)
+	}
+
+	id, err := protocol.DeviceIDFromString(flag.Args()[0])
+	if err != nil {
+		log.Println(err)
+		os.Exit(1)
+	}
+
+	discoverer := discover.NewDiscoverer(protocol.LocalDeviceID, nil)
+	discoverer.StartGlobal([]string{server}, 1)
+	for _, addr := range discoverer.Lookup(id) {
+		log.Println(addr)
+	}
+}