wyxrouter 0.4.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +222 -0
- package/LICENSE +21 -0
- package/README.md +96 -0
- package/README.zh-CN.md +1311 -0
- package/assets/pixel-router-2d.png +0 -0
- package/cli/LICENSE +42 -0
- package/cli/README.md +125 -0
- package/cli/app/package.json +58 -0
- package/cli/cli.js +806 -0
- package/cli/package.json +48 -0
- package/i18n/README.ja-JP.md +1209 -0
- package/i18n/README.ru.md +1311 -0
- package/i18n/README.vi.md +1310 -0
- package/i18n/README.zh-CN.md +1306 -0
- package/images/9router.png +0 -0
- package/jsconfig.json +12 -0
- package/next.config.mjs +71 -0
- package/open-sse/config/appConstants.js +203 -0
- package/open-sse/config/codexInstructions.js +119 -0
- package/open-sse/config/constants.js +4 -0
- package/open-sse/config/defaultThinkingSignature.js +12 -0
- package/open-sse/config/errorConfig.js +85 -0
- package/open-sse/config/googleTtsLanguages.js +62 -0
- package/open-sse/config/kiroConstants.js +322 -0
- package/open-sse/config/models.js +13 -0
- package/open-sse/config/ollamaModels.js +19 -0
- package/open-sse/config/providerModels.js +944 -0
- package/open-sse/config/providers.js +458 -0
- package/open-sse/config/runtimeConfig.js +72 -0
- package/open-sse/config/ttsModels.js +129 -0
- package/open-sse/executors/antigravity.js +504 -0
- package/open-sse/executors/azure.js +57 -0
- package/open-sse/executors/base.js +185 -0
- package/open-sse/executors/codex.js +469 -0
- package/open-sse/executors/commandcode.js +88 -0
- package/open-sse/executors/cursor.js +795 -0
- package/open-sse/executors/default.js +497 -0
- package/open-sse/executors/gemini-cli.js +89 -0
- package/open-sse/executors/github.js +379 -0
- package/open-sse/executors/grok-web.js +345 -0
- package/open-sse/executors/iflow.js +108 -0
- package/open-sse/executors/index.js +75 -0
- package/open-sse/executors/kiro.js +508 -0
- package/open-sse/executors/ollama-local.js +14 -0
- package/open-sse/executors/opencode-go.js +41 -0
- package/open-sse/executors/opencode.js +32 -0
- package/open-sse/executors/perplexity-web.js +507 -0
- package/open-sse/executors/qoder.js +450 -0
- package/open-sse/executors/qwen.js +129 -0
- package/open-sse/executors/vertex.js +131 -0
- package/open-sse/executors/xiaomi-tokenplan.js +19 -0
- package/open-sse/handlers/chatCore/nonStreamingHandler.js +230 -0
- package/open-sse/handlers/chatCore/requestDetail.js +102 -0
- package/open-sse/handlers/chatCore/sseToJsonHandler.js +231 -0
- package/open-sse/handlers/chatCore/streamingHandler.js +103 -0
- package/open-sse/handlers/chatCore.js +287 -0
- package/open-sse/handlers/embeddingProviders/_base.js +4 -0
- package/open-sse/handlers/embeddingProviders/gemini.js +54 -0
- package/open-sse/handlers/embeddingProviders/index.js +23 -0
- package/open-sse/handlers/embeddingProviders/openai.js +39 -0
- package/open-sse/handlers/embeddingProviders/openaiCompatNode.js +13 -0
- package/open-sse/handlers/embeddingsCore.js +126 -0
- package/open-sse/handlers/fetch/index.js +237 -0
- package/open-sse/handlers/imageGenerationCore.js +189 -0
- package/open-sse/handlers/imageProviders/_base.js +31 -0
- package/open-sse/handlers/imageProviders/blackForestLabs.js +43 -0
- package/open-sse/handlers/imageProviders/cloudflareAi.js +178 -0
- package/open-sse/handlers/imageProviders/codex.js +198 -0
- package/open-sse/handlers/imageProviders/comfyui.js +8 -0
- package/open-sse/handlers/imageProviders/falAi.js +41 -0
- package/open-sse/handlers/imageProviders/gemini.js +25 -0
- package/open-sse/handlers/imageProviders/huggingface.js +22 -0
- package/open-sse/handlers/imageProviders/index.js +40 -0
- package/open-sse/handlers/imageProviders/nanobanana.js +58 -0
- package/open-sse/handlers/imageProviders/openai.js +40 -0
- package/open-sse/handlers/imageProviders/runwayml.js +47 -0
- package/open-sse/handlers/imageProviders/sdwebui.js +17 -0
- package/open-sse/handlers/imageProviders/stabilityAi.js +34 -0
- package/open-sse/handlers/responsesHandler.js +103 -0
- package/open-sse/handlers/search/callers.js +371 -0
- package/open-sse/handlers/search/chatSearch.js +409 -0
- package/open-sse/handlers/search/index.js +201 -0
- package/open-sse/handlers/search/normalizers.js +223 -0
- package/open-sse/handlers/sttCore.js +194 -0
- package/open-sse/handlers/ttsCore.js +74 -0
- package/open-sse/handlers/ttsProviders/_base.js +39 -0
- package/open-sse/handlers/ttsProviders/edgeTts.js +89 -0
- package/open-sse/handlers/ttsProviders/elevenlabs.js +48 -0
- package/open-sse/handlers/ttsProviders/gemini.js +117 -0
- package/open-sse/handlers/ttsProviders/genericFormats.js +169 -0
- package/open-sse/handlers/ttsProviders/googleTts.js +54 -0
- package/open-sse/handlers/ttsProviders/index.js +50 -0
- package/open-sse/handlers/ttsProviders/localDevice.js +87 -0
- package/open-sse/handlers/ttsProviders/minimax.js +59 -0
- package/open-sse/handlers/ttsProviders/openai.js +30 -0
- package/open-sse/handlers/ttsProviders/openrouter.js +70 -0
- package/open-sse/index.js +82 -0
- package/open-sse/rtk/applyFilter.js +15 -0
- package/open-sse/rtk/autodetect.js +111 -0
- package/open-sse/rtk/caveman.js +100 -0
- package/open-sse/rtk/cavemanPrompts.js +78 -0
- package/open-sse/rtk/constants.js +55 -0
- package/open-sse/rtk/filters/buildOutput.js +127 -0
- package/open-sse/rtk/filters/dedupLog.js +44 -0
- package/open-sse/rtk/filters/find.js +49 -0
- package/open-sse/rtk/filters/gitDiff.js +92 -0
- package/open-sse/rtk/filters/gitStatus.js +117 -0
- package/open-sse/rtk/filters/grep.js +48 -0
- package/open-sse/rtk/filters/ls.js +79 -0
- package/open-sse/rtk/filters/readNumbered.js +27 -0
- package/open-sse/rtk/filters/searchList.js +52 -0
- package/open-sse/rtk/filters/smartTruncate.js +15 -0
- package/open-sse/rtk/filters/tree.js +32 -0
- package/open-sse/rtk/index.js +155 -0
- package/open-sse/rtk/registry.js +38 -0
- package/open-sse/services/accountFallback.js +238 -0
- package/open-sse/services/combo.js +198 -0
- package/open-sse/services/compact.js +71 -0
- package/open-sse/services/kiroModels.js +332 -0
- package/open-sse/services/model.js +261 -0
- package/open-sse/services/oauthCredentialManager.js +151 -0
- package/open-sse/services/projectId.js +306 -0
- package/open-sse/services/provider.js +356 -0
- package/open-sse/services/qoderModels.js +214 -0
- package/open-sse/services/tokenRefresh.js +939 -0
- package/open-sse/services/usage.js +1496 -0
- package/open-sse/transformer/responsesTransformer.js +439 -0
- package/open-sse/transformer/streamToJsonConverter.js +103 -0
- package/open-sse/translator/formats.js +36 -0
- package/open-sse/translator/helpers/claudeHelper.js +216 -0
- package/open-sse/translator/helpers/geminiHelper.js +372 -0
- package/open-sse/translator/helpers/imageHelper.js +34 -0
- package/open-sse/translator/helpers/maxTokensHelper.js +27 -0
- package/open-sse/translator/helpers/openaiHelper.js +130 -0
- package/open-sse/translator/helpers/responsesApiHelper.js +139 -0
- package/open-sse/translator/helpers/toolCallHelper.js +148 -0
- package/open-sse/translator/index.js +251 -0
- package/open-sse/translator/request/antigravity-to-openai.js +229 -0
- package/open-sse/translator/request/claude-to-openai.js +232 -0
- package/open-sse/translator/request/gemini-to-openai.js +147 -0
- package/open-sse/translator/request/openai-responses.js +318 -0
- package/open-sse/translator/request/openai-to-claude.js +401 -0
- package/open-sse/translator/request/openai-to-commandcode.js +170 -0
- package/open-sse/translator/request/openai-to-cursor.js +183 -0
- package/open-sse/translator/request/openai-to-gemini.js +470 -0
- package/open-sse/translator/request/openai-to-kiro.js +629 -0
- package/open-sse/translator/request/openai-to-kiro.old.js +278 -0
- package/open-sse/translator/request/openai-to-ollama.js +192 -0
- package/open-sse/translator/request/openai-to-vertex.js +42 -0
- package/open-sse/translator/response/claude-to-openai.js +206 -0
- package/open-sse/translator/response/commandcode-to-openai.js +197 -0
- package/open-sse/translator/response/cursor-to-openai.js +30 -0
- package/open-sse/translator/response/gemini-to-openai.js +245 -0
- package/open-sse/translator/response/kiro-to-openai.js +195 -0
- package/open-sse/translator/response/ollama-to-openai.js +152 -0
- package/open-sse/translator/response/openai-responses.js +590 -0
- package/open-sse/translator/response/openai-to-antigravity.js +122 -0
- package/open-sse/translator/response/openai-to-claude.js +266 -0
- package/open-sse/utils/bypassHandler.js +298 -0
- package/open-sse/utils/claudeCloaking.js +155 -0
- package/open-sse/utils/claudeHeaderCache.js +70 -0
- package/open-sse/utils/clientDetector.js +63 -0
- package/open-sse/utils/cursorChecksum.js +149 -0
- package/open-sse/utils/cursorProtobuf.js +904 -0
- package/open-sse/utils/debugLog.js +14 -0
- package/open-sse/utils/error.js +147 -0
- package/open-sse/utils/ollamaTransform.js +85 -0
- package/open-sse/utils/proxyFetch.js +368 -0
- package/open-sse/utils/reasoningContentInjector.js +79 -0
- package/open-sse/utils/requestLogger.js +260 -0
- package/open-sse/utils/responsesStreamHelpers.js +49 -0
- package/open-sse/utils/sessionManager.js +82 -0
- package/open-sse/utils/stream.js +462 -0
- package/open-sse/utils/streamHandler.js +250 -0
- package/open-sse/utils/streamHelpers.js +122 -0
- package/open-sse/utils/toolDeduper.js +49 -0
- package/open-sse/utils/usageTracking.js +347 -0
- package/package.json +100 -0
- package/postcss.config.mjs +12 -0
- package/public/favicon.svg +11 -0
- package/public/file.svg +1 -0
- package/public/globe.svg +1 -0
- package/public/i18n/literals/ar.json +195 -0
- package/public/i18n/literals/bn.json +195 -0
- package/public/i18n/literals/cs.json +195 -0
- package/public/i18n/literals/da.json +195 -0
- package/public/i18n/literals/de.json +195 -0
- package/public/i18n/literals/el.json +195 -0
- package/public/i18n/literals/es.json +195 -0
- package/public/i18n/literals/fi.json +195 -0
- package/public/i18n/literals/fr.json +195 -0
- package/public/i18n/literals/he.json +195 -0
- package/public/i18n/literals/hi.json +195 -0
- package/public/i18n/literals/hu.json +195 -0
- package/public/i18n/literals/id.json +195 -0
- package/public/i18n/literals/it.json +195 -0
- package/public/i18n/literals/ja.json +195 -0
- package/public/i18n/literals/ko.json +195 -0
- package/public/i18n/literals/nl.json +195 -0
- package/public/i18n/literals/no.json +195 -0
- package/public/i18n/literals/pl.json +195 -0
- package/public/i18n/literals/pt-BR.json +195 -0
- package/public/i18n/literals/pt-PT.json +195 -0
- package/public/i18n/literals/ro.json +195 -0
- package/public/i18n/literals/ru.json +195 -0
- package/public/i18n/literals/sv.json +195 -0
- package/public/i18n/literals/th.json +195 -0
- package/public/i18n/literals/tl.json +195 -0
- package/public/i18n/literals/tr.json +195 -0
- package/public/i18n/literals/uk.json +195 -0
- package/public/i18n/literals/ur.json +195 -0
- package/public/i18n/literals/vi.json +195 -0
- package/public/i18n/literals/zh-CN.json +772 -0
- package/public/i18n/literals/zh-TW.json +195 -0
- package/public/icons/discord.svg +4 -0
- package/public/icons/icon-192.svg +4 -0
- package/public/icons/icon-512.svg +4 -0
- package/public/next.svg +1 -0
- package/public/providers/alicode-intl.png +0 -0
- package/public/providers/alicode.png +0 -0
- package/public/providers/amp.png +0 -0
- package/public/providers/anthropic-m.png +0 -0
- package/public/providers/anthropic.png +0 -0
- package/public/providers/antigravity.png +0 -0
- package/public/providers/assemblyai.png +0 -0
- package/public/providers/aws-polly.png +0 -0
- package/public/providers/azure.png +0 -0
- package/public/providers/black-forest-labs.png +0 -0
- package/public/providers/blackbox.png +0 -0
- package/public/providers/brave-search.png +0 -0
- package/public/providers/byteplus.png +0 -0
- package/public/providers/cartesia.png +0 -0
- package/public/providers/cerebras.png +0 -0
- package/public/providers/chutes.png +0 -0
- package/public/providers/claude.png +0 -0
- package/public/providers/cline.png +0 -0
- package/public/providers/cloudflare-ai.png +0 -0
- package/public/providers/codebuddy.svg +37 -0
- package/public/providers/codex.png +0 -0
- package/public/providers/cohere.png +0 -0
- package/public/providers/comfyui.png +0 -0
- package/public/providers/commandcode.png +0 -0
- package/public/providers/continue.png +0 -0
- package/public/providers/copilot.png +0 -0
- package/public/providers/coqui.png +0 -0
- package/public/providers/cursor.png +0 -0
- package/public/providers/deepgram.png +0 -0
- package/public/providers/deepseek-tui.png +0 -0
- package/public/providers/deepseek.png +0 -0
- package/public/providers/droid.png +0 -0
- package/public/providers/edge-tts.png +0 -0
- package/public/providers/elevenlabs.png +0 -0
- package/public/providers/exa.png +0 -0
- package/public/providers/fal-ai.png +0 -0
- package/public/providers/firecrawl.png +0 -0
- package/public/providers/fireworks.png +0 -0
- package/public/providers/gemini-cli.png +0 -0
- package/public/providers/gemini.png +0 -0
- package/public/providers/github.png +0 -0
- package/public/providers/glm-cn.png +0 -0
- package/public/providers/glm.png +0 -0
- package/public/providers/google-pse.png +0 -0
- package/public/providers/google-tts.png +0 -0
- package/public/providers/grok-web.png +0 -0
- package/public/providers/groq.png +0 -0
- package/public/providers/hermes.png +0 -0
- package/public/providers/huggingface.png +0 -0
- package/public/providers/hyperbolic.png +0 -0
- package/public/providers/iflow.png +0 -0
- package/public/providers/inworld.png +0 -0
- package/public/providers/jcode.png +0 -0
- package/public/providers/jina-ai.png +0 -0
- package/public/providers/jina-reader.png +0 -0
- package/public/providers/kilocode.png +0 -0
- package/public/providers/kimi-coding.png +0 -0
- package/public/providers/kimi.png +0 -0
- package/public/providers/kiro.png +0 -0
- package/public/providers/linkup.png +0 -0
- package/public/providers/local-device.png +0 -0
- package/public/providers/minimax-cn.png +0 -0
- package/public/providers/minimax.png +0 -0
- package/public/providers/mistral.png +0 -0
- package/public/providers/nanobanana.png +0 -0
- package/public/providers/nebius.png +0 -0
- package/public/providers/nvidia.png +0 -0
- package/public/providers/oai-cc.png +0 -0
- package/public/providers/oai-r.png +0 -0
- package/public/providers/ollama-local.png +0 -0
- package/public/providers/ollama.png +0 -0
- package/public/providers/openai.png +0 -0
- package/public/providers/openclaw.png +0 -0
- package/public/providers/opencode-go.png +0 -0
- package/public/providers/opencode.png +0 -0
- package/public/providers/openrouter.png +0 -0
- package/public/providers/perplexity-web.png +0 -0
- package/public/providers/perplexity.png +0 -0
- package/public/providers/playht.png +0 -0
- package/public/providers/qoder.png +0 -0
- package/public/providers/qwen.png +0 -0
- package/public/providers/recraft.png +0 -0
- package/public/providers/roo.png +0 -0
- package/public/providers/runwayml.png +0 -0
- package/public/providers/sdwebui.png +0 -0
- package/public/providers/searchapi.png +0 -0
- package/public/providers/searxng.png +0 -0
- package/public/providers/serper.png +0 -0
- package/public/providers/siliconflow.png +0 -0
- package/public/providers/stability-ai.png +0 -0
- package/public/providers/tavily.png +0 -0
- package/public/providers/together.png +0 -0
- package/public/providers/topaz.png +0 -0
- package/public/providers/tortoise.png +0 -0
- package/public/providers/vertex-partner.png +0 -0
- package/public/providers/vertex.png +0 -0
- package/public/providers/volcengine-ark.png +0 -0
- package/public/providers/voyage-ai.png +0 -0
- package/public/providers/xai.png +0 -0
- package/public/providers/xiaomi-mimo.png +0 -0
- package/public/providers/xiaomi-tokenplan.png +0 -0
- package/public/providers/youcom.png +0 -0
- package/public/sw.js +22 -0
- package/public/vercel.svg +1 -0
- package/public/window.svg +1 -0
- package/scripts/compact-request-details.mjs +71 -0
- package/scripts/import-codex-gptjson.mjs +342 -0
- package/scripts/start-standalone.mjs +25 -0
- package/scripts/translate-readme.js +201 -0
- package/src/app/(dashboard)/dashboard/automation/page.js +294 -0
- package/src/app/(dashboard)/dashboard/basic-chat/BasicChatPageClient.js +967 -0
- package/src/app/(dashboard)/dashboard/basic-chat/page.js +5 -0
- package/src/app/(dashboard)/dashboard/cli-tools/CLIToolsPageClient.js +66 -0
- package/src/app/(dashboard)/dashboard/cli-tools/[toolId]/ToolDetailClient.js +173 -0
- package/src/app/(dashboard)/dashboard/cli-tools/[toolId]/page.js +11 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/AntigravityToolCard.js +481 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ApiKeySelect.js +66 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/BaseUrlSelect.js +174 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ClaudeToolCard.js +390 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ClineToolCard.js +301 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CodexToolCard.js +458 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CopilotToolCard.js +323 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/CoworkToolCard.js +640 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DeepSeekTuiToolCard.js +338 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DefaultToolCard.js +271 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/DroidToolCard.js +410 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/EndpointPresetControl.js +128 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/HermesToolCard.js +317 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/JcodeToolCard.js +380 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/KiloToolCard.js +275 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmLinkCard.js +40 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmServerCard.js +329 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/MitmToolCard.js +318 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/OpenClawToolCard.js +388 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/OpenCodeToolCard.js +500 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/ToolSummaryCard.js +39 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/cliEndpointMatch.js +13 -0
- package/src/app/(dashboard)/dashboard/cli-tools/components/index.js +19 -0
- package/src/app/(dashboard)/dashboard/cli-tools/page.js +7 -0
- package/src/app/(dashboard)/dashboard/combos/page.js +612 -0
- package/src/app/(dashboard)/dashboard/console-log/ConsoleLogClient.js +91 -0
- package/src/app/(dashboard)/dashboard/console-log/page.js +8 -0
- package/src/app/(dashboard)/dashboard/endpoint/EndpointPageClient.js +1555 -0
- package/src/app/(dashboard)/dashboard/endpoint/page.js +7 -0
- package/src/app/(dashboard)/dashboard/media-providers/[kind]/[id]/page.js +1903 -0
- package/src/app/(dashboard)/dashboard/media-providers/[kind]/page.js +289 -0
- package/src/app/(dashboard)/dashboard/media-providers/combo/[id]/page.js +410 -0
- package/src/app/(dashboard)/dashboard/media-providers/web/page.js +209 -0
- package/src/app/(dashboard)/dashboard/mitm/MitmPageClient.js +117 -0
- package/src/app/(dashboard)/dashboard/mitm/page.js +5 -0
- package/src/app/(dashboard)/dashboard/page.js +7 -0
- package/src/app/(dashboard)/dashboard/profile/page.js +1140 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/AddApiKeyModal.js +389 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/AddCustomModelModal.js +125 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/CompatibleModelsSection.js +250 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/ConnectionRow.js +299 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/CooldownTimer.js +42 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/EditCompatibleNodeModal.js +161 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/ModelRow.js +95 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/PassthroughModelsSection.js +183 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/page.js +2053 -0
- package/src/app/(dashboard)/dashboard/providers/[id]/page.new.js +1724 -0
- package/src/app/(dashboard)/dashboard/providers/components/ConnectionsCard.js +497 -0
- package/src/app/(dashboard)/dashboard/providers/components/ModelAvailabilityBadge.js +185 -0
- package/src/app/(dashboard)/dashboard/providers/components/ModelsCard.js +294 -0
- package/src/app/(dashboard)/dashboard/providers/new/page.js +220 -0
- package/src/app/(dashboard)/dashboard/providers/page.js +1365 -0
- package/src/app/(dashboard)/dashboard/proxy-pools/page.js +1092 -0
- package/src/app/(dashboard)/dashboard/quota/page.js +11 -0
- package/src/app/(dashboard)/dashboard/skills/page.js +112 -0
- package/src/app/(dashboard)/dashboard/translator/page.js +303 -0
- package/src/app/(dashboard)/dashboard/usage/components/OverviewCards.js +35 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/ProviderLimitCard.js +185 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/QuotaProgressBar.js +127 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/QuotaTable.js +259 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/index.js +1394 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderLimits/utils.js +244 -0
- package/src/app/(dashboard)/dashboard/usage/components/ProviderTopology.js +327 -0
- package/src/app/(dashboard)/dashboard/usage/components/RequestDetailsTab.js +433 -0
- package/src/app/(dashboard)/dashboard/usage/components/UsageChart.js +141 -0
- package/src/app/(dashboard)/dashboard/usage/components/UsageTable.js +247 -0
- package/src/app/(dashboard)/dashboard/usage/page.js +75 -0
- package/src/app/(dashboard)/layout.js +6 -0
- package/src/app/api/auth/login/route.js +76 -0
- package/src/app/api/auth/logout/route.js +12 -0
- package/src/app/api/auth/oidc/callback/route.js +87 -0
- package/src/app/api/auth/oidc/start/route.js +52 -0
- package/src/app/api/auth/oidc/test/route.js +84 -0
- package/src/app/api/auth/status/route.js +45 -0
- package/src/app/api/cli-tools/all-statuses/route.js +46 -0
- package/src/app/api/cli-tools/antigravity-mitm/alias/route.js +53 -0
- package/src/app/api/cli-tools/antigravity-mitm/route.js +202 -0
- package/src/app/api/cli-tools/claude-settings/route.js +203 -0
- package/src/app/api/cli-tools/cline-settings/route.js +133 -0
- package/src/app/api/cli-tools/codex-gateway/accounts/route.js +16 -0
- package/src/app/api/cli-tools/codex-settings/route.js +239 -0
- package/src/app/api/cli-tools/copilot-settings/route.js +148 -0
- package/src/app/api/cli-tools/cowork-mcp-registry/route.js +77 -0
- package/src/app/api/cli-tools/cowork-mcp-tools/route.js +95 -0
- package/src/app/api/cli-tools/cowork-settings/route.js +412 -0
- package/src/app/api/cli-tools/deepseek-tui-settings/route.js +164 -0
- package/src/app/api/cli-tools/droid-settings/route.js +213 -0
- package/src/app/api/cli-tools/hermes-settings/route.js +175 -0
- package/src/app/api/cli-tools/jcode-settings/route.js +216 -0
- package/src/app/api/cli-tools/kilo-settings/route.js +131 -0
- package/src/app/api/cli-tools/openclaw-settings/route.js +292 -0
- package/src/app/api/cli-tools/opencode-settings/route.js +259 -0
- package/src/app/api/combos/[id]/route.js +81 -0
- package/src/app/api/combos/route.js +48 -0
- package/src/app/api/health/route.js +15 -0
- package/src/app/api/init/route.js +4 -0
- package/src/app/api/keys/[id]/route.js +58 -0
- package/src/app/api/keys/route.js +42 -0
- package/src/app/api/locale/route.js +30 -0
- package/src/app/api/mcp/[plugin]/message/route.js +21 -0
- package/src/app/api/mcp/[plugin]/sse/route.js +37 -0
- package/src/app/api/media-providers/tts/deepgram/voices/route.js +65 -0
- package/src/app/api/media-providers/tts/elevenlabs/voices/route.js +71 -0
- package/src/app/api/media-providers/tts/inworld/voices/route.js +61 -0
- package/src/app/api/media-providers/tts/minimax/voices/route.js +113 -0
- package/src/app/api/media-providers/tts/voices/route.js +99 -0
- package/src/app/api/models/alias/route.js +53 -0
- package/src/app/api/models/availability/route.js +103 -0
- package/src/app/api/models/custom/route.js +48 -0
- package/src/app/api/models/disabled/route.js +50 -0
- package/src/app/api/models/route.js +64 -0
- package/src/app/api/models/test/ping.js +191 -0
- package/src/app/api/models/test/route.js +14 -0
- package/src/app/api/oauth/[provider]/[action]/route.js +343 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/cancel/route.js +19 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/manual/[workerId]/route.js +30 -0
- package/src/app/api/oauth/codebuddy/bulk-import/[jobId]/route.js +23 -0
- package/src/app/api/oauth/codebuddy/bulk-import/latest/route.js +25 -0
- package/src/app/api/oauth/codebuddy/bulk-import/route.js +49 -0
- package/src/app/api/oauth/codebuddy/quota-cookie/route.js +133 -0
- package/src/app/api/oauth/codex/import-token/route.js +96 -0
- package/src/app/api/oauth/cursor/auto-import/route.js +258 -0
- package/src/app/api/oauth/cursor/import/route.js +100 -0
- package/src/app/api/oauth/gitlab/pat/route.js +62 -0
- package/src/app/api/oauth/iflow/cookie/route.js +137 -0
- package/src/app/api/oauth/kiro/auto-import/route.js +85 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/cancel/route.js +18 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/manual/[workerId]/route.js +29 -0
- package/src/app/api/oauth/kiro/bulk-import/[jobId]/route.js +22 -0
- package/src/app/api/oauth/kiro/bulk-import/latest/route.js +25 -0
- package/src/app/api/oauth/kiro/bulk-import/route.js +49 -0
- package/src/app/api/oauth/kiro/import/route.js +110 -0
- package/src/app/api/oauth/kiro/social-authorize/route.js +27 -0
- package/src/app/api/oauth/kiro/social-exchange/route.js +41 -0
- package/src/app/api/pricing/route.js +134 -0
- package/src/app/api/provider-nodes/[id]/route.js +101 -0
- package/src/app/api/provider-nodes/route.js +104 -0
- package/src/app/api/provider-nodes/validate/route.js +201 -0
- package/src/app/api/providers/[id]/models/route.js +526 -0
- package/src/app/api/providers/[id]/route.js +189 -0
- package/src/app/api/providers/[id]/test/route.js +23 -0
- package/src/app/api/providers/[id]/test/testUtils.js +714 -0
- package/src/app/api/providers/[id]/test-models/route.js +66 -0
- package/src/app/api/providers/client/route.js +126 -0
- package/src/app/api/providers/kilo/free-models/route.js +55 -0
- package/src/app/api/providers/route.js +206 -0
- package/src/app/api/providers/suggested-models/filters.js +20 -0
- package/src/app/api/providers/suggested-models/route.js +32 -0
- package/src/app/api/providers/test-batch/route.js +131 -0
- package/src/app/api/providers/validate/route.js +637 -0
- package/src/app/api/proxy-pools/[id]/route.js +123 -0
- package/src/app/api/proxy-pools/[id]/test/route.js +70 -0
- package/src/app/api/proxy-pools/cloudflare-deploy/route.js +145 -0
- package/src/app/api/proxy-pools/deno-deploy/route.js +175 -0
- package/src/app/api/proxy-pools/route.js +93 -0
- package/src/app/api/proxy-pools/vercel-deploy/route.js +142 -0
- package/src/app/api/settings/database/route.js +36 -0
- package/src/app/api/settings/proxy-test/route.js +23 -0
- package/src/app/api/settings/require-login/route.js +15 -0
- package/src/app/api/settings/route.js +100 -0
- package/src/app/api/shutdown/route.js +24 -0
- package/src/app/api/tags/route.js +18 -0
- package/src/app/api/translator/console-logs/route.js +24 -0
- package/src/app/api/translator/console-logs/stream/route.js +79 -0
- package/src/app/api/translator/load/route.js +45 -0
- package/src/app/api/translator/save/route.js +44 -0
- package/src/app/api/translator/send/route.js +94 -0
- package/src/app/api/translator/translate/route.js +90 -0
- package/src/app/api/tunnel/disable/route.js +12 -0
- package/src/app/api/tunnel/enable/route.js +16 -0
- package/src/app/api/tunnel/status/route.js +13 -0
- package/src/app/api/tunnel/tailscale-check/route.js +50 -0
- package/src/app/api/tunnel/tailscale-disable/route.js +12 -0
- package/src/app/api/tunnel/tailscale-enable/route.js +12 -0
- package/src/app/api/tunnel/tailscale-install/route.js +72 -0
- package/src/app/api/usage/[connectionId]/route.js +188 -0
- package/src/app/api/usage/chart/route.js +21 -0
- package/src/app/api/usage/history/route.js +12 -0
- package/src/app/api/usage/logs/route.js +12 -0
- package/src/app/api/usage/providers/route.js +42 -0
- package/src/app/api/usage/request-details/route.js +57 -0
- package/src/app/api/usage/request-logs/route.js +13 -0
- package/src/app/api/usage/stats/route.js +23 -0
- package/src/app/api/usage/stream/route.js +79 -0
- package/src/app/api/v1/api/chat/route.js +37 -0
- package/src/app/api/v1/audio/speech/route.js +16 -0
- package/src/app/api/v1/audio/transcriptions/route.js +19 -0
- package/src/app/api/v1/audio/voices/route.js +68 -0
- package/src/app/api/v1/chat/completions/route.js +35 -0
- package/src/app/api/v1/embeddings/route.js +21 -0
- package/src/app/api/v1/images/generations/route.js +16 -0
- package/src/app/api/v1/messages/count_tokens/route.js +52 -0
- package/src/app/api/v1/messages/route.js +36 -0
- package/src/app/api/v1/models/[kind]/route.js +55 -0
- package/src/app/api/v1/models/info/route.js +110 -0
- package/src/app/api/v1/models/route.js +451 -0
- package/src/app/api/v1/responses/compact/route.js +37 -0
- package/src/app/api/v1/responses/route.js +30 -0
- package/src/app/api/v1/route.js +1 -0
- package/src/app/api/v1/search/route.js +21 -0
- package/src/app/api/v1/web/fetch/route.js +21 -0
- package/src/app/api/v1beta/models/[...path]/route.js +328 -0
- package/src/app/api/v1beta/models/route.js +44 -0
- package/src/app/api/version/route.js +45 -0
- package/src/app/api/version/shutdown/route.js +15 -0
- package/src/app/api/version/update/route.js +21 -0
- package/src/app/callback/page.js +148 -0
- package/src/app/dashboard/settings/pricing/page.js +173 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/globals.css +496 -0
- package/src/app/landing/components/AnimatedBackground.js +57 -0
- package/src/app/landing/components/Features.js +133 -0
- package/src/app/landing/components/FlowAnimation.js +175 -0
- package/src/app/landing/components/Footer.js +61 -0
- package/src/app/landing/components/GetStarted.js +97 -0
- package/src/app/landing/components/HeroSection.js +47 -0
- package/src/app/landing/components/HowItWorks.js +66 -0
- package/src/app/landing/components/Navigation.js +72 -0
- package/src/app/landing/page.js +106 -0
- package/src/app/layout.js +49 -0
- package/src/app/login/page.js +197 -0
- package/src/app/manifest.js +30 -0
- package/src/app/page.js +5 -0
- package/src/dashboardGuard.js +242 -0
- package/src/i18n/RuntimeI18nProvider.js +27 -0
- package/src/i18n/config.js +146 -0
- package/src/i18n/runtime.js +162 -0
- package/src/lib/appUpdater.js +200 -0
- package/src/lib/auth/dashboardSession.js +68 -0
- package/src/lib/auth/loginLimiter.js +52 -0
- package/src/lib/auth/oidc.js +234 -0
- package/src/lib/consoleLogBuffer.js +79 -0
- package/src/lib/dataDir.js +29 -0
- package/src/lib/db/adapters/betterSqliteAdapter.js +55 -0
- package/src/lib/db/adapters/bunSqliteAdapter.js +63 -0
- package/src/lib/db/adapters/nodeSqliteAdapter.js +84 -0
- package/src/lib/db/adapters/sqljsAdapter.js +115 -0
- package/src/lib/db/backup.js +35 -0
- package/src/lib/db/driver.js +85 -0
- package/src/lib/db/helpers/jsonCol.js +9 -0
- package/src/lib/db/helpers/kvStore.js +39 -0
- package/src/lib/db/helpers/metaStore.js +22 -0
- package/src/lib/db/index.js +171 -0
- package/src/lib/db/migrate.js +286 -0
- package/src/lib/db/migrations/001-initial.js +14 -0
- package/src/lib/db/migrations/index.js +10 -0
- package/src/lib/db/paths.js +18 -0
- package/src/lib/db/repos/aliasRepo.js +62 -0
- package/src/lib/db/repos/apiKeysRepo.js +75 -0
- package/src/lib/db/repos/combosRepo.js +73 -0
- package/src/lib/db/repos/connectionsRepo.js +226 -0
- package/src/lib/db/repos/disabledModelsRepo.js +56 -0
- package/src/lib/db/repos/nodesRepo.js +95 -0
- package/src/lib/db/repos/pricingRepo.js +108 -0
- package/src/lib/db/repos/proxyPoolsRepo.js +103 -0
- package/src/lib/db/repos/requestDetailsRepo.js +259 -0
- package/src/lib/db/repos/settingsRepo.js +104 -0
- package/src/lib/db/repos/usageRepo.js +731 -0
- package/src/lib/db/schema.js +157 -0
- package/src/lib/db/version.js +21 -0
- package/src/lib/disabledModelsDb.js +4 -0
- package/src/lib/localDb.js +21 -0
- package/src/lib/mcp/stdioSseBridge.js +198 -0
- package/src/lib/mitmAliasCache.js +46 -0
- package/src/lib/network/connectionProxy.js +160 -0
- package/src/lib/network/initOutboundProxy.js +25 -0
- package/src/lib/network/outboundProxy.js +68 -0
- package/src/lib/network/proxyTest.js +91 -0
- package/src/lib/oauth/constants/oauth.js +284 -0
- package/src/lib/oauth/constants/xai.js +61 -0
- package/src/lib/oauth/providers.js +1506 -0
- package/src/lib/oauth/services/antigravity.js +321 -0
- package/src/lib/oauth/services/claude.js +136 -0
- package/src/lib/oauth/services/codebuddyBulkImportManager.js +454 -0
- package/src/lib/oauth/services/codex.js +144 -0
- package/src/lib/oauth/services/cursor.js +179 -0
- package/src/lib/oauth/services/gemini.js +240 -0
- package/src/lib/oauth/services/github.js +225 -0
- package/src/lib/oauth/services/iflow.js +202 -0
- package/src/lib/oauth/services/index.js +17 -0
- package/src/lib/oauth/services/kiro.js +334 -0
- package/src/lib/oauth/services/kiroBulkImportManager.js +778 -0
- package/src/lib/oauth/services/kiroConnections.js +93 -0
- package/src/lib/oauth/services/kiroGoogleAutomation.js +1136 -0
- package/src/lib/oauth/services/oauth.js +157 -0
- package/src/lib/oauth/services/openai.js +123 -0
- package/src/lib/oauth/services/qoder.js +216 -0
- package/src/lib/oauth/services/qwen.js +170 -0
- package/src/lib/oauth/services/xai.js +238 -0
- package/src/lib/oauth/utils/banner.js +63 -0
- package/src/lib/oauth/utils/pkce.js +39 -0
- package/src/lib/oauth/utils/server.js +415 -0
- package/src/lib/oauth/utils/ui.js +48 -0
- package/src/lib/providerNormalization.js +45 -0
- package/src/lib/qoder/constants.js +64 -0
- package/src/lib/qoder/cosy.js +175 -0
- package/src/lib/qoder/encoding.js +55 -0
- package/src/lib/requestDetailsDb.js +4 -0
- package/src/lib/tunnel/cloudflare/cloudflared.js +449 -0
- package/src/lib/tunnel/cloudflare/config.js +9 -0
- package/src/lib/tunnel/cloudflare/healthCheck.js +29 -0
- package/src/lib/tunnel/cloudflare/manager.js +151 -0
- package/src/lib/tunnel/cloudflare/pid.js +23 -0
- package/src/lib/tunnel/index.js +48 -0
- package/src/lib/tunnel/shared/dnsResolver.js +17 -0
- package/src/lib/tunnel/shared/internetCheck.js +26 -0
- package/src/lib/tunnel/shared/state.js +41 -0
- package/src/lib/tunnel/shared/watchdogConfig.js +8 -0
- package/src/lib/tunnel/tailscale/config.js +7 -0
- package/src/lib/tunnel/tailscale/healthCheck.js +29 -0
- package/src/lib/tunnel/tailscale/manager.js +129 -0
- package/src/lib/tunnel/tailscale/tailscale.js +790 -0
- package/src/lib/updater/updater.js +235 -0
- package/src/lib/usage/fetcher.js +208 -0
- package/src/lib/usageDb.js +7 -0
- package/src/mitm/antigravityIdeVersion.js +50 -0
- package/src/mitm/cert/generate.js +32 -0
- package/src/mitm/cert/install.js +269 -0
- package/src/mitm/cert/rootCA.js +173 -0
- package/src/mitm/config.js +87 -0
- package/src/mitm/dbReader.js +22 -0
- package/src/mitm/dns/dnsConfig.js +266 -0
- package/src/mitm/handlers/antigravity.js +33 -0
- package/src/mitm/handlers/base.js +226 -0
- package/src/mitm/handlers/copilot.js +35 -0
- package/src/mitm/handlers/cursor.js +15 -0
- package/src/mitm/handlers/kiro.js +526 -0
- package/src/mitm/logger.js +106 -0
- package/src/mitm/manager.js +851 -0
- package/src/mitm/paths.js +32 -0
- package/src/mitm/server.js +435 -0
- package/src/mitm/winElevated.js +81 -0
- package/src/models/index.js +38 -0
- package/src/proxy.js +5 -0
- package/src/shared/components/AddCustomEmbeddingModal.js +183 -0
- package/src/shared/components/Avatar.js +88 -0
- package/src/shared/components/Badge.js +54 -0
- package/src/shared/components/BulkAccountAutomationModal.js +508 -0
- package/src/shared/components/Button.js +56 -0
- package/src/shared/components/Card.js +116 -0
- package/src/shared/components/ChangelogModal.js +97 -0
- package/src/shared/components/CodeBuddyQuotaCookieModal.js +109 -0
- package/src/shared/components/ComboFormModal.js +176 -0
- package/src/shared/components/CursorAuthModal.js +212 -0
- package/src/shared/components/DonateModal.js +136 -0
- package/src/shared/components/Drawer.js +82 -0
- package/src/shared/components/EditConnectionModal.js +286 -0
- package/src/shared/components/Footer.js +132 -0
- package/src/shared/components/GitLabAuthModal.js +194 -0
- package/src/shared/components/Header.js +380 -0
- package/src/shared/components/HeaderLanguage.js +46 -0
- package/src/shared/components/HeaderMenu.js +126 -0
- package/src/shared/components/IFlowCookieModal.js +132 -0
- package/src/shared/components/Input.js +65 -0
- package/src/shared/components/KiroAuthModal.js +1171 -0
- package/src/shared/components/KiroOAuthWrapper.js +149 -0
- package/src/shared/components/KiroSocialOAuthModal.js +205 -0
- package/src/shared/components/LanguageSwitcher.js +190 -0
- package/src/shared/components/Loading.js +75 -0
- package/src/shared/components/ManualConfigModal.js +44 -0
- package/src/shared/components/McpMarketplaceModal.js +255 -0
- package/src/shared/components/Modal.js +146 -0
- package/src/shared/components/ModelSelectModal.js +537 -0
- package/src/shared/components/NineRemoteButton.js +23 -0
- package/src/shared/components/NineRemotePromoModal.js +99 -0
- package/src/shared/components/NoAuthProxyCard.js +86 -0
- package/src/shared/components/OAuthModal.js +682 -0
- package/src/shared/components/Pagination.js +150 -0
- package/src/shared/components/PricingModal.js +208 -0
- package/src/shared/components/ProviderIcon.js +63 -0
- package/src/shared/components/ProviderInfoCard.js +82 -0
- package/src/shared/components/RequestLogger.js +121 -0
- package/src/shared/components/SegmentedControl.js +48 -0
- package/src/shared/components/Select.js +67 -0
- package/src/shared/components/Sidebar.js +441 -0
- package/src/shared/components/ThemeProvider.js +15 -0
- package/src/shared/components/ThemeToggle.js +42 -0
- package/src/shared/components/Toggle.js +69 -0
- package/src/shared/components/Tooltip.js +25 -0
- package/src/shared/components/UsageStats.js +505 -0
- package/src/shared/components/index.js +46 -0
- package/src/shared/components/layouts/AuthLayout.js +29 -0
- package/src/shared/components/layouts/DashboardLayout.js +104 -0
- package/src/shared/components/layouts/index.js +4 -0
- package/src/shared/constants/cliTools.js +397 -0
- package/src/shared/constants/colors.js +77 -0
- package/src/shared/constants/config.js +99 -0
- package/src/shared/constants/coworkPlugins.js +75 -0
- package/src/shared/constants/index.js +4 -0
- package/src/shared/constants/locales.js +36 -0
- package/src/shared/constants/mitmToolHosts.js +12 -0
- package/src/shared/constants/models.js +38 -0
- package/src/shared/constants/pricing.js +303 -0
- package/src/shared/constants/providers.js +289 -0
- package/src/shared/constants/skills.js +78 -0
- package/src/shared/constants/ttsProviders.js +138 -0
- package/src/shared/hooks/index.js +2 -0
- package/src/shared/hooks/useCopyToClipboard.js +43 -0
- package/src/shared/hooks/useTheme.js +60 -0
- package/src/shared/services/bootstrap.js +12 -0
- package/src/shared/services/initializeApp.js +268 -0
- package/src/shared/utils/api.js +93 -0
- package/src/shared/utils/apiKey.js +98 -0
- package/src/shared/utils/clineAuth.js +37 -0
- package/src/shared/utils/cn.js +11 -0
- package/src/shared/utils/connectionStatus.js +162 -0
- package/src/shared/utils/index.js +40 -0
- package/src/shared/utils/machine.js +6 -0
- package/src/shared/utils/machineId.js +66 -0
- package/src/shared/utils/providerModelsFetcher.js +30 -0
- package/src/sse/handlers/chat.js +261 -0
- package/src/sse/handlers/embeddings.js +141 -0
- package/src/sse/handlers/fetch.js +213 -0
- package/src/sse/handlers/imageGeneration.js +142 -0
- package/src/sse/handlers/search.js +206 -0
- package/src/sse/handlers/stt.js +88 -0
- package/src/sse/handlers/tts.js +114 -0
- package/src/sse/services/auth.js +346 -0
- package/src/sse/services/codexGateway.js +215 -0
- package/src/sse/services/model.js +99 -0
- package/src/sse/services/tokenRefresh.js +319 -0
- package/src/sse/utils/logger.js +75 -0
- package/src/store/headerSearchStore.js +19 -0
- package/src/store/index.js +6 -0
- package/src/store/notificationStore.js +45 -0
- package/src/store/providerStore.js +55 -0
- package/src/store/settingsStore.js +51 -0
- package/src/store/themeStore.js +54 -0
- package/src/store/userStore.js +20 -0
- package/start.sh +4 -0
package/cli/cli.js
ADDED
|
@@ -0,0 +1,806 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { spawn, exec, execSync } = require("child_process");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const fs = require("fs");
|
|
6
|
+
const https = require("https");
|
|
7
|
+
const os = require("os");
|
|
8
|
+
|
|
9
|
+
// Native spinner - no external dependency
|
|
10
|
+
function createSpinner(text) {
|
|
11
|
+
const frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
12
|
+
let i = 0;
|
|
13
|
+
let interval = null;
|
|
14
|
+
let currentText = text;
|
|
15
|
+
return {
|
|
16
|
+
start() {
|
|
17
|
+
if (process.stdout.isTTY) {
|
|
18
|
+
process.stdout.write(`\r${frames[0]} ${currentText}`);
|
|
19
|
+
interval = setInterval(() => {
|
|
20
|
+
process.stdout.write(`\r${frames[i++ % frames.length]} ${currentText}`);
|
|
21
|
+
}, 80);
|
|
22
|
+
}
|
|
23
|
+
return this;
|
|
24
|
+
},
|
|
25
|
+
stop() {
|
|
26
|
+
if (interval) {
|
|
27
|
+
clearInterval(interval);
|
|
28
|
+
interval = null;
|
|
29
|
+
}
|
|
30
|
+
if (process.stdout.isTTY) {
|
|
31
|
+
process.stdout.write("\r\x1b[K");
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
succeed(msg) {
|
|
35
|
+
this.stop();
|
|
36
|
+
console.log(`✅ ${msg}`);
|
|
37
|
+
},
|
|
38
|
+
fail(msg) {
|
|
39
|
+
this.stop();
|
|
40
|
+
console.log(`❌ ${msg}`);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const pkg = require("./package.json");
|
|
46
|
+
const { ensureSqliteRuntime, buildEnvWithRuntime } = require("./hooks/sqliteRuntime");
|
|
47
|
+
const { ensureTrayRuntime } = require("./hooks/trayRuntime");
|
|
48
|
+
const args = process.argv.slice(2);
|
|
49
|
+
|
|
50
|
+
// Self-heal SQLite runtime deps (sql.js + better-sqlite3) into ~/.9router/runtime
|
|
51
|
+
// so the server can resolve them via NODE_PATH. Best-effort — sql.js is required,
|
|
52
|
+
// better-sqlite3 is optional. Logs to stderr only on failure.
|
|
53
|
+
try { ensureSqliteRuntime({ silent: true }); } catch {}
|
|
54
|
+
|
|
55
|
+
// Self-heal tray runtime (systray for macOS/Linux only). Windows skipped.
|
|
56
|
+
try { ensureTrayRuntime({ silent: true }); } catch {}
|
|
57
|
+
|
|
58
|
+
// Configuration constants
|
|
59
|
+
const APP_NAME = pkg.name; // Use from package.json
|
|
60
|
+
const INSTALL_CMD_LATEST = `npm i -g ${APP_NAME}@latest --prefer-online`;
|
|
61
|
+
|
|
62
|
+
const DEFAULT_PORT = 20128;
|
|
63
|
+
const DEFAULT_HOST = "0.0.0.0";
|
|
64
|
+
const MAX_PORT_ATTEMPTS = 10;
|
|
65
|
+
// Identifiers for killAllAppProcesses - only kill 9router specifically
|
|
66
|
+
const PROCESS_IDENTIFIERS = [
|
|
67
|
+
'9router' // Only package name - avoid killing other apps
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
// Parse arguments
|
|
71
|
+
let port = DEFAULT_PORT;
|
|
72
|
+
let host = DEFAULT_HOST;
|
|
73
|
+
let noBrowser = false;
|
|
74
|
+
let skipUpdate = false;
|
|
75
|
+
let showLog = false;
|
|
76
|
+
let trayMode = false;
|
|
77
|
+
|
|
78
|
+
for (let i = 0; i < args.length; i++) {
|
|
79
|
+
if (args[i] === "--port" || args[i] === "-p") {
|
|
80
|
+
port = parseInt(args[i + 1], 10) || DEFAULT_PORT;
|
|
81
|
+
i++;
|
|
82
|
+
} else if (args[i] === "--host" || args[i] === "-H") {
|
|
83
|
+
host = args[i + 1] || DEFAULT_HOST;
|
|
84
|
+
i++;
|
|
85
|
+
} else if (args[i] === "--no-browser" || args[i] === "-n") {
|
|
86
|
+
noBrowser = true;
|
|
87
|
+
} else if (args[i] === "--log" || args[i] === "-l") {
|
|
88
|
+
showLog = true;
|
|
89
|
+
} else if (args[i] === "--skip-update") {
|
|
90
|
+
skipUpdate = true;
|
|
91
|
+
} else if (args[i] === "--tray" || args[i] === "-t") {
|
|
92
|
+
trayMode = true;
|
|
93
|
+
process.env.TRAY_MODE = "1";
|
|
94
|
+
} else if (args[i] === "--help" || args[i] === "-h") {
|
|
95
|
+
console.log(`
|
|
96
|
+
Usage: ${APP_NAME} [options]
|
|
97
|
+
|
|
98
|
+
Options:
|
|
99
|
+
-p, --port <port> Port to run the server (default: ${DEFAULT_PORT})
|
|
100
|
+
-H, --host <host> Host to bind (default: ${DEFAULT_HOST})
|
|
101
|
+
-n, --no-browser Don't open browser automatically
|
|
102
|
+
-l, --log Show server logs (default: hidden)
|
|
103
|
+
-t, --tray Run in system tray mode (background)
|
|
104
|
+
--skip-update Skip auto-update check
|
|
105
|
+
-h, --help Show this help message
|
|
106
|
+
-v, --version Show version
|
|
107
|
+
`);
|
|
108
|
+
process.exit(0);
|
|
109
|
+
} else if (args[i] === "--version" || args[i] === "-v") {
|
|
110
|
+
console.log(pkg.version);
|
|
111
|
+
process.exit(0);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Auto-relaunch after update: detached process has no TTY → fallback to tray
|
|
116
|
+
if (skipUpdate && !trayMode && !process.stdin.isTTY) {
|
|
117
|
+
trayMode = true;
|
|
118
|
+
process.env.TRAY_MODE = "1";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Always use Node.js runtime with absolute path
|
|
122
|
+
const RUNTIME = process.execPath;
|
|
123
|
+
|
|
124
|
+
// Compare semver versions: returns 1 if a > b, -1 if a < b, 0 if equal
|
|
125
|
+
function compareVersions(a, b) {
|
|
126
|
+
const partsA = a.split(".").map(Number);
|
|
127
|
+
const partsB = b.split(".").map(Number);
|
|
128
|
+
for (let i = 0; i < 3; i++) {
|
|
129
|
+
if (partsA[i] > partsB[i]) return 1;
|
|
130
|
+
if (partsA[i] < partsB[i]) return -1;
|
|
131
|
+
}
|
|
132
|
+
return 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Get app data dir (matches app/src/lib/dataDir.js convention)
|
|
136
|
+
function getAppDataDir() {
|
|
137
|
+
return process.platform === "win32"
|
|
138
|
+
? path.join(process.env.APPDATA || "", "9router")
|
|
139
|
+
: path.join(os.homedir(), ".9router");
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Kill PID from file (best-effort, removes file after)
|
|
143
|
+
function killByPidFile(pidFile) {
|
|
144
|
+
try {
|
|
145
|
+
if (!fs.existsSync(pidFile)) return;
|
|
146
|
+
const pid = parseInt(fs.readFileSync(pidFile, "utf8").trim(), 10);
|
|
147
|
+
if (!pid) return;
|
|
148
|
+
try {
|
|
149
|
+
if (process.platform === "win32") {
|
|
150
|
+
execSync(`taskkill /F /T /PID ${pid}`, { stdio: "ignore", windowsHide: true, timeout: 3000 });
|
|
151
|
+
} else {
|
|
152
|
+
process.kill(pid, "SIGKILL");
|
|
153
|
+
}
|
|
154
|
+
} catch { }
|
|
155
|
+
try { fs.unlinkSync(pidFile); } catch { }
|
|
156
|
+
} catch { }
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Kill tunnel processes (cloudflared/tailscale) by their PID files
|
|
160
|
+
function killTunnelByPidFile() {
|
|
161
|
+
const tunnelDir = path.join(getAppDataDir(), "tunnel");
|
|
162
|
+
killByPidFile(path.join(tunnelDir, "cloudflared.pid"));
|
|
163
|
+
killByPidFile(path.join(tunnelDir, "tailscale.pid"));
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Kill cloudflared whose --url targets this app's port (covers stale PID file case)
|
|
167
|
+
function killCloudflaredByAppPort(appPort) {
|
|
168
|
+
if (!appPort) return [];
|
|
169
|
+
const portMatchers = [`localhost:${appPort}`, `127.0.0.1:${appPort}`];
|
|
170
|
+
const pids = [];
|
|
171
|
+
try {
|
|
172
|
+
if (process.platform === "win32") {
|
|
173
|
+
const psCmd = `powershell -NonInteractive -WindowStyle Hidden -Command "Get-WmiObject Win32_Process -Filter 'Name=\\"cloudflared.exe\\"' | Select-Object ProcessId,CommandLine | ConvertTo-Csv -NoTypeInformation"`;
|
|
174
|
+
const output = execSync(psCmd, { encoding: "utf8", windowsHide: true, timeout: 5000 });
|
|
175
|
+
const lines = output.split("\n").slice(1).filter(l => l.trim());
|
|
176
|
+
lines.forEach(line => {
|
|
177
|
+
if (portMatchers.some(m => line.includes(m))) {
|
|
178
|
+
const match = line.match(/^"(\d+)"/);
|
|
179
|
+
if (match && match[1]) pids.push(match[1]);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
} else {
|
|
183
|
+
const output = execSync("ps -eo pid,command 2>/dev/null", { encoding: "utf8", timeout: 5000 });
|
|
184
|
+
output.split("\n").forEach(line => {
|
|
185
|
+
if (line.includes("cloudflared") && portMatchers.some(m => line.includes(m))) {
|
|
186
|
+
const parts = line.trim().split(/\s+/);
|
|
187
|
+
const pid = parts[0];
|
|
188
|
+
if (pid && !isNaN(pid)) pids.push(pid);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
} catch { }
|
|
193
|
+
return pids;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Kill all 9router processes
|
|
197
|
+
function killAllAppProcesses(appPort) {
|
|
198
|
+
return new Promise((resolve) => {
|
|
199
|
+
try {
|
|
200
|
+
// Kill MIT first (privileged process, needs special handling)
|
|
201
|
+
killProxyByPidFile();
|
|
202
|
+
// Kill cloudflared/tailscale by PID file (precise, only this app's tunnel)
|
|
203
|
+
killTunnelByPidFile();
|
|
204
|
+
|
|
205
|
+
const platform = process.platform;
|
|
206
|
+
let pids = [];
|
|
207
|
+
|
|
208
|
+
// Catch stale PID files: kill cloudflared bound to this app's port
|
|
209
|
+
pids.push(...killCloudflaredByAppPort(appPort));
|
|
210
|
+
|
|
211
|
+
if (platform === "win32") {
|
|
212
|
+
// Windows: use WMI to get full CommandLine (tasklist /V doesn't include it)
|
|
213
|
+
try {
|
|
214
|
+
const psCmd = `powershell -NonInteractive -WindowStyle Hidden -Command "Get-WmiObject Win32_Process -Filter 'Name=\\"node.exe\\"' | Select-Object ProcessId,CommandLine | ConvertTo-Csv -NoTypeInformation"`;
|
|
215
|
+
const output = execSync(psCmd, {
|
|
216
|
+
encoding: "utf8",
|
|
217
|
+
windowsHide: true,
|
|
218
|
+
timeout: 5000
|
|
219
|
+
});
|
|
220
|
+
const lines = output.split("\n").slice(1).filter(l => l.trim());
|
|
221
|
+
lines.forEach(line => {
|
|
222
|
+
// Whitelist: real node process running 9router/cli.js, or next-server.
|
|
223
|
+
// Avoids killing editors/grep/strace/cursor that just have "9router" in cmdline.
|
|
224
|
+
const cmd = line.toLowerCase();
|
|
225
|
+
const isAppProcess =
|
|
226
|
+
(cmd.includes("node") && cmd.includes("9router") && (cmd.includes("cli.js") || cmd.includes("\\9router") || cmd.includes("/9router")))
|
|
227
|
+
|| cmd.includes("next-server");
|
|
228
|
+
if (isAppProcess) {
|
|
229
|
+
const match = line.match(/^"(\d+)"/);
|
|
230
|
+
if (match && match[1] && match[1] !== process.pid.toString()) {
|
|
231
|
+
pids.push(match[1]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
} catch (e) {
|
|
236
|
+
// No processes found or error - continue
|
|
237
|
+
}
|
|
238
|
+
} else {
|
|
239
|
+
// macOS/Linux: use ps to find all matching processes
|
|
240
|
+
try {
|
|
241
|
+
const output = execSync('ps aux 2>/dev/null', {
|
|
242
|
+
encoding: 'utf8',
|
|
243
|
+
timeout: 5000
|
|
244
|
+
});
|
|
245
|
+
const lines = output.split('\n');
|
|
246
|
+
|
|
247
|
+
lines.forEach(line => {
|
|
248
|
+
// Whitelist: real node process running 9router/cli.js, or next-server.
|
|
249
|
+
// Avoids killing grep/strace/editors/cursor that incidentally match "9router".
|
|
250
|
+
const cmd = line.toLowerCase();
|
|
251
|
+
const isAppProcess =
|
|
252
|
+
(cmd.includes("node") && cmd.includes("9router") && (cmd.includes("cli.js") || cmd.includes("/9router")))
|
|
253
|
+
|| cmd.includes("next-server");
|
|
254
|
+
if (isAppProcess) {
|
|
255
|
+
const parts = line.trim().split(/\s+/);
|
|
256
|
+
const pid = parts[1];
|
|
257
|
+
if (pid && !isNaN(pid) && pid !== process.pid.toString()) {
|
|
258
|
+
pids.push(pid);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
} catch (e) {
|
|
263
|
+
// No processes found or error - continue
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Kill all found processes
|
|
268
|
+
if (pids.length > 0) {
|
|
269
|
+
pids.forEach(pid => {
|
|
270
|
+
try {
|
|
271
|
+
if (platform === "win32") {
|
|
272
|
+
execSync(`taskkill /F /PID ${pid} 2>nul`, { stdio: 'ignore', shell: true, windowsHide: true, timeout: 3000 });
|
|
273
|
+
} else {
|
|
274
|
+
execSync(`kill -9 ${pid} 2>/dev/null`, { stdio: 'ignore', timeout: 3000 });
|
|
275
|
+
}
|
|
276
|
+
} catch (err) {
|
|
277
|
+
// Process already dead or can't kill - continue
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
// Wait for processes to fully terminate
|
|
282
|
+
setTimeout(() => resolve(), 1000);
|
|
283
|
+
} else {
|
|
284
|
+
resolve();
|
|
285
|
+
}
|
|
286
|
+
} catch (err) {
|
|
287
|
+
// Silent fail - continue anyway
|
|
288
|
+
resolve();
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Sleep helper using SharedArrayBuffer wait (sync, no busy-loop)
|
|
294
|
+
function sleepSync(ms) {
|
|
295
|
+
try { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); } catch { /* ignore */ }
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Wait until process dies or timeout reached
|
|
299
|
+
function waitForExit(pid, timeoutMs) {
|
|
300
|
+
const deadline = Date.now() + timeoutMs;
|
|
301
|
+
while (Date.now() < deadline) {
|
|
302
|
+
try { process.kill(pid, 0); } catch { return true; }
|
|
303
|
+
sleepSync(100);
|
|
304
|
+
}
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Kill MIT server by PID file (runs privileged, needs special handling)
|
|
309
|
+
// Sends SIGTERM first so MIT can clean up host entries before dying.
|
|
310
|
+
function killProxyByPidFile() {
|
|
311
|
+
try {
|
|
312
|
+
const pidFile = path.join(getAppDataDir(), "mitm", ".mitm.pid");
|
|
313
|
+
if (!fs.existsSync(pidFile)) return;
|
|
314
|
+
const pid = parseInt(fs.readFileSync(pidFile, "utf8").trim(), 10);
|
|
315
|
+
if (!pid) return;
|
|
316
|
+
|
|
317
|
+
if (process.platform === "win32") {
|
|
318
|
+
// Graceful first (lets server cleanup hosts), then force
|
|
319
|
+
try { execSync(`taskkill /T /PID ${pid}`, { stdio: "ignore", windowsHide: true, timeout: 2000 }); } catch { }
|
|
320
|
+
if (!waitForExit(pid, 1500)) {
|
|
321
|
+
try { execSync(`taskkill /F /T /PID ${pid}`, { stdio: "ignore", windowsHide: true, timeout: 3000 }); } catch { }
|
|
322
|
+
}
|
|
323
|
+
// Last-resort: PowerShell Stop-Process (sometimes succeeds where taskkill fails on admin processes)
|
|
324
|
+
if (!waitForExit(pid, 500)) {
|
|
325
|
+
try { execSync(`powershell -NonInteractive -WindowStyle Hidden -Command "Stop-Process -Id ${pid} -Force"`, { stdio: "ignore", windowsHide: true, timeout: 3000 }); } catch { }
|
|
326
|
+
}
|
|
327
|
+
} else {
|
|
328
|
+
// SIGTERM via cached sudo token first
|
|
329
|
+
try { execSync(`sudo -n kill -TERM ${pid} 2>/dev/null`, { stdio: "ignore", timeout: 2000 }); }
|
|
330
|
+
catch { try { process.kill(pid, "SIGTERM"); } catch { } }
|
|
331
|
+
if (!waitForExit(pid, 1500)) {
|
|
332
|
+
try { execSync(`sudo -n kill -9 ${pid} 2>/dev/null`, { stdio: "ignore", timeout: 2000 }); }
|
|
333
|
+
catch { try { process.kill(pid, "SIGKILL"); } catch { } }
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
try { fs.unlinkSync(pidFile); } catch { }
|
|
337
|
+
} catch { }
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Kill any process on specific port
|
|
341
|
+
function killProcessOnPort(port) {
|
|
342
|
+
return new Promise((resolve) => {
|
|
343
|
+
try {
|
|
344
|
+
const platform = process.platform;
|
|
345
|
+
let pid;
|
|
346
|
+
|
|
347
|
+
if (platform === "win32") {
|
|
348
|
+
try {
|
|
349
|
+
const output = execSync(`netstat -ano | findstr :${port}`, {
|
|
350
|
+
encoding: 'utf8',
|
|
351
|
+
shell: true,
|
|
352
|
+
windowsHide: true,
|
|
353
|
+
timeout: 5000
|
|
354
|
+
}).trim();
|
|
355
|
+
const lines = output.split('\n').filter(l => l.includes('LISTENING'));
|
|
356
|
+
if (lines.length > 0) {
|
|
357
|
+
pid = lines[0].trim().split(/\s+/).pop();
|
|
358
|
+
execSync(`taskkill /F /PID ${pid} 2>nul`, { stdio: 'ignore', shell: true, windowsHide: true, timeout: 3000 });
|
|
359
|
+
}
|
|
360
|
+
} catch (e) {
|
|
361
|
+
// Port is free or error
|
|
362
|
+
}
|
|
363
|
+
} else {
|
|
364
|
+
// macOS/Linux
|
|
365
|
+
try {
|
|
366
|
+
const pidOutput = execSync(`lsof -ti:${port}`, {
|
|
367
|
+
encoding: 'utf8',
|
|
368
|
+
stdio: ['pipe', 'pipe', 'ignore']
|
|
369
|
+
}).trim();
|
|
370
|
+
if (pidOutput) {
|
|
371
|
+
pid = pidOutput.split('\n')[0];
|
|
372
|
+
execSync(`kill -9 ${pid} 2>/dev/null`, { stdio: 'ignore', timeout: 3000 });
|
|
373
|
+
}
|
|
374
|
+
} catch (e) {
|
|
375
|
+
// Port is free or error
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
// Wait for port to be released
|
|
380
|
+
setTimeout(() => resolve(), 500);
|
|
381
|
+
} catch (err) {
|
|
382
|
+
// Silent fail - continue anyway
|
|
383
|
+
resolve();
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
// Detect if running in restricted environment (Codespaces, Docker)
|
|
390
|
+
function isRestrictedEnvironment() {
|
|
391
|
+
// Check for Codespaces
|
|
392
|
+
if (process.env.CODESPACES === "true" || process.env.GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN) {
|
|
393
|
+
return "GitHub Codespaces";
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Check for Docker
|
|
397
|
+
if (fs.existsSync("/.dockerenv") || (fs.existsSync("/proc/1/cgroup") && fs.readFileSync("/proc/1/cgroup", "utf8").includes("docker"))) {
|
|
398
|
+
return "Docker";
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Check if new version available, return latest version or null
|
|
405
|
+
function checkForUpdate() {
|
|
406
|
+
return new Promise((resolve) => {
|
|
407
|
+
if (skipUpdate) {
|
|
408
|
+
resolve(null);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
const spinner = createSpinner("Checking for updates...").start();
|
|
413
|
+
let resolved = false;
|
|
414
|
+
|
|
415
|
+
const safetyTimeout = setTimeout(() => {
|
|
416
|
+
if (!resolved) {
|
|
417
|
+
resolved = true;
|
|
418
|
+
spinner.stop();
|
|
419
|
+
resolve(null);
|
|
420
|
+
}
|
|
421
|
+
}, 8000);
|
|
422
|
+
|
|
423
|
+
const done = (version) => {
|
|
424
|
+
if (resolved) return;
|
|
425
|
+
resolved = true;
|
|
426
|
+
clearTimeout(safetyTimeout);
|
|
427
|
+
spinner.stop();
|
|
428
|
+
resolve(version);
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const req = https.get(`https://registry.npmjs.org/${pkg.name}/latest`, { timeout: 3000 }, (res) => {
|
|
432
|
+
let data = "";
|
|
433
|
+
res.on("data", chunk => data += chunk);
|
|
434
|
+
res.on("end", () => {
|
|
435
|
+
try {
|
|
436
|
+
const latest = JSON.parse(data);
|
|
437
|
+
if (latest.version && compareVersions(latest.version, pkg.version) > 0) {
|
|
438
|
+
done(latest.version);
|
|
439
|
+
} else {
|
|
440
|
+
done(null);
|
|
441
|
+
}
|
|
442
|
+
} catch (e) {
|
|
443
|
+
done(null);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
req.on("error", () => done(null));
|
|
449
|
+
req.on("timeout", () => { req.destroy(); done(null); });
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// Open browser
|
|
454
|
+
function openBrowser(url) {
|
|
455
|
+
const platform = process.platform;
|
|
456
|
+
let cmd;
|
|
457
|
+
|
|
458
|
+
if (platform === "darwin") {
|
|
459
|
+
cmd = `open "${url}"`;
|
|
460
|
+
} else if (platform === "win32") {
|
|
461
|
+
cmd = `start "" "${url}"`;
|
|
462
|
+
} else {
|
|
463
|
+
cmd = `xdg-open "${url}"`;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
exec(cmd, { windowsHide: true }, (err) => {
|
|
467
|
+
if (err) {
|
|
468
|
+
console.log(`Open browser manually: ${url}`);
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Find standalone server (bundled in bin/app for published package)
|
|
474
|
+
const standaloneDir = path.join(__dirname, "app");
|
|
475
|
+
const serverPath = path.join(standaloneDir, "server.js");
|
|
476
|
+
|
|
477
|
+
if (!fs.existsSync(serverPath)) {
|
|
478
|
+
console.error("Error: Standalone build not found.");
|
|
479
|
+
console.error("Please run 'npm run build:cli' first.");
|
|
480
|
+
process.exit(1);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// Check for updates FIRST, then start server
|
|
484
|
+
checkForUpdate().then((latestVersion) => {
|
|
485
|
+
killAllAppProcesses(port).then(() => {
|
|
486
|
+
return killProcessOnPort(port);
|
|
487
|
+
}).then(() => {
|
|
488
|
+
startServer(latestVersion);
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
// Show interface selection menu
|
|
493
|
+
async function showInterfaceMenu(latestVersion) {
|
|
494
|
+
const { selectMenu } = require("./src/cli/utils/input");
|
|
495
|
+
const { clearScreen } = require("./src/cli/utils/display");
|
|
496
|
+
const { getEndpoint } = require("./src/cli/utils/endpoint");
|
|
497
|
+
|
|
498
|
+
clearScreen();
|
|
499
|
+
|
|
500
|
+
const displayHost = host === DEFAULT_HOST ? "localhost" : host;
|
|
501
|
+
|
|
502
|
+
// Detect tunnel/local mode for server URL display
|
|
503
|
+
let serverUrl;
|
|
504
|
+
try {
|
|
505
|
+
const { endpoint, tunnelEnabled } = await getEndpoint(port);
|
|
506
|
+
serverUrl = tunnelEnabled ? endpoint.replace(/\/v1$/, "") : `http://${displayHost}:${port}`;
|
|
507
|
+
} catch (e) {
|
|
508
|
+
serverUrl = `http://${displayHost}:${port}`;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const subtitle = `🚀 Server: \x1b[32m${serverUrl}\x1b[0m`;
|
|
512
|
+
|
|
513
|
+
const menuItems = [];
|
|
514
|
+
|
|
515
|
+
if (latestVersion) {
|
|
516
|
+
menuItems.push({ label: `Update to v${latestVersion} (current: v${pkg.version})`, icon: "⬆" });
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
menuItems.push(
|
|
520
|
+
{ label: "Web UI (Open in Browser)", icon: "🌐" },
|
|
521
|
+
{ label: "Terminal UI (Interactive CLI)", icon: "💻" },
|
|
522
|
+
{ label: "Hide to Tray (Background)", icon: "🔔" },
|
|
523
|
+
{ label: "Exit", icon: "🚪" }
|
|
524
|
+
);
|
|
525
|
+
|
|
526
|
+
const selected = await selectMenu(`Choose Interface (v${pkg.version})`, menuItems, 0, subtitle);
|
|
527
|
+
|
|
528
|
+
const offset = latestVersion ? 1 : 0;
|
|
529
|
+
|
|
530
|
+
if (latestVersion && selected === 0) return "update";
|
|
531
|
+
if (selected === offset) return "web";
|
|
532
|
+
if (selected === offset + 1) return "terminal";
|
|
533
|
+
if (selected === offset + 2) return "hide";
|
|
534
|
+
return "exit";
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const MAX_RESTARTS = 2;
|
|
538
|
+
const RESTART_RESET_MS = 30000; // Reset counter if alive > 30s
|
|
539
|
+
|
|
540
|
+
function startServer(latestVersion) {
|
|
541
|
+
const displayHost = host === DEFAULT_HOST ? "localhost" : host;
|
|
542
|
+
const url = `http://${displayHost}:${port}/dashboard`;
|
|
543
|
+
|
|
544
|
+
let restartCount = 0;
|
|
545
|
+
let serverStartTime = Date.now();
|
|
546
|
+
|
|
547
|
+
const CRASH_LOG_LINES = 50;
|
|
548
|
+
let crashLog = [];
|
|
549
|
+
|
|
550
|
+
function spawnServer() {
|
|
551
|
+
serverStartTime = Date.now();
|
|
552
|
+
crashLog = [];
|
|
553
|
+
const child = spawn(RUNTIME, ["--max-old-space-size=6144", serverPath], {
|
|
554
|
+
cwd: standaloneDir,
|
|
555
|
+
stdio: showLog ? "inherit" : ["ignore", "ignore", "pipe"],
|
|
556
|
+
detached: true,
|
|
557
|
+
windowsHide: true,
|
|
558
|
+
env: {
|
|
559
|
+
...buildEnvWithRuntime(process.env),
|
|
560
|
+
PORT: port.toString(),
|
|
561
|
+
HOSTNAME: host
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
if (!showLog && child.stderr) {
|
|
565
|
+
child.stderr.on("data", (data) => {
|
|
566
|
+
const lines = data.toString().split("\n").filter(Boolean);
|
|
567
|
+
crashLog.push(...lines);
|
|
568
|
+
if (crashLog.length > CRASH_LOG_LINES) crashLog = crashLog.slice(-CRASH_LOG_LINES);
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
return child;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
let server = spawnServer();
|
|
575
|
+
|
|
576
|
+
// Cleanup function - force kill server process
|
|
577
|
+
let isCleaningUp = false;
|
|
578
|
+
function cleanup() {
|
|
579
|
+
if (isCleaningUp) return;
|
|
580
|
+
isCleaningUp = true;
|
|
581
|
+
try {
|
|
582
|
+
// Kill tray if running
|
|
583
|
+
try {
|
|
584
|
+
const { killTray } = require("./src/cli/tray/tray");
|
|
585
|
+
killTray();
|
|
586
|
+
} catch (e) { }
|
|
587
|
+
// Kill MIT server (privileged process) via PID file
|
|
588
|
+
killProxyByPidFile();
|
|
589
|
+
// Kill cloudflared/tailscale via PID file (only this app's tunnel)
|
|
590
|
+
killTunnelByPidFile();
|
|
591
|
+
// Kill server process directly
|
|
592
|
+
if (server.pid) {
|
|
593
|
+
process.kill(server.pid, "SIGKILL");
|
|
594
|
+
}
|
|
595
|
+
// Also try to kill process group
|
|
596
|
+
process.kill(-server.pid, "SIGKILL");
|
|
597
|
+
} catch (e) { }
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// Suppress all errors during shutdown (systray lib throws JSON parse errors)
|
|
601
|
+
let isShuttingDown = false;
|
|
602
|
+
process.on("uncaughtException", (err) => {
|
|
603
|
+
if (isShuttingDown) return;
|
|
604
|
+
console.error("Error:", err.message);
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
// Handle all exit scenarios
|
|
608
|
+
process.on("SIGINT", () => {
|
|
609
|
+
if (isShuttingDown) return;
|
|
610
|
+
isShuttingDown = true;
|
|
611
|
+
console.log("\nExiting...");
|
|
612
|
+
cleanup();
|
|
613
|
+
setTimeout(() => process.exit(0), 100);
|
|
614
|
+
});
|
|
615
|
+
process.on("SIGTERM", () => {
|
|
616
|
+
if (isShuttingDown) return;
|
|
617
|
+
isShuttingDown = true;
|
|
618
|
+
cleanup();
|
|
619
|
+
setTimeout(() => process.exit(0), 100);
|
|
620
|
+
});
|
|
621
|
+
process.on("SIGHUP", () => {
|
|
622
|
+
if (isShuttingDown) return;
|
|
623
|
+
isShuttingDown = true;
|
|
624
|
+
cleanup();
|
|
625
|
+
setTimeout(() => process.exit(0), 100);
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
// Initialize tray icon (runs alongside TUI)
|
|
629
|
+
const initTrayIcon = () => {
|
|
630
|
+
try {
|
|
631
|
+
const { initTray } = require("./src/cli/tray/tray");
|
|
632
|
+
initTray({
|
|
633
|
+
port,
|
|
634
|
+
onQuit: () => {
|
|
635
|
+
isShuttingDown = true;
|
|
636
|
+
console.log("\n👋 Shutting down from tray...");
|
|
637
|
+
cleanup();
|
|
638
|
+
setTimeout(() => process.exit(0), 100);
|
|
639
|
+
},
|
|
640
|
+
onOpenDashboard: () => openBrowser(url)
|
|
641
|
+
});
|
|
642
|
+
} catch (err) {
|
|
643
|
+
// Tray not available - continue without it
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
// Tray-only mode: no TUI, just tray icon
|
|
648
|
+
if (trayMode) {
|
|
649
|
+
// Ignore SIGHUP so macOS terminal close doesn't kill the background tray process
|
|
650
|
+
process.removeAllListeners("SIGHUP");
|
|
651
|
+
process.on("SIGHUP", () => {});
|
|
652
|
+
|
|
653
|
+
console.log(`\n🚀 ${pkg.name} v${pkg.version}`);
|
|
654
|
+
console.log(`Server: http://${displayHost}:${port}`);
|
|
655
|
+
|
|
656
|
+
setTimeout(() => {
|
|
657
|
+
initTrayIcon();
|
|
658
|
+
console.log("\n💡 Router is now running in system tray. Close this terminal if you want.");
|
|
659
|
+
console.log(" Right-click tray icon to open dashboard or quit.\n");
|
|
660
|
+
}, 2000);
|
|
661
|
+
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
// Wait for server to be ready, then show interface menu loop + tray
|
|
666
|
+
setTimeout(async () => {
|
|
667
|
+
// Start tray icon alongside TUI
|
|
668
|
+
initTrayIcon();
|
|
669
|
+
|
|
670
|
+
try {
|
|
671
|
+
while (true) {
|
|
672
|
+
const choice = await showInterfaceMenu(latestVersion);
|
|
673
|
+
|
|
674
|
+
if (choice === "update") {
|
|
675
|
+
isShuttingDown = true;
|
|
676
|
+
const { clearScreen } = require("./src/cli/utils/display");
|
|
677
|
+
clearScreen();
|
|
678
|
+
console.log(`\n⬆ Update v${pkg.version} → v${latestVersion}\n`);
|
|
679
|
+
console.log(`Run this after exit:\n`);
|
|
680
|
+
console.log(` \x1b[33m${INSTALL_CMD_LATEST}\x1b[0m\n`);
|
|
681
|
+
cleanup();
|
|
682
|
+
await killAllAppProcesses(port);
|
|
683
|
+
await killProcessOnPort(port);
|
|
684
|
+
setTimeout(() => process.exit(0), 200);
|
|
685
|
+
return;
|
|
686
|
+
} else if (choice === "web") {
|
|
687
|
+
openBrowser(url);
|
|
688
|
+
// Wait for user to come back
|
|
689
|
+
const { pause } = require("./src/cli/utils/input");
|
|
690
|
+
await pause("\nPress Enter to go back to menu...");
|
|
691
|
+
} else if (choice === "terminal") {
|
|
692
|
+
// Start Terminal UI - it will return when user selects Back
|
|
693
|
+
const { startTerminalUI } = require("./src/cli/terminalUI");
|
|
694
|
+
await startTerminalUI(port);
|
|
695
|
+
// Loop continues, show menu again
|
|
696
|
+
} else if (choice === "hide") {
|
|
697
|
+
const { clearScreen } = require("./src/cli/utils/display");
|
|
698
|
+
clearScreen();
|
|
699
|
+
|
|
700
|
+
// Enable auto startup on OS boot
|
|
701
|
+
try {
|
|
702
|
+
const { enableAutoStart } = require("./src/cli/tray/autostart");
|
|
703
|
+
enableAutoStart(__filename);
|
|
704
|
+
} catch (e) { }
|
|
705
|
+
|
|
706
|
+
if (process.platform === "darwin") {
|
|
707
|
+
// macOS: keep current process alive — spawning a detached child puts
|
|
708
|
+
// it outside the login session so NSStatusItem silently fails.
|
|
709
|
+
process.removeAllListeners("SIGHUP");
|
|
710
|
+
process.on("SIGHUP", () => {});
|
|
711
|
+
|
|
712
|
+
console.log(`\n⏳ Switching to tray mode... (icon already visible in menu bar)`);
|
|
713
|
+
console.log(`🔔 9Router is running in tray (PID: ${process.pid})`);
|
|
714
|
+
console.log(` Server: http://${displayHost}:${port}`);
|
|
715
|
+
console.log(`\n💡 You can close this terminal. Right-click tray icon to quit.\n`);
|
|
716
|
+
|
|
717
|
+
// Tray already init'd at startup — just keep event loop alive.
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
// Windows/Linux: spawn detached bgProcess (systray works fine in child)
|
|
722
|
+
console.log(`\n⏳ Starting background process... (tray icon will appear in ~3s)`);
|
|
723
|
+
|
|
724
|
+
const bgProcess = spawn(process.execPath, [__filename, "--tray", "--skip-update", "-p", port.toString()], {
|
|
725
|
+
detached: true,
|
|
726
|
+
stdio: "ignore",
|
|
727
|
+
windowsHide: true,
|
|
728
|
+
env: { ...process.env }
|
|
729
|
+
});
|
|
730
|
+
bgProcess.unref();
|
|
731
|
+
|
|
732
|
+
console.log(`🔔 9Router is now running in background (PID: ${bgProcess.pid})`);
|
|
733
|
+
console.log(` Server: http://${displayHost}:${port}`);
|
|
734
|
+
console.log(`\n💡 You can close this terminal. Right-click tray icon to quit.\n`);
|
|
735
|
+
|
|
736
|
+
// cleanup() kills server so bgProcess can claim the port fresh
|
|
737
|
+
cleanup();
|
|
738
|
+
process.exit(0);
|
|
739
|
+
} else if (choice === "exit") {
|
|
740
|
+
isShuttingDown = true;
|
|
741
|
+
console.log("\nExiting...");
|
|
742
|
+
cleanup();
|
|
743
|
+
setTimeout(() => process.exit(0), 100);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
} catch (err) {
|
|
747
|
+
console.error("Error:", err.message);
|
|
748
|
+
cleanup();
|
|
749
|
+
process.exit(1);
|
|
750
|
+
}
|
|
751
|
+
}, 3000);
|
|
752
|
+
|
|
753
|
+
function attachServerEvents() {
|
|
754
|
+
server.on("error", (err) => {
|
|
755
|
+
console.error("Failed to start server:", err.message);
|
|
756
|
+
if (!isShuttingDown) tryRestart();
|
|
757
|
+
else { cleanup(); process.exit(1); }
|
|
758
|
+
});
|
|
759
|
+
|
|
760
|
+
server.on("close", (code) => {
|
|
761
|
+
if (isShuttingDown || code === 0) {
|
|
762
|
+
process.exit(code || 0);
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
tryRestart(code);
|
|
766
|
+
});
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
function tryRestart(code) {
|
|
770
|
+
const aliveMs = Date.now() - serverStartTime;
|
|
771
|
+
// Reset counter if last run was stable
|
|
772
|
+
if (aliveMs >= RESTART_RESET_MS) restartCount = 0;
|
|
773
|
+
|
|
774
|
+
if (restartCount >= MAX_RESTARTS) {
|
|
775
|
+
console.error(`\n⚠️ Server crashed ${MAX_RESTARTS} times. Disabling MIT and restarting...`);
|
|
776
|
+
try {
|
|
777
|
+
const dbPath = path.join(os.homedir(), process.platform === "win32" ? path.join("AppData", "Roaming", "9router", "db.json") : path.join(".9router", "db.json"));
|
|
778
|
+
if (fs.existsSync(dbPath)) {
|
|
779
|
+
const db = JSON.parse(fs.readFileSync(dbPath, "utf-8"));
|
|
780
|
+
if (db.settings) db.settings.mitmEnabled = false;
|
|
781
|
+
fs.writeFileSync(dbPath, JSON.stringify(db, null, 2));
|
|
782
|
+
}
|
|
783
|
+
} catch { /* best effort */ }
|
|
784
|
+
restartCount = 0;
|
|
785
|
+
server = spawnServer();
|
|
786
|
+
attachServerEvents();
|
|
787
|
+
return;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
restartCount++;
|
|
791
|
+
const delay = Math.min(1000 * restartCount, 10000);
|
|
792
|
+
console.error(`\n⚠️ Server exited (code=${code ?? "unknown"}). Restarting in ${delay / 1000}s... (${restartCount}/${MAX_RESTARTS})`);
|
|
793
|
+
if (crashLog.length) {
|
|
794
|
+
console.error("\n--- Server crash log ---");
|
|
795
|
+
crashLog.forEach(l => console.error(l));
|
|
796
|
+
console.error("--- End crash log ---\n");
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
setTimeout(() => {
|
|
800
|
+
server = spawnServer();
|
|
801
|
+
attachServerEvents();
|
|
802
|
+
}, delay);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
attachServerEvents();
|
|
806
|
+
}
|