MCPcopy Create free account
hub / github.com/cmliu/WorkerVless2sub / getLink

Function getLink

_worker.js:391–461  ·  view source on GitHub ↗
(重新汇总所有链接)

Source from the content-addressed store, hash-verified

389}
390
391async function getLink(重新汇总所有链接) {
392 let 节点LINK = [];
393 let 订阅链接 = [];
394 for (let x of 重新汇总所有链接) {
395 if (x.toLowerCase().startsWith('http')) {
396 订阅链接.push(x);
397 } else {
398 节点LINK.push(x);
399 }
400 }
401
402 if (订阅链接 && 订阅链接.length !== 0) {
403 function base64Decode(str) {
404 const bytes = new Uint8Array(atob(str).split('').map(c => c.charCodeAt(0)));
405 const decoder = new TextDecoder('utf-8');
406 return decoder.decode(bytes);
407 }
408 const controller = new AbortController(); // 创建一个AbortController实例,用于取消请求
409
410 const timeout = setTimeout(() => {
411 controller.abort(); // 2秒后取消所有请求
412 }, 2000);
413
414 try {
415 // 使用Promise.allSettled等待所有API请求完成,无论成功或失败
416 const responses = await Promise.allSettled(订阅链接.map(apiUrl => fetch(apiUrl, {
417 method: 'get',
418 headers: {
419 'Accept': 'text/html,application/xhtml+xml,application/xml;',
420 'User-Agent': 'v2rayN/' + FileName + ' (https://github.com/cmliu/WorkerVless2sub)'
421 },
422 signal: controller.signal // 将AbortController的信号量添加到fetch请求中
423 }).then(response => response.ok ? response.text() : Promise.reject())));
424
425 // 遍历所有响应
426 const modifiedResponses = responses.map((response, index) => {
427 // 检查是否请求成功
428 return {
429 status: response.status,
430 value: response.status === 'fulfilled' ? response.value : null,
431 apiUrl: 订阅链接[index] // 将原始的apiUrl添加到返回对象中
432 };
433 });
434
435 console.log(modifiedResponses); // 输出修改后的响应数组
436
437 for (const response of modifiedResponses) {
438 // 检查响应状态是否为'fulfilled'
439 if (response.status === 'fulfilled') {
440 const content = await response.value || 'null'; // 获取响应的内容
441 if (content.includes('://')) {
442 const lines = content.includes('\r\n') ? content.split('\r\n') : content.split('\n');
443 节点LINK = 节点LINK.concat(lines);
444 } else {
445 const 尝试base64解码内容 = base64Decode(content);
446 if (尝试base64解码内容.includes('://')) {
447 const lines = 尝试base64解码内容.includes('\r\n') ? 尝试base64解码内容.split('\r\n') : 尝试base64解码内容.split('\n');
448 节点LINK = 节点LINK.concat(lines);

Callers 1

fetchFunction · 0.85

Calls 2

fetchFunction · 0.85
base64DecodeFunction · 0.85

Tested by

no test coverage detected