Preserve the primary Hopper startup failure and launcher diagnostics while retaining resources when cleanup cannot be confirmed. Reauthenticate matching external application sessions before document cleanup, keep the Python bridge available for bounded reconnects, and expose the provider-owned Hopper startup deadline. Real headless Linux Hopper semantic analysis and 65 focused configuration, handshake, cleanup, and bridge tests passed. macOS authorization/crash causes and saved-HOP behavior require their own real-provider verification. Refs #1808 Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
109 lines
3.9 KiB
TypeScript
109 lines
3.9 KiB
TypeScript
import { accessSync, constants } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
import { ConfigurationError } from "../domain/configurationErrors.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { parseEnvironment } from "./environment.js";
|
|
import { homeDirectoryFromEnvironment } from "./homeDirectory.js";
|
|
import { parseStringArray, parseLoaderArgs } from "./parsers.js";
|
|
import type { AppConfig } from "./types.js";
|
|
|
|
const DEFAULT_HOPPER_LAUNCHER_PATH =
|
|
"/Applications/Hopper Disassembler.app/Contents/MacOS/hopper";
|
|
const SYSTEM_LINUX_HOPPER = "/opt/hopper/bin/Hopper";
|
|
|
|
const executableAvailable = (path: string): boolean => {
|
|
try {
|
|
accessSync(path, constants.X_OK);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
const defaultHopperLauncherPath = (
|
|
platform: NodeJS.Platform,
|
|
homeDirectory: string,
|
|
executable: (path: string) => boolean = executableAvailable,
|
|
): string => {
|
|
if (platform !== "linux") return DEFAULT_HOPPER_LAUNCHER_PATH;
|
|
if (executable(SYSTEM_LINUX_HOPPER)) return SYSTEM_LINUX_HOPPER;
|
|
const userLocal = join(homeDirectory, ".local/share/rea/hopper/bin/Hopper");
|
|
return executable(userLocal) ? userLocal : SYSTEM_LINUX_HOPPER;
|
|
};
|
|
|
|
/** Parse provider configuration and runtime prerequisites at the composition root. */
|
|
export const parseConfig = (
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
): Result<AppConfig, ConfigurationError> => {
|
|
const parsed = parseEnvironment(environment);
|
|
if (!parsed.ok) return parsed;
|
|
const env = parsed.value;
|
|
const loaderArgs = parseLoaderArgs(env.HOPPER_LOADER_ARGS_JSON);
|
|
if (!loaderArgs.ok) return loaderArgs;
|
|
const secretPatterns = parseStringArray(
|
|
env.REA_REFERENCE_SECRET_PATTERNS_JSON,
|
|
"REA_REFERENCE_SECRET_PATTERNS_JSON",
|
|
);
|
|
if (!secretPatterns.ok) return secretPatterns;
|
|
// Ghidra's analyzeHeadless accepts -cspec only together with -processor.
|
|
if (
|
|
env.REA_GHIDRA_COMPILER_SPEC_ID !== undefined &&
|
|
env.REA_GHIDRA_LANGUAGE_ID === undefined
|
|
)
|
|
return err(
|
|
new ConfigurationError("Invalid REA environment configuration", {
|
|
settings: [
|
|
{
|
|
setting: "REA_GHIDRA_COMPILER_SPEC_ID",
|
|
constraint: "requires REA_GHIDRA_LANGUAGE_ID",
|
|
},
|
|
],
|
|
}),
|
|
);
|
|
return ok({
|
|
...(env.REA_MCP_MAX_RESPONSE_BYTES === undefined
|
|
? {}
|
|
: { mcpMaxResponseBytes: env.REA_MCP_MAX_RESPONSE_BYTES }),
|
|
...(env.REA_MCP_INPUT_SCHEMA_PROFILE === undefined
|
|
? {}
|
|
: { mcpInputSchemaProfile: env.REA_MCP_INPUT_SCHEMA_PROFILE }),
|
|
analysisProvider: env.REA_ANALYSIS_PROVIDER,
|
|
...(env.REA_IDA_MCP_CONFIG === undefined
|
|
? {}
|
|
: { idaMcpConfigPath: env.REA_IDA_MCP_CONFIG }),
|
|
ghidraInstallDir: env.GHIDRA_INSTALL_DIR,
|
|
ghidraJavaHome: env.JAVA_HOME,
|
|
ghidraStartupTimeoutMs: env.REA_GHIDRA_STARTUP_TIMEOUT_MS,
|
|
...(env.REA_GHIDRA_LANGUAGE_ID === undefined
|
|
? {}
|
|
: {
|
|
ghidraLanguageOverride: {
|
|
languageId: env.REA_GHIDRA_LANGUAGE_ID,
|
|
...(env.REA_GHIDRA_COMPILER_SPEC_ID === undefined
|
|
? {}
|
|
: { compilerSpecId: env.REA_GHIDRA_COMPILER_SPEC_ID }),
|
|
},
|
|
}),
|
|
...(env.REA_GHIDRA_SEED_FILE === undefined
|
|
? {}
|
|
: { ghidraSeedFile: env.REA_GHIDRA_SEED_FILE }),
|
|
...(env.REA_GHIDRA_NATIVEAOT_JAR === undefined
|
|
? {}
|
|
: { ghidraNativeAotJar: env.REA_GHIDRA_NATIVEAOT_JAR }),
|
|
ilspyCmdPath: env.REA_ILSPY_CMD_PATH,
|
|
hopperLauncherPath:
|
|
env.HOPPER_LAUNCHER_PATH ??
|
|
defaultHopperLauncherPath(
|
|
process.platform,
|
|
homeDirectoryFromEnvironment(environment, process.platform),
|
|
),
|
|
hopperStartupTimeoutMs: env.REA_HOPPER_STARTUP_TIMEOUT_MS,
|
|
hopperTargetPath: env.HOPPER_TARGET_PATH,
|
|
hopperTargetKind: env.HOPPER_TARGET_KIND,
|
|
hopperLoaderArgs: loaderArgs.value,
|
|
logLevel: env.REA_LOG_LEVEL,
|
|
referenceSourcePolicy: { secretPatterns: secretPatterns.value },
|
|
});
|
|
};
|