Skip to content

Commit

Permalink
feat: 处理端口跳跃(感谢亚托莉佬)
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Jul 19, 2024
1 parent 317a804 commit afe00df
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
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.353",
"version": "2.14.356",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
13 changes: 13 additions & 0 deletions backend/src/core/proxy-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isValidPortNumber,
isNotBlank,
ipAddress,
getRandomPort,
} from '@/utils';
import PROXY_PROCESSORS, { ApplyProcessor } from './processors';
import PROXY_PREPROCESSORS from './preprocessors';
Expand Down Expand Up @@ -220,6 +221,17 @@ function produce(proxies, targetPlatform, type, opts = {}) {
delete proxy['tls-fingerprint'];
}
}

// 处理 端口跳跃
if (proxy.ports) {
// if (!['ClashMeta'].includes(targetPlatform)) {
// proxy.ports = proxy.ports.replace(/\//g, ',');
// }
if (!proxy.port) {
proxy.port = getRandomPort(proxy.ports);
}
}

return proxy;
});

Expand Down Expand Up @@ -274,6 +286,7 @@ export const ProxyUtils = {
process: processFn,
produce,
ipAddress,
getRandomPort,
isIPv4,
isIPv6,
isIP,
Expand Down
18 changes: 18 additions & 0 deletions backend/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ function getPolicyDescriptor(str) {
// };
// })();

function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getRandomPort(portString) {
let portParts = portString.split(/,|\//);
let randomPart = portParts[Math.floor(Math.random() * portParts.length)];
if (randomPart.includes('-')) {
let [min, max] = randomPart.split('-').map(Number);
return getRandomInt(min, max);
} else {
return Number(randomPart);
}
}

export {
ipAddress,
isIPv4,
Expand All @@ -105,4 +122,5 @@ export {
getIfPresent,
// utf8ArrayToStr,
getPolicyDescriptor,
getRandomPort,
};
1 change: 1 addition & 0 deletions scripts/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function operator(proxies = [], targetPlatform, context) {
// parse, // 订阅解析
// process, // 节点操作/文件操作
// produce, // 输出订阅
// getRandomPort, // 获取随机端口(参考 ports 端口跳跃的格式 443,8443,5000-6000)
// ipAddress, // https://github.com/beaugunderson/ip-address
// isIPv4,
// isIPv6,
Expand Down

0 comments on commit afe00df

Please sign in to comment.