summaryrefslogtreecommitdiff
path: root/csgo-loader/csgo-server/Networking
diff options
context:
space:
mode:
Diffstat (limited to 'csgo-loader/csgo-server/Networking')
-rw-r--r--csgo-loader/csgo-server/Networking/TCPServer.cpp32
-rw-r--r--csgo-loader/csgo-server/Networking/TCPServer.hpp7
-rw-r--r--csgo-loader/csgo-server/Networking/WebSocket.cpp17
3 files changed, 23 insertions, 33 deletions
diff --git a/csgo-loader/csgo-server/Networking/TCPServer.cpp b/csgo-loader/csgo-server/Networking/TCPServer.cpp
index c381c85..d93a710 100644
--- a/csgo-loader/csgo-server/Networking/TCPServer.cpp
+++ b/csgo-loader/csgo-server/Networking/TCPServer.cpp
@@ -4,7 +4,7 @@ namespace Networking
{
void TCPConnection::Close()
{
- printf("[ <= ] disconnected\n");
+ printf("[ <= ] %s disconnected!\n", m_IpAddress);
if(m_Socket)
closesocket(m_Socket);
@@ -18,12 +18,10 @@ namespace Networking
// Send data.
int32_t Result = send(m_Socket, (char *)Bytes.data(), (int)Bytes.size(), 0);
- if(m_IpAddress)
-
- printf("[ => ] %zd bytes\n", Bytes.size());
+ printf("[ => ] Sending %zd bytes to %s.\n", Bytes.size(), m_IpAddress);
if(Result == -1)
- printf("[ => ] %zd bytes failed (%d)\n", Bytes.size(), WSAGetLastError());
+ printf("[ E! ] Failed to send %zd bytes to %s. (Socket %04Ix)\n", Bytes.size(), m_IpAddress, m_Socket);
}
ByteArray TCPConnection::ReceiveRawBytes()
@@ -43,7 +41,7 @@ namespace Networking
// Emplace all received bytes.
for(int n = 0; n < Received; ++n)
{
- ReceivedBytes.emplace_back(RecvBuffer[n]);
+ ReceivedBytes.push_back(RecvBuffer[n]);
}
// No more bytes left to receive.
@@ -51,7 +49,7 @@ namespace Networking
break;
}
- printf("[ <= ] %zd bytes\n", ReceivedBytes.size());
+ printf("[ <= ] Received %zd bytes from %s.\n", ReceivedBytes.size(), m_IpAddress);
return ReceivedBytes;
}
@@ -90,8 +88,8 @@ namespace Networking
// Set up server context.
m_Context.sin_addr.s_addr = INADDR_ANY;
- m_Context.sin_family = AF_INET;
- m_Context.sin_port = htons(ServerPort);
+ m_Context.sin_family = AF_INET;
+ m_Context.sin_port = htons(ServerPort);
int32_t Bind = bind(m_Socket, (sockaddr *)&m_Context, sizeof sockaddr_in);
@@ -99,7 +97,7 @@ namespace Networking
return false;
// Start listening.
- printf("[INFO] Server listening on port %d.\n", ServerPort);
+ printf("[ i! ] Server listening on port %d.\n", ServerPort);
listen(m_Socket, 1);
return true;
@@ -121,23 +119,17 @@ namespace Networking
Encryption.Start();
// Attempt handshake with client.
- TCPConnection Connection(IncomingSocket, inet_ntoa(IncomingConnection.sin_addr), Encryption);
+ const char *IpAddress = inet_ntoa(IncomingConnection.sin_addr);
+ TCPConnection Connection(IncomingSocket, IpAddress, Encryption);
ByteArray EncryptionKey = Connection.GetEncryptionKey();
Connection.SendRawBytes(EncryptionKey);
- // Print out handshake header
- printf("[ => ] Handshake: ");
-
- for(auto &It : EncryptionKey)
- printf("%02x ", It);
-
- printf("\n");
-
// Detach a thread to handle the connection.
std::thread thread([&]
{
- // smol fix :^)
+ // Fix for crashing when there is no connection handler
+ // (literally happened to me once)
if(m_ConnectionHandler)
m_ConnectionHandler(Connection);
diff --git a/csgo-loader/csgo-server/Networking/TCPServer.hpp b/csgo-loader/csgo-server/Networking/TCPServer.hpp
index 31beec8..a29a796 100644
--- a/csgo-loader/csgo-server/Networking/TCPServer.hpp
+++ b/csgo-loader/csgo-server/Networking/TCPServer.hpp
@@ -23,13 +23,14 @@ namespace Networking
{
SOCKET m_Socket;
Wrapper::Encryption m_Encryption;
- const char *m_IpAddress;
+ char m_IpAddress[32];
public:
// Initialiser for TCPConnection class.
TCPConnection(SOCKET Connection, const char *IpAddress, Wrapper::Encryption &RSA) :
- m_Encryption(RSA), m_Socket(Connection), m_IpAddress(IpAddress)
+ m_Encryption(RSA), m_Socket(Connection)
{
- printf("[ => ] connected\n");
+ strcpy_s<32>(m_IpAddress, IpAddress);
+ printf("[ => ] %s connected!\n", m_IpAddress);
}
// Release the connection once it goes out of scope.
diff --git a/csgo-loader/csgo-server/Networking/WebSocket.cpp b/csgo-loader/csgo-server/Networking/WebSocket.cpp
index 75b5731..2c42001 100644
--- a/csgo-loader/csgo-server/Networking/WebSocket.cpp
+++ b/csgo-loader/csgo-server/Networking/WebSocket.cpp
@@ -21,30 +21,27 @@ namespace Networking
// Receives a response from a request.
ByteArray WebSocket::Request(const char *File, const char *Header, ByteArray &Data)
{
- DWORD ReceivedSize{};
ByteArray Response;
InternetHandle WebRequest = HttpOpenRequestA(m_Address, "POST", File, 0, 0, 0, INTERNET_FLAG_SECURE | INTERNET_FLAG_KEEP_CONNECTION, 0);
- if(!WebRequest)
- return Response;
-
// Make connection request.
bool Sent = HttpSendRequestA(WebRequest, Header, (DWORD)strlen(Header), Data.data(), (DWORD)Data.size());
if(Sent)
{
- // Allocate a buffer to read the response into.
- uint8_t *Block = (uint8_t *)malloc(0x1000);
+ DWORD ReceivedSize{};
+
+ uint8_t *Block = (uint8_t *)malloc(4096);
// Read response.
- while(InternetReadFile(WebRequest, Block, 0x1000, &ReceivedSize))
+ while(InternetReadFile(WebRequest, Block, 4096, &ReceivedSize))
{
- const size_t RequestRange = std::min< int >(0x1000, ReceivedSize);
- for(size_t n{}; n < RequestRange; ++n)
+ for(size_t n{}; n < std::min< int >(4096, ReceivedSize); ++n)
+ {
Response.push_back(Block[n]);
+ }
}
- // Free the buffer to avoid leaking memory.
free(Block);
}