Skip to content

Commit

Permalink
Update _worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
amclubs authored Nov 4, 2024
1 parent 0556ee7 commit 98cbd64
Showing 1 changed file with 36 additions and 14 deletions.
50 changes: 36 additions & 14 deletions _worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ let isBase64 = true;
let botToken = '';
let chatID = '';

const httpPattern = /^http(s)?:\/\/.+/;

if (!isValidUUID(userID)) {
throw new Error('uuid is invalid');
Expand Down Expand Up @@ -111,8 +112,28 @@ export default {

userID = (UUID || userID).toLowerCase();
if (PROXYIP) {
proxyIPs = await addIpText(PROXYIP);
proxyIP = proxyIPs[Math.floor(Math.random() * proxyIPs.length)];
if (httpPattern.test(PROXYIP)) {
let ipUrlTxtAndCsv;
if (PROXYIP.endsWith('.txt')) {
ipUrlTxtAndCsv = await getIpUrlTxtAndCsv(noTLS, PROXYIP, null);

} else if (PROXYIP.endsWith('.csv')) {
ipUrlTxtAndCsv = await getIpUrlTxtAndCsv(noTLS, null, PROXYIP);

} else {
return new Response("PROXYIP是无效的文件类型:", {
headers: {
'Content-Type': 'text/html; charset=UTF-8',
'referer': 'https://www.google.com/search?q=AM科技',
},
});
}
const uniqueIpTxt = [...new Set([...ipUrlTxtAndCsv.txt, ...ipUrlTxtAndCsv.csv])];
proxyIP = uniqueIpTxt[Math.floor(Math.random() * uniqueIpTxt.length)];
} else {
proxyIPs = await addIpText(PROXYIP);
proxyIP = proxyIPs[Math.floor(Math.random() * proxyIPs.length)];
}
const [ip, port] = proxyIP.split(':');
proxyIP = ip;
proxyPort = port || proxyPort;
Expand Down Expand Up @@ -394,6 +415,7 @@ async function parseSocks5FromUrl(socks5, url) {
return null;
}


/** ---------------------Get data------------------------------ */

let subParams = ['sub', 'base64', 'b64', 'clash', 'singbox', 'sb'];
Expand Down Expand Up @@ -421,7 +443,7 @@ async function getVLESSConfig(userID, host, userAgent, _url) {

// Get node information
fakeHostName = getFakeHostName(host);
const ipUrlTxtAndCsv = await getIpUrlTxtAndCsv(noTLS);
const ipUrlTxtAndCsv = await getIpUrlTxtAndCsv(noTLS, ipUrlTxt, ipUrlCsv);

// console.log(`txt: ${ipUrlTxtAndCsv.txt} \n csv: ${ipUrlTxtAndCsv.csv}`);
let content = await getSubscribeNode(userAgent, _url, host, fakeHostName, fakeUserID, noTLS, ipUrlTxtAndCsv.txt, ipUrlTxtAndCsv.csv);
Expand Down Expand Up @@ -456,21 +478,21 @@ function getFakeHostName(host) {
return `${fakeHostName}.xyz`;
}

async function getIpUrlTxtAndCsv(noTLS) {
async function getIpUrlTxtAndCsv(noTLS, urlTxts, urlCsvs) {
if (noTLS === 'true') {
return {
txt: await getIpUrlTxt(ipUrlTxt),
csv: await getIpUrlCsv('FALSE')
txt: await getIpUrlTxt(urlTxts),
csv: await getIpUrlCsv(urlCsvs, 'FALSE')
};
}
return {
txt: await getIpUrlTxt(ipUrlTxt),
csv: await getIpUrlCsv('TRUE')
txt: await getIpUrlTxt(urlTxts),
csv: await getIpUrlCsv(urlCsvs, 'TRUE')
};
}

async function getIpUrlTxt(ipUrlTxts) {
if (!ipUrlTxts || ipUrlTxts.length === 0) {
async function getIpUrlTxt(urlTxts) {
if (!urlTxts || urlTxts.length === 0) {
return [];
}

Expand All @@ -487,7 +509,7 @@ async function getIpUrlTxt(ipUrlTxts) {
try {
// Use Promise.allSettled to wait for all API requests to complete, regardless of success or failure
// Iterate over the api array and send a fetch request to each API URL
const responses = await Promise.allSettled(ipUrlTxts.map(apiUrl => fetch(apiUrl, {
const responses = await Promise.allSettled(urlTxts.map(apiUrl => fetch(apiUrl, {
method: 'GET',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;',
Expand Down Expand Up @@ -520,16 +542,16 @@ async function getIpUrlTxt(ipUrlTxts) {
return newIpTxt;
}

async function getIpUrlCsv(tls) {
async function getIpUrlCsv(urlCsvs, tls) {
// Check if the CSV URLs are valid
if (!ipUrlCsv || ipUrlCsv.length === 0) {
if (!urlCsvs || urlCsvs.length === 0) {
return [];
}

const newAddressesCsv = [];

// Fetch and process all CSVs concurrently
const fetchCsvPromises = ipUrlCsv.map(async (csvUrl) => {
const fetchCsvPromises = urlCsvs.map(async (csvUrl) => {
try {
const response = await fetch(csvUrl);

Expand Down

0 comments on commit 98cbd64

Please sign in to comment.