From a86bbca900d022d2d436fa043bb0a8ec12974d52 Mon Sep 17 00:00:00 2001 From: xream Date: Sun, 22 Nov 2026 18:51:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20mieru=20URI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- backend/src/core/proxy-utils/index.js | 9 ++- backend/src/core/proxy-utils/parsers/index.js | 75 ++++++++++++++++++- backend/src/core/proxy-utils/producers/uri.js | 27 +++++++ .../src/core/proxy-utils/transport-path.js | 2 +- backend/src/test/proxy-parsers/uri.spec.js | 57 ++++++++++++++ backend/src/test/proxy-producers/text.spec.js | 38 ++++++++++ 7 files changed, 205 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a981cce7..fc2a02cc 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Push URLs may contain secrets: treat `SUB_STORE_PUSH_SERVICE` as privileged conf example: `socks5+tls://user:pass@ip:port#name` -- [x] URI(AnyTLS, SOCKS, SS, SSR, VMess, VLESS, Trojan, Hysteria, Hysteria 2, TUIC v5, WireGuard) +- [x] URI(AnyTLS, SOCKS, SS, SSR, VMess, VLESS, Trojan, Hysteria, Hysteria 2, TUIC v5, WireGuard, mieru) > Please note, HTTP(s) does not have a standard URI format, so it is not supported. Please use other formats. - [x] Clash Proxies YAML - [x] Clash Proxy JSON/JSON5/YAML(single line) diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index 223efcce..144ed462 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -69,6 +69,11 @@ function parse(raw) { // parse const lines = raw.split('\n'); const proxies = []; + const appendParsed = (parsed) => { + for (const proxy of Array.isArray(parsed) ? parsed : [parsed]) { + proxies.push(lastParse(proxy)); + } + }; let lastParser; for (let line of lines) { @@ -80,7 +85,7 @@ function parse(raw) { if (lastParser) { const [proxy, error] = tryParse(lastParser, line); if (!error) { - proxies.push(lastParse(proxy)); + appendParsed(proxy); success = true; } } @@ -90,7 +95,7 @@ function parse(raw) { for (const parser of PROXY_PARSERS) { const [proxy, error] = tryParse(parser, line); if (!error) { - proxies.push(lastParse(proxy)); + appendParsed(proxy); lastParser = parser; success = true; $.info(`${parser.name} is activated`); diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index cea6ad58..17f69818 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -25,7 +25,11 @@ import { normalizeXhttpStrictPositiveRangeString, normalizeXhttpStrictPositiveRangeValue, } from '../xhttp-utils'; -import { extractPathQueryParam, getPathQueryParam } from '../transport-path'; +import { + extractPathQueryParam, + getPathQueryParam, + splitQueryPart, +} from '../transport-path'; import { buildMihomoEchOptsFromXrayFields, isSupportedXrayEchConfigList, @@ -2191,6 +2195,74 @@ function URI_AnyTLS() { }; return { name, test, parse }; } +function URI_Mieru() { + const name = 'URI Mieru Parser'; + const test = (line) => /^mierus:\/\//i.test(line); + const parse = (line) => { + const { content, fragment } = splitURIFragment(line); + const match = + /^mierus:\/\/([^:@/?]+):([^@/?]+)@(\[[^\]]+\]|[^:/?@\s]+)\/?\?(.+)$/i.exec( + content, + ); + if (!match) throw new Error('Invalid Mieru URI'); + const [, username, password, server, query] = match; + + const params = new Map(); + for (const part of query.split('&')) { + const { key, value } = splitQueryPart(part); + if (!params.has(key)) params.set(key, []); + params.get(key).push(value); + } + const ports = params.get('port') || []; + const protocols = params.get('protocol') || []; + if (ports.length === 0 || ports.length !== protocols.length) { + throw new Error('Mieru URI requires matching ports and protocols'); + } + + const baseName = fragment || params.get('profile')?.[0] || server; + const proxy = { + type: 'mieru', + server, + username: decodeURIComponent(username), + password: decodeURIComponent(password), + }; + // shortcut: 只映射 mihomo 支持的字段,需要保留完整 Mieru profile 时再透传 MTU。 + for (const key of [ + 'multiplexing', + 'handshake-mode', + 'traffic-pattern', + ]) { + const value = params.get(key)?.[0]; + if (value) proxy[key] = value; + } + + return ports.map((port, index) => { + const transport = protocols[index]; + const [, first, last = first] = + /^(\d+)(?:-(\d+))?$/.exec(port) || []; + const start = Number(first); + const end = Number(last); + if ( + !(1 <= start && start <= end && end <= 65535) || + (transport !== 'TCP' && transport !== 'UDP') + ) { + throw new Error('Invalid Mieru port binding'); + } + const binding = { + ...proxy, + name: + ports.length === 1 + ? baseName + : `${baseName}:${port}/${transport}`, + transport, + }; + if (port.includes('-')) binding['port-range'] = port; + else binding.port = start; + return binding; + }); + }; + return { name, test, parse }; +} function URI_Hysteria2() { const name = 'URI Hysteria2 Parser'; const test = (line) => { @@ -3216,6 +3288,7 @@ export default [ URI_Hysteria2(), URI_Trojan(), URI_AnyTLS(), + URI_Mieru(), Clash_All(), Surge_Direct(), Surge_AnyTLS(), diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index f84e21ca..0f2a9523 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -1563,6 +1563,33 @@ export default function URI_Producer() { // proxy.name, // )}`; break; + case 'mieru': { + const port = proxy['port-range'] || proxy.port; + const username = encodeURIComponent(proxy.username ?? ''); + const password = encodeURIComponent(proxy.password ?? ''); + if (!proxy.server || !port || !username || !password) { + throw new Error( + 'Mieru URI requires a server, port and credentials', + ); + } + const params = [ + `port=${encodeURIComponent(port)}`, + `protocol=${encodeURIComponent(proxy.transport || 'TCP')}`, + `profile=${encodeURIComponent(proxy.name || proxy.server)}`, + ]; + for (const key of [ + 'multiplexing', + 'handshake-mode', + 'traffic-pattern', + ]) { + if (proxy[key]) + params.push(`${key}=${encodeURIComponent(proxy[key])}`); + } + result = `mierus://${username}:${password}@${ + proxy.server + }?${params.join('&')}`; + break; + } case 'wireguard': let wireguardParams = []; diff --git a/backend/src/core/proxy-utils/transport-path.js b/backend/src/core/proxy-utils/transport-path.js index 8b277752..359f2978 100644 --- a/backend/src/core/proxy-utils/transport-path.js +++ b/backend/src/core/proxy-utils/transport-path.js @@ -6,7 +6,7 @@ function decodeQueryComponent(value) { } } -function splitQueryPart(part) { +export function splitQueryPart(part) { const separatorIndex = part.indexOf('='); if (separatorIndex === -1) { return { diff --git a/backend/src/test/proxy-parsers/uri.spec.js b/backend/src/test/proxy-parsers/uri.spec.js index 3832fdab..640ad12b 100644 --- a/backend/src/test/proxy-parsers/uri.spec.js +++ b/backend/src/test/proxy-parsers/uri.spec.js @@ -441,6 +441,63 @@ describe('Proxy URI parser coverage', function () { expect(proxy.network).to.equal(undefined); }); + it('parses Mieru simple links with paired ports and protocols', function () { + const proxies = parseAll( + [ + 'mierus://user%3Aname:p%40ss@[2001:db8::1]?port=443&port=8000-8002&protocol=TCP&protocol=UDP&profile=Default&multiplexing=MULTIPLEXING_HIGH&handshake-mode=HANDSHAKE_NO_WAIT&traffic-pattern=CI%2B%2FAQ%3D%3D#Mieru%20Node', + 'mierus://user:pass@example.com?port=8443&port=8444&protocol=TCP&protocol=TCP&profile=Other+Node', + ].join('\n'), + ); + + expect(proxies.map((proxy) => proxy.name)).to.deep.equal([ + 'Mieru Node:443/TCP', + 'Mieru Node:8000-8002/UDP', + 'Other Node:8443/TCP', + 'Other Node:8444/TCP', + ]); + expectSubset(proxies[0], { + type: 'mieru', + server: '2001:db8::1', + username: 'user:name', + password: 'p@ss', + port: 443, + transport: 'TCP', + udp: true, + multiplexing: 'MULTIPLEXING_HIGH', + 'handshake-mode': 'HANDSHAKE_NO_WAIT', + 'traffic-pattern': 'CI+/AQ==', + }); + expectSubset(proxies[1], { + 'port-range': '8000-8002', + transport: 'UDP', + }); + expect(proxies[1]).not.to.have.property('port'); + }); + + it('rejects an entire Mieru link if any port binding is invalid', function () { + const before = + 'mierus://user:pass@example.com?port=1&protocol=TCP&profile=First'; + const after = + 'mierus://user:pass@example.com?port=65535-65535&protocol=UDP&profile=Last'; + + for (const [port, protocol] of [ + ['443-', 'TCP'], + ['0', 'TCP'], + ['65536', 'UDP'], + ['0-443', 'TCP'], + ['443-65536', 'UDP'], + ['444-443', 'TCP'], + ['443', 'QUIC'], + ]) { + const invalid = `mierus://user:pass@example.com?port=443&port=${port}&protocol=TCP&protocol=${protocol}`; + const proxies = parseAll([before, invalid, after].join('\n')); + expect( + proxies.map((proxy) => proxy.name), + `${port}/${protocol}`, + ).to.deep.equal(['First', 'Last']); + } + }); + it('parses Hysteria URIs with fallback SNI and throughput fields', function () { const proxy = parseOne( 'hy://hysteria.example.com:8443?auth=token&peer=sni.example.com&alpn=h3,h2&mport=2000,3000&obfsParam=mask&upmbps=10&downmbps=20&insecure=1#Hysteria', diff --git a/backend/src/test/proxy-producers/text.spec.js b/backend/src/test/proxy-producers/text.spec.js index 6d2f572b..17d4d168 100644 --- a/backend/src/test/proxy-producers/text.spec.js +++ b/backend/src/test/proxy-producers/text.spec.js @@ -7033,6 +7033,44 @@ describe('Proxy text producers', function () { }); } + it('round-trips Mieru through URI and V2Ray exports', function () { + const proxies = [ + { + type: 'mieru', + name: 'Mieru #1', + server: '2001:db8::1', + username: 'user:name', + password: 'p@ss:w/rd#1?', + 'port-range': '8000-8002', + transport: 'UDP', + multiplexing: 'MULTIPLEXING_HIGH', + 'handshake-mode': 'HANDSHAKE_NO_WAIT', + 'traffic-pattern': 'CI+/AQ==', + }, + { + type: 'mieru', + name: 'Mieru TCP', + server: 'example.com', + username: 'user', + password: 'pass', + port: 443, + }, + ]; + const uri = produceExternal('URI', proxies); + const v2ray = produceExternal('V2Ray', proxies); + + expect(uri.split('\n')).to.deep.equal([ + 'mierus://user%3Aname:p%40ss%3Aw%2Frd%231%3F@[2001:db8::1]?port=8000-8002&protocol=UDP&profile=Mieru%20%231&multiplexing=MULTIPLEXING_HIGH&handshake-mode=HANDSHAKE_NO_WAIT&traffic-pattern=CI%2B%2FAQ%3D%3D', + 'mierus://user:pass@example.com?port=443&protocol=TCP&profile=Mieru%20TCP', + ]); + expect(Base64.decode(v2ray)).to.equal(uri); + + const parsed = ProxyUtils.parse(v2ray); + expect(parsed).to.have.length(2); + expectSubset(parsed[0], proxies[0]); + expectSubset(parsed[1], { ...proxies[1], transport: 'TCP' }); + }); + it('keeps every h2 host in URI VMess links', function () { const output = produceExternal('URI', { type: 'vmess',