diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index e4461814..ae4828c9 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -2520,12 +2520,16 @@ function URI_Trojan() { }; const parse = (line) => { - const matched = /^(trojan:\/\/.*?@.*?)(:(\d+))?\/?(\?.*?)?$/.exec(line); + // Split off the name first: with a "#name" but no query string, the port + // check below would not see the port and would append ":443" to the name. + let [newLine, name] = line.split(/#(.+)/, 2); + const matched = /^(trojan:\/\/.*?@.*?)(:(\d+))?\/?(\?.*?)?$/.exec( + newLine, + ); const port = matched?.[2]; if (!port) { - line = line.replace(matched[1], `${matched[1]}:443`); + newLine = newLine.replace(matched[1], `${matched[1]}:443`); } - let [newLine, name] = line.split(/#(.+)/, 2); const parser = getTrojanURIParser(); const proxy = parser.parse(newLine); if (isNotBlank(name)) { diff --git a/backend/src/test/proxy-parsers/uri.spec.js b/backend/src/test/proxy-parsers/uri.spec.js index b0e80c10..3832fdab 100644 --- a/backend/src/test/proxy-parsers/uri.spec.js +++ b/backend/src/test/proxy-parsers/uri.spec.js @@ -699,6 +699,37 @@ describe('Proxy URI parser coverage', function () { ); }); + it('keeps the trojan name when there is no query string', function () { + expectSubset( + parseOne( + 'trojan://trojan-pass@trojan.example.com:8443#My%20Node', + ), + { + server: 'trojan.example.com', + port: 8443, + name: 'My Node', + }, + ); + expectSubset( + parseOne( + 'trojan://trojan-pass@trojan.example.com#Default%20Port', + ), + { + server: 'trojan.example.com', + port: 443, + name: 'Default Port', + }, + ); + expectSubset( + parseOne('trojan://trojan-pass@[2001:db8::2]#IPv6%20Name'), + { + server: '2001:db8::2', + port: 443, + name: 'IPv6 Name', + }, + ); + }); + it('does not double-decode Trojan path query values before extracting early data', function () { const proxy = parseOne( `trojan://trojan-pass@trojan-ws.example.com?type=ws&host=ws.example.com&path=${encodeURIComponent(