diff --git a/extensions/firefox-i2p-torrent.xpi b/extensions/firefox-i2p-torrent.xpi index 1778711..dc774c5 100644 Binary files a/extensions/firefox-i2p-torrent.xpi and b/extensions/firefox-i2p-torrent.xpi differ diff --git a/extensions/firefox-i2p-torrent/background.js b/extensions/firefox-i2p-torrent/background.js index 1d58eb7..1c01188 100644 --- a/extensions/firefox-i2p-torrent/background.js +++ b/extensions/firefox-i2p-torrent/background.js @@ -205,6 +205,128 @@ async function gatherFullState() { }; } +function safePrettyJson(obj, compact = false) { + try { + return compact ? JSON.stringify(obj) : JSON.stringify(obj, null, 2); + } catch (err) { + return `{"ok":false,"error":${JSON.stringify(String(err))}}`; + } +} + +function formatDiagnosticsReport(cfg, nativeDiag, routerStatus, dht, torrents) { + const manifest = browser.runtime.getManifest ? browser.runtime.getManifest() : {}; + const nd = nativeDiag || {}; + const lines = []; + lines.push("=== Moon I2P Firefox Diagnostics ==="); + lines.push(`Timestamp: ${new Date().toISOString()}`); + lines.push(`Extension Version: ${manifest.version || "?"}`); + lines.push(`User Agent: ${typeof navigator !== "undefined" ? navigator.userAgent : "?"}`); + const modeStr = nd.appimage ? "AppImage" : "Standalone"; + lines.push(`Mode: ${modeStr}${nd.platform ? ` · ${nd.platform}` : ""}`); + + if (nd.available) { + lines.push( + `Native Host: available=true · router_pid=${nd.router_pid ?? "none"} · managed_running=${!!nd.managed_running} · api_reachable=${!!nd.api_reachable} · proxy_reachable=${!!nd.proxy_reachable}` + ); + if (nd.router_bin) { + lines.push(`Router Binary: ${nd.router_bin}`); + } + if (nd.active_ports) { + lines.push(`Active Ports: ${safePrettyJson(nd.active_ports, true)}`); + } + } else { + lines.push( + `Native Host: available=false · error=${nd.error || "unavailable (run `just extension` to install native host)"}` + ); + } + + lines.push( + `Settings: proxyEnabled=${!!cfg.proxyEnabled} · proxy=${cfg.proxyHost || "127.0.0.1"}:${cfg.proxyPort || 4444} · api=${cfg.apiHost || "127.0.0.1"}:${cfg.apiPort || 4445} · interceptTorrents=${!!cfg.interceptTorrents}` + ); + lines.push(`Download Dir: ${cfg.downloadDir || "(router default)"}`); + + if (nd.log_path) { + if (nd.log_exists) { + lines.push( + `Log File: ${nd.log_path} (${nd.log_size_bytes || 0} bytes, mtime=${nd.log_mtime_iso || "?"})${nd.log_error ? ` · err=${nd.log_error}` : ""}` + ); + } else { + lines.push(`Log File: MISSING (${nd.log_path})`); + } + } + + if (nd.log_summary) { + const s = nd.log_summary; + lines.push( + `Log Tail Summary (${s.lines || 0} lines): unhandled_garlic=${s.unhandled_garlic || 0} · garlic_decrypted=${s.garlic_decrypted || 0} · stream_connect_timeouts=${s.stream_connect_timeouts || 0} · bt_handshake_timeouts=${s.bt_handshake_timeouts || 0} · ls2_lookup_timeouts=${s.ls2_lookup_timeouts || 0} · pieces_verified=${s.pieces_verified || 0}` + ); + if (s.last_public_line) { + lines.push(`Last Router Status: ${s.last_public_line}`); + } + } + + lines.push(""); + lines.push("=== Router API /api/status ==="); + lines.push(safePrettyJson(routerStatus)); + + lines.push(""); + lines.push("=== Router API /api/torrents ==="); + lines.push(safePrettyJson(torrents)); + + lines.push(""); + lines.push("=== Router API /api/dht/nodes ==="); + lines.push(safePrettyJson(dht, true)); + + lines.push(""); + lines.push("=== Native moon-i2p Router Log (tail 32 KiB) ==="); + if (!nd.available) { + lines.push( + `(native host unavailable: ${nd.error || "run `just extension` to inspect desktop router logs"})` + ); + } else if (nd.log_error) { + lines.push(`(error reading ${nd.log_path || "router.log"}: ${nd.log_error})`); + } else if (!nd.log_exists) { + lines.push(`(log file not found at ${nd.log_path || "router.log"})`); + } else if (!nd.log_tail) { + lines.push("(empty)"); + } else { + lines.push(nd.log_tail.replace(/\n+$/, "")); + } + + return lines.join("\n") + "\n"; +} + +async function gatherDiagnostics(maxBytes = 32768) { + await loadSettings(); + if (!syncedAppImageEnv) { + await syncFromNativeEnv(); + } + const [nativeDiag, routerStatus, dht, torrents] = await Promise.all([ + nativeCommand("diagnostics", { max_bytes: maxBytes }), + apiJson("/api/status", "GET", null, 3000).catch((e) => ({ ok: false, error: String(e) })), + apiJson("/api/dht/nodes", "GET", null, 3000).catch((e) => ({ ok: false, error: String(e) })), + apiJson("/api/torrents", "GET", null, 3000).catch((e) => ({ ok: false, error: String(e) })), + ]); + const report = formatDiagnosticsReport( + cachedSettings, + nativeDiag, + routerStatus, + dht, + torrents + ); + return { + ok: true, + report, + meta: { + logPath: nativeDiag?.log_path || "", + logExists: !!nativeDiag?.log_exists, + logSizeBytes: Number(nativeDiag?.log_size_bytes || 0), + nativeAvailable: !!nativeDiag?.available, + nativeError: nativeDiag?.error || "", + }, + }; +} + // Intercept completed/started .torrent downloads from .i2p when enabled. browser.downloads.onCreated.addListener(async (item) => { try { @@ -231,6 +353,8 @@ browser.runtime.onMessage.addListener(async (msg) => { return { ok: true, settings: cachedSettings }; case "getState": return gatherFullState(); + case "getDiagnostics": + return gatherDiagnostics(msg.maxBytes || 32768); case "saveSettings": { const incoming = msg.settings || {}; const patch = {}; diff --git a/extensions/firefox-i2p-torrent/manager.html b/extensions/firefox-i2p-torrent/manager.html index b11cb9c..1f89e0a 100644 --- a/extensions/firefox-i2p-torrent/manager.html +++ b/extensions/firefox-i2p-torrent/manager.html @@ -13,6 +13,7 @@
Loading diagnostics...+
Open to load diagnostics...+