-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.cpp
305 lines (298 loc) · 11.3 KB
/
updater.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "updater.h"
#include <QSystemTrayIcon>
#include <QMenu>
#include <QMessageBox>
#include "blocklist.h"
#include <QBitmap>
#include <QDir>
#include <QApplication>
#include "settings.h"
#include <QInputDialog>
#include "items/block.h"
extern QNetworkAccessManager manager;
QNetworkReply*grep;
Updater::Updater(QObject *parent) : QObject(parent)
{
startUpdate();
authCount=0;
}
void Updater::update()
{
QEventLoop loop;
if(grep->error()==QNetworkReply::NoError)
{
QByteArray resp=grep->readAll();
qDebug()<<"Update data" <<resp;
QJsonParseError error;
QJsonDocument doc=QJsonDocument::fromJson(resp,&error);
if(error.error==error.NoError)
{
qDebug()<<"NO ERROR";
QJsonObject root=doc.object();
QJsonArray rm=root["rm"].toArray();
for(int i=0;i<rm.size();i++)
{
QString cur=toPath(rm.at(i).toString());
QFile file(cur);
if(file.exists())
file.remove();
}
qDebug()<<"RM"<<root["rm"];
#ifdef Q_OS_WIN
QString nhash=root["wapp"].toString();
#else
QString nhash=root["lapp"].toString();
#endif
QString fname=QApplication::applicationFilePath();
bool updateExe=false;
QCryptographicHash hash(QCryptographicHash::Sha512);
QFile file(fname);
file.open(QFile::ReadOnly);
hash.addData(&file);
file.close();
QString chash=hash.result().toHex();
qDebug()<<"EXE"<<chash<<nhash;
if(chash.length()==nhash.length())
#ifdef Q_OS_LINUX
if(Settings::final()->username()!="admin")
#endif
if(chash!=nhash)
{
QSystemTrayIcon *icon=new QSystemTrayIcon;
icon->setIcon(QIcon(":/icon.png"));
icon->setContextMenu(new QMenu());
icon->show();
#ifdef Q_OS_WIN
icon->showMessage("QDigi","qdigi.exe aktualisieren");
#else
icon->showMessage("QDigi","qdigi aktualisieren");
#endif
QTimer::singleShot(10000,[icon](){
icon->deleteLater();
});
#ifdef Q_PROCESSOR_X86_64
updateExe=true;
#else
#ifndef Q_OS_LINUX
updateExe=true;
#endif
#endif
}
QJsonArray files=root["files"].toArray();
QStringList requestFiles;
for(int i=0;i<files.size();i++)
{
QJsonObject curFile=files[i].toObject();
qDebug()<<curFile;
QCryptographicHash hash(QCryptographicHash::Sha512);
QString name=toPath(curFile["name"].toString());
QFile file(name);
if(file.exists())
{
file.open(QFile::ReadOnly);
hash.addData(&file);
file.close();
QString chash=hash.result().toHex();
qDebug()<<"HASH"<<curFile["name"].toString()<<chash<<curFile["hash"].toString();
if(curFile["hash"].toString()!=chash)
requestFiles.append(curFile["name"].toString());
}
else
{
requestFiles.append(curFile["name"].toString());
QSystemTrayIcon *icon=new QSystemTrayIcon;
icon->setIcon(QIcon(":/icon.png"));
icon->setContextMenu(new QMenu());
icon->show();
icon->showMessage("QDigi",requestFiles.last()+" aktualisieren",QSystemTrayIcon::Information);
QTimer::singleShot(10000,[icon](){
icon->deleteLater();
});
}
}
if(updateExe||(requestFiles.length()>0))
{
if(updateExe)
{
if(QMessageBox::information(NULL,"Update Installieren","Neues Update Installieren?",QMessageBox::Ok,QMessageBox::Cancel)==QMessageBox::Ok)
{
#ifdef Q_OS_WIN
QNetworkRequest req(QUrl("https://talstrasse.hp-lichtblick.de/qdigi/downloads/"+root["exeName"].toString()+".exe"));
#else
QNetworkRequest req(QUrl("https://talstrasse.hp-lichtblick.de/qdigi/downloads/"+root["exeName"].toString()));
#endif
req.setRawHeader("token",Settings::final()->token().toUtf8());
QNetworkReply* rep=manager.get(req);
connect(rep,SIGNAL(finished()),&loop,SLOT(quit()));
loop.exec();
if(rep->error()==QNetworkReply::NoError)
{
QByteArray newExe=rep->readAll();
#ifdef Q_OS_WIN
QString path=QDir(QApplication::applicationDirPath()).absoluteFilePath("qdigi.exe");
#else
QString path=QDir(QApplication::applicationDirPath()).absoluteFilePath("qdigi");
#endif
QDir dir(QApplication::applicationDirPath());
dir.rename(QApplication::applicationFilePath(),QApplication::applicationFilePath()+".old");
QFile file(path);
file.open(QFile::WriteOnly|QFile::Truncate);
file.write(newExe);
file.close();
file.setPermissions(file.permissions()|QFile::ExeUser|QFile::ExeGroup|QFile::ExeOther|QFile::ExeOwner);
}
}
else
updateExe=false;
}
for(int i=0;i<requestFiles.length();i++)
{
QNetworkRequest req(QUrl("https://talstrasse.hp-lichtblick.de/qdigi/downloads/"+requestFiles[i]));
req.setRawHeader("token",Settings::final()->token().toUtf8());
QNetworkReply* rep=manager.get(req);
connect(rep,SIGNAL(finished()),&loop,SLOT(quit()));
loop.exec();
if(rep->error()==QNetworkReply::NoError)
{
QByteArray data=rep->readAll();
QString name=toPath(requestFiles[i]);
QFile file(name);
file.open(QFile::WriteOnly|QFile::Truncate);
file.write(data);
file.flush();
file.close();
qDebug()<<"BLKNAME"<<name;
if(Block::usedBlocks.contains(name))
{
qDebug()<<Block::usedBlocks[name];
for(auto block:Block::usedBlocks[name])
{
qDebug()<<block->name;
block->refresh();
}
}
QSystemTrayIcon *icon=new QSystemTrayIcon;
icon->setIcon(QIcon(":/icon.png"));
icon->setContextMenu(new QMenu());
icon->show();
icon->showMessage("QDigi",requestFiles[i]+" aktualisiert");
QTimer::singleShot(10000,[icon](){
icon->deleteLater();
});
}
else
qDebug()<<rep->errorString();
}
QMessageBox::information(NULL,"Update fertig","Update Fertig, bitte noch einmal starten.");
exit(0);
}else
{
QSystemTrayIcon *icon=new QSystemTrayIcon;
icon->setIcon(QIcon(":/icon.png"));
icon->setContextMenu(new QMenu());
icon->show();
icon->showMessage("QDigi","Keine Updates");
QTimer::singleShot(10000,[icon](){
icon->deleteLater();
});
qDebug()<<"NO NEW UPDATES";
return;
}
}
}else if(grep->error()==QNetworkReply::AuthenticationRequiredError){
Settings::final()->resetToken();
//QMessageBox::warning(NULL,"QDigi","Falscher Lizenzschlüssel\nBitte beim nächsten Start Gültigen Schlüssel eingeben");
//exit(0);
}
else
{
qDebug()<<grep->errorString();
}
qDebug()<<"UPDATE FINISHED";
//QTimer::singleShot(1000,&loop,SLOT(quit()));
//loop.exec();
}
void Updater::registerReg()
{
#ifdef Q_OS_WIN
QFile file(":/qdigi.reg");
file.open(QFile::ReadOnly);
QString data=file.readAll();
file.close();
QString fp=QApplication::applicationFilePath();
fp.replace("/","\\");
fp.replace("\\","\\\\");
data.replace("___EXE_PATH___",fp);
data.replace("\r\n","\n");
data.replace("\n","\r\n");
file.setFileName("qdigiFile.reg");
file.open(QFile::WriteOnly|QFile::Truncate);
file.write(data.toUtf8());
file.close();
system("qdigiFile.reg");
file.remove();
#endif
}
QString Updater::toPath(QString in)
{
QStringList parts=in.split('/');
QDir dir(Settings::final()->applicationDir());
for(int i=0;i<parts.length()-1;i++)
{
if(!dir.entryList().contains(parts[i]))
dir.mkdir(parts[i]);
dir.cd(parts[i]);
}
QString name=dir.absoluteFilePath(parts.last());
return name;
}
void Updater::authenticationRequired(QNetworkProxy proxy, QAuthenticator*auth)
{
if(authCount==0)
{
if(Settings::final()->saveFile->value("proxy_user").isValid()&&Settings::final()->saveFile->value("proxy_pass").isValid())
{
auth->setUser(Settings::final()->saveFile->value("proxy_user").toString());
auth->setPassword(Settings::final()->saveFile->value("proxy_pass").toString());
}
authCount=1;
}
bool ok;
QString username=QInputDialog::getText(NULL,"Login","Username",QLineEdit::Normal,"",&ok);
if(!ok)
{
authCount=2;
return;
}
QString password=QInputDialog::getText(NULL,"Login","Passwort",QLineEdit::Password,"",&ok);
if(!ok)
{
authCount=2;
return;
}
Settings::final()->saveFile->setValue("proxy_user",username);
Settings::final()->saveFile->setValue("proxy_pass",password);
auth->setUser(username);
auth->setPassword(password);
}
void Updater::startUpdate()
{
QString platform="";
#ifdef Q_OS_LINUX
platform="linux";
#endif
#ifdef Q_OS_WIN32
platform="windows";
#endif
qDebug()<<"Starting Update";
QNetworkRequest req(QUrl("https://talstrasse.hp-lichtblick.de/qdigi/update.cgi?platform="+platform));
req.setRawHeader("token",Settings::final()->token().toUtf8());
grep=manager.get(req);
//if(Settings::final()->license().isEmpty())
// return;
QFile rfile(QApplication::applicationFilePath()+".old");
if(rfile.exists())
rfile.remove();
connect(grep,SIGNAL(finished()),this,SLOT(update()));
connect(&manager,SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),this,SLOT(authenticationRequired(QNetworkProxy,QAuthenticator*)));
}