From c0ab301160c81860bdc0ccfd4adcecb161d66a41 Mon Sep 17 00:00:00 2001 From: xream Date: Fri, 29 Dec 2023 16:08:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Trojan=20URI=20=E6=94=AF=E6=8C=81=20gRP?= =?UTF-8?q?C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- .../proxy-utils/parsers/peggy/trojan-uri.js | 17 ++++++++++++----- .../proxy-utils/parsers/peggy/trojan-uri.peg | 17 ++++++++++++----- backend/src/core/proxy-utils/producers/uri.js | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/backend/package.json b/backend/package.json index 2460f2d..119cc92 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.135", + "version": "2.14.136", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js b/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js index 9e0e52b..a7fd304 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js +++ b/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js @@ -90,11 +90,18 @@ params = "/"? "?" head:param tail:("&"@param)* { if (params["type"]) { proxy.network = params["type"] - if (params["path"]) { - $set(proxy, proxy.network+"-opts.path", decodeURIComponent(params["path"])); - } - if (params["host"]) { - $set(proxy, proxy.network+"-opts.headers.Host", decodeURIComponent(params["host"])); + if (['grpc'].includes(proxy.network)) { + proxy[proxy.network + '-opts'] = { + 'grpc-service-name': params["serviceName"], + '_grpc-type': params["mode"], + }; + } else { + if (params["path"]) { + $set(proxy, proxy.network+"-opts.path", decodeURIComponent(params["path"])); + } + if (params["host"]) { + $set(proxy, proxy.network+"-opts.headers.Host", decodeURIComponent(params["host"])); + } } } diff --git a/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.peg b/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.peg index f2d2d71..5cb0b7e 100644 --- a/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.peg +++ b/backend/src/core/proxy-utils/parsers/peggy/trojan-uri.peg @@ -88,11 +88,18 @@ params = "/"? "?" head:param tail:("&"@param)* { if (params["type"]) { proxy.network = params["type"] - if (params["path"]) { - $set(proxy, proxy.network+"-opts.path", decodeURIComponent(params["path"])); - } - if (params["host"]) { - $set(proxy, proxy.network+"-opts.headers.Host", decodeURIComponent(params["host"])); + if (['grpc'].includes(proxy.network)) { + proxy[proxy.network + '-opts'] = { + 'grpc-service-name': params["serviceName"], + '_grpc-type': params["mode"], + }; + } else { + if (params["path"]) { + $set(proxy, proxy.network+"-opts.path", decodeURIComponent(params["path"])); + } + if (params["host"]) { + $set(proxy, proxy.network+"-opts.headers.Host", decodeURIComponent(params["host"])); + } } } diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index f8ad2e4..1791735 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -201,6 +201,21 @@ export default function URI_Producer() { let trojanTransport = ''; if (proxy.network) { trojanTransport = `&type=${proxy.network}`; + if (['grpc'].includes(proxy.network)) { + let trojanTransportServiceName = + proxy[`${proxy.network}-opts`]?.[ + `${proxy.network}-service-name` + ]; + if (trojanTransportServiceName) { + trojanTransport += `&serviceName=${encodeURIComponent( + trojanTransportServiceName, + )}`; + } + trojanTransport += `&mode=${encodeURIComponent( + proxy[`${proxy.network}-opts`]?.['_grpc-type'] || + 'gun', + )}`; + } let trojanTransportPath = proxy[`${proxy.network}-opts`]?.path; let trojanTransportHost =