CV1.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "DB.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #ifndef ZT_CONTROLLER_CV1_HPP
  16. #define ZT_CONTROLLER_CV1_HPP
  17. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  18. #include "../node/Metrics.hpp"
  19. #include "ConnectionPool.hpp"
  20. #include "PostgreSQL.hpp"
  21. #include <memory>
  22. #include <pqxx/pqxx>
  23. #include <redis++/redis++.h>
  24. namespace smeeclient {
  25. struct SmeeClient;
  26. }
  27. namespace ZeroTier {
  28. struct RedisConfig;
  29. /**
  30. * A controller database driver that talks to PostgreSQL
  31. *
  32. * This is for use with ZeroTier Central. Others are free to build and use it
  33. * but be aware that we might change it at any time.
  34. */
  35. class CV1 : public DB {
  36. friend class MemberNotificationReceiver<CV1>;
  37. friend class NetworkNotificationReceiver<CV1>;
  38. public:
  39. CV1(const Identity& myId, const char* path, int listenPort, RedisConfig* rc);
  40. virtual ~CV1();
  41. virtual bool waitForReady();
  42. virtual bool isReady();
  43. virtual bool save(nlohmann::json& record, bool notifyListeners);
  44. virtual void eraseNetwork(const uint64_t networkId);
  45. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  46. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress);
  47. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress, const char* osArch);
  48. virtual AuthInfo getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL);
  49. virtual bool ready()
  50. {
  51. return _ready == 2;
  52. }
  53. protected:
  54. struct _PairHasher {
  55. inline std::size_t operator()(const std::pair<uint64_t, uint64_t>& p) const
  56. {
  57. return (std::size_t)(p.first ^ p.second);
  58. }
  59. };
  60. virtual void _memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners)
  61. {
  62. DB::_memberChanged(old, memberConfig, notifyListeners);
  63. }
  64. virtual void _networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners)
  65. {
  66. DB::_networkChanged(old, networkConfig, notifyListeners);
  67. }
  68. private:
  69. void initializeNetworks();
  70. void initializeMembers();
  71. void heartbeat();
  72. void membersDbWatcher();
  73. void _membersWatcher_Postgres();
  74. void networksDbWatcher();
  75. void _networksWatcher_Postgres();
  76. void _membersWatcher_Redis();
  77. void _networksWatcher_Redis();
  78. void commitThread();
  79. void onlineNotificationThread();
  80. void onlineNotification_Postgres();
  81. void onlineNotification_Redis();
  82. uint64_t _doRedisUpdate(sw::redis::Transaction& tx, std::string& controllerId, std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher>& lastOnline);
  83. void configureSmee();
  84. void notifyNewMember(const std::string& networkID, const std::string& memberID);
  85. enum OverrideMode { ALLOW_PGBOUNCER_OVERRIDE = 0, NO_OVERRIDE = 1 };
  86. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  87. const Identity _myId;
  88. const Address _myAddress;
  89. std::string _myAddressStr;
  90. std::string _connString;
  91. BlockingQueue<std::pair<nlohmann::json, bool> > _commitQueue;
  92. std::thread _heartbeatThread;
  93. std::thread _membersDbWatcher;
  94. std::thread _networksDbWatcher;
  95. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  96. std::thread _onlineNotificationThread;
  97. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher> _lastOnline;
  98. mutable std::mutex _lastOnline_l;
  99. mutable std::mutex _readyLock;
  100. std::atomic<int> _ready, _connected, _run;
  101. mutable volatile bool _waitNoticePrinted;
  102. int _listenPort;
  103. uint8_t _ssoPsk[48];
  104. RedisConfig* _rc;
  105. std::shared_ptr<sw::redis::Redis> _redis;
  106. std::shared_ptr<sw::redis::RedisCluster> _cluster;
  107. bool _redisMemberStatus;
  108. smeeclient::SmeeClient* _smee;
  109. };
  110. } // namespace ZeroTier
  111. #endif // ZT_CONTROLLER_CV1_HPP
  112. #endif // ZT_CONTROLLER_USE_LIBPQ