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
This commit is contained in:
zhi
2026-03-19 18:20:29 +00:00
parent 0debe835b4
commit 0dc824549a
254 changed files with 495790 additions and 40 deletions

View File

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