feat: Loon XHTTP alpn 逻辑优化

This commit is contained in:
xream committed 2026-12-01 17:53:40 +08:00
1 parent 08b272c55d
commit fbd4230c99
5 files changed
+112 -37

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.44.35",
"version": "2.44.36",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
"main": "src/main.js",
"packageManager": "pnpm@11.0.9",
@@ -1,4 +1,5 @@
import _ from 'lodash';
import { isNotBlank } from '@/utils';
import {
normalizeXhttpIntegerValue,
normalizeXhttpNonNegativeRange,
@@ -71,6 +72,19 @@ export const LOON_XHTTP_FIELDS = {
),
};
export function getLoonXhttpAlpnSettings(endpoint, isDownload = false) {
if (isNotBlank(endpoint['reality-opts']?.['public-key'])) {
return { defaultAlpn: 'h2', priority: ['h2'] };
}
if (
(!isDownload && endpoint.type === 'trojan') ||
[true, 'true'].includes(endpoint.tls)
) {
return { defaultAlpn: 'h2', priority: ['h3', 'h2', 'http/1.1'] };
}
return { defaultAlpn: 'http/1.1', priority: ['http/1.1'] };
}
export function validateLoonXhttpXmux(opts) {
if (!opts) return;
@@ -144,6 +158,8 @@ export function setLoonXhttpOption(opts, key, value) {
value = normalizeXhttpIntegerValue(value) ?? value;
break;
case 'alpn':
value = value.trim();
if (!value) return;
value = [value];
break;
}
@@ -1,7 +1,7 @@
import { Buffer } from 'buffer';
import peggy from 'peggy';
import { getLoonGrpcSettings, LOON_TROJAN_GRPC_H2C } from '../../loon-grpc';
import { setLoonXhttpOption } from '../../loon-xhttp';
import { getLoonXhttpAlpnSettings, setLoonXhttpOption } from '../../loon-xhttp';
const grammars = String.raw`
// global initializer
@@ -62,11 +62,13 @@ const grammars = String.raw`
}
proxy.network = "xhttp";
proxy.udp ??= false;
proxy.alpn ??= [options.getLoonXhttpAlpnSettings(proxy).defaultAlpn];
if (transport.path != null) xhttpOpts.path = transport.path;
if (transport.host != null) xhttpOpts.host = transport.host;
const download = xhttpOpts["download-settings"];
if (download) {
download.tls ??= Boolean(download["reality-opts"]?.["public-key"]);
download.alpn ??= [options.getLoonXhttpAlpnSettings(download, true).defaultAlpn];
const fingerprint = loonClientFingerprint(download._loon_tls_profile);
if (fingerprint) download["client-fingerprint"] = fingerprint;
}
@@ -405,6 +407,7 @@ export default function getParser() {
generated.parse(input, {
getLoonGrpcSettings,
LOON_TROJAN_GRPC_H2C,
getLoonXhttpAlpnSettings,
setLoonXhttpOption,
decodeGeckoPassword,
}),
+13 -19
View File
@@ -6,7 +6,11 @@ import { isPresent, Result } from './utils';
import { getIfNotBlank, isIPv4, isIPv6, isPlainObject } from '@/utils';
import $ from '@/core/app';
import { getLoonGrpcSettings } from '../loon-grpc';
import { LOON_XHTTP_FIELDS, validateLoonXhttpXmux } from '../loon-xhttp';
import {
getLoonXhttpAlpnSettings,
LOON_XHTTP_FIELDS,
validateLoonXhttpXmux,
} from '../loon-xhttp';
import { validateMieruTrafficPattern } from '../mieru-traffic-pattern';
import { formatLoonVmessSecurity } from '../vmess-security';
import { normalizeXhttpIntegerValue } from '../xhttp-utils';
@@ -175,7 +179,7 @@ function appendXhttp(result, proxy) {
server: server.trim(),
port,
host: getXhttpHost(download),
alpn: alpn || undefined,
alpn,
servername: download.servername ?? download.sni,
fingerprint: hasReality ? undefined : fingerprint,
'tls-pubkey-sha256': hasReality ? undefined : pubkeyFingerprint,
@@ -276,26 +280,16 @@ function getLoonAlpn(proxy) {
}
function getLoonXhttpAlpn(endpoint, isDownload = false) {
const alpn = getLoonAlpn(endpoint);
if (!alpn) return alpn;
if (alpn.includes(',')) {
const scope = isDownload ? ' download' : '';
throw new Error(`Loon XHTTP${scope} supports only a single ALPN`);
}
const value = getLoonAlpn(endpoint);
if (!value) return;
const hasReality = Boolean(endpoint['reality-opts']?.['public-key']);
const hasTls =
(!isDownload && endpoint.type === 'trojan') ||
[true, 'true'].includes(endpoint.tls);
const allowed = hasReality
? ['h2']
: hasTls
? ['http/1.1', 'h2', 'h3']
: ['http/1.1'];
if (!allowed.includes(alpn)) {
const candidates = value.split(',').map((item) => item.trim());
const { priority } = getLoonXhttpAlpnSettings(endpoint, isDownload);
const alpn = priority.find((protocol) => candidates.includes(protocol));
if (!alpn) {
const field = isDownload ? 'xhttp-download-alpn' : 'XHTTP alpn';
throw new Error(
`Loon ${field} must be ${allowed.join(
`Loon ${field} must be ${priority.join(
' or ',
)} for the configured security`,
);
+78 -16
View File
@@ -1419,7 +1419,34 @@ describe('Proxy text producers', function () {
);
});
it('enforces Loon XHTTP main ALPN by security', function () {
it('defaults Loon XHTTP ALPN independently for upload and download', function () {
const vless = `Defaults=vless,example.com,443,"${UUID}",transport=xhttp`;
const trojan =
'Defaults=trojan,example.com,443,"secret",transport=xhttp';
const download =
',xhttp-download-server=down.example.com,xhttp-download-port=8443';
for (const [input, uploadAlpn, downloadAlpn] of [
[`${vless},xhttp-download-over-tls=true`, 'http/1.1', 'h2'],
[`${vless},over-tls=true`, 'h2', 'http/1.1'],
[
`${vless},public-key=up-key,xhttp-download-public-key=down-key`,
'h2',
'h2',
],
[`${trojan},alpn="",xhttp-download-alpn=""`, 'h2', 'http/1.1'],
]) {
const [proxy] = ProxyUtils.parse(input + download);
expectSubset(proxy, {
alpn: [uploadAlpn],
'xhttp-opts': { 'download-settings': { alpn: [downloadAlpn] } },
});
expect(
ProxyUtils.parse(produceExternal('Loon', proxy)),
).to.deep.equal([proxy]);
}
});
it('selects Loon XHTTP main ALPN by security', function () {
const proxy = {
type: 'vless',
name: 'Main',
@@ -1435,7 +1462,12 @@ describe('Proxy text producers', function () {
[{ type: 'vmess', alpn: ['h3'] }, 'XHTTP alpn must be http/1.1'],
[{ ...reality, tls: true, alpn: ['h3'] }, 'XHTTP alpn must be h2'],
[{ ...trojan, ...reality, alpn: ['h3'] }, 'XHTTP alpn must be h2'],
[{ tls: true, alpn: ['h2', 'h3'] }, 'only a single ALPN'],
[{ tls: false, alpn: ['h3', 'h2'] }, 'XHTTP alpn must be http/1.1'],
[{ ...reality, alpn: ['http/1.1', 'h3'] }, 'XHTTP alpn must be h2'],
[
{ tls: true, alpn: ['h4', 'unknown'] },
'XHTTP alpn must be h3 or h2 or http/1.1',
],
]) {
const { result, errors } = captureErrors(() =>
produceInternal('Loon', { ...proxy, ...settings }),
@@ -1447,9 +1479,17 @@ describe('Proxy text producers', function () {
expect(
produceExternal('Loon', { ...proxy, tls: false }),
).not.to.include(',alpn=');
expect(
produceExternal('Loon', { ...proxy, ...trojan, alpn: ['h3'] }),
).to.include(',alpn="h3"');
for (const [settings, alpn] of [
[{ tls: false, alpn: ['h3', 'http/1.1'] }, 'http/1.1'],
[{ tls: true, alpn: ['http/1.1', 'h2', 'h3'] }, 'h3'],
[{ tls: true, alpn: 'http/1.1, h2, unknown' }, 'h2'],
[{ ...reality, tls: true, alpn: ['h3', 'h2'] }, 'h2'],
[{ ...trojan, alpn: ['h2', 'h3'] }, 'h3'],
]) {
expect(
produceExternal('Loon', { ...proxy, ...settings }),
).to.include(`,alpn="${alpn}"`);
}
});
it('rejects incomplete REALITY for Loon XHTTP downloads', function () {
@@ -1497,18 +1537,24 @@ describe('Proxy text producers', function () {
for (const [settings, reason] of [
[{ ...reality, tls: false }, realityConflict],
[{ ...reality, tls: 'false' }, realityConflict],
[{ alpn: ['h2', 'http/1.1'] }, 'a single ALPN'],
[{ alpn: ['h2,http/1.1'] }, 'a single ALPN'],
[{ alpn: 'h2,http/1.1' }, 'a single ALPN'],
[{ alpn: ['h2', 'h3'] }, 'xhttp-download-alpn must be http/1.1'],
[{ alpn: ['h2'] }, 'xhttp-download-alpn must be http/1.1'],
[
{ tls: false, alpn: ['h3'] },
'xhttp-download-alpn must be http/1.1',
],
[{ ...reality, alpn: ['h3'] }, 'xhttp-download-alpn must be h2'],
[
{ ...reality, alpn: ['h3', 'http/1.1'] },
'xhttp-download-alpn must be h2',
],
[
{ tls: true, alpn: ['unknown'] },
'xhttp-download-alpn must be http/1.1 or h2 or h3',
'xhttp-download-alpn must be h3 or h2 or http/1.1',
],
[
{ tls: true, alpn: ['h4', 'unknown'] },
'xhttp-download-alpn must be h3 or h2 or http/1.1',
],
[{ ...reality, 'skip-cert-verify': true }, certificateConflict],
[{ ...reality, 'skip-cert-verify': 'true' }, certificateConflict],
@@ -1529,15 +1575,31 @@ describe('Proxy text producers', function () {
expect(errors[0]).to.include(reason);
}
for (const settings of [
{ alpn: ['http/1.1'] },
{ tls: true, alpn: ['http/1.1'] },
{ tls: true, alpn: ['h2'] },
{ tls: 'true', alpn: ['h3'] },
{ ...reality, alpn: ['h2'] },
for (const [settings, alpn] of [
[{ alpn: ['http/1.1'] }, 'http/1.1'],
[{ alpn: ['h2', 'http/1.1'] }, 'http/1.1'],
[{ alpn: ['h2, http/1.1'] }, 'http/1.1'],
[{ alpn: 'h2, http/1.1' }, 'http/1.1'],
[{ tls: true, alpn: ['http/1.1'] }, 'http/1.1'],
[{ tls: true, alpn: ['http/1.1', 'h2'] }, 'h2'],
[{ tls: 'true', alpn: ['http/1.1', 'h2', 'h3'] }, 'h3'],
[{ ...reality, alpn: ['h3', 'h2'] }, 'h2'],
]) {
expect(produceExternal('Loon', makeProxy(settings))).to.include(
`,xhttp-download-alpn=${settings.alpn[0]}`,
`,xhttp-download-alpn=${alpn}`,
);
}
for (const settings of [{}, { tls: true }, reality]) {
const [proxy] = ProxyUtils.parse(
JSON.stringify(makeProxy(settings)),
);
expect(proxy).not.to.have.property('alpn');
expect(
proxy['xhttp-opts']['download-settings'],
).not.to.have.property('alpn');
expect(produceExternal('Loon', proxy)).not.to.match(
/,(?:alpn|xhttp-download-alpn)=/,
);
}