This commit is contained in:
Adam Ierymenko 2019-10-01 13:09:53 -07:00
commit d1b780c7be
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
4 changed files with 99 additions and 9 deletions

View file

@ -13,6 +13,49 @@
package cli
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"strings"
"zerotier/pkg/zerotier"
)
// AddRoot CLI command
func AddRoot(basePath, authToken string, args []string) {
if len(args) == 0 {
Help()
os.Exit(0)
}
locData, err := ioutil.ReadFile(args[0])
if err != nil {
fmt.Printf("ERROR: unable to read locator: %s\n", err.Error())
os.Exit(1)
}
loc, err := zerotier.NewLocatorFromBytes(locData)
if err != nil {
fmt.Printf("ERROR: invalid locator in file '%s': %s\n", args[0], err.Error())
os.Exit(1)
}
var name string
if len(args) > 1 {
if len(args) > 2 {
Help()
os.Exit(1)
}
name = strings.TrimSpace(args[1])
}
var result zerotier.Root
apiPost(basePath, authToken, "/root/"+url.PathEscape(name), &zerotier.Root{
Name: name,
Locator: loc,
}, &result)
fmt.Println(jsonDump(&result))
os.Exit(0)
}

View file

@ -40,13 +40,13 @@ Commands:
service Start in system service mode
status Show ZeroTier service status and config
peers Show VL1 peers
roots Show VL1 root servers
roots Show configured VL1 root servers
addroot <locator> [name] Add a VL1 root
removeroot <name> Remove a VL1 root
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
getdns <dns key> <locator> Create secure DNS TXT records
identity <command> [args] Identity management commands
new [c25519|p384] Create new identity (including secret)
getpublic <identity> Extract only public part of identity

View file

@ -38,6 +38,23 @@ func apiGet(basePath, authToken, urlPath string, result interface{}) {
}
}
func apiPost(basePath, authToken, urlPath string, post, result interface{}) {
statusCode, err := zerotier.APIPost(basePath, zerotier.APISocketName, authToken, urlPath, post, result)
if err != nil {
fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
os.Exit(1)
return
}
if statusCode != http.StatusOK {
if statusCode == http.StatusUnauthorized {
fmt.Printf("FATAL: API response code %d: unauthorized (authorization token incorrect)\n", statusCode)
}
fmt.Printf("FATAL: API response code %d\n", statusCode)
os.Exit(1)
return
}
}
func enabledDisabled(f bool) string {
if f {
return "ENABLED"