Update worker.js

This commit is contained in:
CMLiussss
2023-12-28 16:28:37 +08:00
committed by GitHub
parent 5b95a6732a
commit f128329c52

View File

@@ -816,7 +816,46 @@ async function getVLESSConfig(userID, hostName, sub, userAgent) {
return `Error fetching content: ${error.message}`; return `Error fetching content: ${error.message}`;
} }
} else { } else {
return 'Error: fetch is not available in this environment.'; return 'Error: fetch is not available in this environment.';//
}
} else if (sub && userAgent.includes('sing-box')) {
// 如果sub不为空且UA为sing-box则发起特定请求
let singBoxVersion = null;
// 提取sing-box版本号
const match = userAgent.match(/sing-box (\d+\.\d+\.\d+)/);
if (match) {
singBoxVersion = match[1];
}
if (singBoxVersion && compareVersions(singBoxVersion, '1.8.0') >= 0) {
// 如果 sing-box 版本是 1.8.0 或更高
if (typeof fetch === 'function') {
try {
const response = await fetch(`https://sing-box-subscribe.vercel.app/config/https:/${hostName}/${userID}`);
const content = await response.text();
return content;
} catch (error) {
console.error('获取内容时出错:', error);
return `获取内容时出错: ${error.message}`;
}
} else {
return '错误: 在此环境中不支持 fetch。';
}
} else {
// 如果 sing-box 版本低于 1.8.0
if (typeof fetch === 'function') {
try {
const response = await fetch(`https://sub2singbox.fuck.cloudns.biz/config/https:/${hostName}/${userID}`);
const content = await response.text();
return content;
} catch (error) {
console.error('获取内容时出错:', error);
return `获取内容时出错: ${error.message}`;
}
} else {
return '错误: 在此环境中不支持 fetch。';
}
} }
} else { } else {
// 如果sub不为空且UA则发起一般请求 // 如果sub不为空且UA则发起一般请求
@@ -835,3 +874,15 @@ async function getVLESSConfig(userID, hostName, sub, userAgent) {
} }
} }
// 辅助函数用于比较版本号
function compareVersions(version1, version2) {
const parts1 = version1.split('.').map(Number);
const parts2 = version2.split('.').map(Number);
for (let i = 0; i < 3; i++) {
if (parts1[i] < parts2[i]) return -1;
if (parts1[i] > parts2[i]) return 1;
}
return 0;
}