feat: 处理端口跳跃(感谢亚托莉佬)

This commit is contained in:
xream
2024-07-19 14:56:37 +08:00
parent 317a804b36
commit 05bdf95a29
4 changed files with 33 additions and 1 deletions

View File

@@ -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,

View File

@@ -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,
};