mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-07-14 00:26:06 +00:00
fix(inbound): re-derive auto tags on edit and keep node tags consistent
Auto-generated inbound tags (in-<port>-<l4>, n<id>- prefixed for node inbounds) now re-derive when port/listen/transport change on update instead of keeping the stale round-tripped value. The resolved tag is mirrored onto the API response, and NodeID is pinned to the stored row so a node inbound never loses its n<id>- prefix on edit. The edit form recomputes the tag live via a Go-parity helper so the JSON preview matches what gets saved. Make node/central tag matching prefix-agnostic in all three places (traffic attribution, remote-id resolution, and the orphan sweep) so an n<id>- prefix present on only one side can no longer spawn duplicate inbounds or drop traffic on sync. Force LF on shell scripts via .gitattributes (CRLF broke the Docker build shebang when the repo is checked out on Windows) and add a .dockerignore to keep node_modules/.git out of the build context. Adds Go and frontend tests covering tag re-derivation, prefix-agnostic matching, and node-snapshot prefix mismatch.
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
// Client-side mirror of the backend inbound-tag derivation
|
||||
// (web/service/port_conflict.go). Keep in sync; inbound-tag.test.ts guards parity.
|
||||
|
||||
type TransportBits = number;
|
||||
const TCP: TransportBits = 1;
|
||||
const UDP: TransportBits = 2;
|
||||
|
||||
function asString(v: unknown): string {
|
||||
return typeof v === 'string' ? v : '';
|
||||
}
|
||||
|
||||
function inboundTransports(
|
||||
protocol: string,
|
||||
streamSettings: Record<string, unknown> | undefined,
|
||||
settings: Record<string, unknown> | undefined,
|
||||
): TransportBits {
|
||||
if (protocol === 'hysteria' || protocol === 'wireguard') return UDP;
|
||||
|
||||
let bits: TransportBits = 0;
|
||||
const network = asString(streamSettings?.network);
|
||||
if (network === 'kcp' || network === 'quic') bits |= UDP;
|
||||
else bits |= TCP;
|
||||
|
||||
if (settings) {
|
||||
if (protocol === 'shadowsocks' || protocol === 'tunnel') {
|
||||
const key = protocol === 'tunnel' ? 'allowedNetwork' : 'network';
|
||||
const n = asString(settings[key]);
|
||||
if (n !== '') {
|
||||
bits = 0;
|
||||
for (const part of n.split(',')) {
|
||||
const p = part.trim();
|
||||
if (p === 'tcp') bits |= TCP;
|
||||
else if (p === 'udp') bits |= UDP;
|
||||
}
|
||||
}
|
||||
} else if (protocol === 'mixed') {
|
||||
if (settings.udp === true) bits |= UDP;
|
||||
}
|
||||
}
|
||||
|
||||
if (bits === 0) bits = TCP;
|
||||
return bits;
|
||||
}
|
||||
|
||||
function transportTagSuffix(bits: TransportBits): string {
|
||||
if (bits === TCP) return 'tcp';
|
||||
if (bits === UDP) return 'udp';
|
||||
if (bits === (TCP | UDP)) return 'tcpudp';
|
||||
return 'any';
|
||||
}
|
||||
|
||||
function isAnyListen(listen: string): boolean {
|
||||
return listen === '' || listen === '0.0.0.0' || listen === '::' || listen === '::0';
|
||||
}
|
||||
|
||||
function baseInboundTag(listen: string, port: number): string {
|
||||
return isAnyListen(listen) ? `in-${port}` : `in-${listen}:${port}`;
|
||||
}
|
||||
|
||||
function nodeTagPrefix(nodeId: number | null | undefined): string {
|
||||
return nodeId == null ? '' : `n${nodeId}-`;
|
||||
}
|
||||
|
||||
export interface InboundTagInput {
|
||||
listen: string;
|
||||
port: number;
|
||||
nodeId: number | null | undefined;
|
||||
protocol: string;
|
||||
streamSettings?: Record<string, unknown>;
|
||||
settings?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function composeInboundTag(input: InboundTagInput): string {
|
||||
const bits = inboundTransports(input.protocol, input.streamSettings, input.settings);
|
||||
return (
|
||||
nodeTagPrefix(input.nodeId)
|
||||
+ baseInboundTag(input.listen ?? '', input.port ?? 0)
|
||||
+ '-'
|
||||
+ transportTagSuffix(bits)
|
||||
);
|
||||
}
|
||||
|
||||
export function isAutoInboundTag(tag: string, input: InboundTagInput): boolean {
|
||||
if (tag === '') return true;
|
||||
const base = composeInboundTag(input);
|
||||
if (tag === base) return true;
|
||||
const prefix = `${base}-`;
|
||||
if (!tag.startsWith(prefix)) return false;
|
||||
const suffix = tag.slice(prefix.length);
|
||||
return suffix !== '' && /^[0-9]+$/.test(suffix);
|
||||
}
|
||||
Reference in New Issue
Block a user