mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-06 20:43:44 +02:00
Do not serve controller requests until init is done.
This commit is contained in:
parent
44cb2f4efd
commit
f4feccc626
2 changed files with 21 additions and 14 deletions
|
@ -26,7 +26,8 @@ static const nlohmann::json _EMPTY_JSON(nlohmann::json::object());
|
||||||
static const std::map<std::string,std::string> _ZT_JSONDB_GET_HEADERS;
|
static const std::map<std::string,std::string> _ZT_JSONDB_GET_HEADERS;
|
||||||
|
|
||||||
JSONDB::JSONDB(const std::string &basePath) :
|
JSONDB::JSONDB(const std::string &basePath) :
|
||||||
_basePath(basePath)
|
_basePath(basePath),
|
||||||
|
_ready(false)
|
||||||
{
|
{
|
||||||
if ((_basePath.length() > 7)&&(_basePath.substr(0,7) == "http://")) {
|
if ((_basePath.length() > 7)&&(_basePath.substr(0,7) == "http://")) {
|
||||||
// TODO: this doesn't yet support IPv6 since bracketed address notiation isn't supported.
|
// TODO: this doesn't yet support IPv6 since bracketed address notiation isn't supported.
|
||||||
|
@ -49,7 +50,7 @@ JSONDB::JSONDB(const std::string &basePath) :
|
||||||
OSUtils::mkdir(_basePath.c_str());
|
OSUtils::mkdir(_basePath.c_str());
|
||||||
OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions
|
OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions
|
||||||
}
|
}
|
||||||
_reload(_basePath,std::string());
|
_ready = _reload(_basePath,std::string());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JSONDB::writeRaw(const std::string &n,const std::string &obj)
|
bool JSONDB::writeRaw(const std::string &n,const std::string &obj)
|
||||||
|
@ -83,9 +84,13 @@ bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
|
||||||
|
|
||||||
const nlohmann::json &JSONDB::get(const std::string &n)
|
const nlohmann::json &JSONDB::get(const std::string &n)
|
||||||
{
|
{
|
||||||
|
while (!_ready) {
|
||||||
|
Thread::sleep(250);
|
||||||
|
_ready = _reload(_basePath,std::string());
|
||||||
|
}
|
||||||
|
|
||||||
if (!_isValidObjectName(n))
|
if (!_isValidObjectName(n))
|
||||||
return _EMPTY_JSON;
|
return _EMPTY_JSON;
|
||||||
|
|
||||||
std::map<std::string,_E>::iterator e(_db.find(n));
|
std::map<std::string,_E>::iterator e(_db.find(n));
|
||||||
if (e != _db.end())
|
if (e != _db.end())
|
||||||
return e->second.obj;
|
return e->second.obj;
|
||||||
|
@ -133,7 +138,7 @@ void JSONDB::erase(const std::string &n)
|
||||||
_db.erase(n);
|
_db.erase(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
void JSONDB::_reload(const std::string &p,const std::string &b)
|
bool JSONDB::_reload(const std::string &p,const std::string &b)
|
||||||
{
|
{
|
||||||
if (_httpAddr) {
|
if (_httpAddr) {
|
||||||
std::string body;
|
std::string body;
|
||||||
|
@ -150,11 +155,11 @@ void JSONDB::_reload(const std::string &p,const std::string &b)
|
||||||
_db[tmp].obj = i.value();
|
_db[tmp].obj = i.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
} catch ( ... ) {
|
} catch ( ... ) {} // invalid JSON, so maybe incomplete request
|
||||||
// TODO: report error?
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
std::vector<std::string> dl(OSUtils::listDirectory(p.c_str(),true));
|
std::vector<std::string> dl(OSUtils::listDirectory(p.c_str(),true));
|
||||||
for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
|
for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
|
||||||
|
@ -164,6 +169,7 @@ void JSONDB::_reload(const std::string &p,const std::string &b)
|
||||||
this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
|
this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#include "../ext/json/json.hpp"
|
#include "../ext/json/json.hpp"
|
||||||
#include "../osdep/OSUtils.hpp"
|
#include "../osdep/OSUtils.hpp"
|
||||||
#include "../osdep/Http.hpp"
|
#include "../osdep/Http.hpp"
|
||||||
|
#include "../osdep/Thread.hpp"
|
||||||
|
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
|
@ -47,12 +48,6 @@ class JSONDB
|
||||||
public:
|
public:
|
||||||
JSONDB(const std::string &basePath);
|
JSONDB(const std::string &basePath);
|
||||||
|
|
||||||
inline void reload()
|
|
||||||
{
|
|
||||||
_db.clear();
|
|
||||||
_reload(_basePath,std::string());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool writeRaw(const std::string &n,const std::string &obj);
|
bool writeRaw(const std::string &n,const std::string &obj);
|
||||||
|
|
||||||
bool put(const std::string &n,const nlohmann::json &obj);
|
bool put(const std::string &n,const nlohmann::json &obj);
|
||||||
|
@ -79,6 +74,11 @@ public:
|
||||||
template<typename F>
|
template<typename F>
|
||||||
inline void filter(const std::string &prefix,F func)
|
inline void filter(const std::string &prefix,F func)
|
||||||
{
|
{
|
||||||
|
while (!_ready) {
|
||||||
|
Thread::sleep(250);
|
||||||
|
_ready = _reload(_basePath,std::string());
|
||||||
|
}
|
||||||
|
|
||||||
for(std::map<std::string,_E>::iterator i(_db.lower_bound(prefix));i!=_db.end();) {
|
for(std::map<std::string,_E>::iterator i(_db.lower_bound(prefix));i!=_db.end();) {
|
||||||
if ((i->first.length() >= prefix.length())&&(!memcmp(i->first.data(),prefix.data(),prefix.length()))) {
|
if ((i->first.length() >= prefix.length())&&(!memcmp(i->first.data(),prefix.data(),prefix.length()))) {
|
||||||
if (!func(i->first,get(i->first))) {
|
if (!func(i->first,get(i->first))) {
|
||||||
|
@ -94,7 +94,7 @@ public:
|
||||||
inline bool operator!=(const JSONDB &db) const { return (!(*this == db)); }
|
inline bool operator!=(const JSONDB &db) const { return (!(*this == db)); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void _reload(const std::string &p,const std::string &b);
|
bool _reload(const std::string &p,const std::string &b);
|
||||||
bool _isValidObjectName(const std::string &n);
|
bool _isValidObjectName(const std::string &n);
|
||||||
std::string _genPath(const std::string &n,bool create);
|
std::string _genPath(const std::string &n,bool create);
|
||||||
|
|
||||||
|
@ -108,6 +108,7 @@ private:
|
||||||
InetAddress _httpAddr;
|
InetAddress _httpAddr;
|
||||||
std::string _basePath;
|
std::string _basePath;
|
||||||
std::map<std::string,_E> _db;
|
std::map<std::string,_E> _db;
|
||||||
|
volatile bool _ready;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ZeroTier
|
} // namespace ZeroTier
|
||||||
|
|
Loading…
Add table
Reference in a new issue