Skip to content

Commit

Permalink
fix: SSR URI 识别问题
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Oct 10, 2019
1 parent fa67819 commit 46184fb
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,16 @@ export const getShadowsocksrSubscription = async (config: {
const result = configList.map<ShadowsocksrNodeConfig>(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.`);
Expand All @@ -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 || '') : '',
};
});

Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -616,7 +629,10 @@ export const getShadowsocksrNodes = (list: ReadonlyArray<ShadowsocksrNodeConfig>
return 'ssr://' + toUrlSafeBase64([
baseUri,
'/?',
queryString.stringify(query),
queryString.stringify(query, {
encode: false,
sort: false,
}),
].join(''));
}

Expand Down

0 comments on commit 46184fb

Please sign in to comment.