mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-08-04 03:32:25 +08:00
feat(proxy): add username/password authentication support
- Add separate username and password input fields - Implement password visibility toggle with eye icon - Add clear button to reset all proxy fields - Auto-extract auth info from saved URL and merge on save - Update i18n translations (zh/en/ja)
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
/**
|
/**
|
||||||
* 全局出站代理设置组件
|
* 全局出站代理设置组件
|
||||||
*
|
*
|
||||||
* 提供配置全局代理的输入界面。
|
* 提供配置全局代理的输入界面,支持用户名密码认证。
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect, useMemo } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Loader2, TestTube2, Search } from "lucide-react";
|
import { Loader2, TestTube2, Search, Eye, EyeOff, X } from "lucide-react";
|
||||||
import {
|
import {
|
||||||
useGlobalProxyUrl,
|
useGlobalProxyUrl,
|
||||||
useSetGlobalProxyUrl,
|
useSetGlobalProxyUrl,
|
||||||
@@ -17,6 +17,56 @@ import {
|
|||||||
type DetectedProxy,
|
type DetectedProxy,
|
||||||
} from "@/hooks/useGlobalProxy";
|
} from "@/hooks/useGlobalProxy";
|
||||||
|
|
||||||
|
/** 从完整 URL 提取认证信息 */
|
||||||
|
function extractAuth(url: string): {
|
||||||
|
baseUrl: string;
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
} {
|
||||||
|
if (!url.trim()) return { baseUrl: "", username: "", password: "" };
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = new URL(url);
|
||||||
|
const username = decodeURIComponent(parsed.username || "");
|
||||||
|
const password = decodeURIComponent(parsed.password || "");
|
||||||
|
// 移除认证信息,获取基础 URL
|
||||||
|
parsed.username = "";
|
||||||
|
parsed.password = "";
|
||||||
|
return { baseUrl: parsed.toString(), username, password };
|
||||||
|
} catch {
|
||||||
|
return { baseUrl: url, username: "", password: "" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 将认证信息合并到 URL */
|
||||||
|
function mergeAuth(
|
||||||
|
baseUrl: string,
|
||||||
|
username: string,
|
||||||
|
password: string,
|
||||||
|
): string {
|
||||||
|
if (!baseUrl.trim()) return "";
|
||||||
|
if (!username.trim()) return baseUrl;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = new URL(baseUrl);
|
||||||
|
parsed.username = encodeURIComponent(username.trim());
|
||||||
|
if (password) {
|
||||||
|
parsed.password = encodeURIComponent(password);
|
||||||
|
}
|
||||||
|
return parsed.toString();
|
||||||
|
} catch {
|
||||||
|
// URL 解析失败,尝试手动插入
|
||||||
|
const match = baseUrl.match(/^(\w+:\/\/)(.+)$/);
|
||||||
|
if (match) {
|
||||||
|
const auth = password
|
||||||
|
? `${encodeURIComponent(username)}:${encodeURIComponent(password)}@`
|
||||||
|
: `${encodeURIComponent(username)}@`;
|
||||||
|
return `${match[1]}${auth}${match[2]}`;
|
||||||
|
}
|
||||||
|
return baseUrl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function GlobalProxySettings() {
|
export function GlobalProxySettings() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { data: savedUrl, isLoading } = useGlobalProxyUrl();
|
const { data: savedUrl, isLoading } = useGlobalProxyUrl();
|
||||||
@@ -25,25 +75,37 @@ export function GlobalProxySettings() {
|
|||||||
const scanMutation = useScanProxies();
|
const scanMutation = useScanProxies();
|
||||||
|
|
||||||
const [url, setUrl] = useState("");
|
const [url, setUrl] = useState("");
|
||||||
|
const [username, setUsername] = useState("");
|
||||||
|
const [password, setPassword] = useState("");
|
||||||
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
const [dirty, setDirty] = useState(false);
|
const [dirty, setDirty] = useState(false);
|
||||||
const [detected, setDetected] = useState<DetectedProxy[]>([]);
|
const [detected, setDetected] = useState<DetectedProxy[]>([]);
|
||||||
|
|
||||||
|
// 计算完整 URL(含认证信息)
|
||||||
|
const fullUrl = useMemo(
|
||||||
|
() => mergeAuth(url, username, password),
|
||||||
|
[url, username, password],
|
||||||
|
);
|
||||||
|
|
||||||
// 同步远程配置
|
// 同步远程配置
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (savedUrl !== undefined) {
|
if (savedUrl !== undefined) {
|
||||||
setUrl(savedUrl || "");
|
const { baseUrl, username: u, password: p } = extractAuth(savedUrl || "");
|
||||||
|
setUrl(baseUrl);
|
||||||
|
setUsername(u);
|
||||||
|
setPassword(p);
|
||||||
setDirty(false);
|
setDirty(false);
|
||||||
}
|
}
|
||||||
}, [savedUrl]);
|
}, [savedUrl]);
|
||||||
|
|
||||||
const handleSave = async () => {
|
const handleSave = async () => {
|
||||||
await setMutation.mutateAsync(url.trim());
|
await setMutation.mutateAsync(fullUrl);
|
||||||
setDirty(false);
|
setDirty(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleTest = async () => {
|
const handleTest = async () => {
|
||||||
if (url.trim()) {
|
if (fullUrl) {
|
||||||
await testMutation.mutateAsync(url.trim());
|
await testMutation.mutateAsync(fullUrl);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -53,11 +115,21 @@ export function GlobalProxySettings() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSelect = (proxyUrl: string) => {
|
const handleSelect = (proxyUrl: string) => {
|
||||||
setUrl(proxyUrl);
|
const { baseUrl, username: u, password: p } = extractAuth(proxyUrl);
|
||||||
|
setUrl(baseUrl);
|
||||||
|
setUsername(u);
|
||||||
|
setPassword(p);
|
||||||
setDirty(true);
|
setDirty(true);
|
||||||
setDetected([]);
|
setDetected([]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleClear = () => {
|
||||||
|
setUrl("");
|
||||||
|
setUsername("");
|
||||||
|
setPassword("");
|
||||||
|
setDirty(true);
|
||||||
|
};
|
||||||
|
|
||||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||||
if (e.key === "Enter" && dirty && !setMutation.isPending) {
|
if (e.key === "Enter" && dirty && !setMutation.isPending) {
|
||||||
handleSave();
|
handleSave();
|
||||||
@@ -80,7 +152,7 @@ export function GlobalProxySettings() {
|
|||||||
{t("settings.globalProxy.hint")}
|
{t("settings.globalProxy.hint")}
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{/* 输入框和按钮 */}
|
{/* 代理地址输入框和按钮 */}
|
||||||
<div className="flex gap-2">
|
<div className="flex gap-2">
|
||||||
<Input
|
<Input
|
||||||
placeholder="http://127.0.0.1:7890 / socks5://127.0.0.1:1080"
|
placeholder="http://127.0.0.1:7890 / socks5://127.0.0.1:1080"
|
||||||
@@ -108,7 +180,7 @@ export function GlobalProxySettings() {
|
|||||||
<Button
|
<Button
|
||||||
variant="outline"
|
variant="outline"
|
||||||
size="icon"
|
size="icon"
|
||||||
disabled={!url.trim() || testMutation.isPending}
|
disabled={!fullUrl || testMutation.isPending}
|
||||||
onClick={handleTest}
|
onClick={handleTest}
|
||||||
title={t("settings.globalProxy.test")}
|
title={t("settings.globalProxy.test")}
|
||||||
>
|
>
|
||||||
@@ -118,6 +190,15 @@ export function GlobalProxySettings() {
|
|||||||
<TestTube2 className="h-4 w-4" />
|
<TestTube2 className="h-4 w-4" />
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="icon"
|
||||||
|
disabled={!url && !username && !password}
|
||||||
|
onClick={handleClear}
|
||||||
|
title={t("settings.globalProxy.clear")}
|
||||||
|
>
|
||||||
|
<X className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={handleSave}
|
onClick={handleSave}
|
||||||
disabled={!dirty || setMutation.isPending}
|
disabled={!dirty || setMutation.isPending}
|
||||||
@@ -130,6 +211,47 @@ export function GlobalProxySettings() {
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* 认证信息:用户名 + 密码(可选) */}
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<Input
|
||||||
|
placeholder={t("settings.globalProxy.username")}
|
||||||
|
value={username}
|
||||||
|
onChange={(e) => {
|
||||||
|
setUsername(e.target.value);
|
||||||
|
setDirty(true);
|
||||||
|
}}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
className="font-mono text-sm flex-1"
|
||||||
|
/>
|
||||||
|
<div className="relative flex-1">
|
||||||
|
<Input
|
||||||
|
type={showPassword ? "text" : "password"}
|
||||||
|
placeholder={t("settings.globalProxy.password")}
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => {
|
||||||
|
setPassword(e.target.value);
|
||||||
|
setDirty(true);
|
||||||
|
}}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
className="font-mono text-sm pr-10"
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
className="absolute right-0 top-0 h-full px-3 hover:bg-transparent"
|
||||||
|
onClick={() => setShowPassword(!showPassword)}
|
||||||
|
tabIndex={-1}
|
||||||
|
>
|
||||||
|
{showPassword ? (
|
||||||
|
<EyeOff className="h-4 w-4 text-muted-foreground" />
|
||||||
|
) : (
|
||||||
|
<Eye className="h-4 w-4 text-muted-foreground" />
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{/* 扫描结果 */}
|
{/* 扫描结果 */}
|
||||||
{detected.length > 0 && (
|
{detected.length > 0 && (
|
||||||
<div className="flex flex-wrap gap-2">
|
<div className="flex flex-wrap gap-2">
|
||||||
|
|||||||
@@ -278,9 +278,12 @@
|
|||||||
"saving": "Saving...",
|
"saving": "Saving...",
|
||||||
"globalProxy": {
|
"globalProxy": {
|
||||||
"label": "Global Proxy",
|
"label": "Global Proxy",
|
||||||
"hint": "Proxy all requests (API, Skills download, etc.). Leave empty for direct connection. Supports HTTP and SOCKS5.",
|
"hint": "Proxy all requests (API, Skills download, etc.). Leave empty for direct connection.",
|
||||||
|
"username": "Username (optional)",
|
||||||
|
"password": "Password (optional)",
|
||||||
"test": "Test Connection",
|
"test": "Test Connection",
|
||||||
"scan": "Scan Local Proxies",
|
"scan": "Scan Local Proxies",
|
||||||
|
"clear": "Clear",
|
||||||
"scanFailed": "Scan failed: {{error}}",
|
"scanFailed": "Scan failed: {{error}}",
|
||||||
"saved": "Proxy settings saved",
|
"saved": "Proxy settings saved",
|
||||||
"saveFailed": "Save failed: {{error}}",
|
"saveFailed": "Save failed: {{error}}",
|
||||||
|
|||||||
@@ -278,9 +278,12 @@
|
|||||||
"saving": "保存中...",
|
"saving": "保存中...",
|
||||||
"globalProxy": {
|
"globalProxy": {
|
||||||
"label": "グローバルプロキシ",
|
"label": "グローバルプロキシ",
|
||||||
"hint": "すべてのリクエスト(API、Skills ダウンロードなど)をプロキシ経由で送信します。空欄で直接接続。HTTP と SOCKS5 をサポート。",
|
"hint": "すべてのリクエスト(API、Skills ダウンロードなど)をプロキシ経由で送信します。空欄で直接接続。",
|
||||||
|
"username": "ユーザー名(任意)",
|
||||||
|
"password": "パスワード(任意)",
|
||||||
"test": "接続テスト",
|
"test": "接続テスト",
|
||||||
"scan": "ローカルプロキシをスキャン",
|
"scan": "ローカルプロキシをスキャン",
|
||||||
|
"clear": "クリア",
|
||||||
"scanFailed": "スキャンに失敗しました: {{error}}",
|
"scanFailed": "スキャンに失敗しました: {{error}}",
|
||||||
"saved": "プロキシ設定を保存しました",
|
"saved": "プロキシ設定を保存しました",
|
||||||
"saveFailed": "保存に失敗しました: {{error}}",
|
"saveFailed": "保存に失敗しました: {{error}}",
|
||||||
|
|||||||
@@ -278,9 +278,12 @@
|
|||||||
"saving": "正在保存...",
|
"saving": "正在保存...",
|
||||||
"globalProxy": {
|
"globalProxy": {
|
||||||
"label": "全局代理",
|
"label": "全局代理",
|
||||||
"hint": "代理所有请求(API、Skills 下载等)。留空表示直连。支持 HTTP 和 SOCKS5 代理。",
|
"hint": "代理所有请求(API、Skills 下载等)。留空表示直连。",
|
||||||
|
"username": "用户名(可选)",
|
||||||
|
"password": "密码(可选)",
|
||||||
"test": "测试连接",
|
"test": "测试连接",
|
||||||
"scan": "扫描本地代理",
|
"scan": "扫描本地代理",
|
||||||
|
"clear": "清除",
|
||||||
"scanFailed": "扫描失败:{{error}}",
|
"scanFailed": "扫描失败:{{error}}",
|
||||||
"saved": "代理设置已保存",
|
"saved": "代理设置已保存",
|
||||||
"saveFailed": "保存失败:{{error}}",
|
"saveFailed": "保存失败:{{error}}",
|
||||||
|
|||||||
Reference in New Issue
Block a user