mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-29 01:25:33 +08:00
feat(proxy): add local proxy auto-scan and fix hot-reload
- Add scan_local_proxies command to detect common proxy ports - Fix SkillService not using updated proxy after hot-reload - Move global proxy settings to advanced tab - Add error handling for scan failures
This commit is contained in:
+72
-47
@@ -8,77 +8,102 @@ import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { toast } from "sonner";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import {
|
||||
getGlobalProxyUrl,
|
||||
setGlobalProxyUrl,
|
||||
testProxyUrl,
|
||||
getUpstreamProxyStatus,
|
||||
type ProxyTestResult,
|
||||
type UpstreamProxyStatus,
|
||||
getGlobalProxyUrl,
|
||||
setGlobalProxyUrl,
|
||||
testProxyUrl,
|
||||
getUpstreamProxyStatus,
|
||||
scanLocalProxies,
|
||||
type ProxyTestResult,
|
||||
type UpstreamProxyStatus,
|
||||
type DetectedProxy,
|
||||
} from "@/lib/api/globalProxy";
|
||||
|
||||
/**
|
||||
* 获取全局代理 URL
|
||||
*/
|
||||
export function useGlobalProxyUrl() {
|
||||
return useQuery({
|
||||
queryKey: ["globalProxyUrl"],
|
||||
queryFn: getGlobalProxyUrl,
|
||||
});
|
||||
return useQuery({
|
||||
queryKey: ["globalProxyUrl"],
|
||||
queryFn: getGlobalProxyUrl,
|
||||
staleTime: 30 * 1000, // 30秒内不重新获取,避免展开时闪烁
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局代理 URL
|
||||
*/
|
||||
export function useSetGlobalProxyUrl() {
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: setGlobalProxyUrl,
|
||||
onSuccess: () => {
|
||||
toast.success(t("settings.globalProxy.saved"));
|
||||
queryClient.invalidateQueries({ queryKey: ["globalProxyUrl"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["upstreamProxyStatus"] });
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(
|
||||
t("settings.globalProxy.saveFailed", { error: error.message })
|
||||
);
|
||||
},
|
||||
});
|
||||
return useMutation({
|
||||
mutationFn: setGlobalProxyUrl,
|
||||
onSuccess: () => {
|
||||
toast.success(t("settings.globalProxy.saved"));
|
||||
queryClient.invalidateQueries({ queryKey: ["globalProxyUrl"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["upstreamProxyStatus"] });
|
||||
},
|
||||
onError: (error: unknown) => {
|
||||
const message =
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: typeof error === "string"
|
||||
? error
|
||||
: "Unknown error";
|
||||
toast.error(t("settings.globalProxy.saveFailed", { error: message }));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试代理连接
|
||||
*/
|
||||
export function useTestProxy() {
|
||||
const { t } = useTranslation();
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: testProxyUrl,
|
||||
onSuccess: (result: ProxyTestResult) => {
|
||||
if (result.success) {
|
||||
toast.success(
|
||||
t("settings.globalProxy.testSuccess", { latency: result.latencyMs })
|
||||
);
|
||||
} else {
|
||||
toast.error(
|
||||
t("settings.globalProxy.testFailed", { error: result.error })
|
||||
);
|
||||
}
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message);
|
||||
},
|
||||
});
|
||||
return useMutation({
|
||||
mutationFn: testProxyUrl,
|
||||
onSuccess: (result: ProxyTestResult) => {
|
||||
if (result.success) {
|
||||
toast.success(
|
||||
t("settings.globalProxy.testSuccess", { latency: result.latencyMs }),
|
||||
);
|
||||
} else {
|
||||
toast.error(
|
||||
t("settings.globalProxy.testFailed", { error: result.error }),
|
||||
);
|
||||
}
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前出站代理状态
|
||||
*/
|
||||
export function useUpstreamProxyStatus() {
|
||||
return useQuery<UpstreamProxyStatus>({
|
||||
queryKey: ["upstreamProxyStatus"],
|
||||
queryFn: getUpstreamProxyStatus,
|
||||
});
|
||||
return useQuery<UpstreamProxyStatus>({
|
||||
queryKey: ["upstreamProxyStatus"],
|
||||
queryFn: getUpstreamProxyStatus,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描本地代理
|
||||
*/
|
||||
export function useScanProxies() {
|
||||
const { t } = useTranslation();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: scanLocalProxies,
|
||||
onError: (error: Error) => {
|
||||
toast.error(
|
||||
t("settings.globalProxy.scanFailed", { error: error.message }),
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export type { DetectedProxy };
|
||||
|
||||
Reference in New Issue
Block a user