diff --git a/backend/package.json b/backend/package.json index f6836f533..8e559ccf4 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.353", + "version": "2.14.357", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/core/proxy-utils/index.js b/backend/src/core/proxy-utils/index.js index b8ee40a87..5bc7bfee0 100644 --- a/backend/src/core/proxy-utils/index.js +++ b/backend/src/core/proxy-utils/index.js @@ -8,6 +8,7 @@ import { isValidPortNumber, isNotBlank, ipAddress, + getRandomPort, } from '@/utils'; import PROXY_PROCESSORS, { ApplyProcessor } from './processors'; import PROXY_PREPROCESSORS from './preprocessors'; @@ -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; }); @@ -274,6 +286,7 @@ export const ProxyUtils = { process: processFn, produce, ipAddress, + getRandomPort, isIPv4, isIPv6, isIP, diff --git a/backend/src/utils/index.js b/backend/src/utils/index.js index 4df152f59..9b74698cf 100644 --- a/backend/src/utils/index.js +++ b/backend/src/utils/index.js @@ -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, @@ -105,4 +122,5 @@ export { getIfPresent, // utf8ArrayToStr, getPolicyDescriptor, + getRandomPort, }; diff --git a/scripts/demo.js b/scripts/demo.js index 3b2d48a24..3223ce3e6 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -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,