Browse Source

Locator CLI plumbing.

Adam Ierymenko 4 years ago
parent
commit
441f4986ac

+ 1 - 1
cmd/zerotier/cli/cert.go

@@ -122,7 +122,7 @@ func Cert(basePath string, authTokenGenerator func() string, args []string, json
 			return 1
 		}
 
-		signingIdentity := readIdentity(args[2])
+		signingIdentity := cliGetIdentityOrFatal(args[2])
 		if signingIdentity == nil {
 			pErr("unable to read identity from %s", args[2])
 			return 1

+ 4 - 4
cmd/zerotier/cli/identity.go

@@ -55,7 +55,7 @@ func Identity(args []string) int {
 
 		case "getpublic":
 			if len(args) == 2 {
-				fmt.Println(readIdentity(args[1]).String())
+				fmt.Println(cliGetIdentityOrFatal(args[1]).String())
 				return 0
 			}
 			pErr("no identity specified")
@@ -63,7 +63,7 @@ func Identity(args []string) int {
 
 		case "fingerprint":
 			if len(args) == 2 {
-				fmt.Println(readIdentity(args[1]).Fingerprint().String())
+				fmt.Println(cliGetIdentityOrFatal(args[1]).Fingerprint().String())
 				return 0
 			}
 			pErr("no identity specified")
@@ -71,7 +71,7 @@ func Identity(args []string) int {
 
 		case "validate":
 			if len(args) == 2 {
-				if readIdentity(args[1]).LocallyValidate() {
+				if cliGetIdentityOrFatal(args[1]).LocallyValidate() {
 					fmt.Println("VALID")
 					return 0
 				}
@@ -81,7 +81,7 @@ func Identity(args []string) int {
 
 		case "sign", "verify":
 			if len(args) > 2 {
-				id := readIdentity(args[1])
+				id := cliGetIdentityOrFatal(args[1])
 				msg, err := ioutil.ReadFile(args[2])
 				if err != nil {
 					pErr("unable to read input file: %s", err.Error())

+ 49 - 0
cmd/zerotier/cli/locator.go

@@ -13,6 +13,11 @@
 
 package cli
 
+import (
+	"fmt"
+	"zerotier/pkg/zerotier"
+)
+
 func Locator(args []string) int {
 	if len(args) < 1 {
 		Help()
@@ -22,10 +27,54 @@ func Locator(args []string) int {
 	switch args[0] {
 
 	case "new":
+		if len(args) < 3 {
+			Help()
+			return 1
+		}
+		id := cliGetIdentityOrFatal(args[1])
+		if !id.HasPrivate() {
+			pErr("identity must include secret key to sign locator")
+			return 1
+		}
+		var eps []*zerotier.Endpoint
+		for i:=2;i<len(args);i++ {
+			ep, err := zerotier.NewEndpointFromString(args[i])
+			if err != nil {
+				pErr("invalid endpoint: %s (%s)", args[i], err.Error())
+				return 1
+			}
+			eps = append(eps, ep)
+		}
+		loc, err := zerotier.NewLocator(zerotier.TimeMs(), eps, id)
+		if err != nil {
+			pErr("error creating or signing locator: %s", err.Error())
+			return 1
+		}
+		fmt.Println(loc.String())
 
 	case "verify":
+		if len(args) != 3 {
+			Help()
+			return 1
+		}
+		id := cliGetIdentityOrFatal(args[1])
+		loc := cliGetLocatorOrFatal(args[2])
+		if !loc.Validate(id) {
+			fmt.Println("FAILED")
+			return 1
+		}
+		fmt.Println("OK")
 
 	case "show":
+		if len(args) != 2 {
+			Help()
+			return 1
+		}
+		loc := cliGetLocatorOrFatal(args[1])
+		fmt.Printf("%s %s\n",loc.Fingerprint.Address.String(),loc.Fingerprint.String())
+		for _, e := range loc.Endpoints {
+			fmt.Printf("\t%s\n",e.String())
+		}
 
 	}
 

+ 6 - 6
cmd/zerotier/cli/misc.go

@@ -160,7 +160,7 @@ func parseAddressFingerprintOrIdentity(s string) (a zerotier.Address, fp *zeroti
 	return
 }
 
-func readIdentity(s string) *zerotier.Identity {
+func cliGetIdentityOrFatal(s string) *zerotier.Identity {
 	if strings.ContainsRune(s, ':') {
 		id, _ := zerotier.NewIdentityFromString(s)
 		if id != nil {
@@ -169,18 +169,18 @@ func readIdentity(s string) *zerotier.Identity {
 	}
 	idData, err := ioutil.ReadFile(s)
 	if err != nil {
-		fmt.Printf("FATAL: identity '%s' cannot be parsed as file or literal: %s", s, err.Error())
+		pErr("identity '%s' cannot be parsed as file or literal: %s", s, err.Error())
 		os.Exit(1)
 	}
 	id, err := zerotier.NewIdentityFromString(string(idData))
 	if err != nil {
-		fmt.Printf("FATAL: identity '%s' cannot be parsed as file or literal: %s", s, err.Error())
+		pErr("identity '%s' cannot be parsed as file or literal: %s", s, err.Error())
 		os.Exit(1)
 	}
 	return id
 }
 
-func readLocator(s string) *zerotier.Locator {
+func cliGetLocatorOrFatal(s string) *zerotier.Locator {
 	if strings.ContainsRune(s, '@') {
 		loc, _ := zerotier.NewLocatorFromString(s)
 		if loc != nil {
@@ -189,12 +189,12 @@ func readLocator(s string) *zerotier.Locator {
 	}
 	locData, err := ioutil.ReadFile(s)
 	if err != nil {
-		fmt.Printf("FATAL: locator '%s' cannot be parsed as file or literal: %s", s, err.Error())
+		pErr("locator '%s' cannot be parsed as file or literal: %s", s, err.Error())
 		os.Exit(1)
 	}
 	loc, err := zerotier.NewLocatorFromString(string(locData))
 	if err != nil {
-		fmt.Printf("FATAL: locator '%s' cannot be parsed as file or literal: %s", s, err.Error())
+		pErr("locator '%s' cannot be parsed as file or literal: %s", s, err.Error())
 		os.Exit(1)
 	}
 	return loc

+ 3 - 1
pkg/zerotier/node.go

@@ -550,7 +550,7 @@ func (n *Node) DeleteCertificate(serialNo []byte) error {
 	return nil
 }
 
-// -------------------------------------------------------------------------------------------------------------------
+/********************************************************************************************************************/
 
 func (n *Node) runMaintenance() {
 	n.localConfigLock.RLock()
@@ -775,6 +775,8 @@ func (n *Node) handleTrace(traceMessage string) {
 	}
 }
 
+/********************************************************************************************************************/
+
 // These are callbacks called by the core and GoGlue stuff to talk to the
 // service. These launch goroutines to do their work where possible to
 // avoid blocking anything in the core.