More plumbing

This commit is contained in:
Adam Ierymenko 2019-10-02 07:09:54 -07:00
parent d1b780c7be
commit b9911d0db7
No known key found for this signature in database
GPG Key ID: C8877CF2D7A5D7F3
18 changed files with 384 additions and 114 deletions

View File

@ -44,7 +44,7 @@ Commands:
addroot <locator> [name] Add a VL1 root addroot <locator> [name] Add a VL1 root
removeroot <name> Remove a VL1 root removeroot <name> Remove a VL1 root
locator <command> [args] Locator management commands locator <command> [args] Locator management commands
new <identity> <address> [...] Create and sign a locator new <identity> <address> [...] Create and sign locator for identity
newdnskey Create a secure DNS name and secret newdnskey Create a secure DNS name and secret
getdns <dns key> <locator> Create secure DNS TXT records getdns <dns key> <locator> Create secure DNS TXT records
identity <command> [args] Identity management commands identity <command> [args] Identity management commands
@ -53,10 +53,10 @@ Commands:
validate <identity> Locally validate an identity validate <identity> Locally validate an identity
sign <identity> <file> Sign a file with an identity's key sign <identity> <file> Sign a file with an identity's key
verify <identity> <file> <sig> Verify a signature verify <identity> <file> <sig> Verify a signature
networks Show joined VL2 virtual networks networks List joined VL2 virtual networks
network <network ID> Show verbose network info
join <network ID> Join a virtual network join <network ID> Join a virtual network
leave <network ID> Leave a virtual network leave <network ID> Leave a virtual network
show <network ID> Show verbose network info
set <network ID> <option> <value> Set a network local config option set <network ID> <option> <value> Set a network local config option
manageips <boolean> Is IP management allowed? manageips <boolean> Is IP management allowed?
manageroutes <boolean> Is route management allowed? manageroutes <boolean> Is route management allowed?

View File

@ -54,33 +54,13 @@ func Identity(args []string) {
case "getpublic": case "getpublic":
if len(args) == 2 { if len(args) == 2 {
idData, err := ioutil.ReadFile(args[1]) fmt.Println(readIdentity(args[1]).String())
if err != nil {
fmt.Printf("ERROR: unable to read identity: %s\n", err.Error())
os.Exit(1)
}
id, err := zerotier.NewIdentityFromString(string(idData))
if err != nil {
fmt.Printf("ERROR: identity in file '%s' invalid: %s\n", args[1], err.Error())
os.Exit(1)
}
fmt.Println(id.String())
os.Exit(0) os.Exit(0)
} }
case "validate": case "validate":
if len(args) == 2 { if len(args) == 2 {
idData, err := ioutil.ReadFile(args[1]) if readIdentity(args[1]).LocallyValidate() {
if err != nil {
fmt.Printf("ERROR: unable to read identity: %s\n", err.Error())
os.Exit(1)
}
id, err := zerotier.NewIdentityFromString(string(idData))
if err != nil {
fmt.Printf("ERROR: identity in file '%s' invalid: %s\n", args[1], err.Error())
os.Exit(1)
}
if id.LocallyValidate() {
fmt.Println("OK") fmt.Println("OK")
os.Exit(0) os.Exit(0)
} }
@ -90,16 +70,7 @@ func Identity(args []string) {
case "sign", "verify": case "sign", "verify":
if len(args) > 2 { if len(args) > 2 {
idData, err := ioutil.ReadFile(args[1]) id := readIdentity(args[1])
if err != nil {
fmt.Printf("ERROR: unable to read identity: %s\n", err.Error())
os.Exit(1)
}
id, err := zerotier.NewIdentityFromString(string(idData))
if err != nil {
fmt.Printf("ERROR: identity in file '%s' invalid: %s\n", args[1], err.Error())
os.Exit(1)
}
msg, err := ioutil.ReadFile(args[2]) msg, err := ioutil.ReadFile(args[2])
if err != nil { if err != nil {
fmt.Printf("ERROR: unable to read input file: %s\n", err.Error()) fmt.Printf("ERROR: unable to read input file: %s\n", err.Error())

View File

@ -13,6 +13,35 @@
package cli package cli
import (
"fmt"
"os"
"strconv"
"zerotier/pkg/zerotier"
)
// Join CLI command // Join CLI command
func Join(basePath, authToken string, args []string) { func Join(basePath, authToken string, args []string) {
if len(args) != 1 {
Help()
os.Exit(1)
}
if len(args[0]) != 16 {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
os.Exit(1)
}
nwid, err := strconv.ParseUint(args[0], 16, 64)
if err != nil {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
os.Exit(1)
}
nwids := fmt.Sprintf("%.16x", nwid)
var network zerotier.APINetwork
network.ID = zerotier.NetworkID(nwid)
apiPost(basePath, authToken, "/network/"+nwids, &network, nil)
fmt.Printf("OK %s", nwids)
os.Exit(0)
} }

View File

@ -13,6 +13,31 @@
package cli package cli
import (
"fmt"
"os"
"strconv"
)
// Leave CLI command // Leave CLI command
func Leave(basePath, authToken string, args []string) { func Leave(basePath, authToken string, args []string) {
if len(args) != 1 {
Help()
os.Exit(1)
}
if len(args[0]) != 16 {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
os.Exit(1)
}
nwid, err := strconv.ParseUint(args[0], 16, 64)
if err != nil {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
os.Exit(1)
}
nwids := fmt.Sprintf("%.16x", nwid)
apiDelete(basePath, authToken, "/network/"+nwids, nil)
fmt.Printf("OK %s", nwids)
os.Exit(0)
} }

View File

@ -29,18 +29,9 @@ func locatorNew(args []string) {
os.Exit(1) os.Exit(1)
} }
identityData, err := ioutil.ReadFile(args[0]) identity := readIdentity(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() { if !identity.HasPrivate() {
fmt.Println("FATAL: identity does not contain secret key") fmt.Println("FATAL: identity does not contain a secret key (required to sign locator)")
os.Exit(1) os.Exit(1)
} }

View File

@ -16,8 +16,11 @@ package cli
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"strings"
"zerotier/pkg/zerotier" "zerotier/pkg/zerotier"
) )
@ -55,6 +58,23 @@ func apiPost(basePath, authToken, urlPath string, post, result interface{}) {
} }
} }
func apiDelete(basePath, authToken, urlPath string, result interface{}) {
statusCode, 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
}
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 { func enabledDisabled(f bool) string {
if f { if f {
return "ENABLED" return "ENABLED"
@ -66,3 +86,37 @@ func jsonDump(obj interface{}) string {
j, _ := json.MarshalIndent(obj, "", " ") j, _ := json.MarshalIndent(obj, "", " ")
return string(j) return string(j)
} }
func readIdentity(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 {
fmt.Printf("FATAL: identity '%s' cannot be resolved as file or literal identity: %s", s, err.Error())
os.Exit(1)
}
id, err := zerotier.NewIdentityFromString(string(idData))
if err != nil {
fmt.Printf("FATAL: identity '%s' cannot be resolved as file or literal identity: %s", s, err.Error())
os.Exit(1)
}
return id
}
func networkStatusStr(int status) string {
switch status {
case zerotier.NetworkStatusNotFound:
return "NOTFOUND"
case zerotier.NetworkStatusAccessDenied:
return "DENIED"
case zerotier.NetworkStatusRequestConfiguration:
return "UPDATING"
case zerotier.NetworkStatusOK:
return "OK"
}
return "???"
}

View File

@ -0,0 +1,105 @@
/*
* 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"
"os"
"strconv"
"zerotier/pkg/zerotier"
)
// Network CLI command
func Network(basePath, authToken string, args []string, jsonOutput bool) {
if len(args) != 1 {
Help()
os.Exit(1)
}
if len(args[0]) != 16 {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
os.Exit(1)
}
nwid, err := strconv.ParseUint(args[0], 16, 64)
if err != nil {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
os.Exit(1)
}
nwids := fmt.Sprintf("%.16x", nwid)
var network zerotier.APINetwork
apiGet(basePath, authToken, "/network/"+nwids, &network)
if jsonOutput {
fmt.Println(jsonDump(&network))
} else {
fmt.Printf("%s: %s\n", nwids, network.Config.Name)
fmt.Printf("\tstatus:\t%s\n", networkStatusStr(network.Config.Status))
enabled := "no"
if network.TapDeviceEnabled {
enabled = "yes"
}
bridge := "no"
if network.Config.Bridge {
bridge = "yes"
}
broadcast := "off"
if network.Config.BroadcastEnabled {
broadcast = "on"
}
fmt.Printf("\tport:\t%s dev %s type %s mtu %d enabled %s bridge %s broadcast %s\n", network.Config.MAC.String(), network.TapDeviceName, network.TapDeviceType, network.Config.MTU, enabled, bridge, broadcast)
fmt.Printf("\tmanaged addresses:\t")
for i, a := range network.Config.AssignedAddresses {
if i > 0 {
fmt.Print(' ')
}
fmt.Print(a.String())
}
fmt.Printf("\n\tmanaged routes:\t")
for i, r := range network.Config.Routes {
if i > 0 {
fmt.Print(' ')
}
fmt.Print(r.Target.String())
if r.Via == nil {
fmt.Print("->LAN")
} else {
fmt.Printf("->%s", r.Via.String())
}
}
managedIPs := "disabled"
if network.Settings.AllowManagedIPs {
managedIPs = "enabled"
}
managedIPsGlobal := "disabled"
if network.Settings.AllowGlobalIPs {
managedIPsGlobal = "enabled"
}
fmt.Printf("\n\tmanaged address local permissions:\t%s global %s\n", managedIPs, managedIPsGlobal)
managedRoutes := "diabled"
if network.Settings.AllowManagedRoutes {
managedRoutes = "enabled"
}
managedGlobalRoutes := "disabled"
if network.Settings.AllowGlobalRoutes {
managedGlobalRoutes = "enabled"
}
managedDefaultRoute := "disabled"
if network.Settings.AllowDefaultRouteOverride {
managedDefaultRoute = "enabled"
}
fmt.Printf("\tmanaged route local permissions:\t%s global %s default %s\n", managedRoutes, managedGlobalRoutes, managedDefaultRoute)
}
}

View File

@ -13,6 +13,37 @@
package cli package cli
import (
"fmt"
"os"
"zerotier/pkg/zerotier"
)
// Networks CLI command // Networks CLI command
func Networks(basePath, authToken string, args []string) { func Networks(basePath, authToken string, args []string, jsonOutput bool) {
var networks []zerotier.APINetwork
apiGet(basePath, authToken, "/network", &networks)
if jsonOutput {
fmt.Println(jsonDump(networks))
} else {
fmt.Printf("%-16s %-24s %-17s %-8s <type> <device> <managed IP(s)>\n", "<id>", "<name>", "<mac>", "<status>")
for _, nw := range networks {
t := "PRIVATE"
if nw.Config.Type == zerotier.NetworkTypePublic {
t = "PUBLIC"
}
fmt.Printf("%.16x %-24s %-17s %-16s %-7s %-16s ", uint64(nw.ID), nw.Config.Name, nw.Config.MAC.String(), networkStatusStr(nw.Config.Status), t, nw.TapDeviceName)
for i, ip := range nw.Config.AssignedAddresses {
if i > 0 {
fmt.Print(',')
}
fmt.Print(ip.String())
}
fmt.Print("\n")
}
}
os.Exit(0)
} }

View File

@ -28,7 +28,7 @@ func Peers(basePath, authToken string, args []string, jsonOutput bool) {
if jsonOutput { if jsonOutput {
fmt.Println(jsonDump(&peers)) fmt.Println(jsonDump(&peers))
} else { } else {
fmt.Printf("<ztaddr> <ver> <role> <lat> <link> <lastTX> <lastRX> <path(s)>\n") fmt.Printf("<address> <ver> <role> <lat> <link> <lastTX> <lastRX> <path(s)>\n")
for _, peer := range peers { for _, peer := range peers {
role := "LEAF" role := "LEAF"
link := "RELAY" link := "RELAY"

View File

@ -13,6 +13,27 @@
package cli package cli
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"zerotier/pkg/zerotier"
)
// RemoveRoot CLI command // RemoveRoot CLI command
func RemoveRoot(basePath, authToken string, args []string) { func RemoveRoot(basePath, authToken string, args []string) {
if len(args) != 1 {
Help()
os.Exit(1)
}
result, _ := zerotier.APIDelete(basePath, zerotier.APISocketName, authToken, "/root/"+url.PathEscape(strings.TrimSpace(args[0])), nil)
if result == http.StatusOK {
fmt.Printf("%s removed\n", args[0])
os.Exit(0)
}
fmt.Printf("ERROR: root %s not found or another error occurred: status code %d\n", args[0], result)
os.Exit(1)
} }

View File

@ -28,7 +28,7 @@ func Roots(basePath, authToken string, args []string, jsonOutput bool) {
if jsonOutput { if jsonOutput {
fmt.Println(jsonDump(roots)) fmt.Println(jsonDump(roots))
} else { } else {
fmt.Printf("%32s <ztaddr> <address(es)>\n", "<name>") fmt.Printf("%32s <address> <physical/virtual>\n", "<name>")
for _, r := range roots { for _, r := range roots {
rn := r.Name rn := r.Name
if len(rn) > 32 { if len(rn) > 32 {

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
// Show CLI command
func Show(basePath, authToken string, args []string) {
}

View File

@ -33,39 +33,39 @@ func Status(basePath, authToken string, args []string, jsonOutput bool) {
online = "OFFLINE" online = "OFFLINE"
} }
fmt.Printf("%.10x: %s %s\n", uint64(status.Address), online, status.Version) 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("\tports:\t%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 search:\t%s\n", enabledDisabled(status.Config.Settings.PortSearch))
fmt.Printf("\tport mapping (uPnP/NAT-PMP): %s\n", enabledDisabled(status.Config.Settings.PortMapping)) fmt.Printf("\tport mapping (uPnP/NAT-PMP):\t%s\n", enabledDisabled(status.Config.Settings.PortMapping))
fmt.Printf("\tmultipath mode: %d\n", status.Config.Settings.MuiltipathMode) fmt.Printf("\tmultipath mode:\t%d\n", status.Config.Settings.MuiltipathMode)
fmt.Printf("\tblacklisted interface prefixes: ") fmt.Printf("\tblacklisted interface prefixes:\t")
for i, bl := range status.Config.Settings.InterfacePrefixBlacklist { for i, bl := range status.Config.Settings.InterfacePrefixBlacklist {
if i > 0 { if i > 0 {
fmt.Print(',') fmt.Print(' ')
} }
fmt.Print(bl) fmt.Print(bl)
} }
fmt.Printf("\n\texplicit external addresses: ") fmt.Printf("\n\texplicit external addresses: ")
for i, ea := range status.Config.Settings.ExplicitAddresses { for i, ea := range status.Config.Settings.ExplicitAddresses {
if i > 0 { if i > 0 {
fmt.Print(',') fmt.Print(' ')
} }
fmt.Print(ea.String()) fmt.Print(ea.String())
} }
fmt.Printf("\n\tsystem interface addresses: ") fmt.Printf("\n\tsystem interface addresses: ")
for i, a := range status.InterfaceAddresses { for i, a := range status.InterfaceAddresses {
if i > 0 { if i > 0 {
fmt.Print(',') fmt.Print(' ')
} }
fmt.Print(a.String()) fmt.Print(a.String())
} }
fmt.Printf("\n\tmapped external addresses: ") fmt.Printf("\n\tmapped external addresses: ")
for i, a := range status.MappedExternalAddresses { for i, a := range status.MappedExternalAddresses {
if i > 0 { if i > 0 {
fmt.Print(',') fmt.Print(' ')
} }
fmt.Print(a.String()) fmt.Print(a.String())
} }
fmt.Printf("\n\tidentity: %s\n", status.Identity.String()) fmt.Printf("\n\tidentity:\t%s\n", status.Identity.String())
} }
os.Exit(0) os.Exit(0)

View File

@ -85,6 +85,11 @@ func main() {
cmdArgs = args[1:] cmdArgs = args[1:]
} }
if *hflag {
cli.Help()
os.Exit(0)
}
basePath := zerotier.PlatformDefaultHomePath basePath := zerotier.PlatformDefaultHomePath
if len(*pflag) > 0 { if len(*pflag) > 0 {
basePath = *pflag basePath = *pflag
@ -128,16 +133,16 @@ func main() {
cli.Identity(cmdArgs) cli.Identity(cmdArgs)
case "networks", "listnetworks": case "networks", "listnetworks":
authTokenRequired(authToken) authTokenRequired(authToken)
cli.Networks(basePath, authToken, cmdArgs) cli.Networks(basePath, authToken, cmdArgs, *jflag)
case "network":
authTokenRequired(authToken)
cli.Network(basePath, authToken, cmdArgs, *jflag)
case "join": case "join":
authTokenRequired(authToken) authTokenRequired(authToken)
cli.Join(basePath, authToken, cmdArgs) cli.Join(basePath, authToken, cmdArgs)
case "leave": case "leave":
authTokenRequired(authToken) authTokenRequired(authToken)
cli.Leave(basePath, authToken, cmdArgs) cli.Leave(basePath, authToken, cmdArgs)
case "show":
authTokenRequired(authToken)
cli.Show(basePath, authToken, cmdArgs)
case "set": case "set":
authTokenRequired(authToken) authTokenRequired(authToken)
cli.Set(basePath, authToken, cmdArgs) cli.Set(basePath, authToken, cmdArgs)

View File

@ -69,6 +69,29 @@ func APIPost(basePath, socketName, authToken, queryPath string, post, result int
if err != nil { if err != nil {
return http.StatusTeapot, err return http.StatusTeapot, err
} }
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "bearer "+authToken)
resp, err := client.Do(req)
if err != nil {
return http.StatusTeapot, err
}
if result != nil {
err = json.NewDecoder(resp.Body).Decode(result)
return resp.StatusCode, err
}
return resp.StatusCode, nil
}
// APIDelete posts DELETE to a path and fills result with the outcome (if any) if result is non-nil
func APIDelete(basePath, socketName, authToken, queryPath string, result interface{}) (int, error) {
client, err := createNamedSocketHTTPClient(basePath, socketName)
if err != nil {
return http.StatusTeapot, err
}
req, err := http.NewRequest("DELETE", "http://socket"+queryPath, nil)
if err != nil {
return http.StatusTeapot, err
}
req.Header.Add("Authorization", "bearer "+authToken) req.Header.Add("Authorization", "bearer "+authToken)
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
@ -90,8 +113,8 @@ type APIStatus struct {
PeerCount int PeerCount int
PathCount int PathCount int
Identity *Identity Identity *Identity
InterfaceAddresses []net.IP InterfaceAddresses []net.IP `json:",omitempty"`
MappedExternalAddresses []*InetAddress MappedExternalAddresses []*InetAddress `json:",omitempty"`
Version string Version string
VersionMajor int VersionMajor int
VersionMinor int VersionMinor int
@ -102,9 +125,9 @@ type APIStatus struct {
// APINetwork is the object returned by API network inquiries // APINetwork is the object returned by API network inquiries
type APINetwork struct { type APINetwork struct {
ID NetworkID ID NetworkID
Config *NetworkConfig Config NetworkConfig
Settings *NetworkLocalSettings Settings *NetworkLocalSettings `json:",omitempty"`
MulticastSubscriptions []*MulticastGroup MulticastSubscriptions []*MulticastGroup `json:",omitempty"`
TapDeviceType string TapDeviceType string
TapDeviceName string TapDeviceName string
TapDeviceEnabled bool TapDeviceEnabled bool
@ -113,8 +136,7 @@ type APINetwork struct {
func apiNetworkFromNetwork(n *Network) *APINetwork { func apiNetworkFromNetwork(n *Network) *APINetwork {
var nn APINetwork var nn APINetwork
nn.ID = n.ID() nn.ID = n.ID()
c := n.Config() nn.Config = n.Config()
nn.Config = &c
ls := n.LocalSettings() ls := n.LocalSettings()
nn.Settings = &ls nn.Settings = &ls
nn.MulticastSubscriptions = n.MulticastSubscriptions() nn.MulticastSubscriptions = n.MulticastSubscriptions()
@ -175,7 +197,7 @@ func apiCheckAuth(out http.ResponseWriter, req *http.Request, token string) bool
} }
// createAPIServer creates and starts an HTTP server for a given node // createAPIServer creates and starts an HTTP server for a given node
func createAPIServer(basePath string, node *Node) (*http.Server, error) { func createAPIServer(basePath string, node *Node) (*http.Server, *http.Server, error) {
// Read authorization token, automatically generating one if it's missing // Read authorization token, automatically generating one if it's missing
var authToken string var authToken string
authTokenFile := path.Join(basePath, "authtoken.secret") authTokenFile := path.Join(basePath, "authtoken.secret")
@ -184,14 +206,14 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
var atb [20]byte var atb [20]byte
_, err = secrand.Read(atb[:]) _, err = secrand.Read(atb[:])
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
atb[i] = "abcdefghijklmnopqrstuvwxyz0123456789"[atb[i]%36] atb[i] = "abcdefghijklmnopqrstuvwxyz0123456789"[atb[i]%36]
} }
err = ioutil.WriteFile(authTokenFile, atb[:], 0600) err = ioutil.WriteFile(authTokenFile, atb[:], 0600)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
_ = acl.Chmod(authTokenFile, 0600) _ = acl.Chmod(authTokenFile, 0600)
authToken = string(atb[:]) authToken = string(atb[:])
@ -348,7 +370,7 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
for _, nw := range networks { for _, nw := range networks {
if nw.id == queriedID { if nw.id == queriedID {
_ = node.Leave(queriedID) _ = node.Leave(queriedID)
_ = apiSendObj(out, req, http.StatusOK, nw) _ = apiSendObj(out, req, http.StatusOK, apiNetworkFromNetwork(nw))
return return
} }
} }
@ -468,7 +490,7 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
listener, err := createNamedSocketListener(basePath, APISocketName) listener, err := createNamedSocketListener(basePath, APISocketName)
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
httpServer := &http.Server{ httpServer := &http.Server{
MaxHeaderBytes: 4096, MaxHeaderBytes: 4096,
@ -479,12 +501,34 @@ func createAPIServer(basePath string, node *Node) (*http.Server, error) {
} }
httpServer.SetKeepAlivesEnabled(true) httpServer.SetKeepAlivesEnabled(true)
go func() { go func() {
err := httpServer.Serve(listener) _ = httpServer.Serve(listener)
if err != nil {
node.log.Printf("ERROR: unable to start API HTTP server: %s (continuing anyway but CLI will not work!)", err.Error())
}
_ = listener.Close() _ = listener.Close()
}() }()
return httpServer, nil var tcpHttpServer *http.Server
tcpBindAddr := node.LocalConfig().Settings.APITCPBindAddress
if tcpBindAddr != nil {
tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: tcpBindAddr.IP,
Port: tcpBindAddr.Port,
})
if err != nil {
node.log.Printf("ERROR: unable to start API HTTP server at TCP bind address %s: %s (continuing anyway)", tcpBindAddr.String(), err.Error())
} else {
tcpHttpServer = &http.Server{
MaxHeaderBytes: 4096,
Handler: smux,
IdleTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
WriteTimeout: 600 * time.Second,
}
tcpHttpServer.SetKeepAlivesEnabled(true)
go func() {
_ = tcpHttpServer.Serve(tcpListener)
_ = tcpListener.Close()
}()
}
}
return httpServer, tcpHttpServer, nil
} }

View File

@ -33,7 +33,7 @@ type LocalConfigPhysicalPathConfiguration struct {
// LocalConfigVirtualAddressConfiguration contains settings for virtual addresses // LocalConfigVirtualAddressConfiguration contains settings for virtual addresses
type LocalConfigVirtualAddressConfiguration struct { type LocalConfigVirtualAddressConfiguration struct {
// Try is a list of IPs/ports to try for this peer in addition to anything learned from roots or direct path push // Try is a list of IPs/ports to try for this peer in addition to anything learned from roots or direct path push
Try []*InetAddress Try []*InetAddress `json:",omitempty"`
} }
// LocalConfigSettings contains node settings // LocalConfigSettings contains node settings
@ -59,26 +59,29 @@ type LocalConfigSettings struct {
// MultipathMode sets the multipath link aggregation mode // MultipathMode sets the multipath link aggregation mode
MuiltipathMode int MuiltipathMode int
// IP/port to bind for TCP access to control API (disabled if null)
APITCPBindAddress *InetAddress `json:",omitempty"`
// InterfacePrefixBlacklist are prefixes of physical network interface names that won't be used by ZeroTier (e.g. "lo" or "utun") // InterfacePrefixBlacklist are prefixes of physical network interface names that won't be used by ZeroTier (e.g. "lo" or "utun")
InterfacePrefixBlacklist []string InterfacePrefixBlacklist []string `json:",omitempty"`
// ExplicitAddresses are explicit IP/port addresses to advertise to other nodes, such as externally mapped ports on a router // ExplicitAddresses are explicit IP/port addresses to advertise to other nodes, such as externally mapped ports on a router
ExplicitAddresses []*InetAddress ExplicitAddresses []*InetAddress `json:",omitempty"`
} }
// LocalConfig is the local.conf file and stores local settings for the node. // LocalConfig is the local.conf file and stores local settings for the node.
type LocalConfig struct { type LocalConfig struct {
// Physical path configurations by CIDR IP/bits // Physical path configurations by CIDR IP/bits
Physical map[string]*LocalConfigPhysicalPathConfiguration Physical map[string]*LocalConfigPhysicalPathConfiguration `json:",omitempty"`
// Virtual node specific configurations by 10-digit hex ZeroTier address // Virtual node specific configurations by 10-digit hex ZeroTier address
Virtual map[Address]*LocalConfigVirtualAddressConfiguration Virtual map[Address]*LocalConfigVirtualAddressConfiguration `json:",omitempty"`
// Network local configurations by 16-digit hex ZeroTier network ID // Network local configurations by 16-digit hex ZeroTier network ID
Network map[NetworkID]*NetworkLocalSettings Network map[NetworkID]*NetworkLocalSettings `json:",omitempty"`
// LocalConfigSettings contains other local settings for this node // LocalConfigSettings contains other local settings for this node
Settings LocalConfigSettings Settings LocalConfigSettings `json:",omitempty"`
} }
// Read this local config from a file, initializing to defaults if the file does not exist // Read this local config from a file, initializing to defaults if the file does not exist

View File

@ -170,6 +170,7 @@ type Node struct {
zn *C.ZT_Node zn *C.ZT_Node
id *Identity id *Identity
apiServer *http.Server apiServer *http.Server
tcpApiServer *http.Server
online uint32 online uint32
running uint32 running uint32
runLock sync.Mutex runLock sync.Mutex
@ -285,7 +286,7 @@ func NewNode(basePath string) (*Node, error) {
return nil, err return nil, err
} }
n.apiServer, err = createAPIServer(basePath, n) n.apiServer, n.tcpApiServer, err = createAPIServer(basePath, n)
if err != nil { if err != nil {
n.log.Printf("FATAL: unable to start API server: %s", err.Error()) n.log.Printf("FATAL: unable to start API server: %s", err.Error())
C.ZT_GoNode_delete(n.gn) C.ZT_GoNode_delete(n.gn)
@ -399,13 +400,21 @@ func NewNode(basePath string) (*Node, error) {
// Close closes this Node and frees its underlying C++ Node structures // Close closes this Node and frees its underlying C++ Node structures
func (n *Node) Close() { func (n *Node) Close() {
if atomic.SwapUint32(&n.running, 0) != 0 { if atomic.SwapUint32(&n.running, 0) != 0 {
if n.apiServer != nil {
_ = n.apiServer.Close() _ = n.apiServer.Close()
}
if n.tcpApiServer != nil {
_ = n.tcpApiServer.Close()
}
C.ZT_GoNode_delete(n.gn) C.ZT_GoNode_delete(n.gn)
n.runLock.Lock() // wait for maintenance gorountine to die
n.runLock.Unlock()
nodesByUserPtrLock.Lock() nodesByUserPtrLock.Lock()
delete(nodesByUserPtr, uintptr(unsafe.Pointer(n.gn))) delete(nodesByUserPtr, uintptr(unsafe.Pointer(n.gn)))
nodesByUserPtrLock.Unlock() nodesByUserPtrLock.Unlock()
n.runLock.Lock() // wait for maintenance gorountine to die
n.runLock.Unlock()
} }
} }
@ -943,9 +952,9 @@ func goVirtualNetworkConfigFunc(gn, _ unsafe.Pointer, nwid C.uint64_t, op C.int,
for i := 0; i < int(ncc.routeCount); i++ { for i := 0; i < int(ncc.routeCount); i++ {
tgt := sockaddrStorageToIPNet(&ncc.routes[i].target) tgt := sockaddrStorageToIPNet(&ncc.routes[i].target)
viaN := sockaddrStorageToIPNet(&ncc.routes[i].via) viaN := sockaddrStorageToIPNet(&ncc.routes[i].via)
var via net.IP var via *net.IP
if viaN != nil { if viaN != nil && len(viaN.IP) > 0 {
via = viaN.IP via = &viaN.IP
} }
if tgt != nil { if tgt != nil {
nc.Routes = append(nc.Routes, Route{ nc.Routes = append(nc.Routes, Route{

View File

@ -24,7 +24,7 @@ type Route struct {
Target net.IPNet Target net.IPNet
// Via is how to reach this target (null/empty if the target IP range is local to this virtual LAN) // Via is how to reach this target (null/empty if the target IP range is local to this virtual LAN)
Via net.IP Via *net.IP
// Route flags (currently unused, always 0) // Route flags (currently unused, always 0)
Flags uint16 Flags uint16
@ -35,7 +35,7 @@ type Route struct {
// String returns a string representation of this route // String returns a string representation of this route
func (r *Route) String() string { func (r *Route) String() string {
if len(r.Via) == 0 { if r.Via != nil {
return r.Target.String() + "->LAN" return r.Target.String() + "->LAN"
} }
return r.Target.String() + "->" + r.Via.String() return r.Target.String() + "->" + r.Via.String()