-
Notifications
You must be signed in to change notification settings - Fork 35
/
tcp.cpp
275 lines (252 loc) · 11.7 KB
/
tcp.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
#include "widget.h"
#include "message.h"
#include "utils.h"
void Widget::tcpConnectClient()
{
logMessage("TCP: New client connected");
QTcpSocket *newClient = tcpServer->nextPendingConnection();
tcpClientsList << newClient;
connect(newClient, SIGNAL(readyRead()), this, SLOT(tcpProcessPendingDatagrams()));
connect(newClient, SIGNAL(disconnected()), this, SLOT(tcpDisconnectClient()));
}
void Widget::tcpDisconnectClient()
{
// On détermine quel client se déconnecte
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (socket == 0) // Si par hasard on n'a pas trouvé le client à l'origine du signal, on arrête la méthode
return;
logMessage("TCP: Client disconnected");
disconnect(socket);
tcpClientsList.removeOne(socket);
socket->deleteLater();
socket=0;
}
void Widget::tcpProcessPendingDatagrams()
{
// On détermine quel client envoie le message (recherche du QTcpSocket du client)
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender());
if (socket == 0) // Si par hasard on n'a pas trouvé le client à l'origine du signal, on arrête la méthode
return;
unsigned nTries = 0;
// Acquire data
while(socket->state()==QAbstractSocket::ConnectedState && nTries<3) // Exit if disconnected, too much retries, malformed HTTP request, or after all requests are processed
{
// TODO: Give up after three tries reading the same message
tcpReceivedDatas->append(socket->readAll());
nTries++;
if (!tcpReceivedDatas->startsWith("POST") && !tcpReceivedDatas->startsWith("GET")) // Not HTTP, clear the buffer
{
logMessage("TCP: Received non-HTTP request");
tcpReceivedDatas->clear();
socket->close();
return;
}
else if (tcpReceivedDatas->contains("Content-Length:")) // POST or GET request, wait for Content-Length header
{
QByteArray contentLength = *tcpReceivedDatas;
contentLength = contentLength.right(contentLength.size() - contentLength.indexOf("Content-Length:") - 15);
QList<QByteArray> lengthList = contentLength.trimmed().split('\n');
if (lengthList.size()>1) // We want a number on this line and a next line to be sure we've got the full number
{
bool isNumeric;
int length = lengthList[0].trimmed().toInt(&isNumeric);
if (!isNumeric) // We've got something but it's not a number
{
logMessage("TCP: Content-Length must be a decimal number");
tcpReceivedDatas->clear();
socket->close();
return;
}
// Detect and send data files if we need to
// TODO: Process and send all the requested data
QByteArray data = *tcpReceivedDatas;
int i1=0;
do
{
i1 = data.indexOf("GET");
if (i1 != -1)
{
int i2 = data.indexOf("HTTP")-1;
QString path = data.mid(i1 + 4, i2-i1-4);
logMessage("Received GET:"+path);
QFile head(QString(NETDATAPATH)+"/test.bin");
QFile res("gameFiles"+path);
head.open(QIODevice::ReadOnly);
res.open(QIODevice::ReadOnly);
socket->write(head.readAll());
socket->write(QString("Content-Length: "+QString().setNum(res.size())+"\n\n").toLatin1());
socket->write(res.readAll());
head.close();
res.close();
logMessage("Sent ("+QString().setNum(res.size())+" bytes)");
data = removeHTTPHeader(data, "POST ");
data = removeHTTPHeader(data, "GET ");
}
} while (i1 != -1);
// Get the HTML data/payload
data = removeHTTPHeader(data, "POST ");
data = removeHTTPHeader(data, "GET ");
data = removeHTTPHeader(data, "User-Agent:");
data = removeHTTPHeader(data, "Host:");
data = removeHTTPHeader(data, "Accept:");
data = removeHTTPHeader(data, "Content-Length:");
data = removeHTTPHeader(data, "Content-Type:");
data = removeHTTPHeader(data, "Server:");
data = removeHTTPHeader(data, "Date:");
data = removeHTTPHeader(data, "Transfert-Encoding:");
data = removeHTTPHeader(data, "Connection:");
data = removeHTTPHeader(data, "Vary:");
data = removeHTTPHeader(data, "X-Powered-By:");
data = removeHTTPHeader(data, "accept-encoding:");
data = removeHTTPHeader(data, "if-modified-since:");
if (data.size() >= length) // Wait until we have all the data
{
data.truncate(length);
// Process data, if the buffer is not empty, keep reading
tcpProcessData(data, socket);
if (tcpReceivedDatas->isEmpty())
return;
nTries=0;
}
}
}
}
}
void Widget::tcpProcessData(QByteArray data, QTcpSocket* socket)
{
if (tcpReceivedDatas->contains("commfunction=login&") && tcpReceivedDatas->contains("&version=")) // Login request
{
QString postData = QString(*tcpReceivedDatas);
*tcpReceivedDatas = tcpReceivedDatas->right(postData.size()-postData.indexOf("version=")-8-4); // 4 : size of version number (ie:version=1344)
logMessage("TCP: Login request received :");
QFile file(QString(NETDATAPATH)+"/loginHeader.bin");
QFile fileServersList(SERVERSLISTFILEPATH);
QFile fileBadPassword(QString(NETDATAPATH)+"/loginWrongPassword.bin");
QFile fileAlready(QString(NETDATAPATH)+"/loginAlreadyConnected.bin");
QFile fileMaxRegistration(QString(NETDATAPATH)+"/loginMaxRegistered.bin");
QFile fileMaxConnected(QString(NETDATAPATH)+"/loginMaxConnected.bin");
if (!file.open(QIODevice::ReadOnly) || !fileBadPassword.open(QIODevice::ReadOnly)
|| !fileAlready.open(QIODevice::ReadOnly) || !fileMaxRegistration.open(QIODevice::ReadOnly)
|| !fileMaxConnected.open(QIODevice::ReadOnly) || !fileServersList.open(QIODevice::ReadOnly))
{
logStatusMessage("Error reading login data files");
stopServer();
}
else
{
bool ok=true;
postData = postData.right(postData.size()-postData.indexOf("username=")-9);
QString username = postData;
username.truncate(postData.indexOf('&'));
postData = postData.right(postData.size()-postData.indexOf("passhash=")-9);
QString passhash = postData;
passhash.truncate(postData.indexOf('&'));
logMessage(QString("IP : ")+socket->peerAddress().toString());
logMessage(QString("Username : ")+username);
logMessage(QString("Passhash : ")+passhash);
// Add player to the players list
Player player = Player::findPlayer(tcpPlayers, username);
if (player.name != username) // Not found, create a new player
{
// Check max registered number
if (tcpPlayers.size() >= maxRegistered)
{
logMessage("TCP: Registration failed, too much players registered");
socket->write(fileMaxRegistration.readAll());
ok = false;
}
else
{
logMessage("TCP: Creating user in database");
Player newPlayer;
newPlayer.name = username;
newPlayer.passhash = passhash;
newPlayer.IP = socket->peerAddress().toString();
newPlayer.connected = false; // The connection checks are done by the game servers
tcpPlayers << newPlayer;
if (!Player::savePlayers(tcpPlayers))
ok = false;
}
}
else // Found, compare passhashes, check if already connected
{
if (player.passhash != passhash) // Bad password
{
logMessage("TCP: Login failed, wrong password");
socket->write(fileBadPassword.readAll());
ok=false;
}
/*
else if (newPlayer.connected) // Already connected
{
logMessage("TCP: Login failed, player already connected");
socket->write(fileAlready.readAll());
ok=false;
}
*/
else // Good password
{
/*
// Check too many connected
int n=0;
for (int i=0;i<tcpPlayers.size();i++)
if (tcpPlayers[i].connected)
n++;
if (n>=maxConnected)
{
logMessage("TCP: Login failed, too much players connected");
socket->write(fileMaxConnected.readAll());
ok=false;
}
else
*/
{
player.reset();
player.IP = socket->peerAddress().toString();
player.lastPingTime = timestampNow();
player.connected = true;
}
}
}
if (ok) // Send servers list
{
QByteArray customData = file.readAll();
QByteArray data1 = QByteArray::fromHex("0D0A61757468726573706F6E73653A0A747275650A");
QByteArray sesskey = QCryptographicHash::hash(QString(passhash + saltPassword).toLatin1(), QCryptographicHash::Md5).toHex();
sesskey += passhash;
QByteArray data2 = QByteArray::fromHex("0A310A");
QByteArray serversList;
QByteArray line;
do {
line = fileServersList.readLine().trimmed();
serversList+=line;
serversList+=0x0A;
} while (!line.isEmpty());
serversList = serversList.trimmed();
QByteArray data3 = QByteArray::fromHex("0D0A300D0A0D0A");
int dataSize = data1.size() + sesskey.size() + data2.size() + serversList.size() - 2;
QString dataSizeStr = QString().setNum(dataSize, 16);
customData += dataSizeStr;
customData += data1;
customData += sesskey;
customData += data2;
customData += serversList;
customData += data3;
logMessage("TCP: Login successful, sending servers list");
socket->write(customData);
}
}
}
else if (data.contains("commfunction=removesession"))
{
logMessage("TCP: Session closed by client");
}
else // Unknown request, erase tcp buffer
{
// Display data
logMessage("TCP: Unknow request received : ");
logMessage(QString(data.data()));
}
// Delete the processed message from the buffer
*tcpReceivedDatas = tcpReceivedDatas->right(tcpReceivedDatas->size() - tcpReceivedDatas->indexOf(data) - data.size());
}