diff --git a/backend/package.json b/backend/package.json index 3f9ad8af..7e699502 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.20.73", + "version": "2.20.74", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/parsers/index.js b/backend/src/core/proxy-utils/parsers/index.js index eabeb56c..b2376d5b 100644 --- a/backend/src/core/proxy-utils/parsers/index.js +++ b/backend/src/core/proxy-utils/parsers/index.js @@ -14,6 +14,7 @@ import getTrojanURIParser from './peggy/trojan-uri'; import $ from '@/core/app'; import JSON5 from 'json5'; import YAML from '@/utils/yaml'; +import _ from 'lodash'; import { Base64 } from 'js-base64'; @@ -121,7 +122,6 @@ function URI_SOCKS() { // 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); @@ -174,6 +174,93 @@ function URI_SS() { const parsed = content.match(/(\?.*)$/); query = parsed[1]; } + const params = {}; + for (const addon of query.replace(/^\?/, '').split('&')) { + if (addon) { + const [key, valueRaw] = addon.split('='); + let value = valueRaw; + value = decodeURIComponent(valueRaw); + params[key] = value; + } + } + proxy.tls = params.security && params.security !== 'none'; + proxy['skip-cert-verify'] = !!params['allowInsecure']; + proxy.sni = params['sni'] || params['peer']; + proxy['client-fingerprint'] = params.fp; + proxy.alpn = params.alpn + ? decodeURIComponent(params.alpn).split(',') + : undefined; + + if (params['ws']) { + proxy.network = 'ws'; + _.set(proxy, 'ws-opts.path', params['wspath']); + } + + 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'], + '_grpc-type': params['mode'], + '_grpc-authority': params['authority'], + }; + } else { + 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']), + ); + } + if (httpupgrade) { + _.set( + proxy, + proxy.network + '-opts.v2ray-http-upgrade', + true, + ); + _.set( + proxy, + proxy.network + '-opts.v2ray-http-upgrade-fast-open', + true, + ); + } + } + if (['reality'].includes(params.security)) { + const opts = {}; + if (params.pbk) { + opts['public-key'] = params.pbk; + } + if (params.sid) { + opts['short-id'] = params.sid; + } + if (params.spx) { + opts['_spider-x'] = params.spx; + } + if (params.mode) { + proxy._mode = params.mode; + } + if (params.extra) { + proxy._extra = params.extra; + } + if (Object.keys(opts).length > 0) { + _.set(proxy, params.security + '-opts', opts); + } + } + } + + proxy.udp = !!params['udp']; const serverAndPort = serverAndPortArray[1]; const portIdx = serverAndPort.lastIndexOf(':'); diff --git a/backend/src/core/proxy-utils/producers/uri.js b/backend/src/core/proxy-utils/producers/uri.js index 7e687272..84f4be1b 100644 --- a/backend/src/core/proxy-utils/producers/uri.js +++ b/backend/src/core/proxy-utils/producers/uri.js @@ -174,26 +174,27 @@ export default function URI_Producer() { )}:${encodeURIComponent(proxy.password)}` : Base64.encode(userinfo) }@${proxy.server}:${proxy.port}${proxy.plugin ? '/' : ''}`; + let query = ''; if (proxy.plugin) { - result += '?plugin='; + query += '&plugin='; const opts = proxy['plugin-opts']; switch (proxy.plugin) { case 'obfs': - result += encodeURIComponent( + query += encodeURIComponent( `simple-obfs;obfs=${opts.mode}${ opts.host ? ';obfs-host=' + opts.host : '' }`, ); break; case 'v2ray-plugin': - result += encodeURIComponent( + query += encodeURIComponent( `v2ray-plugin;obfs=${opts.mode}${ opts.host ? ';obfs-host' + opts.host : '' }${opts.tls ? ';tls' : ''}`, ); break; case 'shadow-tls': - result += encodeURIComponent( + query += encodeURIComponent( `shadow-tls;host=${opts.host};password=${opts.password};version=${opts.version}`, ); break; @@ -204,14 +205,112 @@ export default function URI_Producer() { } } if (proxy['udp-over-tcp']) { - result = `${result}${proxy.plugin ? '&' : '?'}uot=1`; + query += '&uot=1'; } if (proxy.tfo) { - result = `${result}${ - proxy.plugin || proxy['udp-over-tcp'] ? '&' : '?' - }tfo=1`; + query += '&tfo=1'; } - result += `#${encodeURIComponent(proxy.name)}`; + let ssTransport = ''; + if (proxy.network) { + let ssType = proxy.network; + if ( + proxy.network === 'ws' && + proxy['ws-opts']?.['v2ray-http-upgrade'] + ) { + ssType = 'httpupgrade'; + } + ssTransport = `&type=${encodeURIComponent(ssType)}`; + if (['grpc'].includes(proxy.network)) { + let ssTransportServiceName = + proxy[`${proxy.network}-opts`]?.[ + `${proxy.network}-service-name` + ]; + let ssTransportAuthority = + proxy[`${proxy.network}-opts`]?.['_grpc-authority']; + if (ssTransportServiceName) { + ssTransport += `&serviceName=${encodeURIComponent( + ssTransportServiceName, + )}`; + } + if (ssTransportAuthority) { + ssTransport += `&authority=${encodeURIComponent( + ssTransportAuthority, + )}`; + } + ssTransport += `&mode=${encodeURIComponent( + proxy[`${proxy.network}-opts`]?.['_grpc-type'] || + 'gun', + )}`; + } + let ssTransportPath = proxy[`${proxy.network}-opts`]?.path; + let ssTransportHost = + proxy[`${proxy.network}-opts`]?.headers?.Host; + if (ssTransportPath) { + ssTransport += `&path=${encodeURIComponent( + Array.isArray(ssTransportPath) + ? ssTransportPath[0] + : ssTransportPath, + )}`; + } + if (ssTransportHost) { + ssTransport += `&host=${encodeURIComponent( + Array.isArray(ssTransportHost) + ? ssTransportHost[0] + : ssTransportHost, + )}`; + } + } + let ssFp = ''; + if (proxy['client-fingerprint']) { + ssFp = `&fp=${encodeURIComponent( + proxy['client-fingerprint'], + )}`; + } + let ssAlpn = ''; + if (proxy.alpn) { + ssAlpn = `&alpn=${encodeURIComponent( + Array.isArray(proxy.alpn) + ? proxy.alpn + : proxy.alpn.join(','), + )}`; + } + const ssIsReality = proxy['reality-opts']; + let ssSid = ''; + let ssPbk = ''; + let ssSpx = ''; + let ssSecurity = proxy.tls ? '&security=tls' : ''; + let ssMode = ''; + let ssExtra = ''; + if (ssIsReality) { + ssSecurity = `&security=reality`; + const publicKey = proxy['reality-opts']?.['public-key']; + if (publicKey) { + ssPbk = `&pbk=${encodeURIComponent(publicKey)}`; + } + const shortId = proxy['reality-opts']?.['short-id']; + if (shortId) { + ssSid = `&sid=${encodeURIComponent(shortId)}`; + } + const spiderX = proxy['reality-opts']?.['_spider-x']; + if (spiderX) { + ssSpx = `&spx=${encodeURIComponent(spiderX)}`; + } + if (proxy._extra) { + ssExtra = `&extra=${encodeURIComponent(proxy._extra)}`; + } + if (proxy._mode) { + ssMode = `&mode=${encodeURIComponent(proxy._mode)}`; + } + } + if (proxy.tls) { + query += `&sni=${encodeURIComponent( + proxy.sni || proxy.server, + )}${proxy['skip-cert-verify'] ? '&allowInsecure=1' : ''}`; + } + query += `${ssTransport}${ssAlpn}${ssFp}${ssSecurity}${ssSid}${ssPbk}${ssSpx}${ssMode}${ssExtra}#${encodeURIComponent( + proxy.name, + )}`; + result += query.replace(/^&/, '?'); break; case 'ssr': result = `${proxy.server}:${proxy.port}:${proxy.protocol}:${