diff --git a/core/DapSession.cpp b/core/DapSession.cpp index 868a84cbd76d5e01c1a79b4b611ee4f43b488044..9895a7dfed4e0869f3e565822a4c65014938cfeb 100755 --- a/core/DapSession.cpp +++ b/core/DapSession.cpp @@ -43,6 +43,7 @@ const QString DapSession::URL_DB_FILE("/db_file"); const QString DapSession::URL_SERVER_LIST("/nodelist"); const QString DapSession::URL_TX("/tx"); const QString DapSession::URL_BUG_REPORT("/bugreport"); +const QString DapSession::URL_NEWS("/news"); #define SESSION_KEY_ID_LEN 33 @@ -134,6 +135,11 @@ void DapSession::sendBugReport(const QByteArray &data) m_netSendBugReportReply = encRequestRaw(data, URL_BUG_REPORT, QString(), QString(), SLOT(answerBugReport())); } +void DapSession::getNews() +{ + m_netNewsReply = encRequest(nullptr, URL_NEWS, QString(), QString(), SLOT(answerNews()), true); +} + /** * @brief DapSession::onEnc */ @@ -466,6 +472,29 @@ void DapSession::answerBugReport() emit receivedBugReportNumber(bugReportNumber); } +void DapSession::answerNews() +{ + qInfo() << "answerNews"; + + QByteArray arrData(m_netNewsReply->readAll()); + QJsonParseError jsonErr; + QJsonDocument jsonDoc = QJsonDocument::fromJson(arrData, &jsonErr); + + if(!jsonDoc.isNull()) { + if(!jsonDoc.isArray()) { + qCritical() << "Error parse response. Must be array"; +// emit sigParseResponseError(); + return; + } + emit sigReceivedNewsMessage(jsonDoc); + } else { + qWarning() << "Server response:" << arrData; + qCritical() << "Can't parse server response to JSON: "<<jsonErr.errorString()<< " on position "<< jsonErr.offset ; +// emit sigParseResponseError(); + return; + } +} + void DapSession::clearCredentials() { qDebug() << "clearCredentials()"; diff --git a/core/DapSession.h b/core/DapSession.h index 9f95d31a8db2c0655ee1fcdd95dfa8d846be26b8..4ae06e7bc9176639d8562f2c3baae05779b0b011 100644 --- a/core/DapSession.h +++ b/core/DapSession.h @@ -47,6 +47,7 @@ public: static const QString URL_SERVER_LIST; static const QString URL_TX; static const QString URL_BUG_REPORT; + static const QString URL_NEWS; DapSession(QObject * obj = Q_NULLPTR, int requestTimeout = DEFAULT_REQUEST_TIMEOUT); ~DapSession(); @@ -86,6 +87,7 @@ public slots: QNetworkReply * streamOpenRequest(const QString& subUrl, const QString& query); void sendBugReport(const QByteArray &data); + void getNews(); void abortEncryptionInitRequest() { m_netEncryptReply->abort(); } void abortAuthorizeRequest() { m_netAuthorizeReply->abort(); } @@ -107,6 +109,7 @@ protected: QNetworkReply * m_netAuthorizeReply; QNetworkReply * m_netLogoutReply; QNetworkReply * m_netSendBugReportReply; + QNetworkReply * m_netNewsReply; QMap<QString,QString> m_userInform; @@ -147,6 +150,7 @@ private slots: void onKeyActivated(); void onLogout(); void answerBugReport(); + void answerNews(); signals: void pubKeyRequested(); void pubKeyServerRecived(); @@ -170,6 +174,7 @@ signals: void logouted(); void receivedBugReportNumber(const QString& bugReportNumber); + void sigReceivedNewsMessage(const QJsonDocument& news); }; diff --git a/ui/auxiliary/Utilz.cpp b/ui/auxiliary/Utilz.cpp index 2cebacf96877a87e17dd83051374c9c0d50dfb8a..16bc7b095d6a2b9c538534eea37fc9a30ff74e00 100644 --- a/ui/auxiliary/Utilz.cpp +++ b/ui/auxiliary/Utilz.cpp @@ -14,6 +14,36 @@ namespace Utils return QRegularExpression("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b", QRegularExpression::CaseInsensitiveOption) ; } + + int getNumberVersion(const QString &version,TypeVersion type) + { + int minVersion(0); + int majVersion(0); + int patVersion(0); + + QStringList versionList = version.split("."); + if(!versionList[0].isEmpty()) + majVersion = versionList[0].toInt(); + if(versionList.count()>1) + { + QStringList minVersionList = versionList[1].split("-"); + if(!minVersionList[0].isEmpty()) + minVersion = minVersionList[0].toInt(); + if(minVersionList.count()>1) + if(!minVersionList[1].isEmpty()) + patVersion = minVersionList[1].toInt(); + } + + if(type == TypeVersion::MAJOR) + return majVersion; + if(type == TypeVersion::MINOR) + return minVersion; + if(type == TypeVersion::PAT) + return patVersion; + + return 0; + } + QColor toColor(const QString &strRGBA) { QString strColor(strRGBA.simplified()); diff --git a/ui/auxiliary/Utilz.h b/ui/auxiliary/Utilz.h index 8a05fc5f919d1c5c2d4f6dab37dde640fa7bb38e..d61e79305780371fb714f1916b2b930644d6a466 100644 --- a/ui/auxiliary/Utilz.h +++ b/ui/auxiliary/Utilz.h @@ -15,6 +15,10 @@ namespace Utils ///Returns a regular expression for the email match. QRegularExpression regularEmail(); + ///Returns the required version type + enum TypeVersion{MAJOR,MINOR,PAT}; + int getNumberVersion(const QString &version,TypeVersion type); + QColor toColor(const QString &strRGBA); ///The function returns the first integer in the string. /// 100%=>100 100px=>100 100**=>100 100=>100 diff --git a/vpn/client/DapCmdHandlers/DapCmdNews.cpp b/vpn/client/DapCmdHandlers/DapCmdNews.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c80492311de723f9b9e9b932b4f9e02a0fe3d919 --- /dev/null +++ b/vpn/client/DapCmdHandlers/DapCmdNews.cpp @@ -0,0 +1,29 @@ +#include "DapCmdNews.h" +#include "DapJsonParams.h" +#include <QJsonArray> + +DapCmdNews::DapCmdNews(QObject *parent) + : DapCmdServiceAbstract(DapJsonCmdType::GET_NEWS, parent) +{ + +} + +void DapCmdNews::handle(const QJsonObject* params) +{ + Q_UNUSED(params); + + connect(this, &DapCmdNews::sigReceivedNewsMessage, [=](const QJsonDocument& news){ + + auto arr = news.array(); + if (arr.isEmpty()) { + sendSimpleError(-4563, "Empty news list"); + } else { + QJsonObject obj; + obj["news_list"] = arr; + sendCmd(&obj); + } + }); + + //if (params->value("message").toString() == "news") + emit getNews(); +} diff --git a/vpn/client/DapCmdHandlers/DapCmdNews.h b/vpn/client/DapCmdHandlers/DapCmdNews.h new file mode 100644 index 0000000000000000000000000000000000000000..48e15eed577abe99c84c6272d1f0d35a63a069e0 --- /dev/null +++ b/vpn/client/DapCmdHandlers/DapCmdNews.h @@ -0,0 +1,19 @@ +#ifndef DAPCMDNEWS_H +#define DAPCMDNEWS_H + +#include "DapCmdServiceAbstract.h" + +class DapCmdNews: public DapCmdServiceAbstract +{ + Q_OBJECT +public: + + DapCmdNews(QObject *parent = nullptr); + void handle(const QJsonObject *params) override; + +signals: + void sigReceivedNewsMessage(const QJsonDocument& news); + void getNews(); +}; + +#endif // DAPCMDNEWS_H diff --git a/vpn/client/DapCmdHandlers/dap-cmd-handlers.pri b/vpn/client/DapCmdHandlers/dap-cmd-handlers.pri index aa51dbeacb888532d6bb268a792bb63539cbd6f1..fd527ee882568292c0bb2e158486c6d11dabd61b 100644 --- a/vpn/client/DapCmdHandlers/dap-cmd-handlers.pri +++ b/vpn/client/DapCmdHandlers/dap-cmd-handlers.pri @@ -1,5 +1,6 @@ SOURCES += \ $$PWD/DapCmdLastConnectionData.cpp \ + $$PWD/DapCmdNews.cpp \ $$PWD/DapCmdPingServer.cpp \ $$PWD/DapCmdSendBugReport.cpp \ $$PWD/DapCmdTunTap.cpp \ @@ -14,6 +15,7 @@ SOURCES += \ HEADERS += \ $$PWD/DapCmdLastConnectionData.h \ + $$PWD/DapCmdNews.h \ $$PWD/DapCmdPingServer.h \ $$PWD/DapCmdSendBugReport.h \ $$PWD/DapCmdTunTap.h \ diff --git a/vpn/common/DapCmdAbstract.cpp b/vpn/common/DapCmdAbstract.cpp index 9844a1571f744466eed19c52cb1f6a5df6db5e3a..6303e8886c6e8188d44baf3288a54c61a7b97145 100644 --- a/vpn/common/DapCmdAbstract.cpp +++ b/vpn/common/DapCmdAbstract.cpp @@ -13,6 +13,7 @@ QMap<DapJsonCmdType, QString> DapCmdAbstract::cmdString = { {DapJsonCmdType::PING_SERVER, "ping server"}, {DapJsonCmdType::SEND_SERVER_DATA, "send server data"}, {DapJsonCmdType::SEND_BUG_REPORT, "send bug report"}, + {DapJsonCmdType::GET_NEWS, "get news"}, {DapJsonCmdType::GET_SERVERS_LIST, "get servers list"}, {DapJsonCmdType::CREATE_ANDROID_TUNNEL, "create android tunnel"} }; diff --git a/vpn/common/DapJsonParams.h b/vpn/common/DapJsonParams.h index 0cd8c09aaa760c2343295d18b8afe536d22b96cc..80eaa5c09c2ff71b3b283e6c9e7912f483aa62ac 100644 --- a/vpn/common/DapJsonParams.h +++ b/vpn/common/DapJsonParams.h @@ -17,6 +17,7 @@ enum class DapJsonCmdType LAST_CONNECTION_DATA, CREATE_ANDROID_TUNNEL, GET_SERVERS_LIST, + GET_NEWS, TAP_DRIVER, PING_SERVER, SEND_SERVER_DATA, diff --git a/vpn/ui/DapCmdHandlers/DapCmdNews.cpp b/vpn/ui/DapCmdHandlers/DapCmdNews.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c0803ac4c1ec359d6a61c1df06a330064e6a71eb --- /dev/null +++ b/vpn/ui/DapCmdHandlers/DapCmdNews.cpp @@ -0,0 +1,71 @@ +#include "DapCmdNews.h" +#include <QJsonArray> +#include <QList> +#include <QMap> + +DapCmdNews::DapCmdNews(QObject *parent) + : DapCmdClientAbstract (DapJsonCmdType::GET_NEWS, parent) +{ + +} + +void DapCmdNews::sendingGetNewsRequest() +{ + QJsonObject obj; + sendCmd(&obj); +} + +void DapCmdNews::handleResult(const QJsonObject& result) +{ + if(result.value("news_list").isArray()) { + QList<QMap<QString, QString>> listNews; + + if (fromJSON(result.value("news_list").toArray(), &listNews)) + { + emit sigGetedNews(listNews); + } else { + qCritical() << "Error parse response from service"; + } + } else { + qCritical() << "Bad response from service"; + } +} + +bool DapCmdNews::fromJSON(const QJsonArray& jsonArr, QList<QMap<QString, QString>>* listNews) +{ + for (auto it = jsonArr.constBegin(); it != jsonArr.constEnd(); ++it) { + const QJsonValue &val = *it; + + if(!val.isObject()) { + qCritical() << "Error parse QJsonValue to QJsonObject"; + return false; + } + + QJsonObject jsonObj = val.toObject(); + QMap<QString, QString> temp; + if (jsonObj["type"].toString() == "update_version"){ + temp = { {"type", jsonObj["type"].toString()}, + {"message", jsonObj["message"].toString()}, + {"version", jsonObj["version"].toString()}, + {"url", jsonObj["url"].toString()}}; + } else if (jsonObj["type"].toString() == "news"){ + temp = { {"type", jsonObj["type"].toString()}, + {"message", jsonObj["message"].toString()}}; + } + + listNews->append(temp); + } + return true; +} + + +void DapCmdNews::handleError(int code, const QString& message) +{/* + Q_UNUSED(message) + if (code == -666) { + emit sigEmptyList(message); + } else if (code == -32002) { + emit sigErrorNetwork(message); + } + qWarning() << *m_errorObject;*/ +} diff --git a/vpn/ui/DapCmdHandlers/DapCmdNews.h b/vpn/ui/DapCmdHandlers/DapCmdNews.h new file mode 100644 index 0000000000000000000000000000000000000000..cd001faa15a40e87a49de39009dde72f4b1d1bf5 --- /dev/null +++ b/vpn/ui/DapCmdHandlers/DapCmdNews.h @@ -0,0 +1,24 @@ +#ifndef DAPCMDNEWS_H +#define DAPCMDNEWS_H + +#include "DapCmdClientAbstract.h" + +class DapCmdNews: public DapCmdClientAbstract +{ + Q_OBJECT + +public: + explicit DapCmdNews(QObject *parent = nullptr); + void sendingGetNewsRequest(); + +protected: + void handleResult(const QJsonObject& result) override; + void handleError(int code, const QString& message) override; + bool fromJSON(const QJsonArray& jsonArr, QList<QMap<QString, QString>> *listNews); + +signals: + void sigGetedNews(QList<QMap<QString, QString>>); + +}; + +#endif // DAPCMDNEWS_H diff --git a/vpn/ui/DapCmdHandlers/DapCmdSendBugReport.cpp b/vpn/ui/DapCmdHandlers/DapCmdSendBugReport.cpp index d2b8d4ad12986569b144e345c263467d114f84d0..0b3892ee0148910f60fb098bc4ab7de777cb35d1 100644 --- a/vpn/ui/DapCmdHandlers/DapCmdSendBugReport.cpp +++ b/vpn/ui/DapCmdHandlers/DapCmdSendBugReport.cpp @@ -13,6 +13,7 @@ void DapCmdSendBugReport::sendBugReport(const QString &a_message, const QString obj["message"] = a_message; sendCmd(&obj); } + void DapCmdSendBugReport::handleResult(const QJsonObject& result) { QString request = result.value("bugreport_answer").toString(); diff --git a/vpn/ui/DapCmdHandlers/dap-cmd-handlers.pri b/vpn/ui/DapCmdHandlers/dap-cmd-handlers.pri index ea810ce5c43349ba1139c815e8fdb8355e947128..b673c7acdef2c5da5a3d5d3d88c87a77a10a2f1d 100644 --- a/vpn/ui/DapCmdHandlers/dap-cmd-handlers.pri +++ b/vpn/ui/DapCmdHandlers/dap-cmd-handlers.pri @@ -1,5 +1,6 @@ SOURCES += \ $$PWD/DapCmdLastConnectionData.cpp \ + $$PWD/DapCmdNews.cpp \ $$PWD/DapCmdPingServer.cpp \ $$PWD/DapCmdSendBugReport.cpp \ $$PWD/DapCmdStats.cpp \ @@ -14,6 +15,7 @@ SOURCES += \ HEADERS += \ $$PWD/DapCmdLastConnectionData.h \ + $$PWD/DapCmdNews.h \ $$PWD/DapCmdPingServer.h \ $$PWD/DapCmdSendBugReport.h \ $$PWD/DapCmdStats.h \