Compare commits

..

2 Commits

Author SHA1 Message Date
zhi
6d6d00437d chore: add .gitignore for build artifacts 2026-03-19 18:20:43 +00:00
zhi
0dc824549a feat: fix API Key authentication and payload alignment
- Update openclaw.plugin.json: replace challengeUuid with apiKey (optional)
- Fix tsconfig: use CommonJS module to avoid import.meta.url issues
- Fix plugin/index.ts: remove ESM-specific code, use __dirname
- Fix telemetry.mjs:
  - Add loadavg to os imports, remove require() call
  - Replace challengeUuid with apiKey in config
  - Update endpoint to heartbeat-v2
  - Add X-API-Key header when apiKey is configured
  - Fix payload field names: agents, load_avg (array), uptime_seconds
  - Change missing apiKey from error to warning
2026-03-19 18:20:29 +00:00
6 changed files with 97 additions and 40 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
plugin/node_modules/
plugin/*.js
plugin/*.js.map
plugin/*.d.ts
plugin/*.d.ts.map

View File

@@ -4,18 +4,14 @@
* Manages sidecar lifecycle and provides monitor-related tools. * Manages sidecar lifecycle and provides monitor-related tools.
*/ */
import { spawn } from 'child_process'; import { spawn } from 'child_process';
import { fileURLToPath } from 'url'; import { join } from 'path';
import { dirname, join } from 'path';
import { existsSync } from 'fs'; import { existsSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
interface PluginConfig { interface PluginConfig {
enabled?: boolean; enabled?: boolean;
backendUrl?: string; backendUrl?: string;
identifier?: string; identifier?: string;
challengeUuid?: string; apiKey?: string;
reportIntervalSec?: number; reportIntervalSec?: number;
httpFallbackIntervalSec?: number; httpFallbackIntervalSec?: number;
logLevel?: 'debug' | 'info' | 'warn' | 'error'; logLevel?: 'debug' | 'info' | 'warn' | 'error';
@@ -47,10 +43,9 @@ export default function register(api: PluginAPI, config: PluginConfig) {
return; return;
} }
if (!config.challengeUuid) { if (!config.apiKey) {
logger.error('Missing required config: challengeUuid'); logger.warn('Missing config: apiKey');
logger.error('Please register server in HarborForge Monitor first'); logger.warn('API authentication will fail. Generate apiKey from HarborForge Monitor admin.');
return;
} }
const serverPath = join(__dirname, '..', 'server', 'telemetry.mjs'); const serverPath = join(__dirname, '..', 'server', 'telemetry.mjs');
@@ -74,7 +69,7 @@ export default function register(api: PluginAPI, config: PluginConfig) {
...process.env, ...process.env,
HF_MONITOR_BACKEND_URL: config.backendUrl || 'https://monitor.hangman-lab.top', HF_MONITOR_BACKEND_URL: config.backendUrl || 'https://monitor.hangman-lab.top',
HF_MONITOR_IDENTIFIER: config.identifier || '', HF_MONITOR_IDENTIFIER: config.identifier || '',
HF_MONITOR_CHALLENGE_UUID: config.challengeUuid, HF_MONITOR_API_KEY: config.apiKey || '',
HF_MONITOR_REPORT_INTERVAL: String(config.reportIntervalSec || 30), HF_MONITOR_REPORT_INTERVAL: String(config.reportIntervalSec || 30),
HF_MONITOR_HTTP_FALLBACK_INTERVAL: String(config.httpFallbackIntervalSec || 60), HF_MONITOR_HTTP_FALLBACK_INTERVAL: String(config.httpFallbackIntervalSec || 60),
HF_MONITOR_LOG_LEVEL: config.logLevel || 'info', HF_MONITOR_LOG_LEVEL: config.logLevel || 'info',

View File

@@ -22,9 +22,9 @@
"type": "string", "type": "string",
"description": "Server identifier (auto-detected from hostname if not set)" "description": "Server identifier (auto-detected from hostname if not set)"
}, },
"challengeUuid": { "apiKey": {
"type": "string", "type": "string",
"description": "Registration challenge UUID from Monitor" "description": "API Key from HarborForge Monitor admin panel (optional but required for authentication)"
}, },
"reportIntervalSec": { "reportIntervalSec": {
"type": "number", "type": "number",
@@ -42,7 +42,6 @@
"default": "info", "default": "info",
"description": "Logging level" "description": "Logging level"
} }
}, }
"required": ["challengeUuid"]
} }
} }

48
plugin/package-lock.json generated Normal file
View File

@@ -0,0 +1,48 @@
{
"name": "harborforge-monitor-plugin",
"version": "0.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "harborforge-monitor-plugin",
"version": "0.1.0",
"license": "MIT",
"devDependencies": {
"@types/node": "^20.0.0",
"typescript": "^5.0.0"
}
},
"node_modules/@types/node": {
"version": "20.19.37",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.19.37.tgz",
"integrity": "sha512-8kzdPJ3FsNsVIurqBs7oodNnCEVbni9yUEkaHbgptDACOPW04jimGagZ51E6+lXUwJjgnBw+hyko/lkFWCldqw==",
"dev": true,
"license": "MIT",
"dependencies": {
"undici-types": "~6.21.0"
}
},
"node_modules/typescript": {
"version": "5.9.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
"dev": true,
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/undici-types": {
"version": "6.21.0",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
"dev": true,
"license": "MIT"
}
}
}

View File

@@ -1,8 +1,8 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2022", "target": "ES2022",
"module": "NodeNext", "module": "CommonJS",
"moduleResolution": "NodeNext", "moduleResolution": "node",
"esModuleInterop": true, "esModuleInterop": true,
"strict": true, "strict": true,
"skipLibCheck": true, "skipLibCheck": true,

View File

@@ -8,20 +8,23 @@ import { readFile, access } from 'fs/promises';
import { constants } from 'fs'; import { constants } from 'fs';
import { exec } from 'child_process'; import { exec } from 'child_process';
import { promisify } from 'util'; import { promisify } from 'util';
import { platform, hostname, freemem, totalmem, uptime } from 'os'; import { platform, hostname, freemem, totalmem, uptime, loadavg } from 'os';
const execAsync = promisify(exec); const execAsync = promisify(exec);
// Config from environment (set by plugin) // Config from environment (set by plugin)
const openclawPath = process.env.OPENCLAW_PATH || `${process.env.HOME}/.openclaw`;
const CONFIG = { const CONFIG = {
backendUrl: process.env.HF_MONITOR_BACKEND_URL || 'https://monitor.hangman-lab.top', backendUrl: process.env.HF_MONITOR_BACKEND_URL || 'https://monitor.hangman-lab.top',
identifier: process.env.HF_MONITOR_IDENTIFIER || hostname(), identifier: process.env.HF_MONITOR_IDENTIFIER || hostname(),
challengeUuid: process.env.HF_MONITOR_CHALLENGE_UUID, apiKey: process.env.HF_MONITOR_API_KEY,
reportIntervalSec: parseInt(process.env.HF_MONITOR_REPORT_INTERVAL || '30', 10), reportIntervalSec: parseInt(process.env.HF_MONITOR_REPORT_INTERVAL || '30', 10),
httpFallbackIntervalSec: parseInt(process.env.HF_MONITOR_HTTP_FALLBACK_INTERVAL || '60', 10), httpFallbackIntervalSec: parseInt(process.env.HF_MONITOR_HTTP_FALLBACK_INTERVAL || '60', 10),
logLevel: process.env.HF_MONITOR_LOG_LEVEL || 'info', logLevel: process.env.HF_MONITOR_LOG_LEVEL || 'info',
openclawPath: process.env.OPENCLAW_PATH || `${process.env.HOME}/.openclaw`, openclawPath,
openclawVersion: process.env.OPENCLAW_VERSION || 'unknown', openclawVersion: process.env.OPENCLAW_VERSION || 'unknown',
cachePath: process.env.HF_MONITOR_CACHE_PATH || `${openclawPath}/telemetry_cache.json`,
maxCacheSize: parseInt(process.env.HF_MONITOR_MAX_CACHE_SIZE || '100', 10),
}; };
// Logging // Logging
@@ -48,7 +51,7 @@ async function collectSystemMetrics() {
const memFree = freemem(); const memFree = freemem();
const memUsed = memTotal - memFree; const memUsed = memTotal - memFree;
const diskInfo = await getDiskUsage(); const diskInfo = await getDiskUsage();
const loadAvg = platform() !== 'win32' ? require('os').loadavg() : [0, 0, 0]; const loadAvg = platform() !== 'win32' ? loadavg() : [0, 0, 0];
return { return {
cpu_pct: cpuUsage, cpu_pct: cpuUsage,
@@ -59,8 +62,12 @@ async function collectSystemMetrics() {
disk_used_gb: Math.round(diskInfo.usedGB * 10) / 10, disk_used_gb: Math.round(diskInfo.usedGB * 10) / 10,
disk_total_gb: Math.round(diskInfo.totalGB * 10) / 10, disk_total_gb: Math.round(diskInfo.totalGB * 10) / 10,
swap_pct: diskInfo.swapUsedPct || 0, swap_pct: diskInfo.swapUsedPct || 0,
uptime_sec: Math.floor(uptime()), uptime_seconds: Math.floor(uptime()),
load_avg_1m: Math.round(loadAvg[0] * 100) / 100, load_avg: [
Math.round(loadAvg[0] * 100) / 100,
Math.round(loadAvg[1] * 100) / 100,
Math.round(loadAvg[2] * 100) / 100,
],
platform: platform(), platform: platform(),
hostname: hostname(), hostname: hostname(),
}; };
@@ -177,12 +184,10 @@ async function buildPayload() {
return { return {
identifier: CONFIG.identifier, identifier: CONFIG.identifier,
challenge_uuid: CONFIG.challengeUuid,
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
...system, ...system,
openclaw_version: openclaw.version, openclaw_version: openclaw.version,
openclaw_agents: openclaw.agents, agents: openclaw.agents,
openclaw_agent_count: openclaw.agent_count,
}; };
} }
@@ -194,13 +199,18 @@ async function sendHttpHeartbeat() {
const payload = await buildPayload(); const payload = await buildPayload();
log.debug('Sending HTTP heartbeat...'); log.debug('Sending HTTP heartbeat...');
const response = await fetch(`${CONFIG.backendUrl}/monitor/server/heartbeat`, { const headers = {
'Content-Type': 'application/json',
'X-Server-Identifier': CONFIG.identifier,
};
if (CONFIG.apiKey) {
headers['X-API-Key'] = CONFIG.apiKey;
}
const response = await fetch(`${CONFIG.backendUrl}/monitor/server/heartbeat-v2`, {
method: 'POST', method: 'POST',
headers: { headers,
'Content-Type': 'application/json',
'X-Server-Identifier': CONFIG.identifier,
'X-Challenge-UUID': CONFIG.challengeUuid,
},
body: JSON.stringify(payload), body: JSON.stringify(payload),
}); });
@@ -268,11 +278,11 @@ log.info('Config:', {
identifier: CONFIG.identifier, identifier: CONFIG.identifier,
backendUrl: CONFIG.backendUrl, backendUrl: CONFIG.backendUrl,
reportIntervalSec: CONFIG.reportIntervalSec, reportIntervalSec: CONFIG.reportIntervalSec,
hasApiKey: !!CONFIG.apiKey,
}); });
if (!CONFIG.challengeUuid) { if (!CONFIG.apiKey) {
log.error('Missing HF_MONITOR_CHALLENGE_UUID environment variable'); log.warn('Missing HF_MONITOR_API_KEY environment variable - API authentication will fail');
process.exit(1);
} }
reportingLoop(); reportingLoop();