{"record":{"id":"c67c457ca5e2ccb5","repo":"decolua/9router","slug":"mitm-server-is-not-running-start-the-server-first","errorCode":null,"errorMessage":"MITM server is not running. Start the server first.","messagePattern":"MITM server is not running\\. Start the server first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/mitm/manager.js","lineNumber":825,"sourceCode":"      if (e) log(`[reg] Failed to unset NODE_EXTRA_CA_CERTS: ${e.message}`);\n      else log(`[reg] NODE_EXTRA_CA_CERTS unset`);\n    });\n  }\n\n  try { fs.unlinkSync(PID_FILE); } catch { /* ignore */ }\n  try { fs.unlinkSync(LOCK_FILE); } catch { /* ignore */ }\n  await saveMitmSettings(false, null);\n  mitmIsRestarting = false;\n\n  return { running: false, pid: null };\n}\n\n/**\n * Enable DNS for a specific tool (requires server running)\n */\nasync function enableToolDNS(tool, sudoPassword) {\n  const status = await getMitmStatus();\n  if (!status.running) throw new Error(\"MITM server is not running. Start the server first.\");\n\n  const password = sudoPassword || getCachedPassword() || await loadEncryptedPassword();\n  await addDNSEntry(tool, password);\n  await saveDnsToolState(tool, true);\n  return { success: true };\n}\n\n/**\n * Disable DNS for a specific tool\n */\nasync function disableToolDNS(tool, sudoPassword) {\n  const password = sudoPassword || getCachedPassword() || await loadEncryptedPassword();\n  await removeDNSEntry(tool, password);\n  await saveDnsToolState(tool, false);\n  return { success: true };\n}\n\n/**","sourceCodeStart":807,"sourceCodeEnd":843,"githubUrl":"https://github.com/decolua/9router/blob/90b52e06ffd666b7929554211474d01588f6b1f8/src/mitm/manager.js#L807-L843","documentation":"enableToolDNS in src/mitm/manager.js refuses to modify /etc/hosts DNS entries when the MITM proxy server process is not currently running. DNS redirection for a tool only makes sense while the proxy is live, so this is an explicit state precondition check via getMitmStatus().status.running. It prevents silently adding host entries that route traffic nowhere.","triggerScenarios":"Calling enableToolDNS(tool, sudoPassword) (or its exported wrapper) before startServer() has completed, after the server was stopped, or after it crashed/exited.","commonSituations":"App freshly launched where the user toggles DNS for a tool without starting the MITM server; server died on a prior run leaving stale tool state; calling the manager API programmatically from a script without the start step.","solutions":["Start the MITM server first (await startServer() or the UI equivalent) and confirm getMitmStatus().running === true","Re-check status after starting, since startup is async — await it before enabling DNS","If the server crashed, restart it and check logs in MITM_DIR before retrying","If DNS entries should persist regardless, add the /etc/hosts entry manually and let the server pick it up on start"],"exampleFix":"// before\nawait enableToolDNS(\"claude\", password);\n// after\nconst status = await getMitmStatus();\nif (!status.running) await startServer({ sudoPassword: password });\nawait enableToolDNS(\"claude\", password);","handlingStrategy":"validation","validationCode":"const status = await getMitmStatus();\nif (!status.running) await startServer({ sudoPassword });\nawait enableToolDNS(tool, sudoPassword);","typeGuard":"function isMitmRunning(s) { return Boolean(s && s.running === true); }","tryCatchPattern":"try {\n  await enableToolDNS(tool, sudoPassword);\n} catch (err) {\n  if (err.message.includes(\"MITM server is not running\")) {\n    await startServer({ sudoPassword });\n    await enableToolDNS(tool, sudoPassword);\n  } else throw err;\n}","preventionTips":["Always check getMitmStatus().running before any manager call that requires the server","Await server startup fully before issuing DNS/cert operations","Expose a single 'ensureRunning()' helper in app code and use it everywhere","Watch for server crashes and surface status in the UI before users toggle DNS"],"tags":["mitm","dns","lifecycle"],"backgroundTag":"mitm-server-not-running","analyzedSha":"90b52e06ffd666b7929554211474d01588f6b1f8","analyzedAt":"2026-08-30T21:05:45.952Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}