forked from Drifter321/admintool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rcon.cpp
133 lines (109 loc) · 3.48 KB
/
rcon.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
#include "rcon.h"
#include "serverinfo.h"
#include "worker.h"
#include "mainwindow.h"
RconQuery::RconQuery(MainWindow *main, ServerInfo *info)
{
this->isAuthed = false;
this->socket = new QTcpSocket();
this->server = info;
connect(this->socket, &QTcpSocket::destroyed, this, &RconQuery::deleteLater);
connect(this->socket, &QTcpSocket::readyRead, this, &RconQuery::socketReadyRead);
connect(this->socket, &QTcpSocket::disconnected, this, &RconQuery::socketDisconnected);
connect(this, &RconQuery::rconAuth, main, &MainWindow::RconAuthReady);
connect(this, &RconQuery::rconOutput, main, &MainWindow::RconOutput);
connect(this, &RconQuery::rconHistory, main, &MainWindow::AddRconHistory);
}
void RconQuery::socketDisconnected()
{
this->isAuthed = false;
this->socket->readAll();
}
void RconQuery::socketReadyRead()
{
static qint32 id = 0;
static qint32 type = 0;
static qint32 size = 0;
QDataStream stream(this->socket);
stream.setByteOrder(QDataStream::LittleEndian);
do
{
if(size == 0)
{
stream >> size;
stream >> id;
stream >> type;
}
if(this->socket->bytesAvailable() < (qint64)(size-sizeof(qint32)*2))
{
return;
}
if(type == RCON_AUTH_RESPONSE)
{
if(id == -1)
{
this->isAuthed = false;
}
else if(id == rconID)
{
this->isAuthed = true;
}
stream.skipRawData(size-sizeof(qint32)*2);
emit rconAuth(this->server, this->queuedList);
this->queuedList.clear();
}
else if(type == RCON_EXEC_RESPONSE && id == rconID)
{
char data[4097];
stream.readRawData(data, size-sizeof(qint32)*2);
emit rconOutput(this->server, data);
}
else //Something we dont handle skip the bytes
{
stream.skipRawData(size-sizeof(qint32)*2);
}
size = 0;
}while(this->socket->bytesAvailable() > 4 && this->socket->state() == QAbstractSocket::ConnectedState);
}
void RconQuery::auth()
{
if(this->socket->state() != QAbstractSocket::ConnectedState)
{
this->socket->connectToHost(this->server->host, this->server->port);
}
this->socket->readAll();
if(this->server->rconPassword.length() == 0 || this->isAuthed)
return;
QByteArray query;
QDataStream data(&query, QIODevice::ReadWrite);
data.setByteOrder(QDataStream::LittleEndian);
qint32 size = sizeof(qint32)*2 + this->server->rconPassword.length()+1 + sizeof(qint8);
data << size;
data << rconID;
data << RCON_AUTH;
data.writeRawData(this->server->rconPassword.toUtf8().data(), this->server->rconPassword.toUtf8().length()+1);
data << qint8(0x00);
this->socket->write(query);
}
void RconQuery::execCommand(QString command, bool history)
{
if(!this->isAuthed)
{
this->auth();
return;
}
if(command.length() == 0)
return;
if(history)
emit rconHistory(command);
QByteArray query;
QDataStream data(&query, QIODevice::ReadWrite);
data.setByteOrder(QDataStream::LittleEndian);
qint32 size = sizeof(qint32)*2 + command.length()+1 + sizeof(qint8);
data << size;
data << rconID;
data << RCON_EXEC_COMMAND;
data.writeRawData(command.toUtf8().data(), command.toUtf8().length()+1);
data << qint8(0x00);
this->socket->write(query);
}