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

Create AutoUpdater, teach it to check new updates

It can't read a diff or apply an update yet, just check.
This commit is contained in:
Tux3 / Mlkj / !Lev.uXFMLA 2014-11-09 15:51:00 +01:00
parent 0adb963603
commit 747fd33592
No known key found for this signature in database
GPG Key ID: 7E086DD661263264
4 changed files with 134 additions and 5 deletions

View File

@ -57,7 +57,7 @@ contains(JENKINS,YES) {
# Rules for Windows, Mac OSX, and Linux # Rules for Windows, Mac OSX, and Linux
win32 { win32 {
RC_FILE = windows/qtox.rc RC_FILE = windows/qtox.rc
LIBS += -liphlpapi -L$$PWD/libs/lib -ltoxav -ltoxcore -ltoxencryptsave -ltoxdns -lvpx -lpthread LIBS += -liphlpapi -L$$PWD/libs/lib -ltoxav -ltoxcore -ltoxencryptsave -ltoxdns -lvpx -lpthread -lsodium
LIBS += -L$$PWD/libs/lib -lopencv_core248 -lopencv_highgui248 -lopencv_imgproc248 -lOpenAL32 -lopus LIBS += -L$$PWD/libs/lib -lopencv_core248 -lopencv_highgui248 -lopencv_imgproc248 -lOpenAL32 -lopus
LIBS += -lz -lopengl32 -lole32 -loleaut32 -luuid -lvfw32 -ljpeg -ltiff -lpng -ljasper -lIlmImf -lHalf -lws2_32 LIBS += -lz -lopengl32 -lole32 -loleaut32 -luuid -lvfw32 -ljpeg -ltiff -lpng -ljasper -lIlmImf -lHalf -lws2_32
} else { } else {
@ -76,7 +76,7 @@ win32 {
LIBS += -Wl,-Bdynamic -lv4l1 -lv4l2 -lavformat -lavcodec -lavutil -lswscale -lusb-1.0 LIBS += -Wl,-Bdynamic -lv4l1 -lv4l2 -lavformat -lavcodec -lavutil -lswscale -lusb-1.0
} else { } else {
LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -ltoxencryptsave -ltoxdns -lvpx -lopenal -lopencv_core -lopencv_highgui -lopencv_imgproc LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -ltoxencryptsave -ltoxdns -lvpx -lsodium -lopenal -lopencv_core -lopencv_highgui -lopencv_imgproc
} }
contains(JENKINS, YES) { contains(JENKINS, YES) {
@ -146,7 +146,8 @@ HEADERS += src/widget/form/addfriendform.h \
src/ipc.h \ src/ipc.h \
src/widget/toxuri.h \ src/widget/toxuri.h \
src/toxdns.h \ src/toxdns.h \
src/widget/toxsave.h src/widget/toxsave.h \
src/autoupdate.h
SOURCES += \ SOURCES += \
src/widget/form/addfriendform.cpp \ src/widget/form/addfriendform.cpp \
@ -209,4 +210,5 @@ SOURCES += \
src/widget/toxuri.cpp \ src/widget/toxuri.cpp \
src/toxdns.cpp \ src/toxdns.cpp \
src/ipc.cpp \ src/ipc.cpp \
src/widget/toxsave.cpp src/widget/toxsave.cpp \
src/autoupdate.cpp

76
src/autoupdate.cpp Normal file
View File

@ -0,0 +1,76 @@
#include "src/autoupdate.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QCoreApplication>
#include <sodium.h>
#ifdef _WIN32
const QString AutoUpdater::platform = "win32";
#else
const QString AutoUpdater::platform = "win32"; ///TODO: FIXME: undefine, we want an empty qstring
#endif
const QString AutoUpdater::updateServer = "http://127.0.0.1";
const QString AutoUpdater::checkURI = AutoUpdater::updateServer+"/qtox/"+AutoUpdater::platform+"/version";
const QString AutoUpdater::flistURI = AutoUpdater::updateServer+"/qtox/"+AutoUpdater::platform+"/flist";
const QString AutoUpdater::filesURI = AutoUpdater::updateServer+"/qtox/"+AutoUpdater::platform+"/files/";
unsigned char AutoUpdater::key[crypto_sign_PUBLICKEYBYTES] =
{
0xa5, 0x80, 0xf3, 0xb7, 0xd0, 0x10, 0xc0, 0xf9, 0xd6, 0xcf, 0x48, 0x15, 0x99, 0x70, 0x92, 0x49,
0xf6, 0xe8, 0xe5, 0xe2, 0x6c, 0x73, 0x8c, 0x48, 0x25, 0xed, 0x01, 0x72, 0xf7, 0x6c, 0x17, 0x28
};
bool AutoUpdater::isUpdateAvailable()
{
QString newVersion = getUpdateVersion();
if (newVersion.isEmpty() || newVersion == GIT_VERSION)
return false;
else
return true;
}
QString AutoUpdater::getUpdateVersion()
{
QString version;
// Updates only for supported platforms
if (platform.isEmpty())
return version;
QNetworkAccessManager *manager = new QNetworkAccessManager;
QNetworkReply* reply = manager->get(QNetworkRequest(QUrl(checkURI)));
while (!reply->isFinished())
qApp->processEvents();
if (reply->error() != QNetworkReply::NoError)
{
qWarning() << "AutoUpdater: getUpdateVersion: network error: "<<reply->errorString();
return version;
}
QByteArray data = reply->readAll();
// Check updater protocol version
if ((int)data[0] != '1')
{
qWarning() << "AutoUpdater: getUpdateVersion: Bad protocol version "<<(uint8_t)data[0];
return version;
}
// Check the signature
QByteArray sigData = data.mid(1, crypto_sign_BYTES);
unsigned char* sig = (unsigned char*)sigData.data();
QByteArray msgData = data.mid(1+crypto_sign_BYTES);
unsigned char* msg = (unsigned char*)msgData.data();
if (crypto_sign_verify_detached(sig, msg, msgData.size(), key) != 0)
{
qCritical() << "AutoUpdater: getUpdateVersion: RECEIVED FORGED VERSION FILE FROM "<<updateServer;
return version;
}
version = msgData;
return version;
}

47
src/autoupdate.h Normal file
View File

@ -0,0 +1,47 @@
/*
Copyright (C) 2014 by Project Tox <https://tox.im>
This file is part of qTox, a Qt-based graphical interface for Tox.
This program is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the COPYING file for more details.
*/
#ifndef AUTOUPDATE_H
#define AUTOUPDATE_H
#include <QString>
/// Handles checking and applying updates for qTox
class AutoUpdater
{
public:
/// Connects to the qTox update server, returns true if an update is available for download
/// Will call getUpdateVersion, and as such may block and processEvents
static bool isUpdateAvailable();
/// Fetch the version string of the last update available from the qTox update server
/// Will try to follow qTox's proxy settings, may block and processEvents
static QString getUpdateVersion();
private:
AutoUpdater() = delete;
private:
// Constants
static const QString updateServer; ///< Hostname of the qTox update server
static const QString platform; ///< Name of platform we're trying to get updates for
static const QString checkURI; ///< URI of the file containing the latest version string
static const QString flistURI; ///< URI of the file containing info on each file (hash, signature, size, name, ..)
static const QString filesURI; ///< URI of the actual files of the latest version
static unsigned char key[];
};
#endif // AUTOUPDATE_H

View File

@ -27,6 +27,8 @@
#include <QDateTime> #include <QDateTime>
#include <QMutexLocker> #include <QMutexLocker>
#include <sodium.h>
#ifdef LOG_TO_FILE #ifdef LOG_TO_FILE
static QtMessageHandler dflt; static QtMessageHandler dflt;
static QTextStream* logFile {nullptr}; static QTextStream* logFile {nullptr};
@ -55,6 +57,8 @@ int main(int argc, char *argv[])
a.setApplicationName("qTox"); a.setApplicationName("qTox");
a.setOrganizationName("Tox"); a.setOrganizationName("Tox");
sodium_init(); // For the auto-updater
#ifdef LOG_TO_FILE #ifdef LOG_TO_FILE
logFile = new QTextStream; logFile = new QTextStream;
dflt = qInstallMessageHandler(nullptr); dflt = qInstallMessageHandler(nullptr);
@ -127,7 +131,7 @@ int main(int argc, char *argv[])
} }
// Run // Run
Widget* w = Widget::getInstance(); Widget* w = Widget::getInstance();
int errorcode = a.exec(); int errorcode = a.exec();
delete w; delete w;