Skip to content

Commit 65bf549

Browse files
committed
fix: 更新生成随机IP函数以支持指定端口配置
1 parent ec120ff commit 65bf549

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

_worker.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export default {
169169
return new Response(JSON.stringify(config_JSON, null, 2), { status: 200, headers: { 'Content-Type': 'application/json' } });
170170
} else if (区分大小写访问路径 === 'admin/ADD.txt') {// 处理 admin/ADD.txt 请求,返回本地优选IP
171171
let 本地优选IP = await env.KV.get('ADD.txt') || 'null';
172-
if (本地优选IP == 'null') 本地优选IP = (await 生成随机IP(request, config_JSON.优选订阅生成.本地IP库.随机数量))[1];
172+
if (本地优选IP == 'null') 本地优选IP = (await 生成随机IP(request, config_JSON.优选订阅生成.本地IP库.随机数量, config_JSON.优选订阅生成.本地IP库.指定端口))[1];
173173
return new Response(本地优选IP, { status: 200, headers: { 'Content-Type': 'text/plain;charset=utf-8', 'asn': request.cf.asn } });
174174
} else if (访问路径 === 'admin/cf.json') {// CF配置文件
175175
return new Response(JSON.stringify(request.cf, null, 2), { status: 200, headers: { 'Content-Type': 'application/json;charset=utf-8' } });
@@ -223,7 +223,7 @@ export default {
223223
let 订阅内容 = '';
224224
if (订阅类型 === 'mixed') {
225225
const 节点路径 = config_JSON.启用0RTT ? config_JSON.PATH + '?ed=2560' : config_JSON.PATH;
226-
const 完整优选列表 = config_JSON.优选订阅生成.本地IP库.随机IP ? (await 生成随机IP(request, config_JSON.优选订阅生成.本地IP库.随机数量))[0] : await env.KV.get('ADD.txt') ? await 整理成数组(await env.KV.get('ADD.txt')) : (await 生成随机IP(request, config_JSON.优选订阅生成.本地IP库.随机数量))[0];
226+
const 完整优选列表 = config_JSON.优选订阅生成.本地IP库.随机IP ? (await 生成随机IP(request, config_JSON.优选订阅生成.本地IP库.随机数量, config_JSON.优选订阅生成.本地IP库.指定端口))[0] : await env.KV.get('ADD.txt') ? await 整理成数组(await env.KV.get('ADD.txt')) : (await 生成随机IP(request, config_JSON.优选订阅生成.本地IP库.随机数量, config_JSON.优选订阅生成.本地IP库.指定端口))[0];
227227
const 优选API = [], 优选IP = [], 其他节点 = [];
228228
for (const 元素 of 完整优选列表) {
229229
if (元素.toLowerCase().startsWith('https://')) 优选API.push(元素);
@@ -825,7 +825,8 @@ async function 读取config_JSON(env, host, userID, 重置配置 = false) {
825825
local: true, // true: 基于本地的优选地址 false: 优选订阅生成器
826826
本地IP库: {
827827
随机IP: true, // 当 随机IP 为true时生效,启用随机IP的数量,否则使用KV内的ADD.txt
828-
随机数量: 16, // 当local为true时生效,随机IP的数量
828+
随机数量: 16,
829+
指定端口: -1,
829830
},
830831
SUB: null,
831832
SUBNAME: "edge" + "tunnel",
@@ -922,10 +923,11 @@ async function 读取config_JSON(env, host, userID, 重置配置 = false) {
922923
return config_JSON;
923924
}
924925

925-
async function 生成随机IP(request, count = 16) {
926+
async function 生成随机IP(request, count = 16, 指定端口 = -1) {
926927
const asnMap = { '9808': 'cmcc', '4837': 'cu', '4134': 'ct' }, asn = request.cf.asn;
927928
const cidr_url = asnMap[asn] ? `https://raw.githubusercontent.com/cmliu/cmliu/main/CF-CIDR/${asnMap[asn]}.txt` : 'https://raw.githubusercontent.com/cmliu/cmliu/main/CF-CIDR.txt';
928929
const cfname = { '9808': 'CF移动优选', '4837': 'CF联通优选', '4134': 'CF电信优选' }[asn] || 'CF官方优选';
930+
const cfport = [443, 2053, 2083, 2087, 2096, 8443];
929931
let cidrList = [];
930932
try { const res = await fetch(cidr_url); cidrList = res.ok ? await 整理成数组(await res.text()) : ['104.16.0.0/13']; } catch { cidrList = ['104.16.0.0/13']; }
931933

@@ -939,7 +941,7 @@ async function 生成随机IP(request, count = 16) {
939941

940942
const randomIPs = Array.from({ length: count }, () => {
941943
const ip = generateRandomIPFromCIDR(cidrList[Math.floor(Math.random() * cidrList.length)]);
942-
return `${ip}#${cfname}`;
944+
return `${ip}:${指定端口 === -1 ? cfport[Math.floor(Math.random() * cfport.length)] : 指定端口}#${cfname}`;
943945
});
944946
return [randomIPs, randomIPs.join('\n')];
945947
}

0 commit comments

Comments
 (0)