EmbeddedNetworkController.hpp 6.0 KB

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