From b42f5a2ae787a4340c81d048b84c7d0206527851 Mon Sep 17 00:00:00 2001 From: mikhailm-coder Date: Mon, 10 Aug 2026 16:50:05 +0200 Subject: [PATCH] Restore upstream server core execution in OpenFrame mode Removes the two openFrameMode branches that executed a disk CoreModule.js instead of the server-pushed core (push handler and CoreOk handler), and the now-unused buildOpenframeCoreModulePath helper. The core is delivered by the meshcentral server again, per upstream design; the OpenFrame gateway URL patch now lives in the server's agents/meshcore.js. Kept: authToken, control-channel auth header, binary self-update suppression, cert-rotation and NodeID relaxations. Co-Authored-By: Claude Fable 5 --- meshcore/agentcore.c | 178 +++---------------------------------------- 1 file changed, 12 insertions(+), 166 deletions(-) diff --git a/meshcore/agentcore.c b/meshcore/agentcore.c index 3a6bfe2b..9537bf60 100644 --- a/meshcore/agentcore.c +++ b/meshcore/agentcore.c @@ -2481,73 +2481,6 @@ char* MeshAgent_MakeAbsolutePathEx(char *basePath, char *localPath, int escapeBa return(ILibScratchPad2); } -// TODO: make as external cmd arg and configure from openframe agent -// Build path to CoreModule.js for OpenFrame mode with fallback logic: -// 1. First try next to the executable -// 2. If not found and we're in a .app bundle, try next to the .app -// Returns: Path to CoreModule.js (uses ILibScratchPad for the result) -static char* buildOpenframeCoreModulePath(const char* exePath) -{ - char* lastSep; - FILE* testFile; - -#ifdef WIN32 - lastSep = strrchr(exePath, '\\'); - if (lastSep != NULL) - { - snprintf(ILibScratchPad, sizeof(ILibScratchPad), "%.*s\\CoreModule.js", - (int)(lastSep - exePath), exePath); - } - else - { - strcpy_s(ILibScratchPad, sizeof(ILibScratchPad), "CoreModule.js"); - } -#else - // First try: next to the executable - lastSep = strrchr(exePath, '/'); - if (lastSep != NULL) - { - snprintf(ILibScratchPad, sizeof(ILibScratchPad), "%.*s/CoreModule.js", - (int)(lastSep - exePath), exePath); - } - else - { - strcpy_s(ILibScratchPad, sizeof(ILibScratchPad), "CoreModule.js"); - } - - // Check if file exists at first location - testFile = fopen(ILibScratchPad, "rb"); - if (testFile != NULL) - { - fclose(testFile); - return ILibScratchPad; - } - - // Second try: if we're in a .app bundle, look next to the .app - // Path pattern: /path/to/App.app/Contents/MacOS/binary - char* appMarker = strstr(exePath, ".app/Contents/MacOS/"); - if (appMarker != NULL) - { - // Find the start of .app (go back to find the app name) - char* appStart = appMarker; - while (appStart > exePath && *(appStart - 1) != '/') appStart--; - - // Build path: /path/to/CoreModule.js (next to .app) - snprintf(ILibScratchPad, sizeof(ILibScratchPad), "%.*s/CoreModule.js", - (int)(appStart - exePath - 1), exePath); - - testFile = fopen(ILibScratchPad, "rb"); - if (testFile != NULL) - { - fclose(testFile); - return ILibScratchPad; - } - } -#endif - - return ILibScratchPad; -} - #ifndef MICROSTACK_NOTLS int agent_GenerateCertificates(MeshAgentHostContainer *agent, char* certfile) { @@ -3648,49 +3581,16 @@ void MeshServer_ProcessCommand(ILibWebClient_StateObject WebStateObject, MeshAge break; } - // Check if we are in openFrame mode - if (agent->openFrameMode) - { - // For openFrame mode: start CoreModule from file instead of server data - char* coreModulePath = buildOpenframeCoreModulePath(agent->exePath); - FILE *file = fopen(coreModulePath, "rb"); - if (file != NULL) { - // Get file size - fseek(file, 0, SEEK_END); - int fileSize = ftell(file); - fseek(file, 0, SEEK_SET); - - // Allocate memory and read file - char* fileCoreModule = (char*)ILibMemory_Allocate(fileSize, 0, NULL, NULL); - if (fileCoreModule != NULL) { - size_t bytesRead = fread(fileCoreModule, 1, fileSize, file); - if (bytesRead == fileSize) { - ILibRemoteLogging_printf(ILibChainGetLogger(agent->chain), ILibRemoteLogging_Modules_Microstack_Generic | ILibRemoteLogging_Modules_ConsolePrint, - ILibRemoteLogging_Flags_VerbosityLevel_1, "MeshCore: Restart (OpenFrame from file)"); - if ((coreException = ScriptEngine_Restart(agent, MeshAgent_JavaCore_ContextGuid, fileCoreModule, fileSize)) != NULL) - { - ILibRemoteLogging_printf(ILibChainGetLogger(agent->chain), ILibRemoteLogging_Modules_Microstack_Generic | ILibRemoteLogging_Modules_ConsolePrint, - ILibRemoteLogging_Flags_VerbosityLevel_1, "MeshCore: Error (OpenFrame): %s", coreException); - } - } - ILibMemory_Free(fileCoreModule); - } - fclose(file); - } - } - else + // Stop the current JavaScript core if present and launch the new one. + // JavaScript located at (cmd + 36) of length (cmdLen - 36) + //printf("CORE: Restart\r\n"); + ILibRemoteLogging_printf(ILibChainGetLogger(agent->chain), ILibRemoteLogging_Modules_Microstack_Generic | ILibRemoteLogging_Modules_ConsolePrint, + ILibRemoteLogging_Flags_VerbosityLevel_1, "MeshCore: Restart"); + if ((coreException = ScriptEngine_Restart(agent, MeshAgent_JavaCore_ContextGuid, coremodule + 4, (int)coremoduleLen - 4)) != NULL) { - // Stop the current JavaScript core if present and launch the new one. - // JavaScript located at (cmd + 36) of length (cmdLen - 36) - //printf("CORE: Restart\r\n"); ILibRemoteLogging_printf(ILibChainGetLogger(agent->chain), ILibRemoteLogging_Modules_Microstack_Generic | ILibRemoteLogging_Modules_ConsolePrint, - ILibRemoteLogging_Flags_VerbosityLevel_1, "MeshCore: Restart"); - if ((coreException = ScriptEngine_Restart(agent, MeshAgent_JavaCore_ContextGuid, coremodule + 4, (int)coremoduleLen - 4)) != NULL) - { - ILibRemoteLogging_printf(ILibChainGetLogger(agent->chain), ILibRemoteLogging_Modules_Microstack_Generic | ILibRemoteLogging_Modules_ConsolePrint, - ILibRemoteLogging_Flags_VerbosityLevel_1, "MeshCore: Error: %s", coreException); - // TODO: Ylian: New Java Core threw an exception... Exception String is stored in 'coreException' - } + ILibRemoteLogging_Flags_VerbosityLevel_1, "MeshCore: Error: %s", coreException); + // TODO: Ylian: New Java Core threw an exception... Exception String is stored in 'coreException' } // Since we did a big write to the data store, good time to compact the store @@ -3744,7 +3644,7 @@ void MeshServer_ProcessCommand(ILibWebClient_StateObject WebStateObject, MeshAge } case MeshCommand_CoreOk: // Message from the server indicating our meshcore is ok. No update needed. { - printf("Received CoreOk from server (coreTimeout=%p)\n", agent->coreTimeout); + printf("Server verified meshcore..."); duk_eval_string(agent->meshCoreCtx, "_MSH().setuid;"); if (duk_is_null_or_undefined(agent->meshCoreCtx, -1) == 0) @@ -3766,7 +3666,6 @@ void MeshServer_ProcessCommand(ILibWebClient_StateObject WebStateObject, MeshAge if (agent->coreTimeout != NULL) { - printf("Start meshcore. Openframe mode: %d, coreTimeout: %p\n", agent->openFrameMode, agent->coreTimeout); // Cancel the timeout duk_push_global_object(agent->meshCoreCtx); // [g] duk_get_prop_string(agent->meshCoreCtx, -1, "clearTimeout");// [g][clearTimeout] @@ -3796,77 +3695,24 @@ void MeshServer_ProcessCommand(ILibWebClient_StateObject WebStateObject, MeshAge } else { - if (agent->openFrameMode) - { - printf("[COREMODULE-2] === CoreModule loading (timeout handler) ===\n"); - printf("[COREMODULE-2] OpenFrame mode: loading CoreModule from FILE\n"); - printf("[COREMODULE-2] exePath: %s\n", agent->exePath ? agent->exePath : "NULL"); - - char* coreModulePath = buildOpenframeCoreModulePath(agent->exePath); - printf("[COREMODULE-2] CoreModule path: %s\n", coreModulePath); - - FILE *file = fopen(coreModulePath, "rb"); - if (file != NULL) { - // Get file size - fseek(file, 0, SEEK_END); - CoreModuleLen = ftell(file); - fseek(file, 0, SEEK_SET); - - printf("[COREMODULE-2] File opened, size: %d bytes\n", CoreModuleLen); - - // Allocate memory and read file - CoreModule = (char*)ILibMemory_Allocate(CoreModuleLen, 0, NULL, NULL); - if (CoreModule != NULL) { - printf("[COREMODULE-2] Memory allocated: %d bytes\n", CoreModuleLen); - size_t bytesRead = fread(CoreModule, 1, CoreModuleLen, file); - printf("[COREMODULE-2] Read %zu bytes from file\n", bytesRead); - if (bytesRead != CoreModuleLen) { - printf("[COREMODULE-2] ERROR: Read %zu bytes but expected %d\n", bytesRead, CoreModuleLen); - ILibMemory_Free(CoreModule); - CoreModule = NULL; - CoreModuleLen = 0; - } else { - printf("[COREMODULE-2] SUCCESS: CoreModule loaded from file (%d bytes)\n", CoreModuleLen); - } - } else { - printf("[COREMODULE-2] ERROR: Failed to allocate memory\n"); - } - fclose(file); - printf("[COREMODULE-2] File closed\n"); - } else { - printf("[COREMODULE-2] ERROR: Failed to open CoreModule file at: %s\n", coreModulePath); - CoreModule = NULL; - CoreModuleLen = 0; - } - } - else - { - printf("Use standard CoreModule from database\n"); - CoreModule = (char*)ILibMemory_Allocate(CoreModuleLen, 0, NULL, NULL); - ILibSimpleDataStore_Get(agent->masterDb, "CoreModule", CoreModule, CoreModuleLen); - } + CoreModule = (char*)ILibMemory_Allocate(CoreModuleLen, 0, NULL, NULL); + ILibSimpleDataStore_Get(agent->masterDb, "CoreModule", CoreModule, CoreModuleLen); } - printf("About to compile and execute CoreModule...\n"); if (ILibDuktape_ScriptContainer_CompileJavaScriptEx(agent->meshCoreCtx, CoreModule + 4, CoreModuleLen - 4, "CoreModule.js", 13) != 0 || ILibDuktape_ScriptContainer_ExecuteByteCode(agent->meshCoreCtx) != 0) { - printf("ERROR executing CoreModule: %s\n", duk_safe_to_string(agent->meshCoreCtx, -1)); ILibRemoteLogging_printf(ILibChainGetLogger(agent->chain), ILibRemoteLogging_Modules_Microstack_Generic | ILibRemoteLogging_Modules_ConsolePrint, ILibRemoteLogging_Flags_VerbosityLevel_1, "Error Executing MeshCore: %s", duk_safe_to_string(agent->meshCoreCtx, -1)); duk_pop(agent->meshCoreCtx); } - else - { - printf("CoreModule executed successfully!\n"); - } free(CoreModule); } } else { // There's no timeout, probably because the core is already running - printf("Meshcore already running (coreTimeout=%p)...\n", agent->coreTimeout); + printf(" meshcore already running...\n"); } break; }