Skip to content

Commit

Permalink
Update _worker.js
Browse files Browse the repository at this point in the history
修改生成本地订阅转换逻辑, 修复节点过多的时候无法进行订阅转换的问题
  • Loading branch information
cmliu authored May 11, 2024
1 parent 2d3cf53 commit 4da1942
Showing 1 changed file with 63 additions and 71 deletions.
134 changes: 63 additions & 71 deletions _worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ let parsedSocks5Address = {};
let enableSocks = false;

// 虚假uuid和hostname,用于发送给配置生成服务
let fakeUserID = generateUUID();
let fakeHostName = generateRandomString();
let fakeUserID ;
let fakeHostName ;
let noTLS = 'false';
const expire = 4102329600;//2099-12-31
let proxyIPs;
Expand Down Expand Up @@ -54,6 +54,15 @@ export default {
const UA = request.headers.get('User-Agent') || 'null';
const userAgent = UA.toLowerCase();
userID = (env.UUID || userID).toLowerCase();

const currentDate = new Date();
currentDate.setHours(0, 0, 0, 0);
const timestamp = Math.ceil(currentDate.getTime() / 1000);
const fakeUserIDMD5 = await MD5MD5(`${userID}${timestamp}`);
fakeUserID = fakeUserIDMD5.slice(0, 8) + "-" + fakeUserIDMD5.slice(8, 12) + "-" + fakeUserIDMD5.slice(12, 16) + "-" + fakeUserIDMD5.slice(16, 20) + "-" + fakeUserIDMD5.slice(20);
fakeHostName = fakeUserIDMD5.slice(6, 9) + "." + fakeUserIDMD5.slice(13, 19);
//console.log(`${fakeUserID}\n${fakeHostName}`); // 打印fakeID

proxyIP = env.PROXYIP || proxyIP;
proxyIPs = await ADD(proxyIP);
proxyIP = proxyIPs[Math.floor(Math.random() * proxyIPs.length)];
Expand Down Expand Up @@ -97,6 +106,9 @@ export default {
return envKey === 'URL302' ? Response.redirect(URL, 302) : fetch(new Request(URL, request));
}
return new Response(JSON.stringify(request.cf, null, 4), { status: 200 });
case `/${fakeUserID}`:
const fakeConfig = await getVLESSConfig(userID, request.headers.get('Host'), sub, 'CF-Workers-SUB', RproxyIP, url);
return new Response(`${fakeConfig}`, { status: 200 });
case `/${userID}`: {
await sendMessage(`#获取订阅 ${FileName}`, request.headers.get('CF-Connecting-IP'), `UA: ${UA}</tg-spoiler>\n域名: ${url.hostname}\n<tg-spoiler>入口: ${url.pathname + url.search}</tg-spoiler>`);
if ((!sub || sub == '') && (addresses.length + addressesapi.length + addressesnotls.length + addressesnotlsapi.length + addressescsv.length) == 0) sub = 'vless-4ca.pages.dev';
Expand Down Expand Up @@ -858,35 +870,18 @@ function revertFakeInfo(content, userID, hostName, isBase64) {
return content;
}

function generateRandomNumber() {
let minNum = 100000;
let maxNum = 999999;
return Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum;
}

function generateRandomString() {
let minLength = 2;
let maxLength = 3;
let length = Math.floor(Math.random() * (maxLength - minLength + 1)) + minLength;
let characters = 'abcdefghijklmnopqrstuvwxyz';
let result = '';
for (let i = 0; i < length; i++) {
result += characters[Math.floor(Math.random() * characters.length)];
}
return result;
}

function generateUUID() {
let uuid = '';
for (let i = 0; i < 32; i++) {
let num = Math.floor(Math.random() * 16);
if (num < 10) {
uuid += num;
} else {
uuid += String.fromCharCode(num + 55);
}
}
return uuid.replace(/(.{8})(.{4})(.{4})(.{4})(.{12})/, '$1-$2-$3-$4-$5').toLowerCase();
async function MD5MD5(text) {
const encoder = new TextEncoder();

const firstPass = await crypto.subtle.digest('MD5', encoder.encode(text));
const firstPassArray = Array.from(new Uint8Array(firstPass));
const firstHex = firstPassArray.map(b => b.toString(16).padStart(2, '0')).join('');

const secondPass = await crypto.subtle.digest('MD5', encoder.encode(firstHex.slice(7, 27)));
const secondPassArray = Array.from(new Uint8Array(secondPass));
const secondHex = secondPassArray.map(b => b.toString(16).padStart(2, '0')).join('');

return secondHex.toLowerCase();
}

async function ADD(envadd) {
Expand Down Expand Up @@ -1011,59 +1006,57 @@ https://github.com/cmliu/edgetunnel
return 'Error: fetch is not available in this environment.';
}

const newAddressesapi = await getAddressesapi(addressesapi);
const newAddressescsv = await getAddressescsv('TRUE');
let newAddressesapi ;
let newAddressescsv ;
let newAddressesnotlsapi;
let newAddressesnotlscsv;

// 如果是使用默认域名,则改成一个workers的域名,订阅器会加上代理
if (hostName.includes(".workers.dev")){
fakeHostName = `${fakeHostName}.${generateRandomString()}${generateRandomNumber()}.workers.dev`;
fakeHostName = `${fakeHostName}.workers.dev`;
newAddressesnotlsapi = await getAddressesapi(addressesnotlsapi);
newAddressesnotlscsv = await getAddressescsv('FALSE');
} else if (hostName.includes(".pages.dev")){
fakeHostName = `${fakeHostName}.${generateRandomString()}${generateRandomNumber()}.pages.dev`;
fakeHostName = `${fakeHostName}.pages.dev`;
} else if (hostName.includes("worker") || hostName.includes("notls") || noTLS == 'true'){
fakeHostName = `notls.${fakeHostName}${generateRandomNumber()}.net`;
fakeHostName = `notls.${fakeHostName}.net`;
newAddressesnotlsapi = await getAddressesapi(addressesnotlsapi);
newAddressesnotlscsv = await getAddressescsv('FALSE');
} else {
fakeHostName = `${fakeHostName}.${generateRandomNumber()}.xyz`
}

if(hostName.includes('workers.dev') || hostName.includes('pages.dev')) {
if (proxyhostsURL && (!proxyhosts || proxyhosts.length == 0)) {
try {
const response = await fetch(proxyhostsURL);

if (!response.ok) {
console.error('获取地址时出错:', response.status, response.statusText);
return; // 如果有错误,直接返回
}

const text = await response.text();
const lines = text.split('\n');
// 过滤掉空行或只包含空白字符的行
const nonEmptyLines = lines.filter(line => line.trim() !== '');

proxyhosts = proxyhosts.concat(nonEmptyLines);
} catch (error) {
console.error('获取地址时出错:', error);
}
}
// 使用Set对象去重
proxyhosts = [...new Set(proxyhosts)];
fakeHostName = `${fakeHostName}.xyz`
}

let url = `https://${sub}/sub?host=${fakeHostName}&uuid=${fakeUserID}&edgetunnel=cmliu&proxyip=${RproxyIP}`;
let isBase64 = true;

if (!sub || sub == ""){
const 生成本地节点 = await subAddresses(fakeHostName,fakeUserID,noTLS,newAddressesapi,newAddressescsv,newAddressesnotlsapi,newAddressesnotlscsv);
const 解码本地节点 = atob(生成本地节点)
const 本地节点数组 = 解码本地节点.split('\n');
url = 本地节点数组.join('|');
//console.log(url);
if(hostName.includes('workers.dev') || hostName.includes('pages.dev')) {
if (proxyhostsURL && (!proxyhosts || proxyhosts.length == 0)) {
try {
const response = await fetch(proxyhostsURL);

if (!response.ok) {
console.error('获取地址时出错:', response.status, response.statusText);
return; // 如果有错误,直接返回
}

const text = await response.text();
const lines = text.split('\n');
// 过滤掉空行或只包含空白字符的行
const nonEmptyLines = lines.filter(line => line.trim() !== '');

proxyhosts = proxyhosts.concat(nonEmptyLines);
} catch (error) {
console.error('获取地址时出错:', error);
}
}
// 使用Set对象去重
proxyhosts = [...new Set(proxyhosts)];
}

newAddressesapi = await getAddressesapi(addressesapi);
newAddressescsv = await getAddressescsv('TRUE');
url = `https://${hostName}/${fakeUserID}`;
}

if (!userAgent.includes(('CF-Workers-SUB').toLowerCase())){
Expand All @@ -1087,9 +1080,8 @@ https://github.com/cmliu/edgetunnel
}});
content = await response.text();
}
let 输出内容 = revertFakeInfo(content, userID, hostName, isBase64);
//console.log(输出内容);
return 输出内容;
if (!_url.pathname.includes(`/${fakeUserID}`)) content = revertFakeInfo(content, userID, hostName, isBase64);
return content;
} catch (error) {
console.error('Error fetching content:', error);
return `Error fetching content: ${error.message}`;
Expand Down Expand Up @@ -1396,7 +1388,7 @@ function subAddresses(host,UUID,noTLS,newAddressesapi,newAddressescsv,newAddress
}).join('\n');

let base64Response = responseBody; // 重新进行 Base64 编码
if(noTLS == 'true') base64Response += `\nnotlsresponseBody`
if(noTLS == 'true') base64Response += `\nnotlsresponseBody`;
return btoa(base64Response);
}

Expand Down

0 comments on commit 4da1942

Please sign in to comment.