EmbeddedNetworkController.hpp 5.8 KB

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