Skip to content

Commit

Permalink
fix: 修复 Clash 节点名为 binary 的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Mar 3, 2024
1 parent 468d136 commit a4384f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.233",
"version": "2.14.235",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
20 changes: 19 additions & 1 deletion backend/src/core/proxy-utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import YAML from '@/utils/yaml';
import download from '@/utils/download';
import { isIPv4, isIPv6, isValidPortNumber, isNotBlank } from '@/utils';
import {
isIPv4,
isIPv6,
isValidPortNumber,
isNotBlank,
utf8ArrayToStr,
} from '@/utils';
import PROXY_PROCESSORS, { ApplyProcessor } from './processors';
import PROXY_PREPROCESSORS from './preprocessors';
import PROXY_PRODUCERS from './producers';
Expand Down Expand Up @@ -370,6 +376,18 @@ function lastParse(proxy) {
}
}
}
if (typeof proxy.name !== 'string') {
try {
if (proxy.name?.data) {
proxy.name = Buffer.from(proxy.name.data).toString('utf8');
} else {
proxy.name = utf8ArrayToStr(proxy.name);
}
} catch (e) {
$.error(`proxy.name decode failed\nReason: ${e}`);
proxy.name = `${proxy.type} ${proxy.server}:${proxy.port}`;
}
}
return proxy;
}

Expand Down
48 changes: 48 additions & 0 deletions backend/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,53 @@ function getIfPresent(obj, defaultValue) {
return isPresent(obj) ? obj : defaultValue;
}

const utf8ArrayToStr =
typeof TextDecoder !== 'undefined'
? (v) => new TextDecoder().decode(new Uint8Array(v))
: (function () {
var charCache = new Array(128); // Preallocate the cache for the common single byte chars
var charFromCodePt = String.fromCodePoint || String.fromCharCode;
var result = [];

return function (array) {
var codePt, byte1;
var buffLen = array.length;

result.length = 0;

for (var i = 0; i < buffLen; ) {
byte1 = array[i++];

if (byte1 <= 0x7f) {
codePt = byte1;
} else if (byte1 <= 0xdf) {
codePt = ((byte1 & 0x1f) << 6) | (array[i++] & 0x3f);
} else if (byte1 <= 0xef) {
codePt =
((byte1 & 0x0f) << 12) |
((array[i++] & 0x3f) << 6) |
(array[i++] & 0x3f);
} else if (String.fromCodePoint) {
codePt =
((byte1 & 0x07) << 18) |
((array[i++] & 0x3f) << 12) |
((array[i++] & 0x3f) << 6) |
(array[i++] & 0x3f);
} else {
codePt = 63; // Cannot convert four byte code points, so use "?" instead
i += 3;
}

result.push(
charCache[codePt] ||
(charCache[codePt] = charFromCodePt(codePt)),
);
}

return result.join('');
};
})();

export {
isIPv4,
isIPv6,
Expand All @@ -43,4 +90,5 @@ export {
getIfNotBlank,
isPresent,
getIfPresent,
utf8ArrayToStr,
};

0 comments on commit a4384f4

Please sign in to comment.