bug fixes

This commit is contained in:
Grant Limberg 2023-04-27 14:31:18 -07:00
commit 474f5b9f4b
No known key found for this signature in database
GPG key ID: 8F2F97D3BE8D7735
4 changed files with 270 additions and 264 deletions

View file

@ -15,9 +15,12 @@
namespace ZeroTier { namespace ZeroTier {
DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener) : DBMirrorSet::DBMirrorSet(DB::ChangeListener *listener)
_listener(listener), : _listener(listener)
_running(true) , _running(true)
, _syncCheckerThread()
, _dbs()
, _dbs_l()
{ {
_syncCheckerThread = std::thread([this]() { _syncCheckerThread = std::thread([this]() {
for(;;) { for(;;) {

View file

@ -554,37 +554,8 @@ void EmbeddedNetworkController::request(
_queue.post(qe); _queue.post(qe);
} }
void EmbeddedNetworkController::configureHTTPControlPlane( std::string EmbeddedNetworkController::networkUpdateFromPostData(uint64_t networkID, const std::string &body)
httplib::Server &s,
const std::function<void(const httplib::Request&, httplib::Response&, std::string)> setContent)
{ {
s.Get("/controller/network", [&](const httplib::Request &req, httplib::Response &res) {
std::set<uint64_t> networkIds;
_db.networks(networkIds);
char tmp[64];
auto out = json::array();
for(std::set<uint64_t>::const_iterator i(networkIds.begin()); i != networkIds.end(); ++i) {
OSUtils::ztsnprintf(tmp, sizeof(tmp), "%.16llx", *i);
out.push_back(tmp);
}
setContent(req, res, out.dump());
});
s.Get("/controller/network/([0-9a-fA-F]{16})", [&](const httplib::Request &req, httplib::Response &res) {
auto networkID = req.matches[1];
uint64_t nwid = Utils::hexStrToU64(networkID.str().c_str());
json network;
if (!_db.get(nwid, network)) {
res.status = 404;
return;
}
setContent(req, res, network.dump());
});
auto _networkUpdateFromPostData = [&](uint64_t networkID, const std::string &body) -> std::string {
json b = OSUtils::jsonParse(body); json b = OSUtils::jsonParse(body);
char nwids[24]; char nwids[24];
@ -837,9 +808,40 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
_db.save(network, true); _db.save(network, true);
return network.dump(); return network.dump();
}; }
void EmbeddedNetworkController::configureHTTPControlPlane(
httplib::Server &s,
const std::function<void(const httplib::Request&, httplib::Response&, std::string)> setContent)
{
s.Get("/controller/network", [&](const httplib::Request &req, httplib::Response &res) {
std::set<uint64_t> networkIds;
_db.networks(networkIds);
char tmp[64];
auto out = json::array();
for(std::set<uint64_t>::const_iterator i(networkIds.begin()); i != networkIds.end(); ++i) {
OSUtils::ztsnprintf(tmp, sizeof(tmp), "%.16llx", *i);
out.push_back(tmp);
}
setContent(req, res, out.dump());
});
s.Get("/controller/network/([0-9a-fA-F]{16})", [&](const httplib::Request &req, httplib::Response &res) {
auto networkID = req.matches[1];
uint64_t nwid = Utils::hexStrToU64(networkID.str().c_str());
json network;
if (!_db.get(nwid, network)) {
res.status = 404;
return;
}
setContent(req, res, network.dump());
});
auto createNewNetwork = [&](const httplib::Request &req, httplib::Response &res) { auto createNewNetwork = [&](const httplib::Request &req, httplib::Response &res) {
fprintf(stderr, "creating new network (new style)\n");
uint64_t nwid = 0; uint64_t nwid = 0;
uint64_t nwidPrefix = (Utils::hexStrToU64(_signingIdAddressString.c_str()) << 24) & 0xffffffffff000000ULL; uint64_t nwidPrefix = (Utils::hexStrToU64(_signingIdAddressString.c_str()) << 24) & 0xffffffffff000000ULL;
uint64_t nwidPostfix = 0; uint64_t nwidPostfix = 0;
@ -857,7 +859,7 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
return; return;
} }
setContent(req, res, _networkUpdateFromPostData(nwid, req.body)); setContent(req, res, networkUpdateFromPostData(nwid, req.body));
}; };
s.Put("/controller/network", createNewNetwork); s.Put("/controller/network", createNewNetwork);
s.Post("/controller/network", createNewNetwork); s.Post("/controller/network", createNewNetwork);
@ -886,7 +888,7 @@ void EmbeddedNetworkController::configureHTTPControlPlane(
res.status = 503; res.status = 503;
return; return;
} }
setContent(req, res, _networkUpdateFromPostData(nwid, req.body)); setContent(req, res, networkUpdateFromPostData(nwid, req.body));
}; };
s.Put("/controller/network/([0-9a-fA-F]{10})______", createNewNetworkOldAndBusted); s.Put("/controller/network/([0-9a-fA-F]{10})______", createNewNetworkOldAndBusted);
s.Post("/controller/network/([0-9a-fA-F]{10})______", createNewNetworkOldAndBusted); s.Post("/controller/network/([0-9a-fA-F]{10})______", createNewNetworkOldAndBusted);

View file

@ -82,6 +82,8 @@ private:
void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData); void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary<ZT_NETWORKCONFIG_METADATA_DICT_CAPACITY> &metaData);
void _startThreads(); void _startThreads();
std::string networkUpdateFromPostData(uint64_t networkID, const std::string &body);
struct _RQEntry struct _RQEntry
{ {
uint64_t nwid; uint64_t nwid;

View file

@ -1002,8 +1002,6 @@ public:
return _termReason; return _termReason;
} }
startHTTPControlPlane();
// Save primary port to a file so CLIs and GUIs can learn it easily // Save primary port to a file so CLIs and GUIs can learn it easily
char portstr[64]; char portstr[64];
OSUtils::ztsnprintf(portstr,sizeof(portstr),"%u",_ports[0]); OSUtils::ztsnprintf(portstr,sizeof(portstr),"%u",_ports[0]);
@ -1056,6 +1054,8 @@ public:
} }
_node->setNetconfMaster((void *)_controller); _node->setNetconfMaster((void *)_controller);
startHTTPControlPlane();
// Join existing networks in networks.d // Join existing networks in networks.d
{ {
std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str())); std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S "networks.d").c_str()));
@ -1971,7 +1971,7 @@ public:
_controlPlane.set_exception_handler([&](const httplib::Request &req, httplib::Response &res, std::exception_ptr ep) { _controlPlane.set_exception_handler([&](const httplib::Request &req, httplib::Response &res, std::exception_ptr ep) {
char buf[1024]; char buf[1024];
auto fmt = "{\"error\": %d, \"description\": \"%\"}"; auto fmt = "{\"error\": %d, \"description\": \"%s\"}";
try { try {
std::rethrow_exception(ep); std::rethrow_exception(ep);
} catch (std::exception &e) { } catch (std::exception &e) {
@ -1984,7 +1984,6 @@ public:
}); });
if (_controller) { if (_controller) {
// TODO: Wire up controller
_controller->configureHTTPControlPlane(_controlPlane, setContent); _controller->configureHTTPControlPlane(_controlPlane, setContent);
} }