EmbeddedNetworkController.hpp 5.9 KB

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