EmbeddedNetworkController.hpp 5.7 KB

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