每路入站配一组专属策略组,换线不再改配置文件
原本换一条线要改 config.yaml、跑校验、等内核重载,几秒起步;而换线恰恰是这个工具 最高频的动作,把它放在最重的路径上方向就错了。现在每一路配一个 select 组加 AUTO、 FALLBACK 两个,入站的 proxy 永久指向 select,换线只是调一次内核接口。这样做的前提是 选中不会丢:内核把它写进 cache 文件,重启后由 patchSelectGroup 恢复,而 profile.store-selected 默认为真。 链式代理靠给来源订阅复制一份声明、挂上 override.dialer-proxy 实现,因为这个字段写在 策略组上内核只会打条日志然后忽略。副本是 type: file 读原订阅已经落盘的缓存,而不是 自己再拉一次:自己拉的话订阅地址一不可达副本就是空的,那一路入站会一条线都不剩, 表现是端口还在监听却没有任何出口。 顺带把 shared/outbound-chain.ts 里「只有 Selector 接受手动选择」那条断言删了,它是 错的(内核检查的是 SelectAble 接口,URLTest 和 Fallback 也实现了)。界面仍然只让 Selector 可点,但理由换成了另外两种的手动指定会被它们自己撤销。 完整取舍见工作区决策记录 0007。
This commit is contained in:
@@ -0,0 +1,501 @@
|
||||
/**
|
||||
* 每一路入站的专属策略组。
|
||||
*
|
||||
* 一路入站配三个组,入站的 `proxy:` 指向其中的 SELECT,换线是换那个组的当前选中,
|
||||
* 不改配置文件(决策记录 0007):
|
||||
* SELECT 手动选。它的成员是 DIRECT、下面两个组,加上订阅里的线路
|
||||
* AUTO url-test,自动挑延迟最低的
|
||||
* FALLBACK fallback,按顺序挑第一个测得通的
|
||||
* 界面上这三个是并排的 tab。做成三个而不是让用户自己配,是因为它们就是所有 clash 配置
|
||||
* 里那三种固定的挑法,让人从零拼一遍没有意义。
|
||||
*
|
||||
* 开了链式代理时再多一个入口组,以及若干份订阅声明的副本——`dialer-proxy` 写不到策略组
|
||||
* 上,只能挂在整个订阅上(内核源码 adapter/outboundgroup/parser.go:68-70)。
|
||||
*
|
||||
* 配置里的东西属于谁全靠三条线索还原,不靠解析名字:
|
||||
* 四个组 名字由入站名推出来
|
||||
* 订阅副本 override.dialer-proxy 指向哪一路的入口组
|
||||
* 副本的来源 url 和哪个用户订阅相同
|
||||
*/
|
||||
import type { ConfigObject } from '../vendor/config-editor/index.ts'
|
||||
import { resolveDestination } from './inbounds.ts'
|
||||
import type { Inbound } from './inbounds.ts'
|
||||
import { lastSwitchableGroup, resolveChain } from './outbound-chain.ts'
|
||||
import type { RuntimeNode } from './outbound-chain.ts'
|
||||
|
||||
/**
|
||||
* Patchbay 自动创建的策略组和订阅的名字前缀。
|
||||
*
|
||||
* 名字是识别它们的唯一可靠标记。注释靠不住——配置改写引擎重建整个被碰到的顶层段落,
|
||||
* 段落内的注释会丢(决策记录 0002)。改这个前缀会让已有配置里的组失去标记,别改。
|
||||
*/
|
||||
export const GENERATED_PREFIX = 'patchbay:'
|
||||
|
||||
export function isGenerated(name: string): boolean {
|
||||
return name.startsWith(GENERATED_PREFIX)
|
||||
}
|
||||
|
||||
/** 手动选的那个组。入站的 `proxy:` 指向它,它的当前选中决定这一路走哪。 */
|
||||
export function proxyGroupName(inbound: string): string {
|
||||
return `${GENERATED_PREFIX}${inbound}`
|
||||
}
|
||||
|
||||
/** 自动测速组,作为 SELECT 的一个成员出现。 */
|
||||
export function autoGroupName(inbound: string): string {
|
||||
return `${GENERATED_PREFIX}${inbound} AUTO`
|
||||
}
|
||||
|
||||
/** 故障转移组,同上。 */
|
||||
export function fallbackGroupName(inbound: string): string {
|
||||
return `${GENERATED_PREFIX}${inbound} FALLBACK`
|
||||
}
|
||||
|
||||
/** 链式代理的入口组。订阅副本的 `dialer-proxy` 指向它。 */
|
||||
export function chainGroupName(inbound: string): string {
|
||||
return `${GENERATED_PREFIX}${inbound} 链式`
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个出站在界面上叫什么。
|
||||
*
|
||||
* 生成的组要去掉 `patchbay:` 前缀和入站名。用户从没建过这些组,让他在卡片和链条上读到
|
||||
* `patchbay:test1 FALLBACK` 只是噪音——那一屏本来就是这一路入站的详情,名字里再重复
|
||||
* 一遍入站名不增加任何信息。
|
||||
*/
|
||||
export function displayName(name: string): string {
|
||||
if (!isGenerated(name)) return name
|
||||
const rest = name.slice(GENERATED_PREFIX.length)
|
||||
for (const [suffix, label] of [
|
||||
[' AUTO', 'AUTO'],
|
||||
[' FALLBACK', 'FALLBACK'],
|
||||
[' 链式', '链式代理'],
|
||||
] as const) {
|
||||
if (rest.endsWith(suffix)) return label
|
||||
}
|
||||
return rest
|
||||
}
|
||||
|
||||
/** 这一路的三个组,按界面上的显示顺序。 */
|
||||
function ownGroups(inbound: string): Array<{ name: string; label: string }> {
|
||||
return [
|
||||
{ name: proxyGroupName(inbound), label: 'SELECT' },
|
||||
{ name: autoGroupName(inbound), label: 'AUTO' },
|
||||
{ name: fallbackGroupName(inbound), label: 'FALLBACK' },
|
||||
]
|
||||
}
|
||||
|
||||
/** FNV-1a。只用来给名字和文件名去重,不是安全用途,所以不引哈希库,浏览器端也要能算。 */
|
||||
function shortHash(text: string): string {
|
||||
let h = 0x811c9dc5
|
||||
for (let i = 0; i < text.length; i += 1) {
|
||||
h ^= text.charCodeAt(i)
|
||||
h = Math.imul(h, 0x01000193) >>> 0
|
||||
}
|
||||
return h.toString(16).padStart(8, '0')
|
||||
}
|
||||
|
||||
const pairKey = (inbound: string, source: string) => `${inbound} ${source}`
|
||||
|
||||
/**
|
||||
* 「某一路入站 × 某个来源订阅」这个组合对应的订阅副本叫什么。
|
||||
*
|
||||
* 名字里带哈希是因为 `proxy-providers` 是个映射,两个组合撞名会让其中一个被静默丢掉,
|
||||
* 表现是链式代理挂到了别的入站上。带上入站名只是为了翻配置文件时读得懂。
|
||||
*/
|
||||
export function chainProviderName(inbound: string, source: string): string {
|
||||
return `${GENERATED_PREFIX}chain ${inbound} ${shortHash(pairKey(inbound, source))}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个订阅的缓存文件落在哪。
|
||||
*
|
||||
* 内核把拉回来的内容写在这里(内核源码 component/resource/vehicle.go:42-47)。没写
|
||||
* `path:` 时它按 url 哈希自己生成一个(adapter/provider/parser.go:87),那个路径 Patchbay
|
||||
* 算不出来,所以缺的时候补一个确定的进去。
|
||||
*/
|
||||
export function providerCachePath(name: string): string {
|
||||
const safe = name.replace(/[^\p{L}\p{N}_-]+/gu, '-').replace(/^-+|-+$/g, '')
|
||||
return `./providers/${safe || shortHash(name)}.yaml`
|
||||
}
|
||||
|
||||
/** 一组线路从哪来、要哪些。三个组共用同一份,它们只是挑法不同。 */
|
||||
export interface GroupSpec {
|
||||
/** 成员来自哪几个订阅,写进组的 `use:` */
|
||||
sources: string[]
|
||||
/** 只要名字匹配这个正则的线路。null 表示全要 */
|
||||
filter: string | null
|
||||
}
|
||||
|
||||
/** 一路入站的出站设置。 */
|
||||
export interface OutboundSetup {
|
||||
proxy: GroupSpec
|
||||
/** 链式代理的入口。null 表示没开 */
|
||||
chain: GroupSpec | null
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is ConfigObject {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
function groupsOf(data: ConfigObject): ConfigObject[] {
|
||||
return (Array.isArray(data['proxy-groups']) ? data['proxy-groups'] : []).filter(isPlainObject)
|
||||
}
|
||||
|
||||
function providersOf(data: ConfigObject): ConfigObject {
|
||||
return isPlainObject(data['proxy-providers']) ? data['proxy-providers'] : {}
|
||||
}
|
||||
|
||||
function findGroup(data: ConfigObject, name: string): ConfigObject | null {
|
||||
return groupsOf(data).find((g) => g.name === name) ?? null
|
||||
}
|
||||
|
||||
function stringList(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : []
|
||||
}
|
||||
|
||||
function specOf(group: ConfigObject): GroupSpec {
|
||||
return {
|
||||
sources: stringList(group.use),
|
||||
filter: typeof group.filter === 'string' && group.filter ? group.filter : null,
|
||||
}
|
||||
}
|
||||
|
||||
/** 用户自己配的订阅,不含 Patchbay 生成的副本。界面上「来源」能选的就是这些。 */
|
||||
export function userProviders(data: ConfigObject): string[] {
|
||||
return Object.keys(providersOf(data)).filter((name) => !isGenerated(name))
|
||||
}
|
||||
|
||||
/** 某个策略组从哪几个订阅取成员。组不存在时返回 null。 */
|
||||
export function groupUses(data: ConfigObject, name: string): string[] | null {
|
||||
const group = findGroup(data, name)
|
||||
return group ? stringList(group.use) : null
|
||||
}
|
||||
|
||||
/** 这个订阅副本是哪一路入站的。不是副本、或者认不出归属时返回 null。 */
|
||||
function chainOwnerOf(provider: unknown): string | null {
|
||||
if (!isPlainObject(provider)) return null
|
||||
const override = provider.override
|
||||
if (!isPlainObject(override)) return null
|
||||
const target = override['dialer-proxy']
|
||||
if (typeof target !== 'string' || !isGenerated(target)) return null
|
||||
const suffix = ' 链式'
|
||||
if (!target.endsWith(suffix)) return null
|
||||
return target.slice(GENERATED_PREFIX.length, target.length - suffix.length)
|
||||
}
|
||||
|
||||
/** 副本的来源订阅叫什么。靠读同一个缓存文件来认,因为名字不可靠解析。 */
|
||||
function sourceOfCopy(data: ConfigObject, copy: ConfigObject): string | null {
|
||||
if (typeof copy.path !== 'string') return null
|
||||
for (const [name, entry] of Object.entries(providersOf(data))) {
|
||||
if (isGenerated(name) || !isPlainObject(entry)) continue
|
||||
if (entry.path === copy.path) return name
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 读出这一路入站当前的出站设置。
|
||||
*
|
||||
* 返回 null 表示它还没在专属组模式下,可能是走规则表、直连,或者指向用户自己建的组。
|
||||
*/
|
||||
export function readSetup(data: ConfigObject, inbound: string): OutboundSetup | null {
|
||||
const proxyGroup = findGroup(data, proxyGroupName(inbound))
|
||||
if (!proxyGroup) return null
|
||||
|
||||
const spec = specOf(proxyGroup)
|
||||
const providers = providersOf(data)
|
||||
const copies = spec.sources.filter((name) => chainOwnerOf(providers[name]) === inbound)
|
||||
|
||||
if (copies.length === 0) return { proxy: spec, chain: null }
|
||||
|
||||
const chainGroup = findGroup(data, chainGroupName(inbound))
|
||||
const sources = copies
|
||||
.map((name) => sourceOfCopy(data, providers[name] as ConfigObject))
|
||||
.filter((name): name is string => name !== null)
|
||||
|
||||
return {
|
||||
proxy: { sources, filter: spec.filter },
|
||||
chain: chainGroup ? specOf(chainGroup) : { sources: [], filter: null },
|
||||
}
|
||||
}
|
||||
|
||||
function makeGroup(
|
||||
name: string,
|
||||
type: string,
|
||||
use: string[],
|
||||
filter: string | null,
|
||||
members: string[] = [],
|
||||
): ConfigObject {
|
||||
const group: ConfigObject = { name, type }
|
||||
if (members.length > 0) group.proxies = members
|
||||
if (use.length > 0) group.use = use
|
||||
if (filter) group.filter = filter
|
||||
// url-test 和 fallback 要有测速地址才知道谁快谁通。不写的话内核会借用来源订阅自己的
|
||||
// 健康检查地址(adapter/outboundgroup/parser.go:139-148),但那依赖订阅写了它,
|
||||
// 不能指望;写死一个和内核默认一致的更稳。
|
||||
if (type !== 'select') {
|
||||
group.url = 'https://www.gstatic.com/generate_204'
|
||||
group.interval = 300
|
||||
}
|
||||
return group
|
||||
}
|
||||
|
||||
function upsert(list: ConfigObject[], entry: ConfigObject): ConfigObject[] {
|
||||
const at = list.findIndex((item) => item.name === entry.name)
|
||||
if (at < 0) return [...list, entry]
|
||||
const next = [...list]
|
||||
next[at] = entry
|
||||
return next
|
||||
}
|
||||
|
||||
/**
|
||||
* 照着一个用户订阅做出带 `dialer-proxy` 的那一份。
|
||||
*
|
||||
* 副本是 `type: file`,读的正是原订阅那个缓存文件,自己不联网。这一点是硬性的:副本要是
|
||||
* 自己去拉,订阅地址一不可达它就是空的,而这一路入站的线路全部来自它,结果是端口还在
|
||||
* 监听却一条线都没有。派生自原订阅已经落盘的内容,原订阅有什么副本就有什么。
|
||||
*
|
||||
* 内核对 file 类型的 provider 自带文件监视(component/resource/fetcher.go:212-228),
|
||||
* 原订阅刷新写盘之后副本自动跟着更新,不需要 Patchbay 做任何事。
|
||||
*
|
||||
* `filter` 这些影响「这个订阅里有哪些线路」的字段照抄,`url` 和 `interval` 去掉,
|
||||
* 它们只对 http 类型有意义。
|
||||
*/
|
||||
function makeChainProvider(source: ConfigObject, inbound: string, cachePath: string): ConfigObject {
|
||||
const override = isPlainObject(source.override) ? { ...source.override } : {}
|
||||
override['dialer-proxy'] = chainGroupName(inbound)
|
||||
const { url: _url, interval: _interval, ...rest } = source
|
||||
return { ...rest, type: 'file', path: cachePath, override }
|
||||
}
|
||||
|
||||
/**
|
||||
* 把一路入站的出站设置写回配置。
|
||||
*
|
||||
* 开了链式代理时,三个组的成员都改从订阅副本里取,因为只有副本挂着 `dialer-proxy`。
|
||||
* 用户选的来源订阅仍然可以从副本的 `url` 还原出来,所以配置文件之外不需要另存状态。
|
||||
*/
|
||||
export function applySetup(data: ConfigObject, inbound: string, setup: OutboundSetup): ConfigObject {
|
||||
const next = { ...data }
|
||||
const chained = setup.chain !== null
|
||||
const providers = { ...providersOf(next) }
|
||||
|
||||
// 先清掉这一路原有的副本,再按新的来源重建。改来源时旧的副本必须消失,
|
||||
// 否则代理组不再引用它,它就成了一份没人用却还在定时拉取的订阅。
|
||||
for (const [name, entry] of Object.entries(providers)) {
|
||||
if (chainOwnerOf(entry) === inbound) delete providers[name]
|
||||
}
|
||||
|
||||
const use: string[] = []
|
||||
if (chained) {
|
||||
for (const sourceName of setup.proxy.sources) {
|
||||
const source = providers[sourceName]
|
||||
if (!isPlainObject(source)) continue
|
||||
// 副本靠「读同一个缓存文件」认亲。原订阅没写 path 时内核按 url 哈希自己生成一个,
|
||||
// 那个路径这边算不出来,所以先给原订阅补一个确定的。
|
||||
const cachePath =
|
||||
typeof source.path === 'string' && source.path ? source.path : providerCachePath(sourceName)
|
||||
providers[sourceName] = { ...source, path: cachePath }
|
||||
const copyName = chainProviderName(inbound, sourceName)
|
||||
providers[copyName] = makeChainProvider(providers[sourceName], inbound, cachePath)
|
||||
use.push(copyName)
|
||||
}
|
||||
} else {
|
||||
use.push(...setup.proxy.sources)
|
||||
}
|
||||
|
||||
const { filter } = setup.proxy
|
||||
let groups = groupsOf(next)
|
||||
groups = upsert(
|
||||
groups,
|
||||
makeGroup(proxyGroupName(inbound), 'select', use, filter, [
|
||||
'DIRECT',
|
||||
autoGroupName(inbound),
|
||||
fallbackGroupName(inbound),
|
||||
]),
|
||||
)
|
||||
groups = upsert(groups, makeGroup(autoGroupName(inbound), 'url-test', use, filter))
|
||||
groups = upsert(groups, makeGroup(fallbackGroupName(inbound), 'fallback', use, filter))
|
||||
|
||||
if (chained) {
|
||||
groups = upsert(
|
||||
groups,
|
||||
makeGroup(chainGroupName(inbound), 'select', setup.chain!.sources, setup.chain!.filter),
|
||||
)
|
||||
} else {
|
||||
groups = groups.filter((g) => g.name !== chainGroupName(inbound))
|
||||
}
|
||||
next['proxy-groups'] = groups
|
||||
|
||||
if (Object.keys(providers).length === 0) delete next['proxy-providers']
|
||||
else next['proxy-providers'] = providers
|
||||
|
||||
const listeners = (Array.isArray(next.listeners) ? next.listeners : []).map((entry) =>
|
||||
isPlainObject(entry) && entry.name === inbound
|
||||
? { ...entry, proxy: proxyGroupName(inbound) }
|
||||
: entry,
|
||||
)
|
||||
if (listeners.length > 0) next.listeners = listeners
|
||||
|
||||
return next
|
||||
}
|
||||
|
||||
/** 配置里指向某个名字的地方有几处。用来判断生成的东西还有没有人在用。 */
|
||||
function referencesTo(data: ConfigObject, name: string): number {
|
||||
let count = 0
|
||||
|
||||
for (const entry of Array.isArray(data.listeners) ? data.listeners : []) {
|
||||
if (isPlainObject(entry) && entry.proxy === name) count += 1
|
||||
}
|
||||
|
||||
for (const group of groupsOf(data)) {
|
||||
if (stringList(group.proxies).includes(name)) count += 1
|
||||
if (stringList(group.use).includes(name)) count += 1
|
||||
}
|
||||
|
||||
for (const entry of Object.values(providersOf(data))) {
|
||||
if (!isPlainObject(entry) || !isPlainObject(entry.override)) continue
|
||||
if (entry.override['dialer-proxy'] === name) count += 1
|
||||
}
|
||||
|
||||
for (const rule of Array.isArray(data.rules) ? data.rules : []) {
|
||||
// 规则是逗号分隔的,目标出站是其中一段,形如 MATCH,目标 或 DOMAIN,x,目标
|
||||
if (typeof rule === 'string' && rule.split(',').some((part) => part.trim() === name)) count += 1
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* 删掉没人再引用的生成组和订阅副本。
|
||||
*
|
||||
* 每次改完配置都跑一遍。删一路入站之后,它的几个组和几份副本会先后变成孤儿,不清理的话
|
||||
* 文件里就攒下一堆用户从没主动建过的东西,而副本还在按 interval 定时联网。
|
||||
*
|
||||
* 要反复扫:AUTO 和 FALLBACK 被 SELECT 引用,入口组被副本引用,副本被那三个组引用,
|
||||
* 所以删掉 SELECT 之后下一轮才轮到它们。只动带前缀的,用户自己建的一律不碰。
|
||||
*/
|
||||
export function pruneGenerated(data: ConfigObject): ConfigObject {
|
||||
const groups = Array.isArray(data['proxy-groups']) ? data['proxy-groups'] : []
|
||||
const keptGroups = groups.filter((group) => {
|
||||
if (!isPlainObject(group)) return true
|
||||
const name = group.name
|
||||
if (typeof name !== 'string' || !isGenerated(name)) return true
|
||||
return referencesTo(data, name) > 0
|
||||
})
|
||||
|
||||
const providers = providersOf(data)
|
||||
const keptProviders = Object.entries(providers).filter(
|
||||
([name]) => !isGenerated(name) || referencesTo(data, name) > 0,
|
||||
)
|
||||
const providersChanged = keptProviders.length !== Object.keys(providers).length
|
||||
|
||||
if (keptGroups.length === groups.length && !providersChanged) return data
|
||||
|
||||
const next = { ...data }
|
||||
if (keptGroups.length === 0) delete next['proxy-groups']
|
||||
else next['proxy-groups'] = keptGroups
|
||||
|
||||
if (providersChanged) {
|
||||
if (keptProviders.length === 0) delete next['proxy-providers']
|
||||
else next['proxy-providers'] = Object.fromEntries(keptProviders)
|
||||
}
|
||||
|
||||
return pruneGenerated(next)
|
||||
}
|
||||
|
||||
/**
|
||||
* 把所有已接管入站的生成组按当前的生成规则重建一遍。
|
||||
*
|
||||
* 每次写配置前跑。生成的东西必须永远和生成它的代码一致,否则改过生成规则之后,早先建的
|
||||
* 那些就停在旧形状上,界面按新形状去找,找不到就静默少一块——tab 少了两个正是这么来的。
|
||||
* `applySetup` 幂等,形状没变时返回的内容一样,落盘链路会看出内容没变而跳过重载。
|
||||
*/
|
||||
export function syncGenerated(data: ConfigObject, inbounds: Inbound[]): ConfigObject {
|
||||
let next = data
|
||||
for (const inbound of inbounds) {
|
||||
const setup = readSetup(next, inbound.name)
|
||||
const mode = inbound.outbound
|
||||
if (!setup || mode.kind !== 'named' || mode.name !== proxyGroupName(inbound.name)) continue
|
||||
next = applySetup(next, inbound.name, setup)
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
/** 面板上一个 tab 对应的策略组。 */
|
||||
export interface PanelGroup {
|
||||
name: string
|
||||
/** tab 上显示什么。自己的组显示挑法,共用的组显示它自己的名字 */
|
||||
label: string
|
||||
/** 它从哪几个订阅取成员。测延迟要顺着来源找,见 web/runtime.ts 的 membersOf */
|
||||
uses: string[]
|
||||
/** 能不能点成员换选中 */
|
||||
pickable: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* 这一路入站的面板上能操作哪些策略组。
|
||||
*
|
||||
* 只此一处回答这个问题。之前它散在四个地方(页面算一次能不能点、组件再算一次、
|
||||
* 共用与否又各算一次),加一种情况就要同时改四处,漏一处就是界面和实际操作对不上。
|
||||
*
|
||||
* 两种情况:接管过的入站是它自己那三个组;没接管的(比如 `mixed-port`,配置顶层的端口
|
||||
* 字段没有 `proxy` 可写)落到它顺着规则表走到的那个手动选择组上,那个组是共用的。
|
||||
*/
|
||||
export function panelGroups(
|
||||
data: ConfigObject,
|
||||
runtime: Record<string, RuntimeNode>,
|
||||
inbound: Inbound,
|
||||
): { groups: PanelGroup[]; shared: boolean } {
|
||||
const mode = inbound.outbound
|
||||
const managed = mode.kind === 'named' && mode.name === proxyGroupName(inbound.name)
|
||||
|
||||
if (managed) {
|
||||
return {
|
||||
shared: false,
|
||||
groups: ownGroups(inbound.name)
|
||||
// 保存完到内核重载完之间,组已经在配置里但运行时还没有,这时先不显示那个 tab
|
||||
.filter((g) => runtime[g.name])
|
||||
.map((g) => ({
|
||||
...g,
|
||||
uses: groupUses(data, g.name) ?? [],
|
||||
// 只有手动选的那个能点。另外两个自己决定用哪条,理由见 outbound-chain.ts
|
||||
pickable: runtime[g.name]?.type === 'Selector',
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
const chain = resolveChain(runtime, resolveDestination(data, inbound))
|
||||
const fallback = lastSwitchableGroup(runtime, chain)
|
||||
if (!fallback) return { groups: [], shared: false }
|
||||
return {
|
||||
shared: true,
|
||||
groups: [
|
||||
{
|
||||
name: fallback.group,
|
||||
label: fallback.group,
|
||||
uses: groupUses(data, fallback.group) ?? [],
|
||||
pickable: true,
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 筛选条件会留下哪些线路。界面上用来在保存前当场告诉用户「匹配到几条」。
|
||||
*
|
||||
* 照着内核的规则算(`adapter/outboundgroup/groupbase.go:72-76, 168-195`):反引号把
|
||||
* 条件切成多条独立正则,取并集;每条都是部分匹配,不自动加锚点。
|
||||
*
|
||||
* 内核用的是支持前后向查找的 .NET 风味引擎,浏览器只有 JS 的,所以复杂写法这里算出来的
|
||||
* 数字可能和内核不一致。写法有误时返回 null,界面据此提示而不是显示一个错的数字。
|
||||
*/
|
||||
export function linesMatching(filter: string | null, lines: string[]): string[] | null {
|
||||
if (!filter) return lines
|
||||
let regexps: RegExp[]
|
||||
try {
|
||||
regexps = filter.split('`').map((part) => new RegExp(part))
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
return lines.filter((line) => regexps.some((re) => re.test(line)))
|
||||
}
|
||||
Reference in New Issue
Block a user