Cleanup, revise join command

This commit is contained in:
Adam Ierymenko 2020-06-05 16:36:31 -07:00
parent fe28501126
commit e2ca065f28
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
10 changed files with 57 additions and 45 deletions

View file

@ -34,11 +34,11 @@ Global Options:
Commands:
help Show this help
version Print version
service Start as service
service Start a node (see below)
status Show node status and configuration
join <network> [option] Join a virtual network
auth <token> Join authorization token
fingerprint <fingerprint> Full controller identity fingerprint
join [-options] <network> Join a virtual network
-a <token> Join authorization token
-c <identity|fingerprint> Controller identity or fingerprint
leave <network> Leave a virtual network
networks List VL2 virtual networks
network <network> [command] [option] - Network management commands
@ -82,8 +82,6 @@ Commands:
sign <identity> <file> Sign a file with an identity's key
verify <identity> <file> <sig> Verify a signature
The 'service' command does not exit until the service receives a signal.
An <address> may be specified as a 10-digit short ZeroTier address, a
fingerprint containing both an address and a SHA384 hash, or an identity.
The latter two options are equivalent in terms of specificity and may be
@ -93,5 +91,8 @@ full identities and may be specified either verbatim or as a path to a file.
An <endpoint> is a place where a peer may be reached. Currently these are
just 'IP/port' format addresses but other types may be added in the future.
The 'service' command starts a node. It will run until the node receives
an exit signal and is normally not used directly.
`,zerotier.CoreVersionMajor, zerotier.CoreVersionMinor, zerotier.CoreVersionRevision)
}

View file

@ -14,6 +14,7 @@
package cli
import (
"flag"
"fmt"
"os"
"strconv"
@ -22,17 +23,47 @@ import (
"zerotier/pkg/zerotier"
)
// Join CLI command
func Join(basePath, authToken string, args []string) {
if len(args) < 1 || len(args) > 2 {
joinOpts := flag.NewFlagSet("join", flag.ContinueOnError)
controllerAuthToken := joinOpts.String("a", "", "")
controllerFingerprint := joinOpts.String("c", "", "")
err := joinOpts.Parse(os.Args[1:])
if err != nil {
Help()
os.Exit(1)
return
}
args = joinOpts.Args()
if len(args) < 1 {
Help()
os.Exit(1)
return
}
if len(args[0]) != zerotier.NetworkIDStringLength {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
os.Exit(1)
}
_ = *controllerAuthToken // TODO: not implemented yet
var fp *zerotier.Fingerprint
if len(*controllerFingerprint) > 0 {
if strings.ContainsRune(*controllerFingerprint, '-') {
fp, err = zerotier.NewFingerprintFromString(*controllerFingerprint)
if err != nil {
fmt.Printf("ERROR: invalid network controller fingerprint: %s\n", *controllerFingerprint)
os.Exit(1)
}
} else {
id, err := zerotier.NewIdentityFromString(*controllerFingerprint)
if err != nil {
fmt.Printf("ERROR: invalid network controller identity: %s\n", *controllerFingerprint)
os.Exit(1)
}
fp = id.Fingerprint()
}
}
nwid, err := strconv.ParseUint(args[0], 16, 64)
if err != nil {
fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
@ -40,24 +71,6 @@ func Join(basePath, authToken string, args []string) {
}
nwids := fmt.Sprintf("%.16x", nwid)
var fp *zerotier.Fingerprint
if len(args) == 2 {
if strings.ContainsRune(args[1], '-') {
fp, err = zerotier.NewFingerprintFromString(args[1])
if err != nil {
fmt.Printf("ERROR: invalid network controller fingerprint: %s\n", args[1])
os.Exit(1)
}
} else {
id, err := zerotier.NewIdentityFromString(args[1])
if err != nil {
fmt.Printf("ERROR: invalid network controller identity: %s\n", args[1])
os.Exit(1)
}
fp = id.Fingerprint()
}
}
var network zerotier.APINetwork
network.ID = zerotier.NetworkID(nwid)
network.ControllerFingerprint = fp

View file

@ -20,7 +20,6 @@ import (
"zerotier/pkg/zerotier"
)
// Leave CLI command
func Leave(basePath, authToken string, args []string) {
if len(args) != 1 {
Help()

View file

@ -20,7 +20,6 @@ import (
"zerotier/pkg/zerotier"
)
// Networks CLI command
func Networks(basePath, authToken string, args []string, jsonOutput bool) {
var networks []zerotier.APINetwork
apiGet(basePath, authToken, "/network", &networks)

View file

@ -21,7 +21,6 @@ import (
"zerotier/pkg/zerotier"
)
// Peers CLI command (also used for 'roots' command with rootsOnly set to true)
func Peers(basePath, authToken string, args []string, jsonOutput bool, rootsOnly bool) {
var peers []zerotier.Peer
apiGet(basePath, authToken, "/peer", &peers)

View file

@ -21,6 +21,10 @@ func Root(basePath, authToken string, args []string, jsonOutput bool) {
case "remove":
case "subscribe":
case "unsubscribe":
}
}
}

View file

@ -22,7 +22,6 @@ import (
"zerotier/pkg/zerotier"
)
// Service is "zerotier service ..."
func Service(basePath, authToken string, args []string) {
if len(args) > 0 {
Help()

View file

@ -13,6 +13,5 @@
package cli
// Set CLI command
func Set(basePath, authToken string, args []string) {
}

View file

@ -20,7 +20,6 @@ import (
"zerotier/pkg/zerotier"
)
// Status shows service status info
func Status(basePath, authToken string, args []string, jsonOutput bool) {
var status zerotier.APIStatus
apiGet(basePath, authToken, "/status", &status)

View file

@ -28,7 +28,7 @@ namespace ZeroTier {
class Mutex
{
public:
ZT_INLINE Mutex() noexcept { pthread_mutex_init(&_mh,nullptr); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
ZT_INLINE Mutex() noexcept { pthread_mutex_init(&_mh,nullptr); }
ZT_INLINE ~Mutex() noexcept { pthread_mutex_destroy(&_mh); }
ZT_INLINE void lock() const noexcept { pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh)); }
@ -37,15 +37,15 @@ public:
class Lock
{
public:
explicit ZT_INLINE Lock(Mutex &m) noexcept : _m(&m) { m.lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE Lock(const Mutex &m) noexcept : _m(const_cast<Mutex *>(&m)) { _m->lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE Lock(Mutex &m) noexcept : _m(&m) { m.lock(); }
explicit ZT_INLINE Lock(const Mutex &m) noexcept : _m(const_cast<Mutex *>(&m)) { _m->lock(); }
ZT_INLINE ~Lock() { _m->unlock(); }
private:
Mutex *const _m;
};
private:
ZT_INLINE Mutex(const Mutex &) noexcept {} // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
ZT_INLINE Mutex(const Mutex &) noexcept {}
ZT_INLINE const Mutex &operator=(const Mutex &) noexcept { return *this; }
pthread_mutex_t _mh;
@ -54,7 +54,7 @@ private:
class RWMutex
{
public:
ZT_INLINE RWMutex() noexcept { pthread_rwlock_init(&_mh,nullptr); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
ZT_INLINE RWMutex() noexcept { pthread_rwlock_init(&_mh,nullptr); }
ZT_INLINE ~RWMutex() noexcept { pthread_rwlock_destroy(&_mh); }
ZT_INLINE void lock() const noexcept { pthread_rwlock_wrlock(&((const_cast <RWMutex *> (this))->_mh)); }
@ -68,8 +68,8 @@ public:
class RLock
{
public:
explicit ZT_INLINE RLock(RWMutex &m) noexcept : _m(&m) { m.rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE RLock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)) { _m->rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE RLock(RWMutex &m) noexcept : _m(&m) { m.rlock(); }
explicit ZT_INLINE RLock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)) { _m->rlock(); }
ZT_INLINE ~RLock() { _m->runlock(); }
private:
RWMutex *const _m;
@ -81,8 +81,8 @@ public:
class Lock
{
public:
explicit ZT_INLINE Lock(RWMutex &m) noexcept : _m(&m) { m.lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE Lock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)) { _m->lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE Lock(RWMutex &m) noexcept : _m(&m) { m.lock(); }
explicit ZT_INLINE Lock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)) { _m->lock(); }
ZT_INLINE ~Lock() { _m->unlock(); }
private:
RWMutex *const _m;
@ -97,8 +97,8 @@ public:
class RMaybeWLock
{
public:
explicit ZT_INLINE RMaybeWLock(RWMutex &m) noexcept : _m(&m),_w(false) { m.rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE RMaybeWLock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)),_w(false) { _m->rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
explicit ZT_INLINE RMaybeWLock(RWMutex &m) noexcept : _m(&m),_w(false) { m.rlock(); }
explicit ZT_INLINE RMaybeWLock(const RWMutex &m) noexcept : _m(const_cast<RWMutex *>(&m)),_w(false) { _m->rlock(); }
ZT_INLINE void writing() noexcept { if (!_w) { _w = true; _m->runlock(); _m->lock(); } }
ZT_INLINE void reading() noexcept { if (_w) { _w = false; _m->unlock(); _m->rlock(); } }
ZT_INLINE ~RMaybeWLock() { if (_w) _m->unlock(); else _m->runlock(); }
@ -108,7 +108,7 @@ public:
};
private:
ZT_INLINE RWMutex(const RWMutex &) noexcept {} // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init)
ZT_INLINE RWMutex(const RWMutex &) noexcept {}
ZT_INLINE const RWMutex &operator=(const RWMutex &) noexcept { return *this; }
pthread_rwlock_t _mh;