2
0

EmbeddedNetworkController.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* (c) ZeroTier, Inc.
  2. * See LICENSE.txt in nonfree/
  3. */
  4. #ifndef ZT_SQLITENETWORKCONTROLLER_HPP
  5. #define ZT_SQLITENETWORKCONTROLLER_HPP
  6. #include "../../node/Constants.hpp"
  7. #include "../../node/InetAddress.hpp"
  8. #include "../../node/NetworkController.hpp"
  9. #include "../../osdep/BlockingQueue.hpp"
  10. #include "DB.hpp"
  11. #include "DBMirrorSet.hpp"
  12. #include <cpp-httplib/httplib.h>
  13. #include <nlohmann/json.hpp>
  14. #include <set>
  15. #include <stdint.h>
  16. #include <string>
  17. #include <thread>
  18. #include <unordered_map>
  19. #include <vector>
  20. namespace ZeroTier {
  21. class Node;
  22. struct RedisConfig;
  23. class EmbeddedNetworkController
  24. : public NetworkController
  25. , public DB::ChangeListener {
  26. public:
  27. /**
  28. * @param node Parent node
  29. * @param dbPath Database path (file path or database credentials)
  30. */
  31. EmbeddedNetworkController(Node* node, const char* ztPath, const char* dbPath, int listenPort, RedisConfig* rc);
  32. virtual ~EmbeddedNetworkController();
  33. virtual void init(const Identity& signingId, Sender* sender);
  34. void setSSORedirectURL(const std::string& url);
  35. virtual void request(uint64_t nwid, const InetAddress& fromAddr, uint64_t requestPacketId, const Identity& identity, const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY>& metaData);
  36. void configureHTTPControlPlane(httplib::Server& s, httplib::Server& sV6, const std::function<void(const httplib::Request&, httplib::Response&, std::string)>);
  37. void handleRemoteTrace(const ZT_RemoteTrace& rt);
  38. virtual void onNetworkUpdate(const void* db, uint64_t networkId, const nlohmann::json& network);
  39. virtual void onNetworkMemberUpdate(const void* db, uint64_t networkId, uint64_t memberId, const nlohmann::json& member);
  40. virtual void onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId);
  41. private:
  42. void _request(uint64_t nwid, const InetAddress& fromAddr, uint64_t requestPacketId, const Identity& identity, const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY>& metaData);
  43. void _startThreads();
  44. void _ssoExpiryThread();
  45. std::string networkUpdateFromPostData(uint64_t networkID, const std::string& body);
  46. struct _RQEntry {
  47. uint64_t nwid;
  48. uint64_t requestPacketId;
  49. InetAddress fromAddr;
  50. Identity identity;
  51. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
  52. enum { RQENTRY_TYPE_REQUEST = 0 } type;
  53. };
  54. struct _MemberStatusKey {
  55. _MemberStatusKey() : networkId(0), nodeId(0)
  56. {
  57. }
  58. _MemberStatusKey(const uint64_t nwid, const uint64_t nid) : networkId(nwid), nodeId(nid)
  59. {
  60. }
  61. uint64_t networkId;
  62. uint64_t nodeId;
  63. inline bool operator==(const _MemberStatusKey& k) const
  64. {
  65. return ((k.networkId == networkId) && (k.nodeId == nodeId));
  66. }
  67. inline bool operator<(const _MemberStatusKey& k) const
  68. {
  69. return (k.networkId < networkId) || ((k.networkId == networkId) && (k.nodeId < nodeId));
  70. }
  71. };
  72. struct _MemberStatus {
  73. _MemberStatus() : lastRequestTime(0), authenticationExpiryTime(-1), vMajor(-1), vMinor(-1), vRev(-1), vProto(-1)
  74. {
  75. }
  76. int64_t lastRequestTime;
  77. int64_t authenticationExpiryTime;
  78. int vMajor, vMinor, vRev, vProto;
  79. Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
  80. Identity identity;
  81. inline bool online(const int64_t now) const
  82. {
  83. return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2));
  84. }
  85. };
  86. struct _MemberStatusHash {
  87. inline std::size_t operator()(const _MemberStatusKey& networkIdNodeId) const
  88. {
  89. return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
  90. }
  91. };
  92. const int64_t _startTime;
  93. int _listenPort;
  94. Node* const _node;
  95. std::string _ztPath;
  96. std::string _path;
  97. Identity _signingId;
  98. std::string _signingIdAddressString;
  99. NetworkController::Sender* _sender;
  100. DBMirrorSet _db;
  101. BlockingQueue<_RQEntry*> _queue;
  102. std::vector<std::thread> _threads;
  103. std::mutex _threads_l;
  104. std::unordered_map<_MemberStatusKey, _MemberStatus, _MemberStatusHash> _memberStatus;
  105. std::mutex _memberStatus_l;
  106. std::set<std::pair<int64_t, _MemberStatusKey> > _expiringSoon;
  107. std::mutex _expiringSoon_l;
  108. RedisConfig* _rc;
  109. std::string _ssoRedirectURL;
  110. bool _ssoExpiryRunning;
  111. std::thread _ssoExpiry;
  112. #ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK
  113. prometheus::simpleapi::benchmark_family_t _member_status_lookup;
  114. prometheus::simpleapi::counter_family_t _member_status_lookup_count;
  115. prometheus::simpleapi::benchmark_family_t _node_is_online;
  116. prometheus::simpleapi::counter_family_t _node_is_online_count;
  117. prometheus::simpleapi::benchmark_family_t _get_and_init_member;
  118. prometheus::simpleapi::counter_family_t _get_and_init_member_count;
  119. prometheus::simpleapi::benchmark_family_t _have_identity;
  120. prometheus::simpleapi::counter_family_t _have_identity_count;
  121. prometheus::simpleapi::benchmark_family_t _determine_auth;
  122. prometheus::simpleapi::counter_family_t _determine_auth_count;
  123. prometheus::simpleapi::benchmark_family_t _sso_check;
  124. prometheus::simpleapi::counter_family_t _sso_check_count;
  125. prometheus::simpleapi::benchmark_family_t _auth_check;
  126. prometheus::simpleapi::counter_family_t _auth_check_count;
  127. prometheus::simpleapi::benchmark_family_t _json_schlep;
  128. prometheus::simpleapi::counter_family_t _json_schlep_count;
  129. prometheus::simpleapi::benchmark_family_t _issue_certificate;
  130. prometheus::simpleapi::counter_family_t _issue_certificate_count;
  131. prometheus::simpleapi::benchmark_family_t _save_member;
  132. prometheus::simpleapi::counter_family_t _save_member_count;
  133. prometheus::simpleapi::benchmark_family_t _send_netconf;
  134. prometheus::simpleapi::counter_family_t _send_netconf_count;
  135. #endif
  136. };
  137. } // namespace ZeroTier
  138. #endif