Files

165 lines
5.4 KiB
C++
Raw Permalink Normal View History

/* (c) ZeroTier, Inc.
* See LICENSE.txt in nonfree/
*/
#ifndef ZT_SQLITENETWORKCONTROLLER_HPP
#define ZT_SQLITENETWORKCONTROLLER_HPP
#include "../../node/Constants.hpp"
#include "../../node/InetAddress.hpp"
#include "../../node/NetworkController.hpp"
#include "../../osdep/BlockingQueue.hpp"
#include "DB.hpp"
2019-08-06 11:00:35 -05:00
#include "DBMirrorSet.hpp"
2025-07-03 11:26:23 -04:00
#include <cpp-httplib/httplib.h>
#include <nlohmann/json.hpp>
#include <set>
#include <stdint.h>
#include <string>
#include <thread>
#include <unordered_map>
#include <vector>
namespace ZeroTier {
2015-10-06 15:56:18 -07:00
class Node;
2020-05-11 15:03:56 -07:00
struct RedisConfig;
2015-10-06 15:56:18 -07:00
2025-07-03 11:26:23 -04:00
class EmbeddedNetworkController
: public NetworkController
, public DB::ChangeListener {
public:
/**
* @param node Parent node
2017-11-03 11:39:27 -07:00
* @param dbPath Database path (file path or database credentials)
*/
2025-07-03 11:26:23 -04:00
EmbeddedNetworkController(Node* node, const char* ztPath, const char* dbPath, int listenPort, RedisConfig* rc);
virtual ~EmbeddedNetworkController();
2025-07-03 11:26:23 -04:00
virtual void init(const Identity& signingId, Sender* sender);
2025-07-03 11:26:23 -04:00
void setSSORedirectURL(const std::string& url);
2021-06-04 16:29:03 -07:00
2025-07-03 11:26:23 -04:00
virtual void request(uint64_t nwid, const InetAddress& fromAddr, uint64_t requestPacketId, const Identity& identity, const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY>& metaData);
2025-07-03 11:26:23 -04:00
void configureHTTPControlPlane(httplib::Server& s, httplib::Server& sV6, const std::function<void(const httplib::Request&, httplib::Response&, std::string)>);
2015-04-21 16:41:35 -07:00
2025-07-03 11:26:23 -04:00
void handleRemoteTrace(const ZT_RemoteTrace& rt);
2025-07-03 11:26:23 -04:00
virtual void onNetworkUpdate(const void* db, uint64_t networkId, const nlohmann::json& network);
virtual void onNetworkMemberUpdate(const void* db, uint64_t networkId, uint64_t memberId, const nlohmann::json& member);
virtual void onNetworkMemberDeauthorize(const void* db, uint64_t networkId, uint64_t memberId);
2025-07-03 11:26:23 -04:00
private:
void _request(uint64_t nwid, const InetAddress& fromAddr, uint64_t requestPacketId, const Identity& identity, const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY>& metaData);
2017-08-31 18:01:21 -04:00
void _startThreads();
void _ssoExpiryThread();
2025-07-03 11:26:23 -04:00
std::string networkUpdateFromPostData(uint64_t networkID, const std::string& body);
2025-07-03 11:26:23 -04:00
struct _RQEntry {
2017-11-03 11:39:27 -07:00
uint64_t nwid;
uint64_t requestPacketId;
InetAddress fromAddr;
Identity identity;
Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> metaData;
2025-07-03 11:26:23 -04:00
enum { RQENTRY_TYPE_REQUEST = 0 } type;
2017-11-03 11:39:27 -07:00
};
2025-07-03 11:26:23 -04:00
struct _MemberStatusKey {
_MemberStatusKey() : networkId(0), nodeId(0)
{
}
_MemberStatusKey(const uint64_t nwid, const uint64_t nid) : networkId(nwid), nodeId(nid)
{
}
uint64_t networkId;
uint64_t nodeId;
2025-07-03 11:26:23 -04:00
inline bool operator==(const _MemberStatusKey& k) const
{
return ((k.networkId == networkId) && (k.nodeId == nodeId));
}
inline bool operator<(const _MemberStatusKey& k) const
{
return (k.networkId < networkId) || ((k.networkId == networkId) && (k.nodeId < nodeId));
}
};
2025-07-03 11:26:23 -04:00
struct _MemberStatus {
_MemberStatus() : lastRequestTime(0), authenticationExpiryTime(-1), vMajor(-1), vMinor(-1), vRev(-1), vProto(-1)
{
}
int64_t lastRequestTime;
int64_t authenticationExpiryTime;
2025-07-03 11:26:23 -04:00
int vMajor, vMinor, vRev, vProto;
Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> lastRequestMetaData;
Identity identity;
2025-07-03 11:26:23 -04:00
inline bool online(const int64_t now) const
{
return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2));
}
};
2025-07-03 11:26:23 -04:00
struct _MemberStatusHash {
inline std::size_t operator()(const _MemberStatusKey& networkIdNodeId) const
{
return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId);
}
};
2017-11-03 11:39:27 -07:00
const int64_t _startTime;
2019-01-21 11:18:20 -08:00
int _listenPort;
2025-07-03 11:26:23 -04:00
Node* const _node;
std::string _ztPath;
2017-11-03 11:39:27 -07:00
std::string _path;
Identity _signingId;
std::string _signingIdAddressString;
2025-07-03 11:26:23 -04:00
NetworkController::Sender* _sender;
2019-08-06 11:00:35 -05:00
DBMirrorSet _db;
2025-07-03 11:26:23 -04:00
BlockingQueue<_RQEntry*> _queue;
2017-11-03 11:39:27 -07:00
std::vector<std::thread> _threads;
std::mutex _threads_l;
2025-07-03 11:26:23 -04:00
std::unordered_map<_MemberStatusKey, _MemberStatus, _MemberStatusHash> _memberStatus;
2017-11-03 11:39:27 -07:00
std::mutex _memberStatus_l;
2020-05-11 15:03:56 -07:00
2025-07-03 11:26:23 -04:00
std::set<std::pair<int64_t, _MemberStatusKey> > _expiringSoon;
std::mutex _expiringSoon_l;
2025-07-03 11:26:23 -04:00
RedisConfig* _rc;
2021-06-04 16:29:03 -07:00
std::string _ssoRedirectURL;
bool _ssoExpiryRunning;
std::thread _ssoExpiry;
#ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK
prometheus::simpleapi::benchmark_family_t _member_status_lookup;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _member_status_lookup_count;
prometheus::simpleapi::benchmark_family_t _node_is_online;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _node_is_online_count;
prometheus::simpleapi::benchmark_family_t _get_and_init_member;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _get_and_init_member_count;
prometheus::simpleapi::benchmark_family_t _have_identity;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _have_identity_count;
prometheus::simpleapi::benchmark_family_t _determine_auth;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _determine_auth_count;
prometheus::simpleapi::benchmark_family_t _sso_check;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _sso_check_count;
prometheus::simpleapi::benchmark_family_t _auth_check;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _auth_check_count;
prometheus::simpleapi::benchmark_family_t _json_schlep;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _json_schlep_count;
prometheus::simpleapi::benchmark_family_t _issue_certificate;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _issue_certificate_count;
prometheus::simpleapi::benchmark_family_t _save_member;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _save_member_count;
prometheus::simpleapi::benchmark_family_t _send_netconf;
2025-07-03 11:26:23 -04:00
prometheus::simpleapi::counter_family_t _send_netconf_count;
#endif
};
2025-07-03 11:26:23 -04:00
} // namespace ZeroTier
#endif