1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

feat: load bootstrap nodes directly from JSON

This allows us to easily update the list of bootstrap nodes.
This commit is contained in:
sudden6 2019-02-24 21:41:15 +01:00
parent 8fbffa9fe7
commit 1f2bdf3a1b
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
5 changed files with 38 additions and 0 deletions

View File

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

1
res/nodes.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,7 @@
#include "bootstrapnodeupdater.h" #include "bootstrapnodeupdater.h"
#include <QDirIterator>
#include <QFile>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
@ -12,6 +14,7 @@ const QUrl NodeListAddress{"https://nodes.tox.chat/json"};
const QLatin1Literal jsonNodeArrayName{"nodes"}; const QLatin1Literal jsonNodeArrayName{"nodes"};
const QLatin1Literal emptyAddress{"-"}; const QLatin1Literal emptyAddress{"-"};
const QRegularExpression ToxPkRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(64)); const QRegularExpression ToxPkRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(64));
const QLatin1Literal builtinNodesFile{":/conf/nodes.json"};
} // namespace } // namespace
namespace NodeFields { namespace NodeFields {
@ -47,6 +50,29 @@ void BootstrapNodeUpdater::requestBootstrapNodes()
nam.get(request); 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) void BootstrapNodeUpdater::onRequestComplete(QNetworkReply* reply)
{ {
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {

View File

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

View File

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