-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.cpp
executable file
·327 lines (281 loc) · 9 KB
/
request.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
306
307
308
309
310
311
312
313
314
315
#include "request.h"
Request::Request(QAbstractSocket *s, QObject *parent) : QObject(parent)
{
socket = s;
rawData = new QString();
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(handleError(QAbstractSocket::SocketError)));
}
Request::~Request ()
{
delete rawData;
socket->deleteLater();
}
void Request::readData()
{
qDebug() << "Request received.";
if(rawData->length() == 0)
{
// First package, with header ...
rawData->append(QString(socket->readAll()));
QStringList sl = rawData->split("\r\n\r\n");
if(sl.length() < 2)
{
qDebug() << "INVALID HTTP FORMAT!";
socket->close();
this->deleteLater();
return;
}
rawHead = sl.at(0);
request.data = sl.at(1);
sl = rawHead.split("\r\n");
// check if it is correct POST http request
if(!sl.at(0).contains("HTTP/1.1")) // TODO: better integrity checking!!
{
qDebug() << "INVALID HTTP REQUEST!";
socket->close();
this->deleteLater();
return;
}
qDebug() << "IS VALID HTTP";
for(int i = 1; i < sl.length(); i++)
{
if(!sl.at(i).contains(':'))
{
continue;
}
QString val = sl.at(i).split(':').at(1).simplified();
if(sl.at(i).contains("Rosi-Request:", Qt::CaseInsensitive))
{
request.name = val;
}
else if(sl.at(i).contains("User-Agent:", Qt::CaseInsensitive))
{
request.rosiVersion = val;
}
else if(sl.at(i).contains("User-Id:", Qt::CaseInsensitive))
{
request.userId = val;
}
else if(sl.at(i).contains("Request-Checksum:", Qt::CaseInsensitive))
{
request.checksum = val;
}
else if(sl.at(i).contains("Content-Length:", Qt::CaseInsensitive))
{
request.datalen = val.toInt();
}
else if(sl.at(i).contains("Pw-Hash:", Qt::CaseInsensitive))
{
request.pw = val;
}
}
qDebug() << "Request: " << request.name;
qDebug() << "From user " << request.userId << " with agent " << request.rosiVersion;
qDebug() << "Data checksum: " << request.checksum;
// qDebug() << "Data: " << request.data;
}
else
{
// Additional data
QString *data = new QString(socket->readAll());
rawData->append(data);
request.data.append(data);
delete data;
}
if(request.data.length() == request.datalen && isValid())
{
qDebug() << "Checksum OK.";
executeRequest();
socket->waitForBytesWritten();
socket->close();
this->deleteLater();
}
else
{
qDebug() << "Invalid Data length/Checksum, waiting for additional tcp package...";
}
}
// Check integrity of request by calcualting checksum
bool Request::isValid()
{
QCryptographicHash *reqHash = new QCryptographicHash(QCryptographicHash::Sha256);
QString data = request.name + request.rosiVersion + request.userId + request.pw + request.data;
reqHash->addData(data.toLatin1());
QString hash = QString(reqHash->result().toHex());
delete reqHash;
return hash == request.checksum;
}
void Request::handleError(QAbstractSocket::SocketError error)
{
qDebug() << "Socket error occurred" << error;
}
void Request::handleError(QString error)
{
qDebug() << "Error occurred: " << error;
}
void Request::executeRequest()
{
if(request.name == "Add-Backup")
{
addBackup();
}
else if(request.name == "Restore-Backup")
{
restoreBackup();
}
else if(request.name == "List-Backups")
{
listBackups();
}
else if(request.name == "Test-User")
{
testUser();
}
}
void Request::addBackup()
{
// Save to disk
if(isValidUserPw(true) < 0)
{
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nERROR INVALID USER PASSWORD").toLatin1());
return;
}
QString path("data/");
QString filename(request.userId + "." + ("000000000000" + QString::number(QDateTime::currentDateTime().toTime_t())).right(12));
QDir dir;
if(!dir.exists(path))
{
dir.mkpath(path);
}
QFile file(path + filename);
file.open(QIODevice::WriteOnly);
file.write(request.data.toLatin1());
file.close();
qDebug() << "Backup written.";
socket->write("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nBACKUP_SAVED");
}
void Request::restoreBackup()
{
if(isValidUserPw(true) < 0)
{
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nERROR INVALID USER PASSWORD").toLatin1());
return;
}
QDir dir("data/");
QStringList nameFilters;
nameFilters.append(request.userId + ".????????????");
QFileInfoList files = dir.entryInfoList(nameFilters, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable, QDir::Name | QDir::Reversed);
QStringList sl = request.data.split("\r\n");
int restoreNo = -1;
for(int i = 0, total = sl.length(); i < total; ++i)
{
if(!sl.at(i).contains(':'))
{
continue;
}
QString val = sl.at(i).split(':').at(1).simplified();
if(sl.at(i).contains("Restore-Number:"))
{
restoreNo = val.toInt();
break;
}
}
if(restoreNo < 0 || restoreNo > files.length() - 1)
{
handleError("Invalid Request Data!");
socket->write("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nERROR: INVALID RESTORE NUMBER");
return;
}
qDebug() << "Opening file: " << files.at(restoreNo).filePath();
QFile backupFile(files.at(restoreNo).filePath());
backupFile.open(QFile::ReadOnly);
QString data = backupFile.readAll();
QCryptographicHash *reqHash = new QCryptographicHash(QCryptographicHash::Sha256);
reqHash->addData(data.toLatin1());
QString hash = QString(reqHash->result().toHex());
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\nData-Checksum:" + hash + "\r\n\r\n" + data).toLatin1());
backupFile.close();
}
void Request::listBackups()
{
if(isValidUserPw(true) < 0)
{
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nBackups-Count: -1\r\nERROR INVALID USER PASSWORD").toLatin1());
return;
}
QString path("data/");
QDir dir(path);
QStringList nameFilters;
nameFilters.append(request.userId + ".????????????");
QFileInfoList files = dir.entryInfoList(nameFilters, QDir::Files | QDir::NoDotAndDotDot | QDir::Readable, QDir::Name | QDir::Reversed);
QString backupList("");
for(int i = 0; i < files.length(); i++)
{
QStringList splittedFile = files.at(i).fileName().split(".");
backupList += "\r\n" + QString::number(i) + ":" + splittedFile.at(splittedFile.length() - 1);
}
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nBackups-Count: " + QString::number(files.length()) + backupList).toLatin1());
}
// Check if user exists, if so, check PW; if not and create == true, create new userfile
// return 0 user exist, PW OK
// 1 user not existing, NOT created
// 2 user not existing, created
// -1 user existing, PW FAIL
// -2 UNKNOWN ERROR
int Request::isValidUserPw(bool create)
{
QString path("data/");
QDir dir(path);
if(!dir.exists(path))
{
dir.mkpath(path);
}
QFile file(path + request.userId);
if(file.open(QIODevice::ReadOnly))
{
// User exists
QString pw(file.readAll());
pw = pw.simplified();
file.close();
if(QString::compare(pw, request.pw) == 0)
return 0; // Known user OK
return -1; // Known user FAIL
}
else
{
// User does not exist
if(!create)
return 1;
QFile usrFileNew(path + request.userId);
if(usrFileNew.open(QIODevice::WriteOnly))
{
usrFileNew.write(request.pw.toLatin1());
usrFileNew.close();
return 2;
}
else
return -2;
}
}
void Request::testUser()
{
switch(isValidUserPw(false))
{
case 0:
// Known user OK
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nKNOWN_USER").toLatin1());
break;
case 1:
case 2:
// User does not exist
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nNEW_USER").toLatin1());
break;
case -2:
case -1:
default:
// Unknown user with wrong password or ERROR
socket->write(QString("HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\n\r\nWRONG_USER").toLatin1());
break;
}
}