node_state_sections.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "diagnostic/node_state_sections.hpp"
  2. #include "osdep/Http.hpp"
  3. #include "osdep/OSUtils.hpp"
  4. #include <string>
  5. #include <map>
  6. void addNodeStateStatusJson(nlohmann::json& j, const ZeroTier::InetAddress& addr, std::map<std::string,std::string>& requestHeaders) {
  7. try {
  8. std::map<std::string, std::string> responseHeaders;
  9. std::string responseBody;
  10. unsigned int scode = ZeroTier::Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/status",requestHeaders,responseHeaders,responseBody);
  11. if (scode == 200) {
  12. try {
  13. nlohmann::json status_json = ZeroTier::OSUtils::jsonParse(responseBody);
  14. j["status"] = status_json;
  15. if (status_json.contains("address")) {
  16. j["nodeId"] = status_json["address"];
  17. } else {
  18. j["nodeId"] = nullptr;
  19. }
  20. } catch (const std::exception& e) {
  21. j["status"] = { {"error", std::string("JSON parse error: ") + e.what()} };
  22. j["nodeId"] = nullptr;
  23. } catch (...) {
  24. j["status"] = { {"error", "Unknown JSON parse error"} };
  25. j["nodeId"] = nullptr;
  26. }
  27. } else {
  28. j["status"] = { {"error", std::string("HTTP error ") + std::to_string(scode) + ": " + responseBody} };
  29. j["nodeId"] = nullptr;
  30. }
  31. } catch (const std::exception& e) {
  32. j["status"] = { {"error", std::string("Exception: ") + e.what()} };
  33. j["nodeId"] = nullptr;
  34. } catch (...) {
  35. j["status"] = { {"error", "Unknown error retrieving /status"} };
  36. j["nodeId"] = nullptr;
  37. }
  38. }
  39. void addNodeStateNetworksJson(nlohmann::json& j, const ZeroTier::InetAddress& addr, std::map<std::string,std::string>& requestHeaders) {
  40. try {
  41. std::map<std::string, std::string> responseHeaders;
  42. std::string responseBody;
  43. unsigned int scode = ZeroTier::Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/network",requestHeaders,responseHeaders,responseBody);
  44. if (scode == 200) {
  45. try {
  46. j["networks"] = ZeroTier::OSUtils::jsonParse(responseBody);
  47. } catch (...) {
  48. j["networks"] = responseBody;
  49. }
  50. } else {
  51. j["networks_error"] = responseBody;
  52. }
  53. } catch (const std::exception& e) {
  54. j["networks_error"] = std::string("Exception: ") + e.what();
  55. } catch (...) {
  56. j["networks_error"] = "Unknown error retrieving /network";
  57. }
  58. }
  59. void addNodeStatePeersJson(nlohmann::json& j, const ZeroTier::InetAddress& addr, std::map<std::string,std::string>& requestHeaders) {
  60. try {
  61. std::map<std::string, std::string> responseHeaders;
  62. std::string responseBody;
  63. unsigned int scode = ZeroTier::Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/peer",requestHeaders,responseHeaders,responseBody);
  64. if (scode == 200) {
  65. try {
  66. j["peers"] = ZeroTier::OSUtils::jsonParse(responseBody);
  67. } catch (...) {
  68. j["peers"] = responseBody;
  69. }
  70. } else {
  71. j["peers_error"] = responseBody;
  72. }
  73. } catch (const std::exception& e) {
  74. j["peers_error"] = std::string("Exception: ") + e.what();
  75. } catch (...) {
  76. j["peers_error"] = "Unknown error retrieving /peer";
  77. }
  78. }
  79. void addNodeStateLocalConfJson(nlohmann::json& j, const std::string& homeDir) {
  80. try {
  81. std::string localConf;
  82. ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "local.conf").c_str(), localConf);
  83. if (localConf.empty()) {
  84. j["local_conf"] = nullptr;
  85. } else {
  86. j["local_conf"] = localConf;
  87. }
  88. } catch (const std::exception& e) {
  89. j["local_conf"] = std::string("Exception: ") + e.what();
  90. } catch (...) {
  91. j["local_conf"] = "Unknown error retrieving local.conf";
  92. }
  93. }