From 0ad6b4105a50bc081ac1c8bd4e4ffe656b8f0b26 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 22 Jun 2016 17:54:07 -0700 Subject: Outline of new CLI with ability to control controllers/central as well as service. --- cli/zerotier.cpp | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 cli/zerotier.cpp (limited to 'cli/zerotier.cpp') diff --git a/cli/zerotier.cpp b/cli/zerotier.cpp new file mode 100644 index 00000000..ca9263fe --- /dev/null +++ b/cli/zerotier.cpp @@ -0,0 +1,190 @@ +#include +#include +#include +#include + +#include "../node/Constants.hpp" +#include "../version.h" +#include "../osdep/OSUtils.hpp" +#include "../ext/json/json.hpp" + +#ifdef __WINDOWS__ +#include +#include +#include +#include +#else +#include +#include +#endif + +#include +#include +#include +#include + +#include + +using json = nlohmann::json; +using OSUtils = ZeroTier::OSUtils; + +namespace { + +static inline std::string getSettingsFilePath() +{ +#ifdef __WINDOWS__ +#else + const char *home = getenv("HOME"); + if (!home) + home = "/"; + return (std::string(home) + "/.zerotierCliSettings"); +#endif +} + +static json loadSettings() +{ + json settings; + std::string buf; + if (OSUtils::readFile(getSettingsFilePath().c_str(),buf)) + settings = json::parse(buf); + return settings; +} + +static bool saveSettings(const json &settings) +{ + std::string sfp(getSettingsFilePath().c_str()); + std::string buf(settings.dump(2)); + if (OSUtils::writeFile(sfp.c_str(),buf)) { + OSUtils::lockDownFile(sfp.c_str(),false); + return true; + } + return false; +} + +static void dumpHelp() +{ + std::cout << "ZeroTier Newer-Spiffier CLI " << ZEROTIER_ONE_VERSION_MAJOR << "." << ZEROTIER_ONE_VERSION_MINOR << "." << ZEROTIER_ONE_VERSION_REVISION << std::endl; + std::cout << "(c)2016 ZeroTier, Inc. / Licensed under the GNU GPL v3" << std::endl; + std::cout << std::endl; + std::cout << "Configuration path: " << getSettingsFilePath() << std::endl; + std::cout << std::endl; + std::cout << "Usage: zerotier [-option] [@name] []" << std::endl; + std::cout << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " -v - Verbose JSON output" << std::endl; + std::cout << " " + std::cout << std::endl; + std::cout << "CLI Configuration Commands:" << std::endl; + std::cout << " cli-set - Set a CLI config option" << std::endl; + std::cout << " cli-ls - List configured @things" << std::endl; + std::cout << " cli-rm @name - Remove a configured @thing" << std::endl; + std::cout << " cli-add-zt @name - Add a ZeroTier service" << std::endl; + std::cout << " cli-add-central @name - Add ZeroTier Central instance" << std::endl; + std::cout << std::endl; + std::cout << "ZeroTier Service Commands:" << std::endl; + std::cout << " ls - List currently joined networks" << std::endl; + std::cout << " join [opt=value ...] - Join a network" << std::endl; + std::cout << " leave - Leave a network" << std::endl; + std::cout << " peers - List ZeroTier VL1 peers" << std::endl; + std::cout << " show [] - Get info about self or object" << std::endl; + std::cout << std::endl; + std::cout << "Network Controller Commands:" << std::endl; + std::cout << " net-create - Create a new network" << std::endl; + std::cout << " net-rm - Delete a network (BE CAREFUL!)" << std::endl; + std::cout << " net-ls - List administered networks" << std::endl; + std::cout << " net-members - List members of a network" << std::endl; + std::cout << " net-show [
] - Get network or member info" << std::endl; + std::cout << " net-auth
- Authorize a member" << std::endl; + std::cout << " net-set - See 'net-set help'" << std::endl; + std::cout << std::endl; + std::cout << "Identity Commands:" << std::endl; + std::cout << " id-generate [] - Generate a ZeroTier identity" << std::endl; + std::cout << " id-validate - Locally validate an identity" << std::endl; + std::cout << " id-sign - Sign a file" << std::endl; + std::cout << " id-verify - Verify a file's signature" << std::endl; + std::cout << " id-getpublic - Get full identity's public portion" << std::endl; + std::cout << std::endl; +} + +} // anonymous namespace + +////////////////////////////////////////////////////////////////////////////// + +#ifdef __WINDOWS__ +int _tmain(int argc, _TCHAR* argv[]) +#else +int main(int argc,char **argv) +#endif +{ +#ifdef __WINDOWS__ + { + WSADATA wsaData; + WSAStartup(MAKEWORD(2,2),&wsaData); + } +#endif + + CURL *const curl = curl_easy_init(); + + std::string atname; + std::string command; + std::vector args; + std::map opts; + char nextIsOptValue = 0; + for(int i=1;i