refactor: external-controller-cors to always show (#4243)

* refactor: external-controller-cors to always show

* i18n: update translation

* Update UPDATELOG.md
This commit is contained in:
Dyna
2025-07-28 11:24:16 +08:00
committed by GitHub
parent f756b37f97
commit 6a6bde3764
4 changed files with 26 additions and 47 deletions

View File

@@ -43,6 +43,7 @@
- 修复 `Windows` 安装器参数使用错误问题
- 修复 `IPC` 迁移后节点测速功能异常
- 修复 `IPC` 迁移后连接上下行速率计算功能异常
- 修复 `External-Controller-Cors` 无法保存所需前置条件
### 🔧 技术改进

View File

@@ -25,29 +25,19 @@ const DEV_URLS = [
"http://localhost:3000",
];
// 判断是否处于开发模式
const isDevMode = import.meta.env.MODE === "development";
// 过滤开发环境URL
const filterDevOrigins = (origins: string[]) => {
if (isDevMode) {
return origins;
}
return origins.filter((origin: string) => !DEV_URLS.includes(origin.trim()));
};
// 获取完整的源列表包括开发URL
const getFullOrigins = (origins: string[]) => {
if (!isDevMode) {
return origins;
}
// 合并现有源和开发URL并去重
const allOrigins = [...origins, ...DEV_URLS];
const uniqueOrigins = [...new Set(allOrigins)];
return uniqueOrigins;
};
// 过滤基础URL(确保后续添加)
const filterBaseOriginsForUI = (origins: string[]) => {
return origins.filter((origin: string) => !DEV_URLS.includes(origin.trim()));
};
// 统一使用的按钮样式
const buttonStyle = {
borderRadius: "8px",
@@ -63,16 +53,6 @@ const buttonStyle = {
},
};
// 保存按钮样式
const saveButtonStyle = {
...buttonStyle,
backgroundColor: "#165DFF",
color: "white",
"&:hover": {
backgroundColor: "#0E42D2",
},
};
// 添加按钮样式
const addButtonStyle = {
...buttonStyle,
@@ -110,10 +90,10 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
allowOrigins: string[];
}>(() => {
const cors = clash?.["external-controller-cors"];
const origins = cors?.["allow-origins"] ?? ["*"];
const origins = cors?.["allow-origins"] ?? [];
return {
allowPrivateNetwork: cors?.["allow-private-network"] ?? true,
allowOrigins: filterDevOrigins(origins),
allowOrigins: filterBaseOriginsForUI(origins),
};
});
@@ -178,10 +158,10 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
useImperativeHandle(ref, () => ({
open: () => {
const cors = clash?.["external-controller-cors"];
const origins = cors?.["allow-origins"] ?? ["*"];
const origins = cors?.["allow-origins"] ?? [];
setCorsConfig({
allowPrivateNetwork: cors?.["allow-private-network"] ?? true,
allowOrigins: filterDevOrigins(origins),
allowOrigins: filterBaseOriginsForUI(origins),
});
setOpen(true);
},
@@ -257,7 +237,7 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
color="error"
size="small"
onClick={() => handleDeleteOrigin(index)}
disabled={corsConfig.allowOrigins.length <= 1}
disabled={corsConfig.allowOrigins.length <= 0}
sx={deleteButtonStyle}
>
<DeleteIcon fontSize="small" />
@@ -273,24 +253,22 @@ export const HeaderConfiguration = forwardRef<ClashHeaderConfigingRef>(
{t("Add")}
</Button>
{isDevMode && (
<div
style={{
marginTop: 12,
padding: 8,
backgroundColor: "#f5f5f5",
borderRadius: 4,
}}
>
<div
style={{
marginTop: 12,
padding: 8,
backgroundColor: "#f5f5f5",
borderRadius: 4,
}}
style={{ color: "#666", fontSize: 12, fontStyle: "italic" }}
>
<div
style={{ color: "#666", fontSize: 12, fontStyle: "italic" }}
>
{t(
"Development mode: Automatically includes Tauri and localhost origins",
)}
</div>
{t("Always included origins: {{urls}}", {
urls: DEV_URLS.join(", "),
})}
</div>
)}
</div>
</div>
</ListItem>
</List>

View File

@@ -652,7 +652,7 @@
"Allowed Origins": "Allowed Origins",
"Please enter a valid url": "Please enter a valid url",
"Add": "Add",
"Development mode: Automatically includes Tauri and localhost origins": "Development mode: Automatically includes Tauri and localhost origins",
"Always included origins: {{urls}}": "Always included origins: {{urls}}",
"Invalid regular expression": "Invalid regular expression",
"Copy Version": "Copy Version",
"Version copied to clipboard": "Version copied to clipboard",

View File

@@ -652,7 +652,7 @@
"Allowed Origins": "允许的来源",
"Please enter a valid url": "请输入有效的网址",
"Add": "添加",
"Development mode: Automatically includes Tauri and localhost origins": "开发模式:自动包含 Tauri 和 localhost 来源",
"Always included origins: {{urls}}": "始终包含来源:{{urls}}",
"Invalid regular expression": "无效的正则表达式",
"Copy Version": "复制Verge版本号",
"Version copied to clipboard": "Verge版本已复制到剪贴板",