EmbeddedNetworkController.hpp 5.9 KB

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