Files
9router/src/app/(dashboard)/dashboard/mitm/MitmPageClient.js
decolua 4ba546afe7 Enhance token refresh logic and improve MITM server handling
- Introduced a caching mechanism for in-flight token refresh requests to prevent race conditions and reduce unnecessary API calls.
- Added error handling for unrecoverable refresh errors, ensuring that the application can gracefully handle token reuse and invalidation scenarios.
- Updated the MITM server management to handle port 443 conflicts, allowing users to kill processes occupying the port before starting the server.
- Improved user feedback in the MitmServerCard component regarding port conflicts and admin privileges.
- Refactored the ComboList component to streamline the display of media provider combos.

This update aims to enhance the reliability and user experience of the token management and MITM functionalities.
2026-05-03 22:10:03 +07:00

111 lines
3.5 KiB
JavaScript

"use client";
import { useState, useEffect } from "react";
import { MITM_TOOLS } from "@/shared/constants/cliTools";
import { getModelsByProviderId } from "@/shared/constants/models";
import { isOpenAICompatibleProvider, isAnthropicCompatibleProvider } from "@/shared/constants/providers";
import { MitmServerCard, MitmToolCard } from "@/app/(dashboard)/dashboard/cli-tools/components";
export default function MitmPageClient() {
const [connections, setConnections] = useState([]);
const [apiKeys, setApiKeys] = useState([]);
const [modelAliases, setModelAliases] = useState({});
const [cloudEnabled, setCloudEnabled] = useState(false);
const [expandedTool, setExpandedTool] = useState(null);
const [mitmStatus, setMitmStatus] = useState({ running: false, certExists: false, dnsStatus: {}, hasCachedPassword: false });
useEffect(() => {
fetchConnections();
fetchApiKeys();
fetchAliases();
fetchCloudSettings();
}, []);
const fetchConnections = async () => {
try {
const res = await fetch("/api/providers");
if (res.ok) {
const data = await res.json();
setConnections(data.connections || []);
}
} catch { /* ignore */ }
};
const fetchApiKeys = async () => {
try {
const res = await fetch("/api/keys");
if (res.ok) {
const data = await res.json();
setApiKeys(data.keys || []);
}
} catch { /* ignore */ }
};
const fetchAliases = async () => {
try {
const res = await fetch("/api/models/alias");
if (res.ok) {
const data = await res.json();
setModelAliases(data.aliases || {});
}
} catch { /* ignore */ }
};
const fetchCloudSettings = async () => {
try {
const res = await fetch("/api/settings");
if (res.ok) {
const data = await res.json();
setCloudEnabled(data.cloudEnabled || false);
}
} catch { /* ignore */ }
};
const getActiveProviders = () => connections.filter(c => c.isActive !== false);
const hasActiveProviders = () => {
const active = getActiveProviders();
return active.some(conn =>
getModelsByProviderId(conn.provider).length > 0 ||
isOpenAICompatibleProvider(conn.provider) ||
isAnthropicCompatibleProvider(conn.provider)
);
};
const mitmTools = Object.entries(MITM_TOOLS);
return (
<div className="flex w-full flex-col gap-6">
{/* MITM Server Card */}
<MitmServerCard
apiKeys={apiKeys}
cloudEnabled={cloudEnabled}
onStatusChange={setMitmStatus}
/>
{/* Tool Cards */}
<div className="grid gap-3 sm:gap-4">
{mitmTools.map(([toolId, tool]) => (
<MitmToolCard
key={toolId}
tool={tool}
isExpanded={expandedTool === toolId}
onToggle={() => setExpandedTool(expandedTool === toolId ? null : toolId)}
serverRunning={mitmStatus.running}
dnsActive={mitmStatus.dnsStatus?.[toolId] || false}
hasCachedPassword={mitmStatus.hasCachedPassword || false}
needsSudoPassword={mitmStatus.needsSudoPassword !== false}
isWin={mitmStatus.isWin === true}
apiKeys={apiKeys}
activeProviders={getActiveProviders()}
hasActiveProviders={hasActiveProviders()}
modelAliases={modelAliases}
cloudEnabled={cloudEnabled}
onDnsChange={(data) => setMitmStatus(prev => ({ ...prev, dnsStatus: data.dnsStatus ?? prev.dnsStatus }))}
/>
))}
</div>
</div>
);
}