Files
3x-ui/frontend/src/pages/xray/basics/helpers.ts
T
BlindMaster24 a5a4c9cd83 fix(panel): read an outbound protocol id the way the core does (#6522)
* fix(panel): read an outbound protocol id the way the core does

xray-core lowercases a protocol id before it resolves the handler, so a
template that spells the direct outbound "Freedom" is that outbound. The
outbound editor fell through to the vless default and rendered it as an
empty vless server, and the Basics tab did not find it and appended a
second "direct", which the core refuses to load with "existing tag found".

* fix(panel): never leave the direct tag on two outbounds

A "direct" tag held by a non-freedom egress made both Basics-tab setters
push a fresh freedom outbound, and the core refuses a config whose tags
repeat ("existing tag found: direct"). The tag is now checked on its own
before anything is added, matching setDefaultOutboundTag.

* fix(panel): disable the freedom controls when direct is held elsewhere

When a non-freedom outbound holds the "direct" tag, both Basics setters
drop the edit so the core never sees the tag twice, but the Freedom
Strategy select and the Happy Eyeballs switch stayed enabled and snapped
back with no sign of why. isDirectTagTaken now disables both controls in
that state.

The find-or-create-plus-guard was also copied into BasicsTab's happy
eyeballs setter with no test of its own; ensureDirectFreedomOutbound now
owns the lookup, the guard and the creation for both setters, so the
existing helper tests cover that path too.

---------

Co-authored-by: Sanaei <ho3ein.sanaei@gmail.com>
2026-09-14 15:59:34 +02:00

182 lines
6.4 KiB
TypeScript

import type { XraySettingsValue } from '@/hooks/useXraySetting';
import { freedomDomainStrategyFromWire } from '@/lib/xray/outbound-form-adapter';
import { blockedSettings, directSettings } from './constants';
// Freedom resolves through the socket layer, so the outbound root and its own
// settings only hold legacy aliases the core warns about (infra/conf/xray.go).
const LEGACY_FREEDOM_STRATEGY_KEYS = ['domainStrategy', 'targetStrategy'] as const;
type Outbound = Record<string, unknown>;
// The core lowercases a protocol id before it resolves the handler, so matching
// it exactly would append a second "direct" the core refuses to load.
export function isDirectFreedomOutbound(o: Outbound | undefined): boolean {
const protocol = o?.protocol;
return (
typeof protocol === 'string' && protocol.toLowerCase() === 'freedom' && o?.tag === 'direct'
);
}
function directFreedom(t: XraySettingsValue | null): Outbound | undefined {
return t?.outbounds?.find((o) => isDirectFreedomOutbound(o)) as Outbound | undefined;
}
export function directFreedomStrategy(t: XraySettingsValue | null): string {
const outbound = directFreedom(t);
if (!outbound) return 'AsIs';
return freedomDomainStrategyFromWire(outbound) || 'AsIs';
}
// The core refuses to load two outbounds sharing a tag, so a "direct" held by
// a non-freedom egress keeps it and the Basics controls have nothing to edit.
export function isDirectTagTaken(t: XraySettingsValue | null): boolean {
return !directFreedom(t) && !!t?.outbounds?.some((o) => o?.tag === 'direct');
}
export function ensureDirectFreedomOutbound(t: XraySettingsValue): Outbound | undefined {
if (!Array.isArray(t.outbounds)) t.outbounds = [];
const found = directFreedom(t);
if (found) return found;
if (isDirectTagTaken(t)) return undefined;
const created: Outbound = { protocol: 'freedom', tag: 'direct', settings: {} };
t.outbounds.push(created as never);
return created;
}
export function setDirectFreedomStrategy(t: XraySettingsValue, next: string): void {
const ob = ensureDirectFreedomOutbound(t);
if (!ob) return;
// Drop the legacy placements, or the loader keeps warning and the core keeps
// preferring the root key it resets over the sockopt value set here.
const settings = (ob.settings ?? {}) as Outbound;
for (const key of LEGACY_FREEDOM_STRATEGY_KEYS) delete settings[key];
ob.settings = settings;
const stream = (ob.streamSettings ?? {}) as Outbound;
const sockopt = (stream.sockopt ?? {}) as Outbound;
if (next === 'AsIs') delete sockopt.domainStrategy;
else sockopt.domainStrategy = next;
if (Object.keys(sockopt).length === 0) delete stream.sockopt;
else stream.sockopt = sockopt;
if (Object.keys(stream).length === 0) delete ob.streamSettings;
else ob.streamSettings = stream;
}
export function ruleGetter(
t: XraySettingsValue | null,
outboundTag: string,
property: string,
): string[] {
if (!t?.routing?.rules) return [];
const out: string[] = [];
for (const rule of t.routing.rules) {
if (
rule &&
Object.prototype.hasOwnProperty.call(rule, property) &&
Object.prototype.hasOwnProperty.call(rule, 'outboundTag') &&
rule.outboundTag === outboundTag
) {
const v = (rule as Record<string, unknown>)[property];
if (Array.isArray(v)) out.push(...(v as string[]));
}
}
return out;
}
export function ruleSetter(
t: XraySettingsValue,
outboundTag: string,
property: string,
data: string[],
): void {
if (!t.routing) return;
if (!Array.isArray(t.routing.rules)) t.routing.rules = [];
const current = ruleGetter(t, outboundTag, property);
if (current.length === 0) {
t.routing.rules.push({ type: 'field', outboundTag, [property]: data });
return;
}
const next: typeof t.routing.rules = [];
let inserted = false;
for (const rule of t.routing.rules) {
const matches =
rule &&
Object.prototype.hasOwnProperty.call(rule, property) &&
Object.prototype.hasOwnProperty.call(rule, 'outboundTag') &&
rule.outboundTag === outboundTag;
if (matches) {
if (!inserted && data.length > 0) {
(rule as Record<string, unknown>)[property] = data;
next.push(rule);
inserted = true;
}
} else {
next.push(rule);
}
}
t.routing.rules = next;
}
export function syncOutbound(t: XraySettingsValue, tag: string, settings: Record<string, unknown>) {
if (!t.outbounds || !t.routing) return;
const rules = t.routing.rules || [];
const haveRules = rules.some((r) => r?.outboundTag === tag);
const idx = t.outbounds.findIndex((o) => o?.tag === tag);
if (!haveRules && idx > 0) t.outbounds.splice(idx, 1);
if (haveRules && idx < 0) t.outbounds.push(settings as never);
}
export function getDefaultOutboundTag(t: XraySettingsValue | null): string {
const tag = t?.outbounds?.[0]?.tag;
return typeof tag === 'string' && tag.length > 0 ? tag : 'direct';
}
export function setDefaultOutboundTag(t: XraySettingsValue, tag: string): void {
if (!tag) return;
if (!Array.isArray(t.outbounds)) t.outbounds = [];
const idx = t.outbounds.findIndex((o) => o?.tag === tag);
if (idx < 0) {
if (tag === 'direct') t.outbounds.push(directSettings as never);
else if (tag === 'blocked') t.outbounds.push(blockedSettings as never);
else return;
const newIdx = t.outbounds.length - 1;
const [moved] = t.outbounds.splice(newIdx, 1);
t.outbounds.unshift(moved);
} else if (idx > 0) {
const [moved] = t.outbounds.splice(idx, 1);
t.outbounds.unshift(moved);
}
}
export function propagateOutboundTagRename(
t: XraySettingsValue,
oldTag: string,
newTag: string,
): void {
if (!oldTag || !newTag || oldTag === newTag) return;
const rules = t.routing?.rules;
if (Array.isArray(rules)) {
for (const rule of rules) {
if (rule?.outboundTag === oldTag) rule.outboundTag = newTag;
}
}
const balancers = t.routing?.balancers;
if (Array.isArray(balancers)) {
for (const balancer of balancers) {
if (balancer?.fallbackTag === oldTag) balancer.fallbackTag = newTag;
if (Array.isArray(balancer?.selector)) {
balancer.selector = balancer.selector.map((sel) => (sel === oldTag ? newTag : sel));
}
}
}
if (Array.isArray(t.outbounds)) {
for (const outbound of t.outbounds) {
const sockopt = (outbound as { streamSettings?: { sockopt?: { dialerProxy?: string } } })
?.streamSettings?.sockopt;
if (sockopt?.dialerProxy === oldTag) sockopt.dialerProxy = newTag;
}
}
}