fix(profile): verify saved proxy exists before activation

- check selector-like groups’ all list before applying saved nodes
- warn and fall back when a stored proxy disappeared from the group
- keep existing auto-switch flow for matching nodes to avoid regressions
This commit is contained in:
Slinetrac
2025-10-16 19:27:30 +08:00
parent fd5bddeb80
commit 67d254236d

View File

@@ -108,26 +108,64 @@ export const useProfiles = () => {
let hasChange = false; let hasChange = false;
const newSelected: typeof selected = []; const newSelected: typeof selected = [];
const { global, groups } = proxiesData; const { global, groups } = proxiesData;
const selectableTypes = new Set([
"Selector",
"URLTest",
"Fallback",
"LoadBalance",
]);
// 处理所有代理组 // 处理所有代理组
[global, ...groups].forEach(({ type, name, now }) => { [global, ...groups].forEach((group) => {
if (!now || type !== "Selector") { if (!group) {
if (selectedMap[name] != null) { return;
newSelected.push({ name, now: now || selectedMap[name] }); }
const { type, name, now } = group;
const savedProxy = selectedMap[name];
const availableProxies = Array.isArray(group.all) ? group.all : [];
if (!selectableTypes.has(type)) {
if (savedProxy != null || now != null) {
const preferredProxy = now ? now : savedProxy;
newSelected.push({ name, now: preferredProxy });
} }
return; return;
} }
const targetProxy = selectedMap[name]; if (savedProxy == null) {
if (targetProxy != null && targetProxy !== now) { if (now != null) {
console.log( newSelected.push({ name, now });
`[ActivateSelected] 需要切换代理组 ${name}: ${now} -> ${targetProxy}`, }
); return;
hasChange = true;
selectNodeForGroup(name, targetProxy);
} }
newSelected.push({ name, now: targetProxy || now }); const existsInGroup = availableProxies.some((proxy) => {
if (typeof proxy === "string") {
return proxy === savedProxy;
}
return proxy?.name === savedProxy;
});
if (!existsInGroup) {
console.warn(
`[ActivateSelected] 保存的代理 ${savedProxy} 不存在于代理组 ${name}`,
);
hasChange = true;
newSelected.push({ name, now: now ?? savedProxy });
return;
}
if (savedProxy !== now) {
console.log(
`[ActivateSelected] 需要切换代理组 ${name}: ${now} -> ${savedProxy}`,
);
hasChange = true;
selectNodeForGroup(name, savedProxy);
}
newSelected.push({ name, now: savedProxy });
}); });
if (!hasChange) { if (!hasChange) {