Switching from asn.1 to protobuf as chat cleaner protocol.

This commit is contained in:
lotodore
2013-08-29 22:18:44 +02:00
parent 8adbe8edb9
commit 9686257e41
13 changed files with 3295 additions and 336 deletions
-2
View File
@@ -13,11 +13,9 @@ OBJECTS_DIR = obj
TEMPLATE = app TEMPLATE = app
INCLUDEPATH += src/ \ INCLUDEPATH += src/ \
src/chatcleaner/ \ src/chatcleaner/ \
src/third_party/asn1/ \
src/net/ src/net/
DEPENDPATH += src/ \ DEPENDPATH += src/ \
src/chatcleaner/ \ src/chatcleaner/ \
src/third_party/asn1/ \
src/net/ src/net/
SOURCES += src/chatcleaner/chatcleaner.cpp \ SOURCES += src/chatcleaner/chatcleaner.cpp \
src/chatcleaner/cleanerserver.cpp \ src/chatcleaner/cleanerserver.cpp \
-1
View File
@@ -43,7 +43,6 @@ INCLUDEPATH += . \
src/engine/local_engine \ src/engine/local_engine \
src/engine/network_engine \ src/engine/network_engine \
src/config \ src/config \
src/third_party/asn1 \
src/gui/qt \ src/gui/qt \
src/gui/qt/connecttoserverdialog \ src/gui/qt/connecttoserverdialog \
src/core \ src/core \
-3
View File
@@ -27,7 +27,6 @@ INCLUDEPATH += . \
src/engine/local_engine \ src/engine/local_engine \
src/engine/network_engine \ src/engine/network_engine \
src/config \ src/config \
src/third_party/asn1 \
src/core src/core
DEPENDPATH += . \ DEPENDPATH += . \
@@ -100,7 +99,6 @@ HEADERS += \
src/net/uploadhelper.h \ src/net/uploadhelper.h \
src/net/downloaderthread.h \ src/net/downloaderthread.h \
src/net/downloadhelper.h \ src/net/downloadhelper.h \
src/net/internalchatcleanerpacket.h \
src/engine/local_engine/cardsvalue.h \ src/engine/local_engine/cardsvalue.h \
src/engine/local_engine/localboard.h \ src/engine/local_engine/localboard.h \
src/engine/local_engine/localenginefactory.h \ src/engine/local_engine/localenginefactory.h \
@@ -201,7 +199,6 @@ SOURCES += \
src/net/common/transferhelper.cpp \ src/net/common/transferhelper.cpp \
src/net/common/uploaderthread.cpp \ src/net/common/uploaderthread.cpp \
src/net/common/uploadhelper.cpp \ src/net/common/uploadhelper.cpp \
src/net/common/internalchatcleanerpacket.cpp \
src/gui/generic/serverguiwrapper.cpp \ src/gui/generic/serverguiwrapper.cpp \
src/gui/qttoolsinterface.cpp \ src/gui/qttoolsinterface.cpp \
src/net/common/sendbuffer.cpp \ src/net/common/sendbuffer.cpp \
+9 -81
View File
@@ -18,96 +18,24 @@ QT -= core \
# PRECOMPILED_HEADER = src/pch_lib.h # PRECOMPILED_HEADER = src/pch_lib.h
INCLUDEPATH += . \ INCLUDEPATH += . \
src \ src
src/third_party/asn1
DEPENDPATH += . \ DEPENDPATH += . \
src \ src
src/third_party/asn1
# Input # Input
HEADERS += src/third_party/asn1/asn_application.h \
src/third_party/asn1/asn_codecs.h \ HEADERS += src/third_party/protobuf/pokerth.pb.h \
src/third_party/asn1/asn_codecs_prim.h \ src/third_party/protobuf/chatcleaner.pb.h
src/third_party/asn1/asn_internal.h \ SOURCES += src/third_party/protobuf/pokerth.pb.cc \
src/third_party/asn1/asn_SEQUENCE_OF.h \ src/third_party/protobuf/chatcleaner.pb.cc
src/third_party/asn1/asn_SET_OF.h \ win32 {
src/third_party/asn1/asn_system.h \
src/third_party/asn1/ber_decoder.h \
src/third_party/asn1/ber_tlv_length.h \
src/third_party/asn1/ber_tlv_tag.h \
src/third_party/asn1/BIT_STRING.h \
src/third_party/asn1/BOOLEAN.h \
src/third_party/asn1/constraints.h \
src/third_party/asn1/constr_CHOICE.h \
src/third_party/asn1/constr_SEQUENCE.h \
src/third_party/asn1/constr_SEQUENCE_OF.h \
src/third_party/asn1/constr_SET_OF.h \
src/third_party/asn1/constr_TYPE.h \
src/third_party/asn1/der_encoder.h \
src/third_party/asn1/INTEGER.h \
src/third_party/asn1/NativeEnumerated.h \
src/third_party/asn1/NativeInteger.h \
src/third_party/asn1/OCTET_STRING.h \
src/third_party/asn1/per_decoder.h \
src/third_party/asn1/per_encoder.h \
src/third_party/asn1/per_opentype.h \
src/third_party/asn1/per_support.h \
src/third_party/asn1/UTF8String.h \
src/third_party/asn1/xer_decoder.h \
src/third_party/asn1/xer_encoder.h \
src/third_party/asn1/xer_support.h \
src/third_party/asn1/ChatCleanerMessage.h \
src/third_party/asn1/CleanerChatTypeLobby.h \
src/third_party/asn1/CleanerChatTypeGame.h \
src/third_party/asn1/CleanerChatType.h \
src/third_party/asn1/CleanerInitMessage.h \
src/third_party/asn1/CleanerInitAckMessage.h \
src/third_party/asn1/CleanerChatRequestMessage.h \
src/third_party/asn1/CleanerChatReplyMessage.h
HEADERS += src/third_party/protobuf/pokerth.pb.h
SOURCES += src/third_party/asn1/ChatCleanerMessage.c \
src/third_party/asn1/CleanerChatTypeLobby.c \
src/third_party/asn1/CleanerChatTypeGame.c \
src/third_party/asn1/CleanerChatType.c \
src/third_party/asn1/CleanerInitMessage.c \
src/third_party/asn1/CleanerInitAckMessage.c \
src/third_party/asn1/CleanerChatRequestMessage.c \
src/third_party/asn1/CleanerChatReplyMessage.c \
src/third_party/asn1/xer_support.c \
src/third_party/asn1/xer_encoder.c \
src/third_party/asn1/xer_decoder.c \
src/third_party/asn1/UTF8String.c \
src/third_party/asn1/per_support.c \
src/third_party/asn1/per_opentype.c \
src/third_party/asn1/per_encoder.c \
src/third_party/asn1/per_decoder.c \
src/third_party/asn1/OCTET_STRING.c \
src/third_party/asn1/NativeInteger.c \
src/third_party/asn1/NativeEnumerated.c \
src/third_party/asn1/INTEGER.c \
src/third_party/asn1/der_encoder.c \
src/third_party/asn1/constraints.c \
src/third_party/asn1/constr_TYPE.c \
src/third_party/asn1/constr_SET_OF.c \
src/third_party/asn1/constr_SEQUENCE_OF.c \
src/third_party/asn1/constr_SEQUENCE.c \
src/third_party/asn1/constr_CHOICE.c \
src/third_party/asn1/BOOLEAN.c \
src/third_party/asn1/BIT_STRING.c \
src/third_party/asn1/ber_tlv_tag.c \
src/third_party/asn1/ber_tlv_length.c \
src/third_party/asn1/ber_decoder.c \
src/third_party/asn1/asn_SET_OF.c \
src/third_party/asn1/asn_SEQUENCE_OF.c \
src/third_party/asn1/asn_codecs_prim.c
SOURCES += src/third_party/protobuf/pokerth.pb.cc
win32 {
DEFINES += CURL_STATICLIB DEFINES += CURL_STATICLIB
DEFINES += _WIN32_WINNT=0x0501 DEFINES += _WIN32_WINNT=0x0501
} }
unix : !mac { unix : !mac {
INCLUDEPATH += $${PREFIX}/include INCLUDEPATH += $${PREFIX}/include
system(protoc pokerth.proto --cpp_out=src/third_party/protobuf) system(protoc pokerth.proto --cpp_out=src/third_party/protobuf)
system(protoc chatcleaner.proto --cpp_out=src/third_party/protobuf)
system(protoc pokerth.proto --java_out=tests/src) system(protoc pokerth.proto --java_out=tests/src)
} }
mac { mac {
-1
View File
@@ -30,7 +30,6 @@ INCLUDEPATH += . \
src/engine/local_engine \ src/engine/local_engine \
src/engine/network_engine \ src/engine/network_engine \
src/config \ src/config \
src/third_party/asn1 \
src/core \ src/core \
DEPENDPATH += . \ DEPENDPATH += . \
+69 -67
View File
@@ -34,7 +34,8 @@
#include <QtCore> #include <QtCore>
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
#include <third_party/asn1/ChatCleanerMessage.h> #include <third_party/protobuf/chatcleaner.pb.h>
#include "messagefilter.h" #include "messagefilter.h"
#include "cleanerconfig.h" #include "cleanerconfig.h"
@@ -93,23 +94,38 @@ void CleanerServer::onRead()
bool error = bytesRead < 1; bool error = bytesRead < 1;
if (!error) { if (!error) {
m_recvBufUsed += bytesRead; m_recvBufUsed += bytesRead;
bool valid;
asn_dec_rval_t retVal;
do { do {
// Try to decode the packets. valid = false;
InternalChatCleanerPacket recvMsg; if (m_recvBufUsed >= CLEANER_NET_HEADER_SIZE) {
retVal = ber_decode(0, &asn_DEF_ChatCleanerMessage, (void **)recvMsg.GetMsgPtr(), m_recvBuf, m_recvBufUsed); // Read the size of the packet (first 4 bytes in network byte order).
if(retVal.code == RC_OK) { uint32_t nativeVal;
if (retVal.consumed < m_recvBufUsed) { memcpy(&nativeVal, &m_recvBuf[0], sizeof(uint32_t));
m_recvBufUsed -= retVal.consumed; size_t packetSize = ntohl(nativeVal);
memmove(m_recvBuf, m_recvBuf + retVal.consumed, m_recvBufUsed); if (packetSize > MAX_CLEANER_PACKET_SIZE) {
} else
m_recvBufUsed = 0; m_recvBufUsed = 0;
qDebug() << "Invalid packet size: " << packetSize;
// Handle the packets. } else if (m_recvBufUsed >= packetSize + CLEANER_NET_HEADER_SIZE) {
error = handleMessage(recvMsg); try {
// Try to decode the packet.
boost::shared_ptr<ChatCleanerMessage> recvMsg(ChatCleanerMessage::default_instance().New());
if (recvMsg->ParseFromArray(&m_recvBuf[CLEANER_NET_HEADER_SIZE], static_cast<int>(packetSize))) {
m_recvBufUsed -= (packetSize + CLEANER_NET_HEADER_SIZE);
if (m_recvBufUsed) {
memmove(m_recvBuf, m_recvBuf + packetSize + CLEANER_NET_HEADER_SIZE, m_recvBufUsed);
}
}
// Handle the packet.
error = handleMessage(*recvMsg);
valid = true;
} catch (const exception &e) {
// Reset buffer on error.
m_recvBufUsed = 0;
qDebug() << "Exception while decoding packet: " << e.what();
}
}
} }
} while (!error && retVal.code == RC_OK); } while (valid && !error);
} }
if (error) { if (error) {
@@ -131,71 +147,58 @@ void CleanerServer::onRead()
tcpSocket->write(checkMessage.toAscii().data(), checkMessage.length());*/ tcpSocket->write(checkMessage.toAscii().data(), checkMessage.length());*/
} }
bool CleanerServer::handleMessage(InternalChatCleanerPacket &msg) bool CleanerServer::handleMessage(ChatCleanerMessage &msg)
{ {
bool error = true; bool error = true;
if (msg.GetMsg()->present == ChatCleanerMessage_PR_cleanerInitMessage) { if (msg.messagetype() == ChatCleanerMessage::Type_CleanerInitMessage) {
CleanerInitMessage_t *netInit = &msg.GetMsg()->choice.cleanerInitMessage; const CleanerInitMessage &netInit = msg.cleanerinitmessage();
if (netInit->requestedVersion == CLEANER_PROTOCOL_VERSION) { if (netInit.requestedversion() == CLEANER_PROTOCOL_VERSION) {
string tmpClientSecret((const char *)netInit->clientSecret.buf, netInit->clientSecret.size); if (clientSecret == QString::fromStdString(netInit.clientsecret())) {
if (clientSecret == QString::fromStdString(tmpClientSecret)) {
error = false; error = false;
InternalChatCleanerPacket tmpAck; boost::shared_ptr<ChatCleanerMessage> tmpAck(ChatCleanerMessage::default_instance().New());
tmpAck.GetMsg()->present = ChatCleanerMessage_PR_cleanerInitAckMessage; tmpAck->set_messagetype(ChatCleanerMessage::Type_CleanerInitAckMessage);
CleanerInitAckMessage_t *netAck = &tmpAck.GetMsg()->choice.cleanerInitAckMessage; CleanerInitAckMessage *netAck = tmpAck->mutable_cleanerinitackmessage();
netAck->serverVersion = CLEANER_PROTOCOL_VERSION; netAck->set_serverversion(CLEANER_PROTOCOL_VERSION);
string tmpServerSecret(serverSecret.toStdString()); netAck->set_serversecret(serverSecret.toStdString());
OCTET_STRING_fromBuf(&netAck->serverSecret, sendMessageToClient(*tmpAck);
tmpServerSecret.c_str(),
tmpServerSecret.length());
sendMessageToClient(tmpAck);
} else } else
qDebug() << "Invalid client secret."; qDebug() << "Invalid client secret.";
} else } else
qDebug() << "Invalid client version: " << netInit->requestedVersion; qDebug() << "Invalid client version: " << netInit.requestedversion();
} else if (msg.GetMsg()->present == ChatCleanerMessage_PR_cleanerChatRequestMessage) { } else if (msg.messagetype() == ChatCleanerMessage::Type_CleanerChatRequestMessage) {
error = false; error = false;
CleanerChatRequestMessage_t *netRequest = &msg.GetMsg()->choice.cleanerChatRequestMessage; const CleanerChatRequestMessage &netRequest = msg.cleanerchatrequestmessage();
unsigned playerId = netRequest->playerId; unsigned playerId = netRequest.playerid();
QString nick(QString::fromUtf8( QString nick(QString::fromUtf8(netRequest.playername().c_str()));
string((const char *)netRequest->playerName.buf, netRequest->playerName.size).c_str())); QString message(QString::fromUtf8(netRequest.chatmessage().c_str()));
QString message(QString::fromUtf8( unsigned gameId = netRequest.gameid();
string((const char *)netRequest->chatMessage.buf, netRequest->chatMessage.size).c_str()));
unsigned gameId = 0;
if (netRequest->cleanerChatType.present == CleanerChatType_PR_cleanerChatTypeGame) {
gameId = netRequest->cleanerChatType.choice.cleanerChatTypeGame.gameId;
}
QStringList checkreturn = myMessageFilter->check(gameId, playerId, nick, message); QStringList checkreturn = myMessageFilter->check(gameId, playerId, nick, message);
QString checkAction = checkreturn.at(0); QString checkAction = checkreturn.at(0);
QString checkMessage = checkreturn.at(1); QString checkMessage = checkreturn.at(1);
if (!checkAction.isEmpty()) { if (!checkAction.isEmpty()) {
InternalChatCleanerPacket tmpReply; boost::shared_ptr<ChatCleanerMessage> tmpReply(ChatCleanerMessage::default_instance().New());
tmpReply.GetMsg()->present = ChatCleanerMessage_PR_cleanerChatReplyMessage; tmpReply->set_messagetype(ChatCleanerMessage::Type_CleanerChatReplyMessage);
CleanerChatReplyMessage_t *netReply = &tmpReply.GetMsg()->choice.cleanerChatReplyMessage; CleanerChatReplyMessage *netReply = tmpReply->mutable_cleanerchatreplymessage();
netReply->requestId = netRequest->requestId; netReply->set_requestid(netRequest.requestid());
netReply->cleanerChatType = netRequest->cleanerChatType; netReply->set_gameid(netRequest.gameid());
netReply->playerId = netRequest->playerId; netReply->set_cleanerchattype(netRequest.cleanerchattype());
netReply->set_playerid(netRequest.playerid());
if(checkAction == "warn") { if(checkAction == "warn") {
netReply->cleanerActionType = cleanerActionType_cleanerActionWarning; netReply->set_cleaneractiontype(CleanerChatReplyMessage_CleanerActionType_cleanerActionWarning);
} else if (checkAction == "kick") { } else if (checkAction == "kick") {
netReply->cleanerActionType = cleanerActionType_cleanerActionKick; netReply->set_cleaneractiontype(CleanerChatReplyMessage_CleanerActionType_cleanerActionKick);
} else if (checkAction == "kickban") { } else if (checkAction == "kickban") {
netReply->cleanerActionType = cleanerActionType_cleanerActionBan; netReply->set_cleaneractiontype(CleanerChatReplyMessage_CleanerActionType_cleanerActionBan);
} else if (checkAction == "mute") { } else if (checkAction == "mute") {
netReply->cleanerActionType = cleanerActionType_cleanerActionMute; netReply->set_cleaneractiontype(CleanerChatReplyMessage_CleanerActionType_cleanerActionMute);
} }
string tmpCheck(checkMessage.toUtf8()); netReply->set_cleanertext(checkMessage.toUtf8());
netReply->cleanerText = sendMessageToClient(*tmpReply);
OCTET_STRING_new_fromBuf(
&asn_DEF_OCTET_STRING,
(const char *)tmpCheck.c_str(),
tmpCheck.length());
sendMessageToClient(tmpReply);
} }
} }
return error; return error;
@@ -220,14 +223,13 @@ void CleanerServer::refreshConfig()
myMessageFilter->refreshConfig(); myMessageFilter->refreshConfig();
} }
void CleanerServer::sendMessageToClient(InternalChatCleanerPacket &msg) void CleanerServer::sendMessageToClient(ChatCleanerMessage &msg)
{ {
unsigned char buf[MAX_CLEANER_PACKET_SIZE]; uint32_t packetSize = msg.ByteSize();
asn_enc_rval_t e = der_encode_to_buffer(&asn_DEF_ChatCleanerMessage, msg.GetMsg(), buf, MAX_CLEANER_PACKET_SIZE); google::protobuf::uint8 *buf = new google::protobuf::uint8[packetSize + CLEANER_NET_HEADER_SIZE];
*((uint32_t *)buf) = htonl(packetSize);
if (e.encoded == -1) msg.SerializeWithCachedSizesToArray(&buf[CLEANER_NET_HEADER_SIZE]);
qDebug() << "Failed to encode chat cleaner packet: " << msg.GetMsg()->present; tcpSocket->write((const char *)buf, packetSize + CLEANER_NET_HEADER_SIZE);
else delete[] buf;
tcpSocket->write((const char *)buf, e.encoded);
} }
+8 -4
View File
@@ -33,10 +33,14 @@
#include <QtCore> #include <QtCore>
#include <QtNetwork> #include <QtNetwork>
#include <net/internalchatcleanerpacket.h>
#define CLEANER_NET_HEADER_SIZE 4
#define MAX_CLEANER_PACKET_SIZE 512
#define CLEANER_PROTOCOL_VERSION 2
class MessageFilter; class MessageFilter;
class CleanerConfig; class CleanerConfig;
class ChatCleanerMessage;
class CleanerServer: public QObject class CleanerServer: public QObject
{ {
@@ -49,10 +53,10 @@ public:
private slots: private slots:
void newCon(); void newCon();
void onRead(); void onRead();
bool handleMessage(InternalChatCleanerPacket &msg); bool handleMessage(ChatCleanerMessage &msg);
void socketStateChanged(QAbstractSocket::SocketState); void socketStateChanged(QAbstractSocket::SocketState);
void refreshConfig(); void refreshConfig();
void sendMessageToClient(InternalChatCleanerPacket &msg); void sendMessageToClient(ChatCleanerMessage &msg);
private: private:
QTcpServer *tcpServer; QTcpServer *tcpServer;
@@ -66,7 +70,7 @@ private:
QString serverSecret; QString serverSecret;
unsigned char m_recvBuf[2*MAX_CLEANER_PACKET_SIZE]; unsigned char m_recvBuf[2*MAX_CLEANER_PACKET_SIZE];
unsigned m_recvBufUsed; size_t m_recvBufUsed;
int secondsSinceLastConfigChange; int secondsSinceLastConfigChange;
}; };
+7 -3
View File
@@ -37,9 +37,13 @@
#include <boost/enable_shared_from_this.hpp> #include <boost/enable_shared_from_this.hpp>
#include <string> #include <string>
#include <net/chatcleanercallback.h> #include <net/chatcleanercallback.h>
#include <net/internalchatcleanerpacket.h>
#define CLEANER_NET_HEADER_SIZE 4
#define MAX_CLEANER_PACKET_SIZE 512
#define CLEANER_PROTOCOL_VERSION 2
class SendBuffer; class SendBuffer;
class ChatCleanerMessage;
class ChatCleanerManager : public boost::enable_shared_from_this<ChatCleanerManager> class ChatCleanerManager : public boost::enable_shared_from_this<ChatCleanerManager>
{ {
@@ -58,9 +62,9 @@ protected:
void HandleResolve(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); void HandleResolve(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
void HandleConnect(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); void HandleConnect(const boost::system::error_code& ec, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
void HandleRead(const boost::system::error_code &ec, size_t bytesRead); void HandleRead(const boost::system::error_code &ec, size_t bytesRead);
bool HandleMessage(InternalChatCleanerPacket &msg); bool HandleMessage(ChatCleanerMessage &msg);
void SendMessageToServer(InternalChatCleanerPacket &msg); void SendMessageToServer(ChatCleanerMessage &msg);
unsigned GetNextRequestId(); unsigned GetNextRequestId();
private: private:
+73 -65
View File
@@ -33,7 +33,7 @@
#include <net/sendbuffer.h> #include <net/sendbuffer.h>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include <core/loghelper.h> #include <core/loghelper.h>
#include <third_party/asn1/ChatCleanerMessage.h> #include <third_party/protobuf/chatcleaner.pb.h>
#include <sstream> #include <sstream>
@@ -98,24 +98,20 @@ void
ChatCleanerManager::HandleGameChatText(unsigned gameId, unsigned playerId, const std::string &name, const std::string &text) ChatCleanerManager::HandleGameChatText(unsigned gameId, unsigned playerId, const std::string &name, const std::string &text)
{ {
if (m_connected) { if (m_connected) {
InternalChatCleanerPacket tmpChat; boost::shared_ptr<ChatCleanerMessage> tmpChat(ChatCleanerMessage::default_instance().New());
tmpChat.GetMsg()->present = ChatCleanerMessage_PR_cleanerChatRequestMessage; tmpChat->set_messagetype(ChatCleanerMessage::Type_CleanerChatRequestMessage);
CleanerChatRequestMessage_t *netRequest = &tmpChat.GetMsg()->choice.cleanerChatRequestMessage; CleanerChatRequestMessage *netRequest = tmpChat->mutable_cleanerchatrequestmessage();
netRequest->requestId = GetNextRequestId(); netRequest->set_requestid(GetNextRequestId());
if (gameId) { if (gameId) {
netRequest->cleanerChatType.present = CleanerChatType_PR_cleanerChatTypeGame; netRequest->set_cleanerchattype(cleanerChatTypeGame);
netRequest->cleanerChatType.choice.cleanerChatTypeGame.gameId = gameId; netRequest->set_gameid(gameId);
} else { } else {
netRequest->cleanerChatType.present = CleanerChatType_PR_cleanerChatTypeLobby; netRequest->set_cleanerchattype(cleanerChatTypeLobby);
} }
netRequest->playerId = playerId; netRequest->set_playerid(playerId);
OCTET_STRING_fromBuf(&netRequest->playerName, netRequest->set_playername(name);
name.c_str(), netRequest->set_chatmessage(text);
(int)name.length()); SendMessageToServer(*tmpChat);
OCTET_STRING_fromBuf(&netRequest->chatMessage,
text.c_str(),
(int)text.length());
SendMessageToServer(tmpChat);
} }
} }
@@ -141,14 +137,12 @@ ChatCleanerManager::HandleConnect(const boost::system::error_code& ec,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator) boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
{ {
if (!ec) { if (!ec) {
InternalChatCleanerPacket tmpInit; boost::shared_ptr<ChatCleanerMessage> tmpInit(ChatCleanerMessage::default_instance().New());
tmpInit.GetMsg()->present = ChatCleanerMessage_PR_cleanerInitMessage; tmpInit->set_messagetype(ChatCleanerMessage::Type_CleanerInitMessage);
CleanerInitMessage_t *netInit = &tmpInit.GetMsg()->choice.cleanerInitMessage; CleanerInitMessage *netInit = tmpInit->mutable_cleanerinitmessage();
netInit->requestedVersion = CLEANER_PROTOCOL_VERSION; netInit->set_requestedversion(CLEANER_PROTOCOL_VERSION);
OCTET_STRING_fromBuf(&netInit->clientSecret, netInit->set_clientsecret(m_clientSecret);
m_clientSecret.c_str(), SendMessageToServer(*tmpInit);
(int)m_clientSecret.length());
SendMessageToServer(tmpInit);
m_socket->async_read_some( m_socket->async_read_some(
boost::asio::buffer(m_recvBuf, sizeof(m_recvBuf)), boost::asio::buffer(m_recvBuf, sizeof(m_recvBuf)),
boost::bind( boost::bind(
@@ -180,24 +174,38 @@ ChatCleanerManager::HandleRead(const boost::system::error_code &ec, size_t bytes
bool error = false; bool error = false;
m_recvBufUsed += bytesRead; m_recvBufUsed += bytesRead;
asn_dec_rval_t retVal; bool valid;
do { do {
InternalChatCleanerPacket recvMsg; valid = false;
retVal = ber_decode(0, &asn_DEF_ChatCleanerMessage, (void **)recvMsg.GetMsgPtr(), m_recvBuf, m_recvBufUsed); if (m_recvBufUsed >= CLEANER_NET_HEADER_SIZE) {
if(retVal.code == RC_OK) { // Read the size of the packet (first 4 bytes in network byte order).
// Consume the bytes. uint32_t nativeVal;
if (retVal.consumed < m_recvBufUsed) { memcpy(&nativeVal, &m_recvBuf[0], sizeof(uint32_t));
m_recvBufUsed -= retVal.consumed; size_t packetSize = ntohl(nativeVal);
memmove(m_recvBuf, m_recvBuf + retVal.consumed, m_recvBufUsed); if (packetSize > MAX_CLEANER_PACKET_SIZE) {
} else
m_recvBufUsed = 0; m_recvBufUsed = 0;
LOG_ERROR("Invalid packet size: " << packetSize);
if (asn_check_constraints(&asn_DEF_ChatCleanerMessage, recvMsg.GetMsg(), NULL, NULL) == 0) } else if (m_recvBufUsed >= packetSize + CLEANER_NET_HEADER_SIZE) {
error = HandleMessage(recvMsg); try {
else // Try to decode the packet.
LOG_ERROR("Received invalid chat cleaner packet."); boost::shared_ptr<ChatCleanerMessage> recvMsg(ChatCleanerMessage::default_instance().New());
if (recvMsg->ParseFromArray(&m_recvBuf[CLEANER_NET_HEADER_SIZE], static_cast<int>(packetSize))) {
m_recvBufUsed -= (packetSize + CLEANER_NET_HEADER_SIZE);
if (m_recvBufUsed) {
memmove(m_recvBuf, m_recvBuf + packetSize + CLEANER_NET_HEADER_SIZE, m_recvBufUsed);
}
}
// Handle the packet.
error = HandleMessage(*recvMsg);
valid = true;
} catch (const exception &e) {
// Reset buffer on error.
m_recvBufUsed = 0;
LOG_ERROR("Exception while decoding packet: " << e.what());
}
}
} }
} while (!error && retVal.code == RC_OK); } while (valid && !error);
if (!error) { if (!error) {
m_socket->async_read_some( m_socket->async_read_some(
@@ -224,14 +232,13 @@ ChatCleanerManager::HandleRead(const boost::system::error_code &ec, size_t bytes
} }
bool bool
ChatCleanerManager::HandleMessage(InternalChatCleanerPacket &msg) ChatCleanerManager::HandleMessage(ChatCleanerMessage &msg)
{ {
bool error = true; bool error = true;
if (msg.GetMsg()->present == ChatCleanerMessage_PR_cleanerInitAckMessage) { if (msg.messagetype() == ChatCleanerMessage::Type_CleanerInitAckMessage) {
CleanerInitAckMessage_t *netAck = &msg.GetMsg()->choice.cleanerInitAckMessage; const CleanerInitAckMessage &netAck = msg.cleanerinitackmessage();
if (netAck->serverVersion == CLEANER_PROTOCOL_VERSION) { if (netAck.serverversion() == CLEANER_PROTOCOL_VERSION) {
string tmpSecret((const char *)netAck->serverSecret.buf, netAck->serverSecret.size); if (m_serverSecret == netAck.serversecret()) {
if (m_serverSecret == tmpSecret) {
m_connected = true; m_connected = true;
error = false; error = false;
LOG_MSG("Successfully connected to chat cleaner."); LOG_MSG("Successfully connected to chat cleaner.");
@@ -239,36 +246,37 @@ ChatCleanerManager::HandleMessage(InternalChatCleanerPacket &msg)
} }
if (!m_connected) if (!m_connected)
LOG_ERROR("Chat cleaner handshake failed."); LOG_ERROR("Chat cleaner handshake failed.");
} else if (msg.GetMsg()->present == ChatCleanerMessage_PR_cleanerChatReplyMessage) { } else if (msg.messagetype() == ChatCleanerMessage::Type_CleanerChatReplyMessage) {
CleanerChatReplyMessage_t *netReply = &msg.GetMsg()->choice.cleanerChatReplyMessage; const CleanerChatReplyMessage &netReply = msg.cleanerchatreplymessage();
if (netReply->cleanerText) { if (!netReply.cleanertext().empty()) {
if (netReply->cleanerChatType.present == CleanerChatType_PR_cleanerChatTypeLobby) { if (netReply.cleanerchattype() == cleanerChatTypeLobby) {
m_callback.SignalChatBotMessage(string((const char *)netReply->cleanerText->buf, netReply->cleanerText->size)); m_callback.SignalChatBotMessage(netReply.cleanertext());
} else if (netReply->cleanerChatType.present == CleanerChatType_PR_cleanerChatTypeGame) { } else if (netReply.cleanerchattype() == cleanerChatTypeGame) {
m_callback.SignalChatBotMessage(netReply->cleanerChatType.choice.cleanerChatTypeGame.gameId, string((const char *)netReply->cleanerText->buf, netReply->cleanerText->size)); m_callback.SignalChatBotMessage(netReply.gameid(), netReply.cleanertext());
} }
} }
if (netReply->cleanerActionType == cleanerActionType_cleanerActionKick) if (netReply.cleaneractiontype() == CleanerChatReplyMessage_CleanerActionType_cleanerActionKick)
m_callback.SignalKickPlayer(netReply->playerId); m_callback.SignalKickPlayer(netReply.playerid());
else if (netReply->cleanerActionType == cleanerActionType_cleanerActionBan) else if (netReply.cleaneractiontype() == CleanerChatReplyMessage_CleanerActionType_cleanerActionBan)
m_callback.SignalBanPlayer(netReply->playerId); m_callback.SignalBanPlayer(netReply.playerid());
else if (netReply->cleanerActionType == cleanerActionType_cleanerActionMute) else if (netReply.cleaneractiontype() == CleanerChatReplyMessage_CleanerActionType_cleanerActionMute)
m_callback.SignalMutePlayer(netReply->playerId); m_callback.SignalMutePlayer(netReply.playerid());
error = false; error = false;
} }
return error; return error;
} }
void void
ChatCleanerManager::SendMessageToServer(InternalChatCleanerPacket &msg) ChatCleanerManager::SendMessageToServer(ChatCleanerMessage &msg)
{ {
asn_enc_rval_t e = der_encode(&asn_DEF_ChatCleanerMessage, msg.GetMsg(), &SendBuffer::EncodeToBuf, m_sendManager.get()); uint32_t packetSize = msg.ByteSize();
google::protobuf::uint8 *buf = new google::protobuf::uint8[packetSize + CLEANER_NET_HEADER_SIZE];
*((uint32_t *)buf) = htonl(packetSize);
msg.SerializeWithCachedSizesToArray(&buf[CLEANER_NET_HEADER_SIZE]);
SendBuffer::EncodeToBuf(buf, packetSize + CLEANER_NET_HEADER_SIZE, m_sendManager.get());
delete[] buf;
if (e.encoded == -1) m_sendManager->AsyncSendNextPacket(m_socket);
LOG_ERROR("Failed to encode chat cleaner packet: " << msg.GetMsg()->present);
else {
m_sendManager->AsyncSendNextPacket(m_socket);
}
} }
unsigned unsigned
@@ -1,47 +0,0 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* *
* Additional permission under GNU AGPL version 3 section 7 *
* *
* If you modify this program, or any covered work, by linking or *
* combining it with the OpenSSL project's OpenSSL library (or a *
* modified version of that library), containing parts covered by the *
* terms of the OpenSSL or SSLeay licenses, the authors of PokerTH *
* (Felix Hammer, Florian Thauer, Lothar May) grant you additional *
* permission to convey the resulting work. *
* Corresponding Source for a non-source form of such a combination *
* shall include the source code for the parts of OpenSSL used as well *
* as that of the covered work. *
*****************************************************************************/
#include <net/internalchatcleanerpacket.h>
#include <third_party/asn1/ChatCleanerMessage.h>
#include <cstring>
using namespace std;
InternalChatCleanerPacket::InternalChatCleanerPacket()
{
m_msg = (ChatCleanerMessage_t *)calloc(1, sizeof(ChatCleanerMessage_t));
}
InternalChatCleanerPacket::~InternalChatCleanerPacket()
{
ASN_STRUCT_FREE(asn_DEF_ChatCleanerMessage, m_msg);
}
-62
View File
@@ -1,62 +0,0 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* *
* Additional permission under GNU AGPL version 3 section 7 *
* *
* If you modify this program, or any covered work, by linking or *
* combining it with the OpenSSL project's OpenSSL library (or a *
* modified version of that library), containing parts covered by the *
* terms of the OpenSSL or SSLeay licenses, the authors of PokerTH *
* (Felix Hammer, Florian Thauer, Lothar May) grant you additional *
* permission to convey the resulting work. *
* Corresponding Source for a non-source form of such a combination *
* shall include the source code for the parts of OpenSSL used as well *
* as that of the covered work. *
*****************************************************************************/
/* PokerTH chat cleaner packet. */
#ifndef _INTERNALCHATCLEANERPACKET_H_
#define _INTERNALCHATCLEANERPACKET_H_
#define CLEANER_PROTOCOL_VERSION 2
#define MAX_CLEANER_PACKET_SIZE 384
typedef struct ChatCleanerMessage ChatCleanerMessage_t;
class InternalChatCleanerPacket
{
public:
InternalChatCleanerPacket();
InternalChatCleanerPacket(ChatCleanerMessage_t *msg)
: m_msg(msg) {
}
~InternalChatCleanerPacket();
ChatCleanerMessage_t *GetMsg() {
return m_msg;
}
ChatCleanerMessage_t **GetMsgPtr() {
return &m_msg;
}
private:
ChatCleanerMessage_t *m_msg;
};
#endif
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff