feat: sing-box 官方版支持 Snell

This commit is contained in:
xream committed 2026-07-05 01:41:17 +08:00
1 parent 6a55efb2b6
commit 3374eae700
4 files changed
+89 -54

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.33.2",
"version": "2.34.0",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
"main": "src/main.js",
"packageManager": "pnpm@11.0.9",
@@ -538,10 +538,7 @@ const getShadowTLSPluginOpts = (proxy = {}) => {
if (proxy.plugin === 'shadow-tls' && proxy['plugin-opts']) {
return proxy['plugin-opts'];
}
if (
proxy.type === 'snell' &&
proxy['obfs-opts']?.mode === 'shadow-tls'
) {
if (proxy.type === 'snell' && proxy['obfs-opts']?.mode === 'shadow-tls') {
return {
host: proxy['obfs-opts'].host,
password: proxy['obfs-opts'].password,
@@ -721,17 +718,22 @@ const getSnellVersion = (version) => {
return parseInt(normalized, 10);
};
const snellParser = (proxy = {}) => {
const snellParser = (proxy = {}, includeUnsupportedProxy = false) => {
const version = getSnellVersion(proxy.version);
const shadowTLSPluginOpts = getShadowTLSPluginOpts(proxy);
const supportedVersions = includeUnsupportedProxy
? [1, 2, 3, 4, 5, 6]
: [4, 5, 6];
if (
version != null &&
(![1, 2, 3, 4, 5].includes(version) || Number.isNaN(version))
(!supportedVersions.includes(version) || Number.isNaN(version))
) {
throw new Error(
`Platform sing-box does not support snell version ${proxy.version}`,
);
}
const outputVersion =
!includeUnsupportedProxy && version === 5 ? 4 : version;
const parsedProxy = {
tag: proxy.name,
@@ -742,17 +744,22 @@ const snellParser = (proxy = {}) => {
};
if (parsedProxy.server_port < 0 || parsedProxy.server_port > 65535)
throw 'invalid port';
if (version != null) parsedProxy.version = version;
if (
proxy['obfs-opts']?.mode &&
proxy['obfs-opts'].mode !== 'shadow-tls'
)
parsedProxy.obfs_mode = proxy['obfs-opts'].mode;
if (
proxy['obfs-opts']?.host &&
proxy['obfs-opts']?.mode !== 'shadow-tls'
)
parsedProxy.obfs_host = proxy['obfs-opts'].host;
if (outputVersion != null) parsedProxy.version = outputVersion;
if (proxy._userkey) parsedProxy.userkey = proxy._userkey;
if (outputVersion === 6) {
if (proxy.mode) parsedProxy.mode = proxy.mode;
} else {
if (
proxy['obfs-opts']?.mode &&
proxy['obfs-opts'].mode !== 'shadow-tls'
)
parsedProxy.obfs_mode = proxy['obfs-opts'].mode;
if (
proxy['obfs-opts']?.host &&
proxy['obfs-opts']?.mode !== 'shadow-tls'
)
parsedProxy.obfs_host = proxy['obfs-opts'].host;
}
if (proxy.reuse && (version == null || version >= 4))
parsedProxy.reuse = true;
networkParser(proxy, parsedProxy);
@@ -1393,21 +1400,20 @@ export default function singbox_Producer() {
}
break;
case 'snell':
if (opts['include-unsupported-proxy']) {
list.push(snellParser(proxy));
const shadowTLSPluginOpts =
getShadowTLSPluginOpts(proxy);
if (shadowTLSPluginOpts) {
list.push(
shadowTLSOutboundParser(
proxy,
shadowTLSPluginOpts,
),
);
}
} else {
throw new Error(
`Platform sing-box does not support proxy type: ${proxy.type}`,
list.push(
snellParser(
proxy,
opts['include-unsupported-proxy'],
),
);
const shadowTLSPluginOpts =
getShadowTLSPluginOpts(proxy);
if (shadowTLSPluginOpts) {
list.push(
shadowTLSOutboundParser(
proxy,
shadowTLSPluginOpts,
),
);
}
break;
@@ -334,14 +334,15 @@ describe('Proxy structured producers', function () {
);
});
it('keeps Snell in sing-box only when include-unsupported-proxy is enabled', function () {
it('keeps supported Snell in sing-box by default', function () {
const proxy = {
type: 'snell',
name: 'sing-box Snell',
server: 'snell.example.com',
port: 44046,
psk: 'secret',
version: 4,
version: 5,
_userkey: 'user-secret',
udp: false,
tfo: true,
'fast-open': true,
@@ -359,21 +360,12 @@ describe('Proxy structured producers', function () {
},
};
const { result, errors } = captureErrors(() =>
const { result: internal, errors } = captureErrors(() =>
produceInternal('sing-box', proxy),
);
const internal = produceInternal('sing-box', proxy, {
'include-unsupported-proxy': true,
});
const external = loadProducedJson('sing-box', proxy, {
'include-unsupported-proxy': true,
});
const external = loadProducedJson('sing-box', proxy);
expect(result).to.deep.equal([]);
expect(errors).to.have.length(1);
expect(errors[0]).to.include(
'Platform sing-box does not support proxy type: snell',
);
expect(errors).to.deep.equal([]);
expect(internal).to.have.length(1);
expectSubset(internal[0], {
tag: 'sing-box Snell',
@@ -382,6 +374,7 @@ describe('Proxy structured producers', function () {
server_port: 44046,
psk: 'secret',
version: 4,
userkey: 'user-secret',
reuse: true,
network: 'tcp',
obfs_mode: 'tls',
@@ -399,7 +392,41 @@ describe('Proxy structured producers', function () {
expectSubset(external.outbounds[0], internal[0]);
});
it('keeps sing-box Snell versions documented by reF1nd and rejects newer versions', function () {
it('keeps sing-box supported Snell versions by default and maps v5 to v4', function () {
const proxies = [1, 4, 5, 6, '4x'].map((version) => ({
type: 'snell',
name: `sing-box Snell ${version}`,
server: 'snell.example.com',
port: 44046,
psk: 'secret',
version,
mode: 'unshaped',
udp: true,
reuse: true,
'obfs-opts': {
mode: 'http',
host: 'obfs.example.com',
},
}));
const { result, errors } = captureErrors(() =>
produceInternal('sing-box', proxies),
);
expect(result.map((proxy) => proxy.version)).to.deep.equal([4, 4, 6]);
expect(result[1].tag).to.equal('sing-box Snell 5');
expect(result[2].mode).to.equal('unshaped');
expect(result[2]).to.not.have.property('obfs_mode');
expect(errors).to.have.length(2);
expect(errors[0]).to.include(
'Platform sing-box does not support snell version 1',
);
expect(errors[1]).to.include(
'Platform sing-box does not support snell version 4x',
);
});
it('keeps legacy Snell versions when include-unsupported-proxy is enabled', function () {
const proxies = [1, 2, 3, 4, 5, 6, '4x'].map((version) => ({
type: 'snell',
name: `sing-box Snell ${version}`,
@@ -407,6 +434,7 @@ describe('Proxy structured producers', function () {
port: 44046,
psk: 'secret',
version,
_userkey: `user-${version}`,
udp: true,
reuse: true,
}));
@@ -418,7 +446,7 @@ describe('Proxy structured producers', function () {
);
expect(result.map((proxy) => proxy.version)).to.deep.equal([
1, 2, 3, 4, 5,
1, 2, 3, 4, 5, 6,
]);
expect(
result.find((proxy) => proxy.version === 1),
@@ -429,11 +457,11 @@ describe('Proxy structured producers', function () {
expect(result.find((proxy) => proxy.version === 4).reuse).to.equal(
true,
);
expect(errors).to.have.length(2);
expect(errors[0]).to.include(
'Platform sing-box does not support snell version 6',
expect(result.find((proxy) => proxy.version === 5).userkey).to.equal(
'user-5',
);
expect(errors[1]).to.include(
expect(errors).to.have.length(1);
expect(errors[0]).to.include(
'Platform sing-box does not support snell version 4x',
);
});
+2 -1
View File
@@ -30,7 +30,7 @@ function operator(proxies = [], targetPlatform, context) {
// 14. `ports` 为端口跳跃, `hop-interval` 变换端口号的时间间隔
// 15. `ip-version` 设置节点使用 IP 版本,兼容各家的值. 会进行内部转换. sing-box 以外: 若无法匹配则使用原始值. sing-box: 需有匹配且节点上设置 `_dns_server` 字段, 将自动设置 `domain_resolver.server`. 同时, `sing-box` 支持使用 `_domain_resolver` 设置 `domain_resolver`: 字符串会作为 `server`, 对象会合并到 `domain_resolver`
// 16. `sing-box` 支持使用 `_network` 来设置 `network`, 例如 `tcp`, `udp`
// 仅对 sing-box 源码里有 `network` 字段的协议生效: `ss`, `ssr`, `socks5`, `vmess`, `vless`, `trojan`, `hysteria`, `hysteria2`, `tuic`.
// 仅对 sing-box 源码里有 `network` 字段的协议生效: `ss`, `ssr`, `socks5`, `vmess`, `vless`, `trojan`, `hysteria`, `hysteria2`, `tuic`, `snell`.
// 注意: mihomo 风格的 `udp: true` 表示节点支持 UDP, 不会转换成 sing-box 的 `network: "udp"`; sing-box 默认就是 TCP+UDP. `udp: false` 会转换成 `network: "tcp"`. `_network` 是显式覆盖, 优先级高于 `udp`.
// 17. `block-quic` 支持 `auto`, `on`, `off`. 不同的平台不一定都支持, 会自动转换
// 18. `sing-box` 支持 `_fragment`, `_fragment_fallback_delay`, `_record_fragment` 设置 `tls` 的 `fragment`, `fragment_fallback_delay`, `record_fragment`
@@ -55,6 +55,7 @@ function operator(proxies = [], targetPlatform, context) {
// 30. Loon 支持使用 `_loon_tls_profile` 设置 `tls-profile` 字段('default', 'chrome', 'ios18', 'ios26'), 否则则使用 client-fingerprint 自动转换部分对应的值
// 31. `shadow-tls-password`/`shadow-tls-sni`/`shadow-tls-version` 这套旧字段已废弃. 请使用 `plugin: 'shadow-tls'` 和 `plugin-opts: { password, host, version }`
// 32. mihomo 中 Snell shadow-tls 字段与 ss shadow-tls 字段不同, 使用的是 obfs-opts 而不是 plugin+plugin-opts, 不能与 obfs http/tls 共存. Sub-Store 内部有字段转换, 建议直接使用单行 Surge 格式 `1=snell,a.com,443,version=4,psk="1",obfs=http,obfs-host=a.com,shadow-tls-password="1",shadow-tls-sni=a.com,shadow-tls-version=3,alpn="http/1.1,h2",reuse=true` . 若想使用 JSON/JSON5/YAML 单行格式输入, 可使用 `{ "name": "1", "server": "a.com", "port": 443, "psk": "1", "version": 4, "reuse": true, "type": "snell", "obfs-opts": { "mode": "http", "host": "a.com" }, "plugin": "shadow-tls", "plugin-opts": { "host": "a.com", "password": "1", "version": 3, "alpn": [ "http/1.1", "h2" ] } }`
// 33. sing-box Snell 出站默认允许 version 4/5/6, 其中 version 5 会按 sing-box 行为输出成 version 4. 开启“含不支持的协议”时保留 version 1/2/3/4/5/6. 节点上的 `_userkey` 会输出为 sing-box 的 `userkey`
// require 为 Node.js 的 require, 在 Node.js 运行环境下 可以用来引入模块
// 例如在 Node.js 环境下, 将文件内容写入 /tmp/1.txt 文件