Skip to content

Commit f7fc137

Browse files
committed
feat: 优化反代IP处理逻辑,支持多个IP并增强域名解析功能
1 parent c4a21b5 commit f7fc137

1 file changed

Lines changed: 46 additions & 39 deletions

File tree

_worker.js

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ async function 反代参数获取(request) {
19631963
if (解析代理URL(路参IP)) { /* 继续到下方统一解析 */ }
19641964
else {
19651965
// 否则作为 IP 反代
1966-
反代IP = 路参IP.includes(',') ? 路参IP.split(',')[Math.floor(Math.random() * 路参IP.split(',').length)] : 路参IP;
1966+
反代IP = 路参IP;
19671967
启用反代兜底 = false;
19681968
return;
19691969
}
@@ -1991,7 +1991,7 @@ async function 反代参数获取(request) {
19911991
// proxyip 值以 socks5:// 或 http:// 开头,视为对应协议处理
19921992
if (!解析代理URL(路参IP)) {
19931993
// 否则作为 IP 反代
1994-
反代IP = 路参IP.includes(',') ? 路参IP.split(',')[Math.floor(Math.random() * 路参IP.split(',').length)] : 路参IP;
1994+
反代IP = 路参IP;
19951995
启用反代兜底 = false;
19961996
return;
19971997
}
@@ -2158,11 +2158,11 @@ async function 解析地址端口(proxyIP, 目标域名 = 'dash.cloudflare.com',
21582158
if (proxyIP.includes('.william')) {
21592159
try {
21602160
let txtRecords = await DoH查询(proxyIP, 'TXT');
2161-
let txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */ (r.data));
2161+
let txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */(r.data));
21622162
if (txtData.length === 0) {
21632163
console.log(`[反代解析] 默认DoH未获取到TXT记录,切换Google DoH重试 ${proxyIP}`);
21642164
txtRecords = await DoH查询(proxyIP, 'TXT', 'https://dns.google/dns-query');
2165-
txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */ (r.data));
2165+
txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */(r.data));
21662166
}
21672167
if (txtData.length > 0) {
21682168
let data = txtData[0];
@@ -2174,45 +2174,52 @@ async function 解析地址端口(proxyIP, 目标域名 = 'dash.cloudflare.com',
21742174
console.error('解析William域名失败:', error);
21752175
}
21762176
} else {
2177-
let [地址, 端口] = 解析地址端口字符串(proxyIP);
2177+
const 反代IP数组 = await 整理成数组(proxyIP);
21782178

2179-
if (proxyIP.includes('.tp')) {
2180-
const tpMatch = proxyIP.match(/\.tp(\d+)/);
2181-
if (tpMatch) 端口 = parseInt(tpMatch[1], 10);
2182-
}
2179+
// 遍历数组中的每个IP元素进行处理
2180+
for (const singleProxyIP of 反代IP数组) {
2181+
let [地址, 端口] = 解析地址端口字符串(singleProxyIP);
21832182

2184-
// 判断是否是域名(非IP地址)
2185-
const ipv4Regex = /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
2186-
const ipv6Regex = /^\[?([a-fA-F0-9:]+)\]?$/;
2187-
2188-
if (!ipv4Regex.test(地址) && !ipv6Regex.test(地址)) {
2189-
// 并行查询 A 和 AAAA 记录
2190-
let [aRecords, aaaaRecords] = await Promise.all([
2191-
DoH查询(地址, 'A'),
2192-
DoH查询(地址, 'AAAA')
2193-
]);
2194-
2195-
let ipv4List = aRecords.filter(r => r.type === 1).map(r => r.data);
2196-
let ipv6List = aaaaRecords.filter(r => r.type === 28).map(r => `[${r.data}]`);
2197-
let ipAddresses = [...ipv4List, ...ipv6List];
2198-
2199-
// 默认DoH无结果时,切换Google DoH重试
2200-
if (ipAddresses.length === 0) {
2201-
console.log(`[反代解析] 默认DoH未获取到解析结果,切换Google DoH重试 ${地址}`);
2202-
[aRecords, aaaaRecords] = await Promise.all([
2203-
DoH查询(地址, 'A', 'https://dns.google/dns-query'),
2204-
DoH查询(地址, 'AAAA', 'https://dns.google/dns-query')
2205-
]);
2206-
ipv4List = aRecords.filter(r => r.type === 1).map(r => r.data);
2207-
ipv6List = aaaaRecords.filter(r => r.type === 28).map(r => `[${r.data}]`);
2208-
ipAddresses = [...ipv4List, ...ipv6List];
2183+
if (singleProxyIP.includes('.tp')) {
2184+
const tpMatch = singleProxyIP.match(/\.tp(\d+)/);
2185+
if (tpMatch) 端口 = parseInt(tpMatch[1], 10);
22092186
}
22102187

2211-
所有反代数组 = ipAddresses.length > 0
2212-
? ipAddresses.map(ip => [ip, 端口])
2213-
: [[地址, 端口]];
2214-
} else {
2215-
所有反代数组 = [[地址, 端口]];
2188+
// 判断是否是域名(非IP地址)
2189+
const ipv4Regex = /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
2190+
const ipv6Regex = /^\[?([a-fA-F0-9:]+)\]?$/;
2191+
2192+
if (!ipv4Regex.test(地址) && !ipv6Regex.test(地址)) {
2193+
// 并行查询 A 和 AAAA 记录
2194+
let [aRecords, aaaaRecords] = await Promise.all([
2195+
DoH查询(地址, 'A'),
2196+
DoH查询(地址, 'AAAA')
2197+
]);
2198+
2199+
let ipv4List = aRecords.filter(r => r.type === 1).map(r => r.data);
2200+
let ipv6List = aaaaRecords.filter(r => r.type === 28).map(r => `[${r.data}]`);
2201+
let ipAddresses = [...ipv4List, ...ipv6List];
2202+
2203+
// 默认DoH无结果时,切换Google DoH重试
2204+
if (ipAddresses.length === 0) {
2205+
console.log(`[反代解析] 默认DoH未获取到解析结果,切换Google DoH重试 ${地址}`);
2206+
[aRecords, aaaaRecords] = await Promise.all([
2207+
DoH查询(地址, 'A', 'https://dns.google/dns-query'),
2208+
DoH查询(地址, 'AAAA', 'https://dns.google/dns-query')
2209+
]);
2210+
ipv4List = aRecords.filter(r => r.type === 1).map(r => r.data);
2211+
ipv6List = aaaaRecords.filter(r => r.type === 28).map(r => `[${r.data}]`);
2212+
ipAddresses = [...ipv4List, ...ipv6List];
2213+
}
2214+
2215+
if (ipAddresses.length > 0) {
2216+
所有反代数组.push(...ipAddresses.map(ip => [ip, 端口]));
2217+
} else {
2218+
所有反代数组.push([地址, 端口]);
2219+
}
2220+
} else {
2221+
所有反代数组.push([地址, 端口]);
2222+
}
22162223
}
22172224
}
22182225
const 排序后数组 = 所有反代数组.sort((a, b) => a[0].localeCompare(b[0]));

0 commit comments

Comments
 (0)