A bunch of CLI work

This commit is contained in:
Adam Ierymenko 2019-09-30 16:12:08 -07:00
commit 7fc78129f4
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
19 changed files with 225 additions and 153 deletions

View file

@ -14,6 +14,7 @@
package cli
import (
"encoding/json"
"fmt"
"net/http"
"os"
@ -43,3 +44,8 @@ func enabledDisabled(f bool) string {
}
return "DISABLED"
}
func jsonDump(obj interface{}) string {
j, _ := json.MarshalIndent(obj, "", " ")
return string(j)
}

View file

@ -15,6 +15,7 @@ package cli
import (
"fmt"
"zerotier/pkg/zerotier"
)
@ -26,7 +27,7 @@ Licensed under the ZeroTier BSL (see LICENSE.txt)`, zerotier.CoreVersionMajor, z
func Help() {
fmt.Println(copyrightText)
fmt.Println(`
Usage: zerotier [-options] <command> [-options] [command args]
Usage: zerotier [-options] <command> [command args]
Global Options:
-j Output raw JSON where applicable
@ -40,11 +41,18 @@ Commands:
status Show ZeroTier service status and config
peers Show VL1 peers
roots Show VL1 root servers
addroot <locator> [<name>] Add a VL1 root
addroot <locator> [name] Add a VL1 root
removeroot <name> Remove a VL1 root
makelocator <secret> <address> [...] Make and sign a locator
makelocatordnskey Create a new secure DNS name and key
makelocatordns <key> <locator> Make DNS TXT records for a locator
locator <command> [args] Locator management commands
new <identity> <address> [...] Create and sign a locator
newdnskey Create a secure DNS name and secret
getdns <key> <locator> Create secure DNS TXT records
identity <command> [args] Identity management commands
new Create new identity (including secret)
getpublic <identity> Extract only public part of identity
validate <identity> Locally validate an identity
sign <identity> <file> Sign a file with an identity's key
verify <identity> <file> <sig> Verify a signature
networks Show joined VL2 virtual networks
join <network ID> Join a virtual network
leave <network ID> Leave a virtual network

View file

@ -13,6 +13,6 @@
package cli
// MakeLocator CLI command
func MakeLocator(args []string) {
// Identity command
func Identity(args []string) {
}

View file

@ -0,0 +1,135 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2023-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
package cli
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"zerotier/pkg/zerotier"
)
func locatorNew(args []string) {
if len(args) < 2 {
Help()
os.Exit(1)
}
identityData, err := ioutil.ReadFile(args[0])
if err != nil {
fmt.Printf("FATAL: unable to read identity: %s\n", err.Error())
os.Exit(1)
}
identity, err := zerotier.NewIdentityFromString(string(identityData))
if err != nil {
fmt.Printf("FATAL: invalid identity: %s\n", err.Error())
os.Exit(1)
}
if !identity.HasPrivate() {
fmt.Println("FATAL: identity does not contain secret key")
os.Exit(1)
}
var virt []*zerotier.Identity
var phys []*zerotier.InetAddress
for i := 1; i < len(args); i++ {
if strings.Contains(args[i], "/") {
a := zerotier.NewInetAddressFromString(args[i])
if a == nil {
fmt.Printf("FATAL: IP/port address '%s' is not valid\n", args[i])
os.Exit(1)
}
phys = append(phys, a)
} else {
a, err := zerotier.NewIdentityFromString(args[i])
if err != nil {
fmt.Printf("FATAL: identity (virtual address) '%s' is not valid: %s\n", args[i], err.Error())
os.Exit(1)
}
virt = append(virt, a)
}
}
loc, err := zerotier.NewLocator(identity, virt, phys)
if err != nil {
fmt.Printf("FATAL: internal error creating locator: %s\n", err.Error())
os.Exit(1)
}
fmt.Println(jsonDump(loc))
}
func locatorNewDNSKey(args []string) {
if len(args) != 0 {
Help()
os.Exit(0)
}
sk, err := zerotier.NewLocatorDNSSigningKey()
if err != nil {
fmt.Printf("FATAL: error creating secure DNS signing key: %s", err.Error())
os.Exit(1)
}
fmt.Println(jsonDump(sk))
os.Exit(0)
}
func locatorGetDNS(args []string) {
if len(args) < 2 {
Help()
os.Exit(1)
}
keyData, err := ioutil.ReadFile(args[0])
if err != nil {
fmt.Printf("FATAL: unable to read secure DNS key file: %s\n", err.Error())
os.Exit(1)
}
var sk zerotier.LocatorDNSSigningKey
err = json.Unmarshal(keyData, &sk)
if err != nil {
fmt.Printf("FATAL: DNS key file invalid: %s", err.Error())
os.Exit(1)
}
locData, err := ioutil.ReadFile(args[1])
if err != nil {
fmt.Printf("FATAL: unable to read locator: %s\n", err.Error())
os.Exit(1)
}
var loc zerotier.Locator
err = json.Unmarshal(locData, &loc)
if err != nil {
fmt.Printf("FATAL: locator invalid: %s", err.Error())
os.Exit(1)
}
}
// Locator CLI command
func Locator(args []string) {
if len(args) > 0 {
switch args[0] {
case "new":
locatorNew(args[1:])
case "newdnskey":
locatorNewDNSKey(args[1:])
case "getdns":
locatorGetDNS(args[1:])
}
}
Help()
os.Exit(1)
}

View file

@ -1,18 +0,0 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2023-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
package cli
// MakeLocatorDNS CLI command
func MakeLocatorDNS(args []string) {
}

View file

@ -1,18 +0,0 @@
/*
* Copyright (c)2019 ZeroTier, Inc.
*
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
* Change Date: 2023-01-01
*
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
package cli
// MakeLocatorDNSKey CLI command
func MakeLocatorDNSKey(args []string) {
}

View file

@ -14,9 +14,9 @@
package cli
import (
"encoding/json"
"fmt"
"os"
"zerotier/pkg/zerotier"
)
@ -26,8 +26,7 @@ func Peers(basePath, authToken string, args []string, jsonOutput bool) {
apiGet(basePath, authToken, "/peer", &peers)
if jsonOutput {
j, _ := json.MarshalIndent(&peers, "", " ")
fmt.Println(string(j))
fmt.Println(jsonDump(&peers))
} else {
fmt.Printf("<ztaddr> <ver> <role> <lat> <link> <lastTX> <lastRX> <path(s)>\n")
for _, peer := range peers {

View file

@ -14,9 +14,9 @@
package cli
import (
"encoding/json"
"fmt"
"os"
"zerotier/pkg/zerotier"
)
@ -26,8 +26,7 @@ func Status(basePath, authToken string, args []string, jsonOutput bool) {
apiGet(basePath, authToken, "/status", &status)
if jsonOutput {
j, _ := json.MarshalIndent(&status, "", " ")
fmt.Println(string(j))
fmt.Println(jsonDump(&status))
} else {
online := "ONLINE"
if !status.Online {