1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-17 16:53:13 +00:00

Added rate limiter on MeshAgent.SendCommand for console messages

This commit is contained in:
Bryan Roe
2022-01-11 00:31:11 -08:00
parent 2274477316
commit f7ea61512a
2 changed files with 26 additions and 0 deletions

View File

@@ -728,6 +728,7 @@ duk_ret_t ILibDuktape_MeshAgent_GenerateCertificate(duk_context *ctx)
#endif
}
// Javascript SendCommand(obj), send some data to the MeshCentral server
// This method can handle buffers, string or objects as input.
duk_ret_t ILibDuktape_MeshAgent_SendCommand(duk_context *ctx)
@@ -767,6 +768,25 @@ duk_ret_t ILibDuktape_MeshAgent_SendCommand(duk_context *ctx)
else
{
// We are trying to send an object, perform JSON serialization first
if (strcasecmp(Duktape_GetStringPropertyValue(ctx, 0, "action", ""), "msg") == 0 && strcasecmp(Duktape_GetStringPropertyValue(ctx, 0, "type", ""), "console") == 0)
{
// sendConsoleText()
long current = ILibGetTimeStamp();
if (agent->consoleText_timeStamp == 0 || current - agent->consoleText_timeStamp > 1000)
{
agent->consoleText_timeStamp = current;
agent->consoleText_counter = 1;
}
else
{
if (agent->consoleText_counter++ > agent->consoleText_maxRate)
{
return(0);
}
}
}
duk_dup(ctx, 0); // [object]
duk_json_encode(ctx, -1); // [json]
buffer = (char*)duk_get_lstring(ctx, -1, &bufferLen);
@@ -1896,6 +1916,7 @@ void ILibDuktape_MeshAgent_PUSH(duk_context *ctx, void *chain)
ILibDuktape_CreateEventWithGetter(ctx, "controlChannelDebug", ILibDuktape_MeshAgent_controlChannelDebug);
ILibDuktape_CreateInstanceMethod(ctx, "DataPing", ILibDuktape_MeshAgent_DataPing, DUK_VARARGS);
ILibDuktape_CreateReadonlyProperty_int(ctx, "ARCHID", MESH_AGENTID);
ILibDuktape_CreateReadonlyProperty_int(ctx, "ConsoleTextMaxRate", agent->consoleText_maxRate);
#ifdef _LINKVM
ILibDuktape_CreateReadonlyProperty_int(ctx, "hasKVM", 1);
ILibDuktape_EventEmitter_CreateEventEx(emitter, "kvmConnected");
@@ -4046,6 +4067,7 @@ void MeshServer_Connect(MeshAgentHostContainer *agent)
agent->controlChannelDebug = ILibSimpleDataStore_Get(agent->masterDb, "controlChannelDebug", NULL, 0);
ILibDuktape_HECI_Debug = (ILibSimpleDataStore_Get(agent->masterDb, "heciDebug", NULL, 0) != 0);
agent->timerLogging = ILibSimpleDataStore_Get(agent->masterDb, "timerLogging", NULL, 0);
agent->consoleText_maxRate = ILibSimpleDataStore_GetInt(agent->masterDb, "consoleTextMaxRate", 10);
#if defined(_LINKVM) && defined(_POSIX) && !defined(__APPLE__)
SLAVELOG = ILibSimpleDataStore_Get(agent->masterDb, "slaveKvmLog", NULL, 0);

View File

@@ -250,6 +250,9 @@ typedef struct MeshAgentHostContainer
char *meshServiceName;
char *displayName;
int serviceReserved;
long consoleText_timeStamp;
int consoleText_counter;
int consoleText_maxRate;
#if defined(_WINSERVICE)
int runningAsConsole;
#endif
@@ -272,6 +275,7 @@ char* MeshAgent_MakeAbsolutePathEx(char *basePath, char *localPath, int escapeBa
AgentCapabilities: Integer Mask, specifying supported Agent Capabilities
agentName If set, this will be sent to the server instead of the hostname
compactDirtyMinimum Minimum dirty bytes threshold for db.compact() operation
consoleTextMaxRate: Sets rate limit for sendConsoleText. Default is 10 messages per second.
controlChannelDebug: If set, will log/display controlChannel messages (Except for JSON messages)
controlChannelIdleTimeout: Integer value specifying the idle timeout in seconds, to send Ping/Pong to server, to keep connection alive
coreDumpEnabled: If set, a dump file will be written when the agent crashes