Browse Source

feat: load bootstrap nodes directly from JSON

This allows us to easily update the list of bootstrap nodes.
reviewable/pr5561/r2
sudden6 6 years ago
parent
commit
1f2bdf3a1b
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
  1. 1
      res.qrc
  2. 1
      res/nodes.json
  3. 26
      src/net/bootstrapnodeupdater.cpp
  4. 1
      src/net/bootstrapnodeupdater.h
  5. 9
      test/net/bsu_test.cpp

1
res.qrc

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
<RCC>
<qresource prefix="/conf">
<file alias="settings.ini">res/settings.ini</file>
<file alias="nodes.json">res/nodes.json</file>
</qresource>
<qresource prefix="/font">
<file alias="DejaVuSans.ttf">res/font/DejaVuSans.ttf</file>

1
res/nodes.json

File diff suppressed because one or more lines are too long

26
src/net/bootstrapnodeupdater.cpp

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
#include "bootstrapnodeupdater.h"
#include <QDirIterator>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
@ -12,6 +14,7 @@ const QUrl NodeListAddress{"https://nodes.tox.chat/json"}; @@ -12,6 +14,7 @@ const QUrl NodeListAddress{"https://nodes.tox.chat/json"};
const QLatin1Literal jsonNodeArrayName{"nodes"};
const QLatin1Literal emptyAddress{"-"};
const QRegularExpression ToxPkRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(64));
const QLatin1Literal builtinNodesFile{":/conf/nodes.json"};
} // namespace
namespace NodeFields {
@ -47,6 +50,29 @@ void BootstrapNodeUpdater::requestBootstrapNodes() @@ -47,6 +50,29 @@ void BootstrapNodeUpdater::requestBootstrapNodes()
nam.get(request);
}
/**
* @brief Loads the list of built in boostrap nodes
* @return List of bootstrapnodes on success, empty list on error
*/
QList<DhtServer> BootstrapNodeUpdater::loadDefaultBootstrapNodes()
{
QFile nodesFile{builtinNodesFile};
if (!nodesFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qWarning() << "Couldn't read bootstrap nodes";
return {};
}
QString nodesJson = nodesFile.readAll();
nodesFile.close();
QJsonDocument d = QJsonDocument::fromJson(nodesJson.toUtf8());
if (d.isNull()) {
qWarning() << "Failed to parse JSON document";
return {};
}
return jsonToNodeList(d);
}
void BootstrapNodeUpdater::onRequestComplete(QNetworkReply* reply)
{
if (reply->error() != QNetworkReply::NoError) {

1
src/net/bootstrapnodeupdater.h

@ -16,6 +16,7 @@ class BootstrapNodeUpdater : public QObject @@ -16,6 +16,7 @@ class BootstrapNodeUpdater : public QObject
public:
explicit BootstrapNodeUpdater(const QNetworkProxy& proxy, QObject* parent = nullptr);
void requestBootstrapNodes();
static QList<DhtServer> loadDefaultBootstrapNodes();
signals:
void availableBootstrapNodes(QList<DhtServer> nodes);

9
test/net/bsu_test.cpp

@ -34,11 +34,14 @@ public: @@ -34,11 +34,14 @@ public:
TestBootstrapNodesUpdater();
private slots:
void testOnline();
void testLocal();
};
TestBootstrapNodesUpdater::TestBootstrapNodesUpdater()
{
qRegisterMetaType<QList<DhtServer>>("QList<DhtServer>");
// Contains the builtin nodes list
Q_INIT_RESOURCE(res);
}
void TestBootstrapNodesUpdater::testOnline()
@ -55,5 +58,11 @@ void TestBootstrapNodesUpdater::testOnline() @@ -55,5 +58,11 @@ void TestBootstrapNodesUpdater::testOnline()
QVERIFY(result.size() > 0); // some data should be returned
}
void TestBootstrapNodesUpdater::testLocal()
{
QList<DhtServer> defaultNodes = BootstrapNodeUpdater::loadDefaultBootstrapNodes();
QVERIFY(defaultNodes.size() > 0);
}
QTEST_GUILESS_MAIN(TestBootstrapNodesUpdater)
#include "bsu_test.moc"

Loading…
Cancel
Save