diff --git a/README.md b/README.md index ef8508a..c65e2e4 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,20 @@ A 64-bit Linux binary is available on the releases page, for other platforms use go get -u github.com/microfactory/zero ``` -NOTE: This requires you to install the Go SDK (>1.5.1) \ No newline at end of file +NOTE: This requires you to install the Go SDK (>1.5.1) + +## Configuration +Zero supports several options to customize its behaviour: + +``` +-iface The network interface that is expected receive an address +-name Give this member a descriptive name upon authorizing +-installDir Where zerotier is installed +-api-endpoint Location of the ZeroTier API +``` + +For example the following options allows us to run it on osx: + +``` +sudo zero -install-dir="/Library/Application Support/ZeroTier/One/" -name=macbook 7a9cd21e1ce5b740 EVekkAtG...yTHF2Mu +``` \ No newline at end of file diff --git a/VERSION b/VERSION index afaf360..1cc5f65 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.0 \ No newline at end of file +1.1.0 \ No newline at end of file diff --git a/main.go b/main.go index 285c6d4..c23175a 100644 --- a/main.go +++ b/main.go @@ -151,24 +151,45 @@ func AuthorizeMember(memberid, membername, netid, endpoint, token string) error return nil } -func WaitForIP(exit chan os.Signal, iface string) (net.IP, error) { +func WaitForIP(exit chan os.Signal, installDir, netid, iface string) (net.IP, string, error) { + if iface == "" { + path := filepath.Join(installDir, "devicemap") + f, err := os.Open(path) + if err != nil { + return nil, "", fmt.Errorf("Failed to open ZeroTier devicemap ('%s'): %v", path, err) + } + + defer f.Close() + s := bufio.NewScanner(f) + for s.Scan() { + if strings.HasPrefix(s.Text(), netid) { + iface = strings.SplitAfter(s.Text(), "=")[1] + } + + } + + if err := s.Err(); err != nil { + return nil, "", err + } + } + retries := 0 for { if retries >= 400 { - return nil, ErrMaxRetries + return nil, iface, ErrMaxRetries } i, err := net.InterfaceByName(iface) if err != nil { - return nil, err + return nil, iface, err } addrs, err := i.Addrs() if err != nil { - return nil, err + return nil, iface, err } - if len(addrs) > 1 { + if len(addrs) > 0 { for _, addr := range addrs { ip, _, err := net.ParseCIDR(addr.String()) if err != nil { @@ -176,7 +197,7 @@ func WaitForIP(exit chan os.Signal, iface string) (net.IP, error) { } if ip.To4() != nil { - return ip, nil + return ip, iface, nil //we're done here } } } @@ -184,15 +205,15 @@ func WaitForIP(exit chan os.Signal, iface string) (net.IP, error) { retries += 1 select { case <-exit: - return nil, ErrUserCancelled + return nil, iface, ErrUserCancelled case <-time.After(time.Millisecond * 200): } } - return net.IP{}, nil + return net.IP{}, iface, nil } -var iface = flag.String("iface", "zt0", "The network interface that is expected receive an address") +var iface = flag.String("iface", "", "The network interface that is expected receive an address") var name = flag.String("name", "", "Give this member a descriptive name upon authorizing") var startDaemon = flag.Bool("start-daemon", false, "Also start the daemon (-d): this is for testing only") var installDir = flag.String("install-dir", "/var/lib/zerotier-one", "Where zerotier is installed") @@ -235,11 +256,11 @@ func main() { log.Fatalf("Failed to authorize member '%s': %v", memberid, err) } - log.Printf("Waiting for network address on interface '%s'...", *iface) - ip, err := WaitForIP(exit, *iface) + log.Printf("Waiting for network address...") + ip, ipif, err := WaitForIP(exit, *installDir, netid, *iface) if err != nil { - log.Fatalf("Failed to receive network address on '%s': %v", *iface, err) + log.Fatalf("Failed to receive network address: %v", err) } - log.Printf("Done! Received address '%s'", ip.String()) + log.Printf("Done! Received address '%s' on '%s'", ip.String(), ipif) } diff --git a/make.sh b/make.sh index 20a01f2..cf0ce2c 100755 --- a/make.sh +++ b/make.sh @@ -3,7 +3,7 @@ set -e function print_help { printf "Available Commands:\n"; - printf " run\n" + printf " test\n" } function run_build_container {