Skip to content

Commit

Permalink
feat: 支持 Trojan, VMess, VLESS httpupgrade(暂不支持 Shadowsocks v2ray-plugin)
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed May 10, 2024
1 parent f970ea3 commit d073dfe
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 7 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.307",
"version": "2.14.308",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
24 changes: 22 additions & 2 deletions backend/src/core/proxy-utils/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Base64 } from 'js-base64';
// Parse SS URI format (only supports new SIP002, legacy format is depreciated).
// reference: https://github.com/shadowsocks/shadowsocks-org/wiki/SIP002-URI-Scheme
function URI_SS() {
// TODO: 暂不支持 httpupgrade
const name = 'URI SS Parser';
const test = (line) => {
return /^ss:\/\//.test(line);
Expand Down Expand Up @@ -299,6 +300,7 @@ function URI_VMess() {
if (proxy.tls && proxy.sni) {
proxy.sni = params.sni;
}
let httpupgrade = false;
// handle obfs
if (params.net === 'ws' || params.obfs === 'websocket') {
proxy.network = 'ws';
Expand All @@ -309,6 +311,12 @@ function URI_VMess() {
proxy.network = 'http';
} else if (['grpc'].includes(params.net)) {
proxy.network = 'grpc';
} else if (
params.net === 'httpupgrade' ||
proxy.network === 'httpupgrade'
) {
proxy.network = 'ws';
httpupgrade = true;
}
if (proxy.network) {
let transportHost = params.host ?? params.obfsParam;
Expand Down Expand Up @@ -341,10 +349,15 @@ function URI_VMess() {
'_grpc-type': getIfNotBlank(params.type),
};
} else {
proxy[`${proxy.network}-opts`] = {
const opts = {
path: getIfNotBlank(transportPath),
headers: { Host: getIfNotBlank(transportHost) },
};
if (httpupgrade) {
opts['v2ray-http-upgrade'] = true;
opts['v2ray-http-upgrade-fast-open'] = true;
}
proxy[`${proxy.network}-opts`] = opts;
}
} else {
delete proxy.network;
Expand Down Expand Up @@ -444,10 +457,13 @@ function URI_VLESS() {
proxy[`${params.security}-opts`] = opts;
}
}

let httpupgrade = false;
proxy.network = params.type;
if (proxy.network === 'tcp' && params.headerType === 'http') {
proxy.network = 'http';
} else if (proxy.network === 'httpupgrade') {
proxy.network = 'ws';
httpupgrade = true;
}
if (!proxy.network && isShadowrocket && params.obfs) {
proxy.network = params.obfs;
Expand Down Expand Up @@ -485,6 +501,10 @@ function URI_VLESS() {
if (['grpc'].includes(proxy.network)) {
opts['_grpc-type'] = params.mode || 'gun';
}
if (httpupgrade) {
opts['v2ray-http-upgrade'] = true;
opts['v2ray-http-upgrade-fast-open'] = true;
}
if (Object.keys(opts).length > 0) {
proxy[`${proxy.network}-opts`] = opts;
}
Expand Down
9 changes: 9 additions & 0 deletions backend/src/core/proxy-utils/parsers/peggy/trojan-uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ params = "?" head:param tail:("&"@param)* {
}
if (params["type"]) {
let httpupgrade
proxy.network = params["type"]
if(proxy.network === 'httpupgrade') {
proxy.network = 'ws'
httpupgrade = true
}
if (['grpc'].includes(proxy.network)) {
proxy[proxy.network + '-opts'] = {
'grpc-service-name': params["serviceName"],
Expand All @@ -102,6 +107,10 @@ params = "?" head:param tail:("&"@param)* {
if (params["host"]) {
$set(proxy, proxy.network+"-opts.headers.Host", decodeURIComponent(params["host"]));
}
if (httpupgrade) {
$set(proxy, proxy.network+"-opts.v2ray-http-upgrade", true);
$set(proxy, proxy.network+"-opts.v2ray-http-upgrade-fast-open", true);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions backend/src/core/proxy-utils/parsers/peggy/trojan-uri.peg
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ params = "?" head:param tail:("&"@param)* {
}

if (params["type"]) {
let httpupgrade
proxy.network = params["type"]
if(proxy.network === 'httpupgrade') {
proxy.network = 'ws'
httpupgrade = true
}
if (['grpc'].includes(proxy.network)) {
proxy[proxy.network + '-opts'] = {
'grpc-service-name': params["serviceName"],
Expand All @@ -100,6 +105,10 @@ params = "?" head:param tail:("&"@param)* {
if (params["host"]) {
$set(proxy, proxy.network+"-opts.headers.Host", decodeURIComponent(params["host"]));
}
if (httpupgrade) {
$set(proxy, proxy.network+"-opts.v2ray-http-upgrade", true);
$set(proxy, proxy.network+"-opts.v2ray-http-upgrade-fast-open", true);
}
}
}

Expand Down
26 changes: 22 additions & 4 deletions backend/src/core/proxy-utils/producers/uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export default function URI_Producer() {
if (proxy.network === 'http') {
net = 'tcp';
type = 'http';
} else if (
proxy.network === 'ws' &&
proxy['ws-opts']?.['v2ray-http-upgrade']
) {
net = 'httpupgrade';
}
result = {
v: '2',
Expand Down Expand Up @@ -172,9 +177,15 @@ export default function URI_Producer() {
if (proxy.flow) {
flow = `&flow=${encodeURIComponent(proxy.flow)}`;
}
let vlessTransport = `&type=${encodeURIComponent(
proxy.network,
)}`;
let vlessType = proxy.network;
if (
proxy.network === 'ws' &&
proxy['ws-opts']?.['v2ray-http-upgrade']
) {
vlessType = 'httpupgrade';
}

let vlessTransport = `&type=${encodeURIComponent(vlessType)}`;
if (['grpc'].includes(proxy.network)) {
// https://github.com/XTLS/Xray-core/issues/91
vlessTransport += `&mode=${encodeURIComponent(
Expand Down Expand Up @@ -220,7 +231,14 @@ export default function URI_Producer() {
case 'trojan':
let trojanTransport = '';
if (proxy.network) {
trojanTransport = `&type=${proxy.network}`;
let trojanType = proxy.network;
if (
proxy.network === 'ws' &&
proxy['ws-opts']?.['v2ray-http-upgrade']
) {
trojanType = 'httpupgrade';
}
trojanTransport = `&type=${encodeURIComponent(trojanType)}`;
if (['grpc'].includes(proxy.network)) {
let trojanTransportServiceName =
proxy[`${proxy.network}-opts`]?.[
Expand Down

0 comments on commit d073dfe

Please sign in to comment.