zerotier.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5. #include "../node/Constants.hpp"
  6. #include "../version.h"
  7. #include "../osdep/OSUtils.hpp"
  8. #include "../ext/json/json.hpp"
  9. #ifdef __WINDOWS__
  10. #include <WinSock2.h>
  11. #include <windows.h>
  12. #include <tchar.h>
  13. #include <wchar.h>
  14. #else
  15. #include <ctype.h>
  16. #include <unistd.h>
  17. #endif
  18. #include <iostream>
  19. #include <string>
  20. #include <map>
  21. #include <vector>
  22. #include <curl/curl.h>
  23. using json = nlohmann::json;
  24. using OSUtils = ZeroTier::OSUtils;
  25. namespace {
  26. static inline std::string getSettingsFilePath()
  27. {
  28. #ifdef __WINDOWS__
  29. #else
  30. const char *home = getenv("HOME");
  31. if (!home)
  32. home = "/";
  33. return (std::string(home) + "/.zerotierCliSettings");
  34. #endif
  35. }
  36. static json loadSettings()
  37. {
  38. json settings;
  39. std::string buf;
  40. if (OSUtils::readFile(getSettingsFilePath().c_str(),buf))
  41. settings = json::parse(buf);
  42. return settings;
  43. }
  44. static bool saveSettings(const json &settings)
  45. {
  46. std::string sfp(getSettingsFilePath().c_str());
  47. std::string buf(settings.dump(2));
  48. if (OSUtils::writeFile(sfp.c_str(),buf)) {
  49. OSUtils::lockDownFile(sfp.c_str(),false);
  50. return true;
  51. }
  52. return false;
  53. }
  54. static void dumpHelp()
  55. {
  56. std::cout << "ZeroTier Newer-Spiffier CLI " << ZEROTIER_ONE_VERSION_MAJOR << "." << ZEROTIER_ONE_VERSION_MINOR << "." << ZEROTIER_ONE_VERSION_REVISION << std::endl;
  57. std::cout << "(c)2016 ZeroTier, Inc. / Licensed under the GNU GPL v3" << std::endl;
  58. std::cout << std::endl;
  59. std::cout << "Configuration path: " << getSettingsFilePath() << std::endl;
  60. std::cout << std::endl;
  61. std::cout << "Usage: zerotier [-option] [@name] <command> [<command options>]" << std::endl;
  62. std::cout << std::endl;
  63. std::cout << "Options:" << std::endl;
  64. std::cout << " -v - Verbose JSON output" << std::endl;
  65. std::cout << " "
  66. std::cout << std::endl;
  67. std::cout << "CLI Configuration Commands:" << std::endl;
  68. std::cout << " cli-set <setting> <value> - Set a CLI config option" << std::endl;
  69. std::cout << " cli-ls - List configured @things" << std::endl;
  70. std::cout << " cli-rm @name - Remove a configured @thing" << std::endl;
  71. std::cout << " cli-add-zt @name <url> <auth> - Add a ZeroTier service" << std::endl;
  72. std::cout << " cli-add-central @name <url> <auth> - Add ZeroTier Central instance" << std::endl;
  73. std::cout << std::endl;
  74. std::cout << "ZeroTier Service Commands:" << std::endl;
  75. std::cout << " ls - List currently joined networks" << std::endl;
  76. std::cout << " join <network> [opt=value ...] - Join a network" << std::endl;
  77. std::cout << " leave <network> - Leave a network" << std::endl;
  78. std::cout << " peers - List ZeroTier VL1 peers" << std::endl;
  79. std::cout << " show [<network/peer address>] - Get info about self or object" << std::endl;
  80. std::cout << std::endl;
  81. std::cout << "Network Controller Commands:" << std::endl;
  82. std::cout << " net-create - Create a new network" << std::endl;
  83. std::cout << " net-rm <network> - Delete a network (BE CAREFUL!)" << std::endl;
  84. std::cout << " net-ls - List administered networks" << std::endl;
  85. std::cout << " net-members <network> - List members of a network" << std::endl;
  86. std::cout << " net-show <network> [<address>] - Get network or member info" << std::endl;
  87. std::cout << " net-auth <network> <address> - Authorize a member" << std::endl;
  88. std::cout << " net-set <path> <value> - See 'net-set help'" << std::endl;
  89. std::cout << std::endl;
  90. std::cout << "Identity Commands:" << std::endl;
  91. std::cout << " id-generate [<vanity prefix>] - Generate a ZeroTier identity" << std::endl;
  92. std::cout << " id-validate <identity> - Locally validate an identity" << std::endl;
  93. std::cout << " id-sign <identity> <file> - Sign a file" << std::endl;
  94. std::cout << " id-verify <secret> <file> <sig> - Verify a file's signature" << std::endl;
  95. std::cout << " id-getpublic <secret> - Get full identity's public portion" << std::endl;
  96. std::cout << std::endl;
  97. }
  98. } // anonymous namespace
  99. //////////////////////////////////////////////////////////////////////////////
  100. #ifdef __WINDOWS__
  101. int _tmain(int argc, _TCHAR* argv[])
  102. #else
  103. int main(int argc,char **argv)
  104. #endif
  105. {
  106. #ifdef __WINDOWS__
  107. {
  108. WSADATA wsaData;
  109. WSAStartup(MAKEWORD(2,2),&wsaData);
  110. }
  111. #endif
  112. CURL *const curl = curl_easy_init();
  113. std::string atname;
  114. std::string command;
  115. std::vector<std::string> args;
  116. std::map<char,std::string> opts;
  117. char nextIsOptValue = 0;
  118. for(int i=1;i<argc;++i) {
  119. if ((i == 1)&&(argv[i][0] == '@')) {
  120. atname = argv[i];
  121. } else if (nextIsOptValue) {
  122. opts[nextIsOptValue] = argv[i];
  123. nextIsOptValue = 0;
  124. } else if (command.length() == 0) {
  125. if (argv[i][0] == '-') {
  126. if (!argv[i][1]) {
  127. dumpHelp();
  128. return -1;
  129. } else if (argv[i][2]) {
  130. opts[argv[i][1]] = argv[i] + 2;
  131. } else {
  132. nextIsOptValue = argv[i][1];
  133. }
  134. } else {
  135. command = argv[i];
  136. }
  137. } else {
  138. args.push_back(std::string(argv[i]));
  139. }
  140. }
  141. if ((command.length() == 0)||(command == "help")) {
  142. dumpHelp();
  143. return -1;
  144. } else if (command == "cli-set") {
  145. } else if (command == "cli-ls") {
  146. } else if (command == "cli-rm") {
  147. } else if (command == "cli-add-zt") {
  148. } else if (command == "cli-add-central") {
  149. } else if (command == "ls") {
  150. } else if (command == "join") {
  151. } else if (command == "leave") {
  152. } else if (command == "peers") {
  153. } else if (command == "show") {
  154. } else if (command == "net-create") {
  155. } else if (command == "net-rm") {
  156. } else if (command == "net-ls") {
  157. } else if (command == "net-members") {
  158. } else if (command == "net-show") {
  159. } else if (command == "net-auth") {
  160. } else if (command == "net-set") {
  161. } else if (command == "id-generate") {
  162. } else if (command == "id-validate") {
  163. } else if (command == "id-sign") {
  164. } else if (command == "id-verify") {
  165. } else if (command == "id-getpublic") {
  166. } else {
  167. dumpHelp();
  168. return -1;
  169. }
  170. curl_easy_cleanup(curl);
  171. return 0;
  172. }