mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-14 10:37:33 -07:00
.
This commit is contained in:
parent
5175636d36
commit
7061f13b24
10 changed files with 229 additions and 43 deletions
45
go/cmd/zerotier/cli/common.go
Normal file
45
go/cmd/zerotier/cli/common.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* 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 (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"zerotier/pkg/zerotier"
|
||||
)
|
||||
|
||||
func apiGet(basePath, authToken, urlPath string, result interface{}) {
|
||||
statusCode, 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
|
||||
}
|
||||
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"
|
||||
}
|
||||
return "DISABLED"
|
||||
}
|
|
@ -13,6 +13,51 @@
|
|||
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"zerotier/pkg/zerotier"
|
||||
)
|
||||
|
||||
// Peers CLI command
|
||||
func Peers(basePath, authToken string, args []string) {
|
||||
func Peers(basePath, authToken string, args []string, jsonOutput bool) {
|
||||
var peers []zerotier.Peer
|
||||
apiGet(basePath, authToken, "/peer", &peers)
|
||||
|
||||
if jsonOutput {
|
||||
j, _ := json.MarshalIndent(&peers, "", " ")
|
||||
fmt.Println(string(j))
|
||||
} else {
|
||||
fmt.Printf("<ztaddr> <ver> <role> <lat> <link> <lastTX> <lastRX> <path(s)>\n")
|
||||
for _, peer := range peers {
|
||||
role := "LEAF"
|
||||
link := "RELAY"
|
||||
lastTX, lastRX := int64(0), int64(0)
|
||||
address := ""
|
||||
if len(peer.Paths) > 0 {
|
||||
link = "DIRECT"
|
||||
lastTX, lastRX = peer.Clock-peer.Paths[0].LastSend, peer.Clock-peer.Paths[0].LastReceive
|
||||
if lastTX < 0 {
|
||||
lastTX = 0
|
||||
}
|
||||
if lastRX < 0 {
|
||||
lastRX = 0
|
||||
}
|
||||
address = fmt.Sprintf("%s/%d", peer.Paths[0].IP.String(), peer.Paths[0].Port)
|
||||
}
|
||||
fmt.Printf("%.10x %-7s %-6s %-5d %-6s %-8d %-8d %s\n",
|
||||
uint64(peer.Address),
|
||||
fmt.Sprintf("%d.%d.%d", peer.Version[0], peer.Version[1], peer.Version[2]),
|
||||
role,
|
||||
peer.Latency,
|
||||
link,
|
||||
lastTX,
|
||||
lastRX,
|
||||
address,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@ package cli
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"zerotier/pkg/zerotier"
|
||||
)
|
||||
|
@ -24,25 +23,50 @@ import (
|
|||
// Status shows service status info
|
||||
func Status(basePath, authToken string, args []string, jsonOutput bool) {
|
||||
var status zerotier.APIStatus
|
||||
statusCode, err := zerotier.APIGet(basePath, zerotier.APISocketName, authToken, "/status", &status)
|
||||
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
|
||||
}
|
||||
apiGet(basePath, authToken, "/status", &status)
|
||||
|
||||
if jsonOutput {
|
||||
j, _ := json.MarshalIndent(&status, "", " ")
|
||||
fmt.Println(string(j))
|
||||
} else {
|
||||
online := "ONLINE"
|
||||
if !status.Online {
|
||||
online = "OFFLINE"
|
||||
}
|
||||
fmt.Printf("%.10x: %s %s\n", uint64(status.Address), online, status.Version)
|
||||
fmt.Printf("\tports: %d %d %d\n", status.Config.Settings.PrimaryPort, status.Config.Settings.SecondaryPort, status.Config.Settings.TertiaryPort)
|
||||
fmt.Printf("\tport search: %s\n", enabledDisabled(status.Config.Settings.PortSearch))
|
||||
fmt.Printf("\tport mapping (uPnP/NAT-PMP): %s\n", enabledDisabled(status.Config.Settings.PortMapping))
|
||||
fmt.Printf("\tmultipath mode: %d\n", status.Config.Settings.MuiltipathMode)
|
||||
fmt.Printf("\tblacklisted interface prefixes: ")
|
||||
for i, bl := range status.Config.Settings.InterfacePrefixBlacklist {
|
||||
if i > 0 {
|
||||
fmt.Print(',')
|
||||
}
|
||||
fmt.Print(bl)
|
||||
}
|
||||
fmt.Printf("\n\texplicit external addresses: ")
|
||||
for i, ea := range status.Config.Settings.ExplicitAddresses {
|
||||
if i > 0 {
|
||||
fmt.Print(',')
|
||||
}
|
||||
fmt.Print(ea.String())
|
||||
}
|
||||
fmt.Printf("\n\tsystem interface addresses: ")
|
||||
for i, a := range status.InterfaceAddresses {
|
||||
if i > 0 {
|
||||
fmt.Print(',')
|
||||
}
|
||||
fmt.Print(a.String())
|
||||
}
|
||||
fmt.Printf("\n\tmapped external addresses: ")
|
||||
for i, a := range status.MappedExternalAddresses {
|
||||
if i > 0 {
|
||||
fmt.Print(',')
|
||||
}
|
||||
fmt.Print(a.String())
|
||||
}
|
||||
fmt.Printf("\n\tidentity: %s\n", status.Identity.String())
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
|
|
|
@ -111,7 +111,7 @@ func main() {
|
|||
cli.Status(basePath, authToken, cmdArgs, *jflag)
|
||||
case "peers", "listpeers":
|
||||
authTokenRequired(authToken)
|
||||
cli.Peers(basePath, authToken, cmdArgs)
|
||||
cli.Peers(basePath, authToken, cmdArgs, *jflag)
|
||||
case "roots":
|
||||
authTokenRequired(authToken)
|
||||
cli.Roots(basePath, authToken, cmdArgs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue