cli.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifndef __WINDOWS__
  31. #include <unistd.h>
  32. #endif
  33. #include "node/Node.hpp"
  34. #include "node/Constants.hpp"
  35. #include "node/Utils.hpp"
  36. #include "node/Thread.hpp"
  37. using namespace ZeroTier;
  38. static void printHelp(FILE *out,const char *exename)
  39. {
  40. fprintf(out,"Usage: %s [-switches] <command>"ZT_EOL_S,exename);
  41. fprintf(out,ZT_EOL_S);
  42. fprintf(out,"Switches:"ZT_EOL_S);
  43. fprintf(out," -t<token> - Specify token on command line"ZT_EOL_S);
  44. fprintf(out," -T<file> - Read token from file"ZT_EOL_S);
  45. fprintf(out,ZT_EOL_S);
  46. fprintf(out,"Use the 'help' command to get help from ZeroTier One itself."ZT_EOL_S);
  47. }
  48. static volatile uint64_t lastResultTime = 0ULL;
  49. static volatile unsigned int numResults = 0;
  50. static void resultHandler(void *arg,unsigned long id,const char *line)
  51. {
  52. lastResultTime = Utils::now();
  53. ++numResults;
  54. fprintf(stdout,"%s"ZT_EOL_S,line);
  55. }
  56. int main(int argc,char **argv)
  57. {
  58. if (argc <= 1) {
  59. printHelp(stdout,argv[0]);
  60. return -1;
  61. }
  62. std::string authToken;
  63. std::string command;
  64. bool pastSwitches = false;
  65. for(int i=1;i<argc;++i) {
  66. if ((argv[i][0] == '-')&&(!pastSwitches)) {
  67. if (strlen(argv[i]) <= 1) {
  68. printHelp(stdout,argv[0]);
  69. return -1;
  70. }
  71. switch(argv[i][1]) {
  72. case 't':
  73. authToken.assign(argv[i] + 2);
  74. break;
  75. case 'T':
  76. if (!Utils::readFile(argv[i] + 2,authToken)) {
  77. fprintf(stdout,"FATAL ERROR: unable to read token from '%s'"ZT_EOL_S,argv[i] + 2);
  78. return -2;
  79. }
  80. break;
  81. default:
  82. return -1;
  83. }
  84. } else {
  85. pastSwitches = true;
  86. if (command.length())
  87. command.push_back(' ');
  88. command.append(argv[i]);
  89. }
  90. }
  91. if (!authToken.length()) {
  92. const char *home = getenv("HOME");
  93. if (home) {
  94. std::string dotZeroTierAuthToken(home);
  95. dotZeroTierAuthToken.push_back(ZT_PATH_SEPARATOR);
  96. dotZeroTierAuthToken.append(".zerotierOneAuthToken");
  97. if (!Utils::readFile(dotZeroTierAuthToken.c_str(),authToken)) {
  98. fprintf(stdout,"FATAL ERROR: no token specified on command line and could not read '%s'"ZT_EOL_S,dotZeroTierAuthToken.c_str());
  99. return -2;
  100. }
  101. }
  102. }
  103. if (!authToken.length()) {
  104. fprintf(stdout,"FATAL ERROR: could not find auth token"ZT_EOL_S);
  105. return -2;
  106. }
  107. Node::LocalClient client(authToken.c_str(),&resultHandler,(void *)0);
  108. client.send(command.c_str());
  109. lastResultTime = Utils::now();
  110. while ((Utils::now() - lastResultTime) < 300)
  111. Thread<void>::sleep(50);
  112. if (!numResults) {
  113. fprintf(stdout,"ERROR: no results received. Is ZeroTier One running?"ZT_EOL_S);
  114. return -1;
  115. }
  116. return 0;
  117. }