feat: sing-box WireGuard/Tailscale 支持 _on_demand、_listen_port、_name、_udp_mapping、_udp_filtering、_udp_nat_max、_taildrop_directory 等字段

This commit is contained in:
xream committed 2026-09-22 12:24:21 +08:00
1 parent 79dff18b98
commit 967cc06578
3 files changed
+165

No files matched your search

@@ -1165,9 +1165,20 @@ const anytlsParser = (proxy = {}, includeUnsupportedProxy = false) => {
};
const tailscaleParser = (proxy = {}) => {
const useControlHTTPClient = hasControlHTTPClient(proxy);
const listenPort = parseSafeIntegerValue(proxy._listen_port);
const parsedProxy = {
tag: proxy.name,
type: 'tailscale',
listen_port:
listenPort != null && listenPort <= 65535 ? listenPort : undefined,
taildrop_directory:
typeof proxy._taildrop_directory === 'string'
? proxy._taildrop_directory
: undefined,
on_demand:
typeof proxy._on_demand === 'boolean'
? proxy._on_demand
: undefined,
control_http_client: proxy['control-http-client'],
udp_timeout: proxy['udp-timeout'],
state_directory: proxy['state-dir'] || proxy['state-directory'],
@@ -1225,10 +1236,23 @@ const wireguardParser = (proxy = {}) => {
const address = ['ipv4', 'ipv6']
.map((family) => getWireGuardAddressWithCIDR(proxy, family))
.filter((i) => i);
const listenPort = parseSafeIntegerValue(proxy._listen_port);
const udpNatMax = parseSafeIntegerValue(proxy._udp_nat_max);
const parsedProxy = {
system: !!proxy.system,
name: typeof proxy._name === 'string' ? proxy._name : undefined,
listen_port:
listenPort != null && listenPort <= 65535 ? listenPort : undefined,
on_demand:
typeof proxy._on_demand === 'boolean'
? proxy._on_demand
: undefined,
mtu: proxy.mtu ? parseInt(`${proxy.mtu}`, 10) : undefined,
udp_timeout: proxy['udp-timeout'],
udp_nat_max:
udpNatMax != null && udpNatMax <= 4294967295
? udpNatMax
: undefined,
workers: proxy['workers']
? parseInt(`${proxy['workers']}`, 10)
: undefined,
@@ -1242,6 +1266,17 @@ const wireguardParser = (proxy = {}) => {
pre_shared_key: proxy['pre-shared-key'],
reserved: [],
};
for (const field of ['udp_mapping', 'udp_filtering']) {
if (
[
'endpoint_independent',
'address_dependent',
'address_and_port_dependent',
].includes(proxy[`_${field}`])
) {
parsedProxy[field] = proxy[`_${field}`];
}
}
if (parsedProxy.server_port < 0 || parsedProxy.server_port > 65535)
throw 'invalid port';
if (proxy['fast-open']) parsedProxy.udp_fragment = true;
@@ -4391,6 +4391,13 @@ describe('Proxy structured producers', function () {
name: 'Sing-box WG Explicit CIDR',
server: 'wg-explicit.example.com',
port: 51820,
system: true,
_name: 'wg-test',
_listen_port: '51821',
_on_demand: true,
_udp_mapping: 'address_dependent',
_udp_filtering: 'address_and_port_dependent',
_udp_nat_max: '8192',
'private-key': 'private-key-1',
'public-key': 'public-key-1',
ip: '10.0.0.2',
@@ -4419,18 +4426,37 @@ describe('Proxy structured producers', function () {
expectSubset(explicit, {
type: 'wireguard',
tag: 'Sing-box WG Explicit CIDR',
system: true,
name: 'wg-test',
listen_port: 51821,
on_demand: true,
udp_mapping: 'address_dependent',
udp_filtering: 'address_and_port_dependent',
udp_nat_max: 8192,
address: ['10.0.0.2/24', 'fd00::2/64'],
});
expectSubset(defaults, {
type: 'wireguard',
address: ['10.0.0.3/32', 'fd00::3/128'],
});
expect(defaults).to.not.have.any.keys(
'name',
'listen_port',
'on_demand',
'udp_mapping',
'udp_filtering',
'udp_nat_max',
);
});
it('emits Tailscale endpoint fields for sing-box exports', function () {
const output = loadProducedJson('sing-box', {
type: 'tailscale',
name: 'Mihomo TS',
_listen_port: '41641',
_taildrop_directory: './taildrop',
_on_demand: true,
'state-dir': './mihomo-ts',
'auth-key': 'tskey-auth-test',
'control-url': 'https://headscale.example.com',
@@ -4450,6 +4476,9 @@ describe('Proxy structured producers', function () {
expectSubset(mihomo, {
type: 'tailscale',
listen_port: 41641,
taildrop_directory: './taildrop',
on_demand: true,
state_directory: './mihomo-ts',
auth_key: 'tskey-auth-test',
control_url: 'https://headscale.example.com',
@@ -4464,6 +4493,100 @@ describe('Proxy structured producers', function () {
expect(mihomo).to.not.have.property('udp');
});
it('preserves false and zero in sing-box endpoint options without leaking protocol fields', function () {
const { endpoints } = loadProducedJson(
'sing-box',
['wireguard', 'tailscale'].map((type) => ({
type,
name: type,
server: 'wg.example.com',
port: 51820,
ip: '10.0.0.2',
'private-key': 'private-key',
'public-key': 'public-key',
_name: 'wg-test',
_listen_port: 0,
_on_demand: false,
_udp_mapping: 'endpoint_independent',
_udp_filtering: 'endpoint_independent',
_udp_nat_max: 0,
_taildrop_directory: './taildrop',
})),
);
expect(endpoints).to.have.length(2);
for (const endpoint of endpoints) {
expectSubset(endpoint, { listen_port: 0, on_demand: false });
expect(
Object.keys(endpoint).some((key) => key.startsWith('_')),
).to.equal(false);
}
expectSubset(endpoints[0], {
name: 'wg-test',
udp_mapping: 'endpoint_independent',
udp_filtering: 'endpoint_independent',
udp_nat_max: 0,
});
expect(endpoints[0]).to.not.have.property('taildrop_directory');
expect(endpoints[1].taildrop_directory).to.equal('./taildrop');
expect(endpoints[1]).to.not.have.any.keys(
'name',
'udp_mapping',
'udp_filtering',
'udp_nat_max',
);
});
it('validates optional sing-box endpoint values and integer bounds', function () {
for (const [value, listenPort, udpNatMax] of [
[undefined, undefined, undefined],
[null, undefined, undefined],
['0', 0, 0],
[65535, 65535, 65535],
[65536, undefined, 65536],
['4294967295', undefined, 4294967295],
[4294967296, undefined, undefined],
[-1, undefined, undefined],
[1.5, undefined, undefined],
['51820x', undefined, undefined],
[true, undefined, undefined],
]) {
const { endpoints } = loadProducedJson(
'sing-box',
['wireguard', 'tailscale'].map((type) => ({
type,
name: type,
server: 'wg.example.com',
port: 51820,
ip: '10.0.0.2',
'private-key': 'private-key',
'public-key': 'public-key',
_listen_port: value,
_udp_nat_max: value,
_on_demand: 'false',
_name: 123,
_taildrop_directory: true,
_udp_mapping: 'invalid',
_udp_filtering: 'invalid',
})),
);
expect(endpoints).to.have.length(2);
for (const endpoint of endpoints) {
expect(endpoint.listen_port).to.equal(listenPort);
expect(endpoint).to.not.have.any.keys(
'on_demand',
'name',
'taildrop_directory',
'udp_mapping',
'udp_filtering',
);
}
expect(endpoints[0].udp_nat_max).to.equal(udpNatMax);
expect(endpoints[1]).to.not.have.property('udp_nat_max');
}
});
it('does not mix Tailscale control_http_client with legacy sing-box dialer fields', function () {
const output = loadProducedJson('sing-box', {
type: 'tailscale',
+7
View File
@@ -72,6 +72,13 @@ function operator(proxies = [], targetPlatform, context) {
// Loon 文档要求多个 DNS 用双引号包裹; Sub-Store 输入额外兼容不加双引号的列表, 输出始终带双引号
// 脚本快捷设置: $server['server-dns'] = ['223.5.5.5', 'https://dns.example.com/dns-query', 'quic://dns.example.com', 'h3://dns.example.com/dns-query']
// 移除可用 delete $server['server-dns'], 空数组不输出该字段
// 41. sing-box WireGuard/Tailscale 支持 `_on_demand` 设置 `on_demand`, 值为布尔值 `true`/`false`, 允许 endpoint 在需要时断开连接; sing-box 1.15.0 起支持. 例如: $server._on_demand = true
// 42. sing-box WireGuard/Tailscale 支持 `_listen_port` 设置本地 UDP 监听端口 `listen_port`, 接受 0–65535 的整数或整数字符串, `0` 表示自动选择; Tailscale 需要 sing-box 1.14.0+. WireGuard 的非零 `listen_port` 不能与 `detour`/`dialer-proxy` 同时使用. 例如: $server._listen_port = 51820
// 43. sing-box WireGuard 支持 `_name` 设置系统接口名称 `name`, 配合 `system: true` 使用; 节点的 `name` 仍用于输出 `tag`. 例如: $server._name = 'wg0'
// 44. sing-box WireGuard 支持 `_udp_mapping` 设置 `udp_mapping`, 可选 `endpoint_independent`(默认)、`address_dependent`、`address_and_port_dependent`; 控制 UDP NAT 映射复用方式, sing-box 1.14.0 起支持. 例如: $server._udp_mapping = 'endpoint_independent'
// 45. sing-box WireGuard 支持 `_udp_filtering` 设置 `udp_filtering`, 可选 `endpoint_independent`(默认)、`address_dependent`、`address_and_port_dependent`; 控制 UDP NAT 接受哪些远端回包, sing-box 1.14.0 起支持. 例如: $server._udp_filtering = 'address_and_port_dependent'
// 46. sing-box WireGuard 支持 `_udp_nat_max` 设置 `udp_nat_max`, 接受 0–4294967295 的整数或整数字符串, 限制 UDP NAT 会话数; `0` 使用 sing-box 的平台默认值, sing-box 1.14.0 起支持. 例如: $server._udp_nat_max = 8192
// 47. sing-box Tailscale 支持 `_taildrop_directory` 设置接收 Taildrop 文件的目录 `taildrop_directory`, 值为字符串; 相对路径基于 sing-box 工作目录, 默认 `Taildrop`, sing-box 1.14.0 起支持. 例如: $server._taildrop_directory = './taildrop'
// require 为 Node.js 的 require, 在 Node.js 运行环境下 可以用来引入模块
// 例如在 Node.js 环境下, 将文件内容写入 /tmp/1.txt 文件