From 374001c070599d1d784e14121d67f633331b5cc9 Mon Sep 17 00:00:00 2001 From: xream Date: Wed, 10 Jun 2026 12:57:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Egern=20=E6=94=AF=E6=8C=81=20Snell=201/?= =?UTF-8?q?2/3/4/5,=20SOCKS5=20over=20TLS,=20VMess/VLESS=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20gRPC,=20Trojan/SOCKS5=20over=20TLS/AnyTLS/HTTPS/gRP?= =?UTF-8?q?C(VMess/VLESS)=20=E6=94=AF=E6=8C=81=20REALITY?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../src/core/proxy-utils/producers/egern.js | 192 +++++++++---- .../test/proxy-producers/structured.spec.js | 263 ++++++++++++++++++ 3 files changed, 406 insertions(+), 51 deletions(-) diff --git a/backend/package.json b/backend/package.json index 70ba63c7..0a6f3eda 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.24.11", + "version": "2.24.12", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", diff --git a/backend/src/core/proxy-utils/producers/egern.js b/backend/src/core/proxy-utils/producers/egern.js index adc7b583..46d3c04a 100644 --- a/backend/src/core/proxy-utils/producers/egern.js +++ b/backend/src/core/proxy-utils/producers/egern.js @@ -26,6 +26,7 @@ export default function Egern_Producer() { 'wireguard', 'anytls', 'ssh', + 'snell', ].includes(proxy.type) || (proxy.type === 'ss' && ((proxy.plugin === 'obfs' && @@ -63,16 +64,20 @@ export default function Egern_Producer() { '2022-blake3-aes-256-gcm', ].includes(proxy.cipher))) || (proxy.type === 'vmess' && - !['h2', 'http', 'ws', 'tcp'].includes(proxy.network) && - proxy.network) || + ((!['h2', 'http', 'ws', 'tcp', 'grpc'].includes( + proxy.network, + ) && + proxy.network) || + !isEgernGrpcGun(proxy))) || (proxy.type === 'trojan' && !['http', 'ws', 'tcp'].includes(proxy.network) && proxy.network) || (proxy.type === 'vless' && - ((!['h2', 'http', 'ws', 'tcp'].includes( + ((!['h2', 'http', 'ws', 'tcp', 'grpc'].includes( proxy.network, ) && proxy.network) || + !isEgernGrpcGun(proxy) || (typeof proxy.flow !== 'undefined' && !['xtls-rprx-vision', ''].includes( proxy.flow, @@ -82,12 +87,15 @@ export default function Egern_Producer() { proxy.token.length !== 0) ) { return false; + } else if ( + proxy.type === 'snell' && + normalizeSnellVersion(proxy.version) === null + ) { + return false; } else if ( ['anytls'].includes(proxy.type) && proxy.network && - (!['tcp'].includes(proxy.network) || - (['tcp'].includes(proxy.network) && - proxy['reality-opts'])) + !['tcp'].includes(proxy.network) ) { return false; } else if ( @@ -126,12 +134,13 @@ export default function Egern_Producer() { headers: proxy.headers, } : {}), - tfo: proxy.tfo || proxy['fast-open'], + tfo: getTfo(proxy), ...(proxy.tls ? { sni: proxy.sni, skip_tls_verify: proxy['skip-cert-verify'], + reality: getReality(proxy), } : {}), }; @@ -148,21 +157,29 @@ export default function Egern_Producer() { headers: proxy.headers, } : {}), - tfo: proxy.tfo || proxy['fast-open'], + tfo: getTfo(proxy), sni: proxy.sni, skip_tls_verify: proxy['skip-cert-verify'], + reality: getReality(proxy), }; } else if (proxy.type === 'socks5') { proxy = { - type: 'socks5', + type: proxy.tls ? 'socks5_tls' : 'socks5', name: proxy.name, server: proxy.server, port: proxy.port, username: proxy.username, password: proxy.password, - tfo: proxy.tfo || proxy['fast-open'], - udp_relay: - proxy.udp || proxy.udp_relay || proxy.udp_relay, + tfo: getTfo(proxy), + udp_relay: getUdpRelay(proxy), + ...(proxy.tls + ? { + sni: proxy.sni, + skip_tls_verify: + proxy['skip-cert-verify'], + reality: getReality(proxy), + } + : {}), }; } else if (proxy.type === 'ss') { proxy = { @@ -175,9 +192,8 @@ export default function Egern_Producer() { server: proxy.server, port: proxy.port, password: proxy.password, - tfo: proxy.tfo || proxy['fast-open'], - udp_relay: - proxy.udp || proxy.udp_relay || proxy.udp_relay, + tfo: getTfo(proxy), + udp_relay: getUdpRelay(proxy), }; if (isPresent(original, 'plugin')) { if (original.plugin === 'obfs') { @@ -207,9 +223,8 @@ export default function Egern_Producer() { ), } : {}), - tfo: proxy.tfo || proxy['fast-open'], - udp_relay: - proxy.udp || proxy.udp_relay || proxy.udp_relay, + tfo: getTfo(proxy), + udp_relay: getUdpRelay(proxy), sni: proxy.sni, skip_tls_verify: proxy['skip-cert-verify'], port_hopping: proxy.ports, @@ -251,11 +266,11 @@ export default function Egern_Producer() { server: proxy.server, port: proxy.port, password: proxy.password, - tfo: proxy.tfo || proxy['fast-open'], - udp_relay: - proxy.udp || proxy.udp_relay || proxy.udp_relay, + tfo: getTfo(proxy), + udp_relay: getUdpRelay(proxy), sni: proxy.sni, skip_tls_verify: proxy['skip-cert-verify'], + reality: getReality(proxy), websocket: proxy.websocket, }; } else if (proxy.type === 'anytls') { @@ -265,11 +280,11 @@ export default function Egern_Producer() { server: proxy.server, port: proxy.port, password: proxy.password, - tfo: proxy.tfo || proxy['fast-open'], - udp_relay: - proxy.udp || proxy.udp_relay || proxy.udp_relay, + tfo: getTfo(proxy), + udp_relay: getUdpRelay(proxy), sni: proxy.sni, skip_tls_verify: proxy['skip-cert-verify'], + reality: getReality(proxy), }; } else if (proxy.type === 'vmess') { // Egern:传输层,支持 ws/wss/http1/http2/tls,不配置则为 tcp @@ -319,6 +334,8 @@ export default function Egern_Producer() { skip_tls_verify: proxy['skip-cert-verify'], }, }; + } else if (proxy.network === 'grpc') { + proxy.transport = getGrpcTransport(proxy); } else if ( (proxy.network === 'tcp' || !proxy.network) && proxy.tls @@ -332,10 +349,13 @@ export default function Egern_Producer() { }, }; } - let legacy; + let legacy = false; if (isPresent(proxy, 'aead') && !proxy.aead) { legacy = true; - } else if (proxy.alterId !== 0) { + } else if ( + isPresent(proxy, 'alterId') && + proxy.alterId !== 0 + ) { legacy = true; } proxy = { @@ -345,10 +365,9 @@ export default function Egern_Producer() { port: proxy.port, user_id: proxy.uuid, security, - tfo: proxy.tfo || proxy['fast-open'], + tfo: getTfo(proxy), legacy, - udp_relay: - proxy.udp || proxy.udp_relay || proxy.udp_relay, + udp_relay: getUdpRelay(proxy), transport: proxy.transport, }; } else if (proxy.type === 'vless') { @@ -401,25 +420,16 @@ export default function Egern_Producer() { skip_tls_verify: proxy['skip-cert-verify'], }, }; + } else if (proxy.network === 'grpc') { + proxy.transport = getGrpcTransport(proxy); } else if (proxy.network === 'tcp' || !proxy.network) { - let reality; - if ( - proxy['reality-opts']?.['short-id'] || - proxy['reality-opts']?.['public-key'] - ) { - reality = { - short_id: proxy['reality-opts']['short-id'], - public_key: - proxy['reality-opts']['public-key'], - }; - } proxy.transport = { [proxy.tls ? 'tls' : 'tcp']: { sni: proxy.tls ? proxy.sni : undefined, skip_tls_verify: proxy.tls ? proxy['skip-cert-verify'] : undefined, - reality, + reality: getReality(proxy), }, }; flow = proxy.flow; @@ -432,9 +442,8 @@ export default function Egern_Producer() { port: proxy.port, user_id: proxy.uuid, security: proxy.cipher, - tfo: proxy.tfo || proxy['fast-open'], - udp_relay: - proxy.udp || proxy.udp_relay || proxy.udp_relay, + tfo: getTfo(proxy), + udp_relay: getUdpRelay(proxy), transport: proxy.transport, flow, }; @@ -500,7 +509,31 @@ export default function Egern_Producer() { private_key: proxy['private-key'], // private_key_passphrase: proxy['private-key-passphrase'], host_keys: proxy['host-key'], - tfo: proxy.tfo || proxy['fast-open'], + tfo: getTfo(proxy), + }; + } else if (proxy.type === 'snell') { + const snellVersion = normalizeSnellVersion( + proxy.version, + ); + proxy = { + type: 'snell', + name: proxy.name, + server: proxy.server, + port: proxy.port, + psk: proxy.psk, + version: snellVersion, + ...(snellVersion == null || snellVersion >= 3 + ? { + udp_relay: getUdpRelay(proxy), + } + : {}), + reuse: proxy.reuse, + obfs: proxy['obfs-opts']?.mode || proxy.obfs, + obfs_host: + proxy['obfs-opts']?.host || + proxy['obfs-host'] || + proxy.obfs_host, + tfo: getTfo(proxy), }; } if ( @@ -561,6 +594,7 @@ export default function Egern_Producer() { 'hysteria2', 'anytls', 'ssh', + 'snell', ].includes(original.type) ) { if ( @@ -595,11 +629,12 @@ export default function Egern_Producer() { if (proxy.transport) { for (const key in proxy.transport) { if ( - Object.keys(proxy.transport[key]).length === + key !== 'grpc' && + (Object.keys(proxy.transport[key]).length === 0 || - Object.values(proxy.transport[key]).every( - (value) => value == null, - ) + Object.values(proxy.transport[key]).every( + (value) => value == null, + )) ) { delete proxy.transport[key]; } @@ -644,6 +679,62 @@ function hasHeaders(proxy) { ); } +function getTfo(proxy) { + return proxy.tfo ?? proxy['fast-open']; +} + +function getUdpRelay(proxy) { + return proxy.udp ?? proxy.udp_relay; +} + +function getNonEmptyValue(value) { + if (value == null) return undefined; + if (typeof value === 'string' && value.length === 0) return undefined; + return value; +} + +function getReality(proxy) { + const realityOpts = proxy?.['reality-opts']; + if (!realityOpts) return undefined; + + const reality = {}; + const publicKey = getNonEmptyValue(realityOpts['public-key']); + const shortId = getNonEmptyValue(realityOpts['short-id']); + if (publicKey != null) reality.public_key = publicKey; + if (shortId != null) reality.short_id = shortId; + + return Object.keys(reality).length > 0 ? reality : undefined; +} + +function getGrpcTransport(proxy) { + return { + grpc: { + service_name: proxy['grpc-opts']?.['grpc-service-name'], + sni: proxy.sni, + reality: getReality(proxy), + skip_tls_verify: proxy['skip-cert-verify'], + }, + }; +} + +function isEgernGrpcGun(proxy) { + if (proxy.network !== 'grpc') return true; + + const grpcType = proxy['grpc-opts']?.['_grpc-type']; + if (grpcType == null) return true; + + return `${grpcType}`.trim().toLowerCase() === 'gun'; +} + +function normalizeSnellVersion(version) { + if (version == null) return undefined; + + const normalized = `${version}`.trim(); + if (!/^[1-5]$/.test(normalized)) return null; + + return parseInt(normalized, 10); +} + function getFirstHeaderValue(headers, ...keys) { for (const key of keys) { const value = getFirstValue(headers?.[key]); @@ -699,6 +790,7 @@ function supportsRootFingerprintSha256(original, proxy) { ['anytls', 'https', 'hysteria2', 'trojan', 'tuic'].includes( original.type, ) || + (original.type === 'socks5' && proxy.type === 'socks5_tls') || (original.type === 'http' && proxy.type === 'https') ); } @@ -706,7 +798,7 @@ function supportsRootFingerprintSha256(original, proxy) { function addTransportFingerprintSha256(transport, fingerprintSha256) { if (!transport) return; - for (const key of ['http2', 'tls', 'wss']) { + for (const key of ['grpc', 'http2', 'tls', 'wss']) { if (transport[key]) { transport[key].fingerprint_sha256 = fingerprintSha256; } diff --git a/backend/src/test/proxy-producers/structured.spec.js b/backend/src/test/proxy-producers/structured.spec.js index c55a9dd5..ad09377c 100644 --- a/backend/src/test/proxy-producers/structured.spec.js +++ b/backend/src/test/proxy-producers/structured.spec.js @@ -2068,6 +2068,269 @@ describe('Proxy structured producers', function () { }); }); + it('emits Egern Snell versions 1 through 5', function () { + const proxies = [1, 2, 3, 4, 5, 6, '4x'].map((version) => ({ + type: 'snell', + name: `Egern Snell ${version}`, + server: 'snell.example.com', + port: 44046, + psk: 'secret', + version, + udp: true, + reuse: true, + tfo: true, + 'block-quic': 'on', + 'obfs-opts': { + mode: 'http', + host: 'obfs.example.com', + }, + })); + const getProxy = (items, name) => + items.find((item) => item.snell?.name === name)?.snell; + const getVersions = (items) => items.map((item) => item.snell.version); + + const internal = produceInternal('Egern', proxies); + const external = loadProducedYaml('Egern', proxies); + + expect(getVersions(internal)).to.deep.equal([1, 2, 3, 4, 5]); + expect(getVersions(external.proxies)).to.deep.equal([1, 2, 3, 4, 5]); + expect(getProxy(internal, 'Egern Snell 1')).to.not.have.property( + 'udp_relay', + ); + expect(getProxy(internal, 'Egern Snell 2')).to.not.have.property( + 'udp_relay', + ); + for (const output of [internal, external.proxies]) { + expectSubset(getProxy(output, 'Egern Snell 5'), { + name: 'Egern Snell 5', + server: 'snell.example.com', + port: 44046, + psk: 'secret', + version: 5, + udp_relay: true, + reuse: true, + obfs: 'http', + obfs_host: 'obfs.example.com', + tfo: true, + block_quic: true, + }); + } + }); + + it('emits Egern SOCKS5 over TLS and root REALITY options', function () { + const proxies = [ + { + type: 'socks5', + name: 'Egern SOCKS5 TLS Reality', + server: 'socks.example.com', + port: 1080, + username: 'user', + password: 'pass', + tls: true, + sni: 'socks-sni.example.com', + tfo: false, + udp: true, + 'skip-cert-verify': false, + 'tls-fingerprint': 'SHA256:SOCKS', + 'reality-opts': { + 'public-key': 'socks-pub', + 'short-id': '01', + }, + }, + { + type: 'https', + name: 'Egern HTTPS Reality', + server: 'https.example.com', + port: 443, + sni: 'https-sni.example.com', + 'skip-cert-verify': true, + 'reality-opts': { + 'public-key': 'https-pub', + 'short-id': '02', + }, + }, + { + type: 'trojan', + name: 'Egern Trojan Reality', + server: 'trojan.example.com', + port: 443, + password: 'secret', + tfo: false, + udp: true, + 'skip-cert-verify': false, + 'reality-opts': { + 'public-key': 'trojan-pub', + 'short-id': '03', + }, + }, + { + type: 'anytls', + name: 'Egern AnyTLS Reality', + server: 'anytls.example.com', + port: 443, + password: 'secret', + network: 'tcp', + 'reality-opts': { + 'public-key': 'anytls-pub', + 'short-id': '04', + }, + }, + ]; + const getProxy = (items, type, name) => + items.find((item) => item[type]?.name === name)?.[type]; + + const internal = produceInternal('Egern', proxies); + const external = loadProducedYaml('Egern', proxies); + + for (const output of [internal, external.proxies]) { + expectSubset( + getProxy(output, 'socks5_tls', 'Egern SOCKS5 TLS Reality'), + { + server: 'socks.example.com', + port: 1080, + username: 'user', + password: 'pass', + tfo: false, + udp_relay: true, + skip_tls_verify: false, + fingerprint_sha256: 'SHA256:SOCKS', + reality: { + public_key: 'socks-pub', + short_id: '01', + }, + }, + ); + expectSubset(getProxy(output, 'https', 'Egern HTTPS Reality'), { + skip_tls_verify: true, + reality: { + public_key: 'https-pub', + short_id: '02', + }, + }); + expectSubset(getProxy(output, 'trojan', 'Egern Trojan Reality'), { + tfo: false, + udp_relay: true, + skip_tls_verify: false, + reality: { + public_key: 'trojan-pub', + short_id: '03', + }, + }); + expectSubset(getProxy(output, 'anytls', 'Egern AnyTLS Reality'), { + reality: { + public_key: 'anytls-pub', + short_id: '04', + }, + }); + } + }); + + it('emits Egern VMess and VLESS gRPC Gun transports with REALITY', function () { + const proxies = [ + { + type: 'vmess', + name: 'Egern VMess gRPC Reality', + server: 'vmess.example.com', + port: 443, + uuid: UUID, + cipher: 'aes-128-gcm', + alterId: 0, + tls: true, + sni: 'vmess-sni.example.com', + network: 'grpc', + tfo: false, + udp: true, + 'skip-cert-verify': true, + 'tls-fingerprint': 'SHA256:GRPC', + 'grpc-opts': { + 'grpc-service-name': 'vmess-service', + '_grpc-type': 'gun', + }, + 'reality-opts': { + 'public-key': 'vmess-pub', + 'short-id': '05', + }, + }, + { + type: 'vless', + name: 'Egern VLESS gRPC Reality', + server: 'vless.example.com', + port: 443, + uuid: UUID, + tls: true, + sni: 'vless-sni.example.com', + network: 'grpc', + 'grpc-opts': { + 'grpc-service-name': 'vless-service', + }, + 'reality-opts': { + 'public-key': 'vless-pub', + 'short-id': '06', + }, + }, + { + type: 'vmess', + name: 'Egern VMess gRPC Multi', + server: 'vmess-multi.example.com', + port: 443, + uuid: UUID, + cipher: 'auto', + network: 'grpc', + 'grpc-opts': { + 'grpc-service-name': 'multi-service', + '_grpc-type': 'multi', + }, + }, + ]; + const getProxy = (items, type, name) => + items.find((item) => item[type]?.name === name)?.[type]; + + const internal = produceInternal('Egern', proxies); + const external = loadProducedYaml('Egern', proxies); + + for (const output of [internal, external.proxies]) { + expect(output).to.have.length(2); + expectSubset( + getProxy(output, 'vmess', 'Egern VMess gRPC Reality'), + { + user_id: UUID, + security: 'aes-128-gcm', + legacy: false, + tfo: false, + udp_relay: true, + transport: { + grpc: { + service_name: 'vmess-service', + sni: 'vmess-sni.example.com', + skip_tls_verify: true, + fingerprint_sha256: 'SHA256:GRPC', + reality: { + public_key: 'vmess-pub', + short_id: '05', + }, + }, + }, + }, + ); + expectSubset( + getProxy(output, 'vless', 'Egern VLESS gRPC Reality'), + { + user_id: UUID, + transport: { + grpc: { + service_name: 'vless-service', + sni: 'vless-sni.example.com', + reality: { + public_key: 'vless-pub', + short_id: '06', + }, + }, + }, + }, + ); + } + }); + it('emits Egern fingerprint_sha256 for supported TLS proxy types and transports', function () { const fingerprint = 'SHA256:FINGERPRINT'; const proxies = [