123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /*
- * Copyright (c)2013-2020 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: 2025-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"
- "net/http"
- "os"
- "strings"
- "zerotier/pkg/zerotier"
- )
- func pErr(format string, args ...interface{}) {
- _, _ = fmt.Fprintf(os.Stdout, "ERROR: "+format, args...)
- fmt.Println()
- }
- func pResult(format string, args ...interface{}) {
- _, _ = fmt.Printf(format, args...)
- fmt.Println()
- }
- func apiGet(basePath, authToken, urlPath string, result interface{}) int64 {
- statusCode, clock, err := zerotier.APIGet(basePath, zerotier.APISocketName, authToken, urlPath, result)
- if err != nil {
- fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
- os.Exit(1)
- return 0
- }
- 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 0
- }
- return clock
- }
- func apiPost(basePath, authToken, urlPath string, post, result interface{}) int64 {
- statusCode, clock, 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 0
- }
- 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 0
- }
- return clock
- }
- func apiDelete(basePath, authToken, urlPath string, result interface{}) int64 {
- statusCode, clock, err := zerotier.APIDelete(basePath, zerotier.APISocketName, authToken, urlPath, result)
- if err != nil {
- fmt.Printf("FATAL: API response code %d: %s\n", statusCode, err.Error())
- os.Exit(1)
- return 0
- }
- 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 0
- }
- return clock
- }
- func enabledDisabled(f bool) string {
- if f {
- return "enabled"
- }
- return "disabled"
- }
- func allowedBlocked(f bool) string {
- if f {
- return "allowed"
- }
- return "blocked"
- }
- // isTrueStringPrefixChars matches things like [Tt]rue, [Yy]es, 1, [Ee]nabled, and [Aa]llowed
- var isTrueStringPrefixChars = [9]uint8{'t', 'T', 'y', 'Y', '1', 'e', 'E', 'a', 'A'}
- func isTrue(s string) bool {
- if len(s) > 0 {
- f := s[0]
- for _, c := range isTrueStringPrefixChars {
- if c == f {
- return true
- }
- }
- }
- return false
- }
- func jsonDump(obj interface{}) string {
- j, _ := json.MarshalIndent(obj, "", "\t")
- return string(j)
- }
- // parseAddressFingerprintOrIdentity parses an argument as an address, fingerprint, or identity.
- // If it's an address, only that return variable is filled out. Fingerprints fill out both address and
- // fingerprint. Identity fills out all three.
- func parseAddressFingerprintOrIdentity(s string) (a zerotier.Address, fp *zerotier.Fingerprint, id *zerotier.Identity) {
- var err error
- s = strings.TrimSpace(s)
- hasColon := strings.ContainsRune(s, ':')
- hasDash := strings.ContainsRune(s, '-')
- if len(s) == zerotier.AddressStringLength && !hasColon && !hasDash {
- a, err = zerotier.NewAddressFromString(s)
- if err == nil {
- return
- }
- }
- if hasDash {
- fp, err = zerotier.NewFingerprintFromString(s)
- if err == nil {
- a = fp.Address
- return
- }
- }
- if hasColon {
- id, err = zerotier.NewIdentityFromString(s)
- if err == nil {
- a = id.Address()
- fp = id.Fingerprint()
- return
- }
- }
- a = zerotier.Address(0)
- return
- }
- func cliGetIdentityOrFatal(s string) *zerotier.Identity {
- if strings.ContainsRune(s, ':') {
- id, _ := zerotier.NewIdentityFromString(s)
- if id != nil {
- return id
- }
- }
- idData, err := ioutil.ReadFile(s)
- if err != nil {
- 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 {
- pErr("identity '%s' cannot be parsed as file or literal: %s", s, err.Error())
- os.Exit(1)
- }
- return id
- }
- func cliGetLocatorOrFatal(s string) *zerotier.Locator {
- if strings.ContainsRune(s, '@') {
- loc, _ := zerotier.NewLocatorFromString(s)
- if loc != nil {
- return loc
- }
- }
- locData, err := ioutil.ReadFile(s)
- if err != nil {
- 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 {
- pErr("locator '%s' cannot be parsed as file or literal: %s", s, err.Error())
- os.Exit(1)
- }
- return loc
- }
- func networkStatusStr(status int) string {
- switch status {
- case zerotier.NetworkStatusNotFound:
- return "not-found"
- case zerotier.NetworkStatusAccessDenied:
- return "access-denied"
- case zerotier.NetworkStatusRequestingConfiguration:
- return "updating"
- case zerotier.NetworkStatusOK:
- return "ok"
- }
- return "???"
- }
- func readJSONFile(p string, obj interface{}) error {
- b, err := ioutil.ReadFile(p)
- if err != nil {
- return err
- }
- return json.Unmarshal(b, obj)
- }
- func isValidAddress(a string) bool {
- if len(a) == zerotier.AddressStringLength {
- for _, c := range a {
- if !strings.ContainsRune("0123456789abcdefABCDEF", c) {
- return false
- }
- }
- return true
- }
- return false
- }
- func isValidNetworkID(a string) bool {
- if len(a) == zerotier.NetworkIDStringLength {
- for _, c := range a {
- if !strings.ContainsRune("0123456789abcdefABCDEF", c) {
- return false
- }
- }
- return true
- }
- return false
- }
- /*
- func prompt(str string, dfl string) string {
- if len(dfl) > 0 {
- fmt.Printf("%s [%s]: ", str, dfl)
- text, _ := bufio.NewReader(os.Stdin).ReadString('\n')
- text = strings.TrimSpace(text)
- if len(text) == 0 {
- text = dfl
- }
- return text
- }
- fmt.Print(str)
- text, _ := bufio.NewReader(os.Stdin).ReadString('\n')
- return strings.TrimSpace(text)
- }
- func promptInt(str string, dfl int64) int64 {
- s := prompt(str, "")
- if len(s) > 0 {
- i, err := strconv.ParseInt(s, 10, 64)
- if err == nil {
- return i
- }
- }
- return dfl
- }
- func promptFile(str string) []byte {
- s := prompt(str, "")
- if len(s) > 0 {
- b, err := ioutil.ReadFile(s)
- if err == nil {
- return b
- }
- }
- return nil
- }
- */
|