From 97a945dbc629406cd126322064107afaedd60fda Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 22 Sep 2026 12:54:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20sing-box=20=E6=94=AF=E6=8C=81=20MASQUE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 4 +- backend/src/core/proxy-utils/parsers/index.js | 1 + .../core/proxy-utils/producers/sing-box.js | 117 ++++++++++-- .../src/core/proxy-utils/producers/surge.js | 65 +++++++ .../test/proxy-producers/structured.spec.js | 172 ++++++++++++++++++ backend/src/test/proxy-producers/text.spec.js | 92 ++++++++++ scripts/demo.js | 26 ++- 7 files changed, 453 insertions(+), 24 deletions(-) diff --git a/backend/package.json b/backend/package.json index ad6df3d0..80f22f5a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,7 +1,7 @@ { "name": "sub-store", - "version": "2.39.9", - "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", + "version": "2.40.0", + "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash, Egern and Shadowrocket", "main": "src/main.js", "packageManager": "pnpm@11.0.9", "scripts": { diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index 934b6a25..ec8f341e 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -2554,6 +2554,7 @@ function Clash_All() { } catch (e) { proxy = YAML.parse(line); } + if (proxy.type === 'masque-sing-box') proxy.type = 'masque-surge'; if ( ![ 'zerotier', diff --git a/backend/src/core/proxy-utils/producers/sing-box.js b/backend/src/core/proxy-utils/producers/sing-box.js index afa1e5df..71a060f3 100644 --- a/backend/src/core/proxy-utils/producers/sing-box.js +++ b/backend/src/core/proxy-utils/producers/sing-box.js @@ -77,6 +77,23 @@ const tfoParser = (proxy, parsedProxy) => { if (!parsedProxy.tcp_fast_open) delete parsedProxy.tcp_fast_open; }; +const udpNatParser = (proxy, parsedProxy) => { + const udpNatMax = parseSafeIntegerValue(proxy._udp_nat_max); + if (udpNatMax != null && udpNatMax <= 4294967295) + parsedProxy.udp_nat_max = udpNatMax; + for (const field of ['udp_mapping', 'udp_filtering']) { + if ( + [ + 'endpoint_independent', + 'address_dependent', + 'address_and_port_dependent', + ].includes(proxy[`_${field}`]) + ) { + parsedProxy[field] = proxy[`_${field}`]; + } + } +}; + const smuxParser = (smux, proxy) => { if (!smux || !smux.enabled) return; proxy.multiplex = { enabled: true }; @@ -1163,6 +1180,80 @@ const anytlsParser = (proxy = {}, includeUnsupportedProxy = false) => { domainResolverParser(proxy, parsedProxy); return parsedProxy; }; +const masqueParser = (proxy = {}) => { + const version = parseSafeIntegerValue(proxy._version); + if (proxy._version != null && (version == null || version > 3)) + throw new Error(`Invalid MASQUE HTTP version: ${proxy._version}`); + const httpVersion = version || 3; + const mtu = parseSafeIntegerValue(proxy.mtu); + const parsedProxy = { + tag: proxy.name, + type: 'masque-client', + server: proxy.server, + server_port: parseSafeIntegerValue(proxy.port), + username: proxy.username, + password: proxy.password, + path: typeof proxy.path === 'string' ? proxy.path : undefined, + version: version ?? undefined, + advertise_routes: + typeof proxy['advertise-routes'] === 'string' || + Array.isArray(proxy['advertise-routes']) + ? proxy['advertise-routes'] + : undefined, + system: typeof proxy.system === 'boolean' ? proxy.system : undefined, + name: typeof proxy._name === 'string' ? proxy._name : undefined, + mtu: mtu != null && mtu <= 4294967295 ? mtu : undefined, + udp_timeout: proxy['udp-timeout'], + tls: { enabled: true, server_name: proxy.server, insecure: false }, + }; + if (parsedProxy.server_port == null || parsedProxy.server_port > 65535) + throw new Error('invalid port'); + if (isPlainObject(proxy.headers)) { + parsedProxy.headers = Object.fromEntries( + Object.entries(proxy.headers).map(([key, value]) => [ + key, + Array.isArray(value) ? value.map(String) : String(value), + ]), + ); + } + for (const field of ['on_demand', 'disable_version_fallback']) { + if (typeof proxy[`_${field}`] === 'boolean') + parsedProxy[field] = proxy[`_${field}`]; + } + if (httpVersion >= 2) { + for (const field of [ + 'idle_timeout', + 'keep_alive_period', + 'stream_receive_window', + 'connection_receive_window', + ]) { + if (typeof proxy[`_${field}`] === 'string') + parsedProxy[field] = proxy[`_${field}`]; + } + parsedProxy.max_concurrent_streams = + parseSafeIntegerValue(proxy._max_concurrent_streams) ?? undefined; + } + if (httpVersion === 3) { + const initialPacketSize = parseSafeIntegerValue( + proxy._initial_packet_size, + ); + if (initialPacketSize != null && initialPacketSize <= 65535) + parsedProxy.initial_packet_size = initialPacketSize; + if (typeof proxy._disable_path_mtu_discovery === 'boolean') + parsedProxy.disable_path_mtu_discovery = + proxy._disable_path_mtu_discovery; + } + if (proxy['fast-open']) parsedProxy.udp_fragment = true; + udpNatParser(proxy, parsedProxy); + tfoParser(proxy, parsedProxy); + detourParser(proxy, parsedProxy); + tlsParser(proxy, parsedProxy); + if (httpVersion === 3) delete parsedProxy.tls.utls; + ipVersionParser(proxy, parsedProxy); + domainResolverParser(proxy, parsedProxy); + return parsedProxy; +}; + const tailscaleParser = (proxy = {}) => { const useControlHTTPClient = hasControlHTTPClient(proxy); const listenPort = parseSafeIntegerValue(proxy._listen_port); @@ -1237,7 +1328,6 @@ const wireguardParser = (proxy = {}) => { .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, @@ -1249,10 +1339,6 @@ const wireguardParser = (proxy = {}) => { : 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, @@ -1266,17 +1352,7 @@ 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}`]; - } - } + udpNatParser(proxy, parsedProxy); if (parsedProxy.server_port < 0 || parsedProxy.server_port > 65535) throw 'invalid port'; if (proxy['fast-open']) parsedProxy.udp_fragment = true; @@ -1617,6 +1693,9 @@ export default function singbox_Producer() { case 'tailscale': list.push(tailscaleParser(proxy)); break; + case 'masque-surge': + list.push(masqueParser(proxy)); + break; default: throw new Error( `Platform sing-box does not support proxy type: ${proxy.type}`, @@ -1650,7 +1729,11 @@ export default function singbox_Producer() { const categorized = list.reduce( (result, item) => { - if (['wireguard', 'tailscale'].includes(item.type)) { + if ( + ['wireguard', 'tailscale', 'masque-client'].includes( + item.type, + ) + ) { result.endpoints.push(item); } else { result.outbounds.push(item); diff --git a/backend/src/core/proxy-utils/producers/surge.js b/backend/src/core/proxy-utils/producers/surge.js index 8cac2a4e..cf7a2ca5 100644 --- a/backend/src/core/proxy-utils/producers/surge.js +++ b/backend/src/core/proxy-utils/producers/surge.js @@ -2,6 +2,7 @@ import { Result, isPresent } from './utils'; import { isNotBlank, getIfNotBlank } from '@/utils'; import $ from '@/core/app'; import { formatSurgeVmessEncryptMethod } from '../vmess-security'; +import { parseSafeIntegerValue } from '../transport-path'; const targetPlatform = 'Surge'; @@ -508,6 +509,70 @@ function trusttunnel(proxy) { return result.toString(); } function masque_surge(proxy) { + // https://manual.nssurge.com/policies/masque.html + const unsupportedFields = [ + 'path', + 'headers', + 'advertise-routes', + 'system', + '_name', + 'mtu', + '_on_demand', + 'udp-timeout', + '_udp_mapping', + '_udp_filtering', + '_udp_nat_max', + '_idle_timeout', + '_keep_alive_period', + '_stream_receive_window', + '_connection_receive_window', + '_max_concurrent_streams', + '_initial_packet_size', + '_disable_path_mtu_discovery', + '_certificate', + '_certificate_path', + '_certificate_public_key_sha256', + '_client_certificate', + '_client_certificate_path', + '_client_key', + '_client_key_path', + '_ech', + 'ech-opts', + '_curve_preferences', + '_fragment', + '_fragment_fallback_delay', + '_record_fragment', + 'reality-opts', + '_dns_server', + '_domain_resolver', + 'detour', + ].filter((field) => isPresent(proxy, field)); + if ( + isPresent(proxy, '_version') && + ![0, 3].includes(parseSafeIntegerValue(proxy._version)) + ) { + unsupportedFields.push('_version (only HTTP/3 is supported)'); + } + if ( + isPresent(proxy, '_disable_version_fallback') && + proxy._disable_version_fallback !== true + ) { + unsupportedFields.push('_disable_version_fallback (must be true)'); + } + if (proxy.plugin === 'shadow-tls') unsupportedFields.push('shadow-tls'); + if ( + hasNonBlankValue(proxy.ports) && + hasNonBlankValue(proxy['underlying-proxy']) + ) { + unsupportedFields.push('port-hopping with underlying-proxy'); + } + if (unsupportedFields.length) { + throw unsupported( + `Platform Surge does not support MASQUE options: ${unsupportedFields.join( + ', ', + )}. Proxy has been filtered`, + ); + } const result = new Result(proxy); result.append(`${proxy.name}=masque,${proxy.server},${proxy.port}`); result.appendIfPresent(`,username="${proxy.username}"`, 'username'); diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index 1a5e42af..6030379e 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -4450,6 +4450,178 @@ describe('Proxy structured producers', function () { ); }); + it('exports Surge MASQUE as sing-box endpoints with version-specific options', function () { + const [proxy] = ProxyUtils.parse( + 'MASQUE = masque,masque.example.com,443,username=example-user,password=example-password,sni=sni.example.com,skip-cert-verify=true,underlying-proxy=upstream,ip-version=ipv6-prefer,port-hopping="8443;8445-8447",port-hopping-interval=30,udp-relay=false,ecn=false', + ); + const defaults = loadProducedJson('sing-box', proxy); + expect(defaults.outbounds).to.deep.equal([]); + expect(defaults.endpoints).to.deep.equal([ + { + type: 'masque-client', + tag: 'MASQUE', + server: 'masque.example.com', + server_port: 443, + username: 'example-user', + password: 'example-password', + detour: 'upstream', + tls: { + enabled: true, + server_name: 'sni.example.com', + insecure: true, + }, + }, + ]); + expect( + JSON.parse(JSON.stringify(produceInternal('sing-box', proxy))), + ).to.deep.equal(defaults.endpoints); + const [alias] = ProxyUtils.parse( + JSON.stringify({ ...proxy, type: 'masque-sing-box' }), + ); + expect(alias.type).to.equal('masque-surge'); + expect(loadProducedJson('sing-box', alias)).to.deep.equal(defaults); + + for (const version of [0, 1, 2, 3]) { + const output = loadProducedJson('sing-box', [ + { + type: 'http', + name: 'HTTP', + server: 'http.example.com', + port: 8080, + }, + { + ...proxy, + _version: `${version}`, + path: '/masque/{target}/{ipproto}/', + headers: { + 'X-Test': ['one', 'two'], + Host: 'masque.example.com', + }, + 'advertise-routes': ['10.9.0.0/24'], + system: false, + _name: 'masque-test', + mtu: '1280', + _on_demand: false, + _disable_version_fallback: true, + 'udp-timeout': '30s', + _udp_mapping: 'address_dependent', + _udp_filtering: 'address_and_port_dependent', + _udp_nat_max: 0, + _idle_timeout: '1m', + _keep_alive_period: '10s', + _stream_receive_window: '64 MB', + _connection_receive_window: '128 MB', + _max_concurrent_streams: '10', + _initial_packet_size: '1350', + _disable_path_mtu_discovery: false, + _dns_server: 'dns-default', + _domain_resolver: 'dns-masque', + 'client-fingerprint': 'chrome', + _client_certificate_path: 'client.pem', + _client_key_path: 'client.key', + }, + ]); + expect( + output.outbounds.map((outbound) => outbound.type), + ).to.deep.equal(['http']); + expect(output.endpoints).to.have.length(1); + const [endpoint] = output.endpoints; + expectSubset(endpoint, { + ...defaults.endpoints[0], + version, + path: '/masque/{target}/{ipproto}/', + headers: { + 'X-Test': ['one', 'two'], + Host: 'masque.example.com', + }, + advertise_routes: ['10.9.0.0/24'], + system: false, + name: 'masque-test', + mtu: 1280, + on_demand: false, + disable_version_fallback: true, + udp_timeout: '30s', + udp_mapping: 'address_dependent', + udp_filtering: 'address_and_port_dependent', + udp_nat_max: 0, + domain_resolver: { + server: 'dns-masque', + strategy: 'prefer_ipv6', + }, + }); + expect(endpoint.tls).to.include({ + client_certificate_path: 'client.pem', + client_key_path: 'client.key', + }); + expect(endpoint).to.not.have.any.keys( + 'network', + 'server_ports', + 'hop_interval', + 'ecn', + '_version', + ); + if (version === 1) { + expect(endpoint).to.not.have.any.keys( + 'idle_timeout', + 'keep_alive_period', + 'stream_receive_window', + 'connection_receive_window', + 'max_concurrent_streams', + ); + } else { + expectSubset(endpoint, { + idle_timeout: '1m', + keep_alive_period: '10s', + stream_receive_window: '64 MB', + connection_receive_window: '128 MB', + max_concurrent_streams: 10, + }); + } + if (version === 0 || version === 3) { + expectSubset(endpoint, { + initial_packet_size: 1350, + disable_path_mtu_discovery: false, + }); + expect(endpoint.tls).to.not.have.property('utls'); + } else { + expect(endpoint).to.not.have.any.keys( + 'initial_packet_size', + 'disable_path_mtu_discovery', + ); + expect(endpoint.tls.utls).to.deep.equal({ + enabled: true, + fingerprint: 'chrome', + }); + } + } + + const invalid = loadProducedJson('sing-box', { + ...proxy, + _on_demand: 'false', + _disable_version_fallback: 'true', + _udp_mapping: 'invalid', + _udp_filtering: 'invalid', + _udp_nat_max: 4294967296, + _initial_packet_size: 65536, + _disable_path_mtu_discovery: 'false', + _max_concurrent_streams: '10x', + mtu: -1, + }).endpoints[0]; + expect(invalid).to.deep.equal(defaults.endpoints[0]); + for (const fields of [ + { _version: 4 }, + { _version: '2x' }, + { port: '443x' }, + { type: 'masque' }, + ]) { + const { result, errors } = captureErrors(() => + loadProducedJson('sing-box', { ...proxy, ...fields }), + ); + expect(result).to.deep.equal({ outbounds: [], endpoints: [] }); + expect(errors).to.have.length(1); + } + }); + it('emits Tailscale endpoint fields for sing-box exports', function () { const output = loadProducedJson('sing-box', { type: 'tailscale', diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 1c795431..62797158 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -1244,6 +1244,98 @@ describe('Proxy text producers', function () { ); }); + it('filters incompatible MASQUE options for Surge and SurgeMac without logging their values', function () { + const [proxy] = ProxyUtils.parse( + '{"type":"masque-sing-box","name":"MASQUE","server":"masque.example.com","port":443,"username":"example-user","password":"example-password"}', + ); + expect(proxy.type).to.equal('masque-surge'); + const unsupportedCases = [ + ['_version', { _version: 1 }], + ['_version', { _version: '2' }], + ['_version', { _version: '3x' }], + ['_disable_version_fallback', { _disable_version_fallback: false }], + ['path', { path: '/private-path' }], + ['headers', { headers: { Authorization: 'private-header' } }], + ['advertise-routes', { 'advertise-routes': ['10.9.0.0/24'] }], + ['system', { system: false }], + ['_name', { _name: 'masque0' }], + ['mtu', { mtu: 1280 }], + ['_on_demand', { _on_demand: false }], + ['udp-timeout', { 'udp-timeout': '30s' }], + ['_udp_nat_max', { _udp_nat_max: 0 }], + ['_keep_alive_period', { _keep_alive_period: '10s' }], + [ + '_disable_path_mtu_discovery', + { _disable_path_mtu_discovery: false }, + ], + [ + '_certificate_public_key_sha256', + { _certificate_public_key_sha256: ['private-pin'] }, + ], + ['_client_key', { _client_key: 'private-key-content' }], + ['_ech', { _ech: { enabled: true } }], + ['_domain_resolver', { _domain_resolver: 'dns-masque' }], + [ + 'shadow-tls', + { + plugin: 'shadow-tls', + 'plugin-opts': { password: 'private-shadow-password' }, + }, + ], + [ + 'port-hopping with underlying-proxy', + { ports: '8443,8445-8447', 'underlying-proxy': 'upstream' }, + ], + ]; + for (const platform of ['Surge', 'SurgeMac']) { + for (const version of [undefined, 0, '0', 3, '3']) { + expect( + produceExternal(platform, { + ...proxy, + _version: version, + _disable_version_fallback: true, + 'keystore-client-cert': 'client-cert-name', + alpn: ['custom-h3'], + }), + ).to.equal( + 'MASQUE=masque,masque.example.com,443,username="example-user",password="example-password",alpn="custom-h3",client-cert="client-cert-name"', + ); + } + for (const [field, options] of unsupportedCases) { + for (const includeUnsupported of [false, true]) { + const { result, errors } = captureErrors(() => + ProxyUtils.produce( + [{ ...proxy, ...options }], + platform, + 'external', + { + 'include-unsupported-proxy': includeUnsupported, + }, + ), + ); + expect(result, `${platform}: ${field}`).to.equal(''); + expect(errors).to.have.length(1); + expect(errors[0]).to.include(field).and.include('MASQUE'); + expect(errors[0]) + .to.not.include('private-') + .and.not.include('example-password'); + } + } + } + const { result, errors } = captureErrors(() => + ProxyUtils.produce( + [{ ...proxy, _version: 2 }], + 'SurgeMac', + 'external', + { useMihomoExternal: true }, + ), + ); + expect(result).to.equal(''); + expect(errors.some((message) => message.includes('_version'))).to.equal( + true, + ); + }); + it('omits Surge alpn and server-cert-verify-name for non-TLS outputs', function () { const output = produceExternal('Surge', [ { diff --git a/scripts/demo.js b/scripts/demo.js index c8f9bb33..f20e0877 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -72,13 +72,29 @@ 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 + // 41. sing-box WireGuard/Tailscale/MASQUE 支持 `_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 + // 43. sing-box WireGuard/MASQUE 支持 `_name` 设置系统接口名称 `name`, 配合 `system: true` 使用; 节点的 `name` 仍用于输出 `tag`. 例如: $server._name = 'wg0' + // 44. sing-box WireGuard/MASQUE 支持 `_udp_mapping` 设置 `udp_mapping`, 可选 `endpoint_independent`(默认)、`address_dependent`、`address_and_port_dependent`; 控制 UDP NAT 映射复用方式, WireGuard 需要 sing-box 1.14.0+, MASQUE 需要 1.15.0+. 例如: $server._udp_mapping = 'endpoint_independent' + // 45. sing-box WireGuard/MASQUE 支持 `_udp_filtering` 设置 `udp_filtering`, 可选 `endpoint_independent`(默认)、`address_dependent`、`address_and_port_dependent`; 控制 UDP NAT 接受哪些远端回包, WireGuard 需要 sing-box 1.14.0+, MASQUE 需要 1.15.0+. 例如: $server._udp_filtering = 'address_and_port_dependent' + // 46. sing-box WireGuard/MASQUE 支持 `_udp_nat_max` 设置 `udp_nat_max`, 接受 0–4294967295 的整数或整数字符串, 限制 UDP NAT 会话数; `0` 使用 sing-box 的平台默认值, WireGuard 需要 sing-box 1.14.0+, MASQUE 需要 1.15.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' + // 48. sing-box 1.15.0+ 支持将内部类型 `masque-surge` 输出为 `endpoints` 中的 `masque-client`(CONNECT-IP). JSON/JSON5/YAML 输入也允许写 `type: 'masque-sing-box'`, 解析后内部 type 仍为 `masque-surge`; mihomo 的 `masque` 不会按此转换 + // 协议区别: Surge 文档描述的是 HTTP/3 CONNECT/CONNECT-UDP, sing-box `masque-client` 是 CONNECT-IP; 共用内部 type 不代表服务器协议兼容, 跨客户端使用需要服务器分别支持对应协议. Surge 文档: https://manual.nssurge.com/policies/masque.html + // 可直接输入 Surge 格式: MASQUE = masque,masque.example.com,443,username=example-user,password=example-password,sni=masque.example.com + // 通用字段: `username`, `password`, `path`, `headers`(值支持字符串或字符串数组), `advertise-routes`(CIDR 字符串或数组), `system`(布尔值), `mtu`, `udp-timeout`; `path` 默认 /.well-known/masque/ip/{target}/{ipproto}/, `mtu` 默认 1280 + // `_version` 设置 HTTP 版本 1/2/3(整数或整数字符串), 省略或 0 默认 HTTP/3; `_disable_version_fallback`(布尔值) 禁止自动回退到较低 HTTP 版本 + // HTTP/2 和 HTTP/3 可设置 `_idle_timeout`, `_keep_alive_period`(Go Duration 字符串, 如 '30s'), `_stream_receive_window`, `_connection_receive_window`(容量字符串, 如 '64 MB'), `_max_concurrent_streams`(非负整数或整数字符串) + // 仅 HTTP/3 可设置 `_initial_packet_size`(0–65535 的整数或整数字符串), `_disable_path_mtu_discovery`(布尔值); HTTP/1 不输出 HTTP2/QUIC 字段, HTTP/2 不输出 QUIC 专属字段, HTTP/3 不输出 uTLS + // 复用上面的 `_name`, `_on_demand`, `_udp_mapping`, `_udp_filtering`, `_udp_nat_max` 和 TLS/DNS/前置代理字段; Surge `[Keystore]` 的 client-cert 引用不能直接转换, 请使用 `_client_certificate`/`_client_certificate_path` 和 `_client_key`/`_client_key_path` 提供实际证书和密钥 + // sing-box MASQUE 不支持 Surge 的端口跳跃, 只使用 `port`; `ports`/`hop-interval`/`ecn` 不输出 + // 输出 Surge/SurgeMac 时, `_version` 只能省略或设为 0/3(支持整数字符串), `_disable_version_fallback` 若设置必须为 true; HTTP/1、HTTP/2、需要回退的配置会过滤并记录原因, `alpn` 仍可按 Surge 文档自定义 + // Surge 无法表达以下字段, 显式设置时会过滤整个节点并记录字段名(包括 false、0、空字符串/空容器, null/undefined 视为未设置): `path`, `headers`, `advertise-routes`, `system`, `_name`, `mtu`, `_on_demand`, `udp-timeout`, `_udp_mapping`, `_udp_filtering`, `_udp_nat_max`, `_idle_timeout`, `_keep_alive_period`, `_stream_receive_window`, `_connection_receive_window`, `_max_concurrent_streams`, `_initial_packet_size`, `_disable_path_mtu_discovery` + // 同样会过滤 sing-box 的 `_certificate`/`_certificate_path`/`_certificate_public_key_sha256`、`_client_certificate`/`_client_certificate_path`/`_client_key`/`_client_key_path`、`_ech`/`ech-opts`、`_curve_preferences`、`_fragment`/`_fragment_fallback_delay`/`_record_fragment`、`reality-opts`、`_dns_server`/`_domain_resolver`、`detour`; Surge 客户端证书请用 `keystore-client-cert`, 前置代理请用 `underlying-proxy` + // Surge MASQUE 也会过滤 `plugin: 'shadow-tls'`, 以及同时设置非空 `ports` 和 `underlying-proxy` 的节点. 上述检查在开启“含不支持的协议”时仍生效, 不会静默丢弃参数后输出 + // 脚本示例: if ($server.type === 'masque-surge') { $server._on_demand = true; $server._version = 3; $server._disable_version_fallback = true; $server.mtu = 1280; $server._udp_nat_max = 8192; } + // 完整 MASQUE/HTTP2/QUIC/UDP NAT 字段的单行 JSON 输入示例(含 TLS、DNS 和前置代理; 地址、凭据、证书文件及 DNS/代理 tag 均为示例, 按需替换或删除; 此例会被 Surge/SurgeMac 过滤): + // {"type":"masque-sing-box","name":"MASQUE sing-box","server":"masque.example.com","port":443,"username":"example-user","password":"example-password","path":"/.well-known/masque/ip/{target}/{ipproto}/","headers":{"Host":"masque.example.com","X-Example":["one","two"]},"_version":3,"_disable_version_fallback":true,"advertise-routes":["10.9.0.0/24","fd00:9::/64"],"system":false,"_name":"masque0","mtu":1280,"_on_demand":true,"_idle_timeout":"1m","_keep_alive_period":"10s","_stream_receive_window":"64 MB","_connection_receive_window":"128 MB","_max_concurrent_streams":100,"_initial_packet_size":1331,"_disable_path_mtu_discovery":false,"udp-timeout":"5m","_udp_mapping":"endpoint_independent","_udp_filtering":"address_and_port_dependent","_udp_nat_max":8192,"tls":true,"sni":"masque.example.com","alpn":["h3"],"skip-cert-verify":false,"_certificate_path":"ca.pem","_client_certificate_path":"client.pem","_client_key_path":"client.key","tfo":true,"fast-open":true,"underlying-proxy":"upstream","_domain_resolver":{"server":"dns-masque","strategy":"prefer_ipv6"}} // require 为 Node.js 的 require, 在 Node.js 运行环境下 可以用来引入模块 // 例如在 Node.js 环境下, 将文件内容写入 /tmp/1.txt 文件