Skip to content

Commit cd7764c

Browse files
authored
Merge pull request #988 from cmliu/beta2.0
Beta2.0
2 parents a89b6f9 + af7ac91 commit cd7764c

1 file changed

Lines changed: 66 additions & 58 deletions

File tree

_worker.js

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ export default {
273273
其他节点LINK += 优选生成器其他节点;
274274
}
275275
const ECHLINK参数 = config_JSON.ECH ? `&ech=${encodeURIComponent((config_JSON.ECHConfig.SNI ? config_JSON.ECHConfig.SNI + '+' : '') + config_JSON.ECHConfig.DNS)}` : '';
276+
let 完整节点路径 = config_JSON.随机路径 ? 随机路径(config_JSON.完整节点路径) : config_JSON.完整节点路径;
277+
if (ua.includes('loon') || ua.includes('surge')) 完整节点路径 = 完整节点路径.replace(/,/g, '%2C');
276278
订阅内容 = 其他节点LINK + 完整优选IP.map(原始地址 => {
277279
// 统一正则: 匹配 域名/IPv4/IPv6地址 + 可选端口 + 可选备注
278280
// 示例:
@@ -294,7 +296,7 @@ export default {
294296
return null;
295297
}
296298

297-
return `${协议类型}://00000000-0000-4000-8000-000000000000@${节点地址}:${节点端口}?security=tls&type=${config_JSON.传输协议 + ECHLINK参数}&host=example.com&fp=${config_JSON.Fingerprint}&sni=example.com&path=${encodeURIComponent(config_JSON.随机路径 ? 随机路径(config_JSON.完整节点路径) : config_JSON.完整节点路径) + TLS分片参数}&encryption=none${config_JSON.跳过证书验证 ? '&insecure=1&allowInsecure=1' : ''}#${encodeURIComponent(节点备注)}`;
299+
return `${协议类型}://00000000-0000-4000-8000-000000000000@${节点地址}:${节点端口}?security=tls&type=${config_JSON.传输协议 + ECHLINK参数}&host=example.com&fp=${config_JSON.Fingerprint}&sni=example.com&path=${encodeURIComponent(完整节点路径) + TLS分片参数}&encryption=none${config_JSON.跳过证书验证 ? '&insecure=1&allowInsecure=1' : ''}#${encodeURIComponent(节点备注)}`;
298300
}).filter(item => item !== null).join('\n');
299301
} else { // 订阅转换
300302
const 订阅转换URL = `${config_JSON.订阅转换配置.SUBAPI}/sub?target=${订阅类型}&url=${encodeURIComponent(url.protocol + '//' + url.host + '/sub?target=mixed&token=' + 订阅TOKEN + (url.searchParams.has('sub') && url.searchParams.get('sub') != '' ? `&sub=${url.searchParams.get('sub')}` : ''))}&config=${encodeURIComponent(config_JSON.订阅转换配置.SUBCONFIG)}&emoji=${config_JSON.订阅转换配置.SUBEMOJI}&scv=${config_JSON.跳过证书验证}`;
@@ -1183,13 +1185,13 @@ function Singbox订阅配置文件热补丁(SingBox_原始订阅内容, uuid = n
11831185

11841186
function Surge订阅配置文件热补丁(content, url, config_JSON) {
11851187
const 每行内容 = content.includes('\r\n') ? content.split('\r\n') : content.split('\n');
1186-
1188+
const 完整节点路径 = config_JSON.随机路径 ? 随机路径(config_JSON.完整节点路径) : config_JSON.完整节点路径;
11871189
let 输出内容 = "";
11881190
for (let x of 每行内容) {
11891191
if (x.includes('= tro' + 'jan,') && !x.includes('ws=true') && !x.includes('ws-path=')) {
11901192
const host = x.split("sni=")[1].split(",")[0];
11911193
const 备改内容 = `sni=${host}, skip-cert-verify=${config_JSON.跳过证书验证}`;
1192-
const 正确内容 = `sni=${host}, skip-cert-verify=${config_JSON.跳过证书验证}, ws=true, ws-path=${config_JSON.随机路径 ? 随机路径(config_JSON.完整节点路径) : config_JSON.完整节点路径}, ws-headers=Host:"${host}"`;
1194+
const 正确内容 = `sni=${host}, skip-cert-verify=${config_JSON.跳过证书验证}, ws=true, ws-path=${完整节点路径.replace(/,/g, '%2C')}, ws-headers=Host:"${host}"`;
11931195
输出内容 += x.replace(new RegExp(备改内容, 'g'), 正确内容).replace("[", "").replace("]", "") + '\n';
11941196
} else {
11951197
输出内容 += x + '\n';
@@ -1915,7 +1917,7 @@ async function 请求优选API(urls, 默认端口 = '443', 超时时间 = 3000)
19151917

19161918
async function 反代参数获取(request) {
19171919
const url = new URL(request.url);
1918-
const { pathname, searchParams } = url;
1920+
const { searchParams } = url, pathname = decodeURIComponent(url.pathname);
19191921
const pathLower = pathname.toLowerCase();
19201922

19211923
// 初始化
@@ -1963,7 +1965,7 @@ async function 反代参数获取(request) {
19631965
if (解析代理URL(路参IP)) { /* 继续到下方统一解析 */ }
19641966
else {
19651967
// 否则作为 IP 反代
1966-
反代IP = 路参IP.includes(',') ? 路参IP.split(',')[Math.floor(Math.random() * 路参IP.split(',').length)] : 路参IP;
1968+
反代IP = 路参IP;
19671969
启用反代兜底 = false;
19681970
return;
19691971
}
@@ -1991,7 +1993,7 @@ async function 反代参数获取(request) {
19911993
// proxyip 值以 socks5:// 或 http:// 开头,视为对应协议处理
19921994
if (!解析代理URL(路参IP)) {
19931995
// 否则作为 IP 反代
1994-
反代IP = 路参IP.includes(',') ? 路参IP.split(',')[Math.floor(Math.random() * 路参IP.split(',').length)] : 路参IP;
1996+
反代IP = 路参IP;
19951997
启用反代兜底 = false;
19961998
return;
19971999
}
@@ -2153,66 +2155,72 @@ async function 解析地址端口(proxyIP, 目标域名 = 'dash.cloudflare.com',
21532155
return [地址, 端口];
21542156
}
21552157

2158+
const 反代IP数组 = await 整理成数组(proxyIP);
21562159
let 所有反代数组 = [];
21572160

2158-
if (proxyIP.includes('.william')) {
2159-
try {
2160-
let txtRecords = await DoH查询(proxyIP, 'TXT');
2161-
let txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */ (r.data));
2162-
if (txtData.length === 0) {
2163-
console.log(`[反代解析] 默认DoH未获取到TXT记录,切换Google DoH重试 ${proxyIP}`);
2164-
txtRecords = await DoH查询(proxyIP, 'TXT', 'https://dns.google/dns-query');
2165-
txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */ (r.data));
2161+
// 遍历数组中的每个IP元素进行处理
2162+
for (const singleProxyIP of 反代IP数组) {
2163+
if (singleProxyIP.includes('.william')) {
2164+
try {
2165+
let txtRecords = await DoH查询(singleProxyIP, 'TXT');
2166+
let txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */(r.data));
2167+
if (txtData.length === 0) {
2168+
console.log(`[反代解析] 默认DoH未获取到TXT记录,切换Google DoH重试 ${singleProxyIP}`);
2169+
txtRecords = await DoH查询(singleProxyIP, 'TXT', 'https://dns.google/dns-query');
2170+
txtData = txtRecords.filter(r => r.type === 16).map(r => /** @type {string} */(r.data));
2171+
}
2172+
if (txtData.length > 0) {
2173+
let data = txtData[0];
2174+
if (data.startsWith('"') && data.endsWith('"')) data = data.slice(1, -1);
2175+
const prefixes = data.replace(/\\010/g, ',').replace(/\n/g, ',').split(',').map(s => s.trim()).filter(Boolean);
2176+
所有反代数组.push(...prefixes.map(prefix => 解析地址端口字符串(prefix)));
2177+
}
2178+
} catch (error) {
2179+
console.error('解析William域名失败:', error);
21662180
}
2167-
if (txtData.length > 0) {
2168-
let data = txtData[0];
2169-
if (data.startsWith('"') && data.endsWith('"')) data = data.slice(1, -1);
2170-
const prefixes = data.replace(/\\010/g, ',').replace(/\n/g, ',').split(',').map(s => s.trim()).filter(Boolean);
2171-
所有反代数组 = prefixes.map(prefix => 解析地址端口字符串(prefix));
2181+
} else {
2182+
let [地址, 端口] = 解析地址端口字符串(singleProxyIP);
2183+
2184+
if (singleProxyIP.includes('.tp')) {
2185+
const tpMatch = singleProxyIP.match(/\.tp(\d+)/);
2186+
if (tpMatch) 端口 = parseInt(tpMatch[1], 10);
21722187
}
2173-
} catch (error) {
2174-
console.error('解析William域名失败:', error);
2175-
}
2176-
} else {
2177-
let [地址, 端口] = 解析地址端口字符串(proxyIP);
21782188

2179-
if (proxyIP.includes('.tp')) {
2180-
const tpMatch = proxyIP.match(/\.tp(\d+)/);
2181-
if (tpMatch) 端口 = parseInt(tpMatch[1], 10);
2182-
}
2189+
// 判断是否是域名(非IP地址)
2190+
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?)$/;
2191+
const ipv6Regex = /^\[?([a-fA-F0-9:]+)\]?$/;
21832192

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')
2193+
if (!ipv4Regex.test(地址) && !ipv6Regex.test(地址)) {
2194+
// 并行查询 A 和 AAAA 记录
2195+
let [aRecords, aaaaRecords] = await Promise.all([
2196+
DoH查询(地址, 'A'),
2197+
DoH查询(地址, 'AAAA')
22052198
]);
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];
2209-
}
22102199

2211-
所有反代数组 = ipAddresses.length > 0
2212-
? ipAddresses.map(ip => [ip, 端口])
2213-
: [[地址, 端口]];
2214-
} else {
2215-
所有反代数组 = [[地址, 端口]];
2200+
let ipv4List = aRecords.filter(r => r.type === 1).map(r => r.data);
2201+
let ipv6List = aaaaRecords.filter(r => r.type === 28).map(r => `[${r.data}]`);
2202+
let ipAddresses = [...ipv4List, ...ipv6List];
2203+
2204+
// 默认DoH无结果时,切换Google DoH重试
2205+
if (ipAddresses.length === 0) {
2206+
console.log(`[反代解析] 默认DoH未获取到解析结果,切换Google DoH重试 ${地址}`);
2207+
[aRecords, aaaaRecords] = await Promise.all([
2208+
DoH查询(地址, 'A', 'https://dns.google/dns-query'),
2209+
DoH查询(地址, 'AAAA', 'https://dns.google/dns-query')
2210+
]);
2211+
ipv4List = aRecords.filter(r => r.type === 1).map(r => r.data);
2212+
ipv6List = aaaaRecords.filter(r => r.type === 28).map(r => `[${r.data}]`);
2213+
ipAddresses = [...ipv4List, ...ipv6List];
2214+
}
2215+
2216+
if (ipAddresses.length > 0) {
2217+
所有反代数组.push(...ipAddresses.map(ip => [ip, 端口]));
2218+
} else {
2219+
所有反代数组.push([地址, 端口]);
2220+
}
2221+
} else {
2222+
所有反代数组.push([地址, 端口]);
2223+
}
22162224
}
22172225
}
22182226
const 排序后数组 = 所有反代数组.sort((a, b) => a[0].localeCompare(b[0]));

0 commit comments

Comments
 (0)