Merge pull request #662: fix Trojan URI names without query strings

Preserve the URI fragment before detecting or adding the default Trojan port.
This commit is contained in:
xream authored and GitHub committed 2026-10-25 09:44:31 +08:00
commit 0c6f8f0e87
2 files changed
+38 -3

No files matched your search

@@ -2519,12 +2519,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)) {
@@ -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(