feat: Surge VMess/Trojan/VLESS 支持 HTTPUpgrade

This commit is contained in:
xream committed 2026-11-14 23:53:57 +08:00
1 parent a9028cdd3c
commit 2e7a63c6f4
5 files changed
+75 -18

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.44.13",
"version": "2.44.14",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
"main": "src/main.js",
"packageManager": "pnpm@11.0.9",
@@ -30,6 +30,7 @@ const grammars = String.raw`
proxy.network = "ws";
$set(proxy, "ws-opts.path", obfs.path);
$set(proxy, "ws-opts.headers", obfs['ws-headers']);
if (obfs.httpUpgrade) $set(proxy, "ws-opts.v2ray-http-upgrade", true);
if (proxy['ws-opts'] && proxy['ws-opts']['headers'] && proxy['ws-opts']['headers'].Host) {
proxy['ws-opts']['headers'].Host = proxy['ws-opts']['headers'].Host.replace(/^"(.*)"$/, '$1')
}
@@ -388,7 +389,7 @@ shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/
}
handleShadowTLS();
}
vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_path/ws_headers/vmess_method/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* {
vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_http_upgrade/ws_path/ws_headers/vmess_method/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* {
proxy.type = "vmess";
proxy.cipher = proxy.cipher || "auto";
// Surfboard 与 Surge 默认不一致, 不管 Surfboard https://getsurfboard.com/docs/profile-format/proxy/external-proxy/vmess
@@ -400,7 +401,7 @@ vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_path/ws_headers/
handleWebsocket();
handleShadowTLS();
}
vless = tag equals "vless" address (vless_username/vless_option/reality_option/ws/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/xhttp/xhttp_headers/xhttp_option/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* {
vless = tag equals "vless" address (vless_username/vless_option/reality_option/ws/ws_http_upgrade/ws_path/ws_headers/grpc/grpc_service_name/grpc_mode/grpc_authority/xhttp/xhttp_headers/xhttp_option/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/fast_open/tfo/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* {
proxy.type = "vless";
proxy["packet-encoding"] = "xudp";
if (proxy["reality-opts"] && proxy["reality-opts"]["public-key"]) proxy.tls = true;
@@ -412,7 +413,7 @@ vless = tag equals "vless" address (vless_username/vless_option/reality_option/w
}
handleShadowTLS();
}
trojan = tag equals "trojan" address (passwordk/ws/ws_path/ws_headers/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* {
trojan = tag equals "trojan" address (passwordk/ws/ws_http_upgrade/ws_path/ws_headers/tls/sni/cert_verify_name/alpn/tls_fingerprint/tls_verification/client_cert/ip_version/underlying_proxy/tos/allow_other_interface/interface/test_url/test_udp/test_timeout/hybrid/no_error_alert/fast_open/tfo/udp_relay/shadow_tls_version/shadow_tls_sni/shadow_tls_password/block_quic/others)* {
proxy.type = "trojan";
handleWebsocket();
handleShadowTLS();
@@ -589,6 +590,7 @@ vmess_method = comma "encrypt-method" equals cipher:$[^,]+ {
cipher = ("aes-128-cfb"/"aes-128-ctr"/"aes-128-gcm"/"aes-192-cfb"/"aes-192-ctr"/"aes-192-gcm"/"aes-256-cfb"/"aes-256-ctr"/"aes-256-gcm"/"bf-cfb"/"camellia-128-cfb"/"camellia-192-cfb"/"camellia-256-cfb"/"cast5-cfb"/"chacha20-ietf-poly1305"/"chacha20-ietf"/"chacha20-poly1305"/"chacha20"/"des-cfb"/"idea-cfb"/"none"/"rc2-cfb"/"rc4-md5"/"rc4"/"salsa20"/"seed-cfb"/"xchacha20-ietf-poly1305"/"2022-blake3-aes-128-gcm"/"2022-blake3-aes-256-gcm");
ws = comma "ws" equals flag:bool { if (flag) obfs.type = "ws"; }
ws_http_upgrade = comma "ws-http-upgrade" equals flag:bool { obfs.httpUpgrade = flag; }
ws_headers = comma "ws-headers" equals value:pipe_headers { obfs["ws-headers"] = value; }
pipe_headers = & {
const start = peg$currPos;
@@ -166,8 +166,9 @@ function isUnsupportedSnellV6Obfs(proxy) {
export default function Surge_Producer() {
const produce = (proxy, type, opts = {}) => {
if (
['ws'].includes(proxy.network) &&
proxy['ws-opts']?.['v2ray-http-upgrade']
proxy.network === 'ws' &&
proxy['ws-opts']?.['v2ray-http-upgrade'] &&
!['vmess', 'trojan', 'vless'].includes(proxy.type)
) {
throw unsupported(
`Platform ${targetPlatform} does not support network ${proxy.network} with http upgrade`,
@@ -1436,6 +1437,9 @@ function handleTransport(result, proxy, includeUnsupportedProxy) {
if (proxy.network === 'ws') {
result.append(`,ws=true`);
if (isPresent(proxy, 'ws-opts')) {
if (proxy['ws-opts']['v2ray-http-upgrade']) {
result.append(',ws-http-upgrade=true');
}
result.appendIfPresent(
`,ws-path=${proxy['ws-opts'].path}`,
'ws-opts.path',
+50 -2
View File
@@ -2393,6 +2393,42 @@ describe('Proxy text producers', function () {
);
});
it('round-trips Surge HTTPUpgrade for VMess, Trojan and VLESS', function () {
for (const [type, credentials] of [
['vmess', `username=${UUID},vmess-aead=true`],
['trojan', 'password=test-password'],
['vless', `username=${UUID}`],
]) {
const prefix = `Upgrade = ${type}, example.com, 443, ${credentials}, tls=true`;
const [proxy] = ProxyUtils.parse(
`${prefix}, ws-http-upgrade=true, ws=true, ws-path=/upgrade?x=1, ws-headers=Host:cdn.example.com`,
);
const expected = {
type,
network: 'ws',
tls: true,
'ws-opts': {
'v2ray-http-upgrade': true,
path: '/upgrade?x=1',
headers: { Host: 'cdn.example.com' },
},
};
expectSubset(proxy, expected);
expectSubset(
ProxyUtils.parse(produceExternal('URI', proxy))[0],
expected,
);
for (const target of ['Surge', 'SurgeMac']) {
const output = produceExternal(target, proxy, {
useMihomoExternal: true,
});
expect(output).to.include(',ws=true,ws-http-upgrade=true,');
expectSubset(ProxyUtils.parse(output)[0], expected);
}
}
});
it('round-trips Surge VLESS with RAW, WebSocket, gRPC and REALITY', function () {
const cases = [
[
@@ -2711,6 +2747,18 @@ describe('Proxy text producers', function () {
expect(output).not.to.include('grpc-service-name=');
});
it('keeps ws-http-upgrade disabled unless both Surge options are true', function () {
const prefix = `Upgrade=vless,example.com,443,username=${UUID}`;
const [tcp, ws] = ProxyUtils.parse(
`${prefix},ws=false,ws-http-upgrade=true\n${prefix},ws=true,ws-http-upgrade=false`,
);
expect(tcp.network).to.equal('tcp');
expect(ws.network).to.equal('ws');
expect(produceExternal('Surge', [tcp, ws])).not.to.include(
'ws-http-upgrade=',
);
});
it('wraps unsupported SurgeMac VLESS transports with Mihomo external mode when requested', function () {
const output = produceExternal(
'SurgeMac',
@@ -2721,8 +2769,8 @@ describe('Proxy text producers', function () {
port: 443,
uuid: UUID,
tls: true,
network: 'ws',
'ws-opts': { path: '/upgrade', 'v2ray-http-upgrade': true },
network: 'h2',
'h2-opts': { path: '/edge' },
},
{ useMihomoExternal: true, localPort: 16666 },
);
+13 -10
View File
@@ -7,6 +7,7 @@ const UUID = '11111111-1111-4111-8111-111111111111';
const VLESS_WS = `vless://${UUID}@1.1.1.1:443?security=tls&type=ws&host=cdn.example.com&path=%2Fws&sni=sni.example.com#VLESS%20WS`;
const VLESS_XHTTP = `vless://${UUID}@192.0.2.1:443?security=tls&type=xhttp&path=%2Fxhttp&sni=sni.example.com#VLESS%20XHTTP`;
const VLESS_HTTP_UPGRADE = `vless://${UUID}@192.0.2.2:443?security=tls&type=httpupgrade&path=%2Fupgrade&sni=sni.example.com#VLESS%20HTTPUpgrade`;
const VLESS_H2 = `vless://${UUID}@192.0.2.3:443?security=tls&type=http&path=%2Fh2&sni=sni.example.com#VLESS%20H2`;
let $;
let openApi;
@@ -586,6 +587,7 @@ describe('download routes', function () {
VLESS_WS,
VLESS_XHTTP,
VLESS_HTTP_UPGRADE,
VLESS_H2,
].join('\n');
const output = await downloadSubscription({ target: 'SurgeMac' });
@@ -594,43 +596,44 @@ describe('download routes', function () {
`VLESS XHTTP=vless,192.0.2.1,443,username=${UUID},xhttp=true,xhttp-path=/xhttp,tls=true`,
);
expect(output).to.include(
'VLESS HTTPUpgrade=external,exec="/usr/local/bin/mihomo"',
`VLESS HTTPUpgrade=vless,192.0.2.2,443,username=${UUID},ws=true,ws-http-upgrade=true,ws-path=/upgrade,tls=true`,
);
expect(output).to.not.include('VLESS HTTPUpgrade=socks5');
expect(output).to.include('VLESS H2=external,exec="/usr/local/bin/mihomo"');
expect(output).to.not.include('VLESS H2=socks5');
expect(output).to.not.include('mihomo merged=external');
});
it('enables merged Mihomo external output when the query parameter is present', async function () {
state[SUBS_KEY][0].content = VLESS_HTTP_UPGRADE;
state[SUBS_KEY][0].content = VLESS_H2;
const output = await downloadSubscription({
target: 'SurgeMac',
mihomoMerge: 'true',
mihomoMergeName: 'Shared Mihomo',
});
expect(output).to.include('VLESS HTTPUpgrade=socks5,127.0.0.1,65535');
expect(output).to.include('VLESS H2=socks5,127.0.0.1,65535');
expect(output).to.include(
'Shared Mihomo=external,exec="/usr/local/bin/mihomo"',
);
expect(output).to.not.include(
'VLESS HTTPUpgrade=external,exec="/usr/local/bin/mihomo"',
'VLESS H2=external,exec="/usr/local/bin/mihomo"',
);
});
it('treats mihomoMerge=false as enabled because query flags are presence-based', async function () {
state[SUBS_KEY][0].content = VLESS_HTTP_UPGRADE;
state[SUBS_KEY][0].content = VLESS_H2;
const output = await downloadSubscription({
target: 'SurgeMac',
mihomoMerge: 'false',
mihomoMergeName: 'Shared Mihomo',
});
expect(output).to.include('VLESS HTTPUpgrade=socks5,127.0.0.1,65535');
expect(output).to.include('VLESS H2=socks5,127.0.0.1,65535');
expect(output).to.include(
'Shared Mihomo=external,exec="/usr/local/bin/mihomo"',
);
expect(output).to.not.include(
'VLESS HTTPUpgrade=external,exec="/usr/local/bin/mihomo"',
'VLESS H2=external,exec="/usr/local/bin/mihomo"',
);
});
@@ -650,14 +653,14 @@ describe('download routes', function () {
});
it('enables merged Mihomo external output for collection downloads', async function () {
state[SUBS_KEY][0].content = VLESS_HTTP_UPGRADE;
state[SUBS_KEY][0].content = VLESS_H2;
const output = await downloadCollection({
target: 'SurgeMac',
mihomoMerge: 'true',
mihomoMergeName: 'Shared Mihomo',
});
expect(output).to.include('VLESS HTTPUpgrade=socks5,127.0.0.1,65535');
expect(output).to.include('VLESS H2=socks5,127.0.0.1,65535');
expect(output).to.include(
'Shared Mihomo=external,exec="/usr/local/bin/mihomo"',
);