Skip to content

Commit

Permalink
fix: trojan uri and tls
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Aug 24, 2023
1 parent 497bc26 commit 1f50575
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 14 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.21",
"version": "2.14.22",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
29 changes: 23 additions & 6 deletions backend/src/core/proxy-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ 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);
proxies.push(lastParse(proxy));
success = true;
}
}
Expand All @@ -50,7 +46,7 @@ function parse(raw) {
for (const parser of PROXY_PARSERS) {
const [proxy, error] = tryParse(parser, line);
if (!error) {
proxies.push(proxy);
proxies.push(lastParse(proxy));
lastParser = parser;
success = true;
$.info(`${parser.name} is activated`);
Expand Down Expand Up @@ -187,6 +183,27 @@ function safeMatch(parser, line) {
}
}

function lastParse(proxy) {
if (proxy.type === 'trojan') {
proxy.tls = true;
}
if (proxy.tls && !proxy.sni) {
if (proxy.network) {
let transportHost = proxy[`${proxy.network}-opts`]?.headers?.Host;
transportHost = Array.isArray(transportHost)
? transportHost[0]
: transportHost;
if (transportHost) {
proxy.sni = transportHost;
}
}
if (!proxy.sni && !isIP(proxy.server)) {
proxy.sni = proxy.server;
}
}
return proxy;
}

function isIP(ip) {
return isIPv4(ip) || isIPv6(ip);
}
11 changes: 10 additions & 1 deletion backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,23 @@ port = digits:[0-9]+ {
}
}
params = "?" head:param tail:("&"@param)* {
params = "/"? "?" head:param tail:("&"@param)* {
proxy["skip-cert-verify"] = toBool(params["allowInsecure"]);
proxy.sni = params["sni"] || params["peer"];
if (toBool(params["ws"])) {
proxy.network = "ws";
$set(proxy, "ws-opts.path", params["wspath"]);
}
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"]));
}
}
proxy.udp = toBool(params["udp"]);
proxy.tfo = toBool(params["tfo"]);
Expand Down
12 changes: 11 additions & 1 deletion backend/src/core/proxy-utils/parsers/peggy/trojan-uri.peg
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,24 @@ port = digits:[0-9]+ {
}
}

params = "?" head:param tail:("&"@param)* {
params = "/"? "?" head:param tail:("&"@param)* {
proxy["skip-cert-verify"] = toBool(params["allowInsecure"]);
proxy.sni = params["sni"] || params["peer"];

if (toBool(params["ws"])) {
proxy.network = "ws";
$set(proxy, "ws-opts.path", params["wspath"]);
}

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"]));
}
}

proxy.udp = toBool(params["udp"]);
proxy.tfo = toBool(params["tfo"]);
Expand Down
12 changes: 8 additions & 4 deletions backend/src/core/proxy-utils/producers/qx.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,18 @@ function vmess(proxy) {
} else {
throw new Error(`network ${proxy.network} is unsupported`);
}
let httpPath = proxy[`${proxy.network}-opts`]?.path;
let httpHost = proxy[`${proxy.network}-opts`]?.headers?.Host;
let transportPath = proxy[`${proxy.network}-opts`]?.path;
let transportHost = proxy[`${proxy.network}-opts`]?.headers?.Host;
appendIfPresent(
`,obfs-uri=${Array.isArray(httpPath) ? httpPath[0] : httpPath}`,
`,obfs-uri=${
Array.isArray(transportPath) ? transportPath[0] : transportPath
}`,
`${proxy.network}-opts.path`,
);
appendIfPresent(
`,obfs-host=${Array.isArray(httpHost) ? httpHost[0] : httpHost}`,
`,obfs-host=${
Array.isArray(transportHost) ? transportHost[0] : transportHost
}`,
`${proxy.network}-opts.headers.Host`,
);
} else {
Expand Down
23 changes: 22 additions & 1 deletion backend/src/core/proxy-utils/producers/uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,32 @@ export default function URI_Producer() {
result = 'vmess://' + Base64.encode(JSON.stringify(result));
break;
case 'trojan':
let transport = '';
if (proxy.network) {
transport = `&type=${proxy.network}`;
let transportPath = proxy[`${proxy.network}-opts`]?.path;
let transportHost =
proxy[`${proxy.network}-opts`]?.headers?.Host;
if (transportPath) {
transport += `&path=${encodeURIComponent(
Array.isArray(transportPath)
? transportPath[0]
: transportPath,
)}`;
}
if (transportHost) {
transport += `&host=${encodeURIComponent(
Array.isArray(transportHost)
? transportHost[0]
: transportHost,
)}`;
}
}
result = `trojan://${proxy.password}@${proxy.server}:${
proxy.port
}?sni=${encodeURIComponent(proxy.sni || proxy.server)}${
proxy['skip-cert-verify'] ? '&allowInsecure=1' : ''
}#${encodeURIComponent(proxy.name)}`;
}${transport}#${encodeURIComponent(proxy.name)}`;
break;
}
return result;
Expand Down

0 comments on commit 1f50575

Please sign in to comment.