forked from slawkens/otadmin-plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
661 lines (568 loc) · 14.7 KB
/
client.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
#include <QtWidgets>
#include <QtNetwork>
#include <QTcpSocket>
#include <iostream>
#include <stdio.h>
#include <list>
#include <sstream>
#include <string>
#include "client.h"
#include "statusreader.h"
#include "networkmessage.h"
#include "rsa.h"
#include "definitions.h"
#if defined WIN32 || defined __WINDOWS__
#define ERROR_SOCKET WSAGetLastError()
#else
#include <errno.h>
#define ERROR_SOCKET errno
#endif
Client::Client(QWidget* _parent, QString _host, int _port, QString _password)
{
parent = _parent;
#if defined WIN32 || defined __WINDOWS__
WSADATA wsd;
if(WSAStartup(MAKEWORD(2,2), &wsd) != 0)
return;
#endif
m_socket = SOCKET_ERROR, m_connected = false;;
host = _host, port = _port, password = _password;
}
bool Client::sendCommand(char commandByte, char* command)
{
if(!isConnected())
{
consoleLog("[sendCommand] NOT connected!");
setLastError("NOT Connected!");
return false;
}
NetworkMessage msg;
msg.AddByte(AP_MSG_COMMAND);
msg.AddByte(commandByte);
if(command)
msg.AddString(command);
if(!sendMsg(msg))
{
consoleLog("[sendCommand] Error while sending command!");
setLastError("Error while sending command!");
return false;
}
char ret_code = msg.GetByte();
if(ret_code == AP_MSG_COMMAND_OK)
return true;
if(ret_code == AP_MSG_COMMAND_FAILED)
{
std::string error_desc = msg.GetString();
consoleLog("[sendCommand] Received error: " + QString::fromStdString(error_desc));
setLastError("Received error: " + QString::fromStdString(error_desc));
return false;
}
consoleLog("[sendCommand] Server returned unknown code");
setLastError("Server returned unknown code");
return false;
}
bool Client::sendMsg(NetworkMessage& msg, uint32_t* key/*= NULL*/)
{
#if defined WIN32 || defined __WINDOWS__
unsigned long mode = 0;
ioctlsocket(m_socket, FIONBIO, &mode);
#else
int flags = fcntl(m_socket, F_GETFL);
fcntl(m_socket, F_SETFL, flags & (~O_NONBLOCK));
#endif
bool ret = true;
if(!msg.WriteToSocket(m_socket))
{
consoleLog("[sendMsg] Error while sending - ERROR_NUMBER: " + QString::number(ERROR_SOCKET));
setLastError("Error while sending - ERROR_NUMBER: " + QString::number(ERROR_SOCKET));
ret = false;
}
msg.Reset();
if(ret)
{
if(key)
{
msg.setEncryptionState(true);
msg.setEncryptionKey(key);
}
if(!msg.ReadFromSocket(m_socket))
{
consoleLog("[sendMsg] Error while reading - ERROR_NUMBER: " + QString::number(ERROR_SOCKET));
setLastError("Error while reading - ERROR_NUMBER: " + QString::number(ERROR_SOCKET));
ret = false;
}
else
{
char ret_code = msg.InspectByte();
if(ret_code == AP_MSG_ERROR)
{
msg.GetByte();
std::string error_desc = msg.GetString();
consoleLog("[sendMsg] MSG_ERROR: " + QString::fromStdString(error_desc));
setLastError("MSG_ERROR: " + QString::fromStdString(error_desc));
ret = false;
}
}
}
#if defined WIN32 || defined __WINDOWS__
mode = 1;
ioctlsocket(m_socket, FIONBIO, &mode);
#else
flags = fcntl(m_socket, F_GETFL);
fcntl(m_socket, F_SETFL, flags | O_NONBLOCK);
#endif
return ret;
}
bool Client::connect()
{
if(isConnected())
{
consoleLog("[connect] Already connected!");
setLastError("Already connected!");
return false;
}
char password[128];
strcpy(password, getPassword().toLatin1().data());
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
uint32_t remoteIP = inet_addr(getHost().toStdString().c_str());
if(remoteIP == INADDR_NONE)
{
struct hostent* hp = gethostbyname(getHost().toStdString().c_str());
if(hp != 0)
remoteIP = *(long*)hp->h_addr;
else
{
closesocket(m_socket);
consoleLog("[connect] Cannot resolve server:" + getHost() + " - " + QString::number(ERROR_SOCKET));
setLastError("Cannot resolve server:" + getHost() + " - " + QString::number(ERROR_SOCKET));
return false;
}
}
sockaddr_in serveraddr;
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = remoteIP;
serveraddr.sin_port = htons(getPort());
if(::connect(m_socket, (SOCKADDR*)&serveraddr, sizeof(serveraddr)) == SOCKET_ERROR)
{
closesocket(m_socket);
consoleLog("[connect] Cannot connect to server: " + getHost() + " - " + QString::number(ERROR_SOCKET));
setLastError("Cannot connect to server: " + getHost() + " - " + QString::number(ERROR_SOCKET));
return false;
}
uint8_t ip[4];
*(uint32_t*)&ip = remoteIP;
QString tmp = QString::number(ip[0]) + "." + QString::number(ip[1]) + "." + QString::number(ip[2]) + "." + QString::number(ip[3]);
consoleLog("Connected to server. [IP: " + tmp + "]");
NetworkMessage msg;
msg.AddByte(0xFE);
if(!msg.WriteToSocket(m_socket))
{
closesocket(m_socket);
consoleLog("[connect] Error while sending first byte - " + QString::number(ERROR_SOCKET));
setLastError("Error while sending first byte - " + QString::number(ERROR_SOCKET));
return false;
}
msg.Reset();
//read server hello
if(!msg.ReadFromSocket(m_socket))
{
closesocket(m_socket);
consoleLog("[connect] Error while reading hello - " + QString::number(ERROR_SOCKET) + "(Probably OTAdmin disabled on remote server or your ip is not allowed.)");
setLastError("Error while reading hello - " + QString::number(ERROR_SOCKET) + "(Probably OTAdmin disabled on remote server or your ip is not allowed.)");
return false;
}
char byte = msg.GetByte();
if(byte != AP_MSG_HELLO)
{
qDebug() << " NOT HELLO #SLAW DEBUG = " << byte;
closesocket(m_socket);
consoleLog("[connect] No valid server hello!");
setLastError("No valid server hello!");
return false;
}
msg.GetU32();
std::string strversion = msg.GetString();
uint16_t security = msg.GetU16();
uint32_t options = msg.GetU32();
if(security & REQUIRE_ENCRYPTION)
{
strversion = strversion + " encryption";
if(options & ENCRYPTION_RSA1024XTEA)
strversion = strversion + "(RSA1024XTEA)";
else
strversion = strversion + "(Not supported)";
}
if(security & REQUIRE_LOGIN)
strversion = strversion + " login";
consoleLog("Hello from " + QString::fromStdString(strversion));
if(security & REQUIRE_ENCRYPTION)
{
if(options & ENCRYPTION_RSA1024XTEA)
{
//get public key
msg.Reset();
msg.AddByte(AP_MSG_KEY_EXCHANGE);
msg.AddByte(ENCRYPTION_RSA1024XTEA);
if(!sendMsg(msg))
{
closesocket(m_socket);
consoleLog("[connect] Error while getting public key");
setLastError("Error while getting public key");
return false;
}
char ret_code = msg.GetByte();
if(ret_code == AP_MSG_KEY_EXCHANGE_OK)
{
consoleLog("Key exchange OK");
}
else if(ret_code == AP_MSG_KEY_EXCHANGE_FAILED)
{
std::string error_desc = msg.GetString();
closesocket(m_socket);
consoleLog("[connect] Cannot get public key: " + QString::fromStdString(error_desc));
setLastError("Cannot get public key: " + QString::fromStdString(error_desc));
return false;
}
else
{
closesocket(m_socket);
consoleLog("[connect] Unknown response to key exchange request");
setLastError("Unknown response to key exchange request");
return false;
}
unsigned char key_type = msg.GetByte();
if(key_type != ENCRYPTION_RSA1024XTEA)
{
closesocket(m_socket);
consoleLog("[connect] Invalid key returned");
setLastError("Invalid key returned");
return false;
}
uint32_t rsa_mod[32];
for(unsigned int i = 0; i < 32; ++i){
rsa_mod[i] = msg.GetU32();
}
RSA::getInstance()->setPublicKey((char*)rsa_mod, "65537");
uint32_t random_key[32];
for(unsigned int i = 0; i < 32; ++i)
random_key[i] = rand() << 16 ^ rand();
msg.setRSAInstance(RSA::getInstance());
msg.Reset();
msg.AddByte(AP_MSG_ENCRYPTION);
msg.AddByte(ENCRYPTION_RSA1024XTEA);
//build the 128 bytes block
msg.AddByte(0);
for(unsigned int i = 0; i < 31; ++i){
msg.AddU32(random_key[i]);
}
msg.AddByte(0);
msg.AddByte(0);
msg.AddByte(0);
//
msg.RSA_encrypt();
if(!sendMsg(msg, random_key))
{
closesocket(m_socket);
consoleLog("[connect] Error while sending private key");
setLastError("Error while sending private key");
return false;
}
ret_code = msg.GetByte();
if(ret_code == AP_MSG_ENCRYPTION_FAILED)
{
std::string error_desc = msg.GetString();
closesocket(m_socket);
consoleLog("[connect] Cannot set private key: " + QString::fromStdString(error_desc));
setLastError("Cannot set private key: " + QString::fromStdString(error_desc));
return false;
}
else if(ret_code != AP_MSG_ENCRYPTION_OK)
{
closesocket(m_socket);
consoleLog("[connect] Unknown response to set private key request");
setLastError("Unknown response to set private key request");
return false;
}
}
else
{
closesocket(m_socket);
consoleLog("[connect] Cannot initiate encryption");
setLastError("Cannot initiate encryption");
return false;
}
}
//login
if(security & REQUIRE_LOGIN)
{
msg.Reset();
msg.AddByte(AP_MSG_LOGIN);
msg.AddString(std::string(password));
if(!sendMsg(msg))
{
closesocket(m_socket);
consoleLog("[connect] Error while sending login");
setLastError("Error while sending login");
return false;
}
char ret_code = msg.GetByte();
if(ret_code != AP_MSG_LOGIN_OK)
{
if(ret_code == AP_MSG_LOGIN_FAILED)
{
std::string error_desc = msg.GetString();
consoleLog("[connect] Cannot login: " + QString::fromStdString(error_desc));
setLastError("Cannot login: " + QString::fromStdString(error_desc));
}
else
{
consoleLog("[connect] Unknown response to login request");
setLastError("Unknown response to login request");
}
closesocket(m_socket);
return false;
}
}
consoleLog("Login OK");
m_connected = true;
return true;
}
bool Client::disconnect()
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
closesocket(m_socket);
m_socket = SOCKET_ERROR;
m_connected = false;
//delete this;
return true;
}
bool Client::commandBroadcast(QString message)
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
if(message.isEmpty() || message.size() > 127)
{
consoleLog("[broadcast] Invalid message (Cannot be empty or longer than 127 chars)");
setLastError("Invalid message (Cannot be empty and longer than 127 chars)");
return false;
}
char param[128];
strcpy(param, message.toLatin1().data());
if(!sendCommand(CMD_BROADCAST, param))
{
consoleLog("[broadcast] Error sending broadcast");
setLastError("Error sending broadcast");
return false;
}
consoleLog("[broadcast] Succesfully broadcasted message '" + message + "'");
return true;
}
bool Client::commandKick(QString name)
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
if(name.isEmpty() || name.size() > 30)
{
consoleLog("[kick] Invalid player name (Cannot be empty or longer than 30 characters)");
setLastError("Invalid player name (Cannot be empty or longer than 30 characters)");
return false;
}
char param[128];
strcpy(param, name.toLatin1().data());
if(!sendCommand(CMD_KICK, param))
{
consoleLog("[kick] Error sending kick");
setLastError("Error sending kick");
return false;
}
consoleLog("[kick] Kicked player '" + name + "'");
return true;
}
bool Client::commandSetOwner(int houseId, QString owner)
{
consoleLog("TODO!");
return true;
}
bool Client::commandOpenServer()
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
if(!sendCommand(CMD_OPEN_SERVER, NULL))
{
consoleLog("[openserver] Error in open server");
setLastError("Error in open server");
return false;
}
return true;
}
bool Client::commandCloseServer()
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
if(!sendCommand(CMD_CLOSE_SERVER, NULL))
{
consoleLog("[closeserver] Error in close server");
setLastError("Error in close server");
return false;
}
return true;
}
bool Client::commandPayHouses()
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
if(!sendCommand(CMD_PAY_HOUSES, NULL))
{
consoleLog("[payhouses] Error in payhouses");
setLastError("Error in payhouses");
return false;
}
return true;
}
bool Client::commandSaveServer()
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
if(!sendCommand(CMD_SERVER_SAVE, NULL))
{
consoleLog("[saveserver] Error in serversave");
setLastError("Error in serversave");
return false;
}
return true;
}
bool Client::commandShutdown()
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
if(!sendCommand(CMD_SHUTDOWN_SERVER, NULL))
{
consoleLog("[shutdown] Error in server shutdown");
setLastError("Error in server shutdown");
return false;
}
return true;
}
bool Client::ping()
{
if(!isConnected())
{
setLastError("Not connected!");
return false;
}
NetworkMessage msg;
msg.AddByte(AP_MSG_PING);
if(!sendMsg(msg))
{
consoleLog("[ping] Error while sending ping");
setLastError("Error while sending ping");
return false;
}
char ret_code = msg.GetByte();
if(ret_code != AP_MSG_PING_OK)
{
consoleLog("[ping] Invalid respons for ping");
setLastError("Invalid respons for ping");
return false;
}
return true;
}
connectionError_t Client::getServerData(StatusInfo_t& statusInfo)
{
connectionError_t ret = CONNECT_SUCCESS;
QTcpSocket* clientSocket = new QTcpSocket;
clientSocket->connectToHost(host, port);
if(clientSocket->waitForConnected(400))
{
QByteArray packet;
packet.resize(7);
//packet headers
packet[0] = packet.size() - 1;
packet[1] = 0x00;
packet[2] = 0xFF; //status protocol
packet[3] = 0xFF; //XML server info
packet[4] = 'i';
packet[5] = 'n';
packet[6] = 'f';
packet[7] = 'o';
clientSocket->write(packet);
if(clientSocket->waitForBytesWritten(400))
{
if(clientSocket->waitForReadyRead(1000))
{
QByteArray XMLData = clientSocket->readAll();
StatusReader handler;
QXmlInputSource source;
source.setData(XMLData);
QXmlSimpleReader reader;
reader.setContentHandler(&handler);
reader.parse(source);
statusInfo = handler.getStatusInfo();
}
else
ret = FAIL_READ;
}
else
ret = FAIL_WRITE;
}
else
ret = FAIL_CONNECT;
clientSocket->close();
return ret;
}
connectionError_t Client::getServerInfo(QString host, int port, RequestedInfo_t type, QByteArray &result)
{
connectionError_t ret = CONNECT_SUCCESS;
QTcpSocket* clientSocket = new QTcpSocket;
clientSocket->connectToHost(host, port);
if(clientSocket->waitForConnected(400))
{
QByteArray packet;
packet.resize(4);
//packet headers
packet[0] = packet.size() - 1;
packet[1] = 0x00;
packet[2] = 0xFF; //status protocol
packet[3] = 0x01; //get info
packet[4] = type;
clientSocket->write(packet);
if(clientSocket->waitForBytesWritten(400))
{
if(clientSocket->waitForReadyRead(1000))
result = clientSocket->readAll();
else
ret = FAIL_READ;
}
else
ret = FAIL_WRITE;
}
else
ret = FAIL_CONNECT;
clientSocket->close();
return ret;
}