From feb207b3330283cc2185ff78ced1a73886bd4389 Mon Sep 17 00:00:00 2001 From: xream Date: Tue, 22 Aug 2023 17:27:12 +0800 Subject: [PATCH] fix: servername/sni priority over wss host --- backend/package.json | 2 +- backend/src/core/proxy-utils/index.js | 10 +++++++++- backend/src/core/proxy-utils/parsers/index.js | 8 +++++++- backend/src/core/proxy-utils/producers/uri.js | 7 ++++++- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/backend/package.json b/backend/package.json index 41b07e0..a6a9912 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.18", + "version": "2.14.20", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 9273046..72ca5cb 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -1,5 +1,5 @@ import download from '@/utils/download'; - +import { isIPv4, isIPv6 } from '@/utils'; import PROXY_PROCESSORS, { ApplyProcessor } from './processors'; import PROXY_PREPROCESSORS from './preprocessors'; import PROXY_PRODUCERS from './producers'; @@ -36,6 +36,10 @@ function parse(raw) { if (lastParser) { const [proxy, error] = tryParse(lastParser, line); if (!error) { + // 前面已经处理过普通情况下的 SNI, 这里显式设置 SNI, 防止之后解析成 IP 后丢失域名 SNI + if (proxy.tls && !proxy.sni && !isIP(proxy.server)) { + proxy.sni = proxy.server; + } proxies.push(proxy); success = true; } @@ -182,3 +186,7 @@ function safeMatch(parser, line) { return false; } } + +function isIP(ip) { + return isIPv4(ip) || isIPv6(ip); +} diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index 4b941ad..5d30a1f 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -224,6 +224,10 @@ function URI_VMess() { ? !params.verify_cert : undefined, }; + // https://github.com/2dust/v2rayN/wiki/%E5%88%86%E4%BA%AB%E9%93%BE%E6%8E%A5%E6%A0%BC%E5%BC%8F%E8%AF%B4%E6%98%8E(ver-2) + if (proxy.tls && proxy.sni) { + proxy.sni = params.sni; + } // handle obfs if (params.net === 'ws') { proxy.network = 'ws'; @@ -231,7 +235,9 @@ function URI_VMess() { path: getIfNotBlank(params.path), headers: { Host: getIfNotBlank(params.host) }, }; - if (proxy.tls && params.host) { + // https://github.com/MetaCubeX/Clash.Meta/blob/Alpha/docs/config.yaml#L413 + // sni 优先级应高于 host + if (proxy.tls && !proxy.sni && params.host) { proxy.sni = params.host; } } diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index feb82ae..fbd5462 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -65,10 +65,15 @@ export default function URI_Producer() { net: proxy.network || 'tcp', tls: proxy.tls ? 'tls' : '', }; + if (proxy.tls && proxy.sni) { + result.sni = proxy.sni; + } // obfs if (proxy.network === 'ws') { result.path = proxy['ws-opts'].path || '/'; - result.host = proxy['ws-opts'].headers.Host || proxy.server; + if (proxy['ws-opts'].headers.Host) { + result.host = proxy['ws-opts'].headers.Host; + } } result = 'vmess://' + Base64.encode(JSON.stringify(result)); break;