From 46184fbfdcd1583658db157123902862881413f5 Mon Sep 17 00:00:00 2001 From: Roy Li Date: Thu, 10 Oct 2019 14:16:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20SSR=20URI=20=E8=AF=86=E5=88=AB=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/index.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/utils/index.ts b/lib/utils/index.ts index 6edb83c76..2fac06196 100644 --- a/lib/utils/index.ts +++ b/lib/utils/index.ts @@ -193,8 +193,16 @@ export const getShadowsocksrSubscription = async (config: { const result = configList.map(item => { const pair = fromUrlSafeBase64(item.replace('ssr://', '')).split('/'); const basicInfo = pair[0].split(':'); - const extras = pair[1] ? URL.parse(pair[1], true) : null; - const nodeName = extras ? fromUrlSafeBase64(extras.query.remarks as string) : null; + const extras = pair[1] ? queryString.parse(pair[1], { + decode: false, + }) : null; + + // value 中的 ` ` 要替换成 + ,不然 base64 解码会有问题 + Object.keys(extras).forEach(key => { + extras[key] = (extras[key] as string).replace(' ', '+'); + }); + + const nodeName = extras ? fromUrlSafeBase64(extras.remarks as string) : null; if (!nodeName) { throw new Error(`${item} doesn\`t contain a remark.`); @@ -209,8 +217,8 @@ export const getShadowsocksrSubscription = async (config: { method: basicInfo[3], obfs: basicInfo[4], password: fromUrlSafeBase64(basicInfo[5]), - protoparam: extras ? fromUrlSafeBase64(extras.query.protoparam as string || '') : '', - obfsparam: extras ? fromUrlSafeBase64(extras.query.obfsparam as string || '') : '', + protoparam: extras ? fromUrlSafeBase64(extras.protoparam as string || '') : '', + obfsparam: extras ? fromUrlSafeBase64(extras.obfsparam as string || '') : '', }; }); @@ -529,7 +537,12 @@ export const getClashNodes = ( export const toUrlSafeBase64 = (str: string): string => URLSafeBase64.encode(Buffer.from(str, 'utf8')); // istanbul ignore next -export const fromUrlSafeBase64 = (str: string): string => URLSafeBase64.decode(str).toString(); +export const fromUrlSafeBase64 = (str: string): string => { + if (URLSafeBase64.validate(str)) { + return URLSafeBase64.decode(str).toString(); + } + return fromBase64(str); +}; // istanbul ignore next export const toBase64 = (str: string): string => Buffer.from(str, 'utf8').toString('base64'); @@ -616,7 +629,10 @@ export const getShadowsocksrNodes = (list: ReadonlyArray return 'ssr://' + toUrlSafeBase64([ baseUri, '/?', - queryString.stringify(query), + queryString.stringify(query, { + encode: false, + sort: false, + }), ].join('')); }