legacy 入站可升级为命名入站
release / release (push) Successful in 26s

顶层端口字段没有 proxy 可写(listener/inbound/base.go:97),界面上
切不了订阅。升级删掉顶层键、建同端口的 listener:listen 按 allow-lan
推导后写死,全局规则里 MATCH 之前的部分复制成附加规则,来源照抄
MATCH 目标组的 use:,让流量去向尽量不变。udp 不写,命名入站解析时
默认就是开的(listener/parse.go:52)。
「默认」tag 随术语表改成 legacy,旧叫法进 avoid 列表。
This commit is contained in:
2026-08-12 17:30:47 +08:00
parent d6942c5702
commit b1b251b897
10 changed files with 193 additions and 20 deletions
+51 -1
View File
@@ -18,7 +18,7 @@
* 副本的来源 path 和哪个用户订阅相同(读同一个缓存文件)
*/
import type { ConfigObject } from '../vendor/config-editor/index.ts'
import { resolveDestination } from './inbounds.ts'
import { fallbackTarget, legacyListenAddress, resolveDestination } from './inbounds.ts'
import type { Inbound } from './inbounds.ts'
import { lastSwitchableGroup, resolveChain } from './outbound-chain.ts'
import type { RuntimeNode } from './outbound-chain.ts'
@@ -433,6 +433,56 @@ export function applySetup(data: ConfigObject, inbound: string, setup: OutboundS
return next
}
/**
* 把一路 legacy 入站升级成命名入站,并直接进专属组模式。
*
* legacy 入站(顶层端口字段)没有 `proxy` 可写,只能顺全局规则走,在界面上
* 切不了订阅。升级 = 删掉顶层端口键,在 `listeners:` 里建一个行为等价的入站:
* 端口 原样
* listen 按 allow-lan 推导后写死。升级后改 allow-lan 不再影响它,这是升级的
* 语义变化之一,界面文案要说
* udp 不写。命名入站解析时默认就是开的(内核源码 listener/parse.go:52)
*
* 行为等价还差分流:它原来查全局规则,升级后整表改查自己的附加规则,所以把
* 全局规则里第一条 MATCH 之前的部分复制过来(MATCH 之后的行本来就永远轮不到)。
* 全局规则原文不动——那是用户手写的段落,何况可能还有别的 legacy 入站在用。
* 专属组的来源照抄原 MATCH 目标组的 `use:`,流量的实际去向尽量不变;
* 目标组没有 `use:` 时退回全部订阅。
*
* 同名入站已存在时返回 null,调用方据此报错而不是写出重名的 listener。
*/
export function upgradeLegacyInbound(data: ConfigObject, inbound: Inbound): ConfigObject | null {
if (inbound.legacyKey === null) return null
const port = Number(inbound.port)
if (!Number.isFinite(port) || port <= 0) return null
const listeners = Array.isArray(data.listeners) ? data.listeners : []
if (listeners.some((entry) => isPlainObject(entry) && entry.name === inbound.name)) return null
const next = { ...data }
delete next[inbound.legacyKey]
next.listeners = [
...listeners,
{ name: inbound.name, type: inbound.type, port, listen: legacyListenAddress(data) },
]
const rules: string[] = []
for (const rule of Array.isArray(data.rules) ? data.rules : []) {
if (typeof rule !== 'string') continue
if (rule.split(',')[0]?.trim().toUpperCase() === 'MATCH') break
rules.push(rule)
}
const target = fallbackTarget(data)
const providers = new Set(userProviders(data))
const uses = (target ? (groupUses(data, target) ?? []) : []).filter((name) => providers.has(name))
const sources = uses.length > 0 ? uses : userProviders(data)
return applySetup(applySubRules(next, inbound.name, rules), inbound.name, {
proxy: { sources, filter: null },
chain: null,
})
}
/** 配置里指向某个名字的地方有几处。用来判断生成的东西还有没有人在用。 */
function referencesTo(data: ConfigObject, name: string): number {
let count = 0