forked from keaeye/LinkExtracto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_worker.js
191 lines (157 loc) · 6.51 KB
/
_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
export default {
async fetch(request, env) {
const urls = (env.URL || "").split("\n").map(url => url.trim()).filter(url => url !== "");
if (urls.length === 0) {
return new Response(
"You have not set any URLs. Please provide URLs to fetch data.\n",
{ headers: { 'Content-Type': 'text/plain; charset=utf-8' } }
);
}
// 获取 Cloudflare Pages 环境变量 LINK 的值
const linkEnv = (env.LINK || "").split("\n").map(link => link.trim()).filter(link => link !== "");
// 检查是否包含 /KY 参数
const url = new URL(request.url);
const isUnfiltered = url.pathname.endsWith("/KY");
let allLinks;
try {
// 获取 vless 链接
allLinks = await Promise.all(urls.map(url => fetchLinks(url)));
} catch (err) {
console.error("Error fetching links:", err);
return new Response("Failed to fetch links from provided URLs.", { status: 500 });
}
const validLinks = allLinks.flat().filter(link => link);
if (validLinks.length === 0) {
return new Response("No valid links found.\n", { status: 500 });
}
// 将 LINK 环境变量中的链接添加到结果中
let allFinalLinks = [...validLinks, ...linkEnv];
// 去重链接
const uniqueLinks = Array.from(new Set(allFinalLinks));
let selectedLinks;
if (isUnfiltered) {
// 不过滤国家,按国家排序并返回所有链接
selectedLinks = sortLinksByCountry(uniqueLinks);
} else {
// 按国家分组,随机取一半,排除指定国家
selectedLinks = selectRandomFiveByCountry(uniqueLinks);
}
// 替换第一行的 #国家代码 为 #Keaeye提供
if (selectedLinks.length > 0) {
selectedLinks[0] = selectedLinks[0].replace(/#\w+$/, "#Keaeye提供");
}
const plainTextContent = selectedLinks.join('\n');
return new Response(plainTextContent + "\n", {
headers: { 'Content-Type': 'text/plain; charset=utf-8' }
});
}
};
// 从 URL 中提取 IP 和端口
async function fetchLinks(url) {
let base64Data;
try {
base64Data = await fetch(url).then(res => res.text());
} catch (err) {
console.error(`Failed to fetch from ${url}:`, err);
return [];
}
if (!base64Data) {
return [];
}
let decodedContent;
try {
decodedContent = atob(base64Data);
} catch (e) {
console.error("Failed to decode the content:", e);
return [];
}
decodedContent = decodeURIComponent(decodedContent);
return extractLinks(decodedContent);
}
function extractLinks(decodedContent) {
const regex = /vless:\/\/([a-zA-Z0-9\-]+)@([^:]+):(\d+)\?([^#]+)#([^\n]+)/g;
const links = [];
const countryMapping = {
"香港": "HK", "韩国": "KR", "台湾": "TW", "日本": "JP", "新加坡": "SG", "美国": "US",
"加拿大": "CA", "澳大利亚": "AU", "英国": "GB", "法国": "FR", "意大利": "IT", "荷兰": "NL",
"德国": "DE", "挪威": "NO", "芬兰": "FI", "瑞典": "SE", "丹麦": "DK", "立陶宛": "LT", "俄罗斯": "RU",
"印度": "IN", "土耳其": "TR", "阿根廷": "AR", "巴西": "BR", "墨西哥": "MX", "南非": "ZA", "新西兰": "NZ",
"西班牙": "ES", "瑞士": "CH", "比利时": "BE", "奥地利": "AT", "爱尔兰": "IE", "葡萄牙": "PT"
};
let match;
while ((match = regex.exec(decodedContent)) !== null) {
const ip = match[2];
const port = match[3];
let countryCode = match[5];
// 映射国家
for (let country in countryMapping) {
if (countryCode.includes(country)) {
countryCode = countryMapping[country];
break;
}
}
// 去除#后面的特殊字符和文本
countryCode = countryCode.replace(/[^A-Za-z]/g, ''); // 只保留字母字符
// 形成格式化的链接
const formattedLink = `${ip}:${port}#${countryCode}`;
links.push(formattedLink); // 返回链接而非对象
}
// 过滤无效的链接,确保是有效的 IP 地址格式
return links.filter(link => /^(\d{1,3}\.){3}\d{1,3}(:\d+)?$/.test(link.split('#')[0]));
}
// 返回只包含字符串的链接
function sortLinksByCountry(links) {
const countryOrder = [
"US", "KR", "TW", "JP", "SG", "HK", "CA", "AU", "GB", "FR", "IT", "NL", "DE", "NO", "FI", "SE", "DK", "LT", "RU", "IN", "TR"
];
// 按国家排序
return links.sort((a, b) => {
const countryIndexA = countryOrder.indexOf(a.split("#")[1]);
const countryIndexB = countryOrder.indexOf(b.split("#")[1]);
if (countryIndexA === -1) return 1; // 如果没有找到,排在最后
if (countryIndexB === -1) return -1;
return countryIndexA - countryIndexB;
});
}
// 过滤和返回纯链接
function selectRandomFiveByCountry(links) {
const countryOrder = [
"US", "HK", "JP", "KR", "SG", "CA", "AU", "GB", "TW", "FR", "IT", "NL", "DE", "NO", "FI", "SE", "DK", "LT", "RU", "IN", "TR"
];
const excludeCountries = ["TR", "RU", "LT", "DK", "SE", "FI", "NO", "DE", "NL", "IT", "TW", "AU", "CA", "PL"];
const groupedLinks = {};
// 分组链接
links.forEach(link => {
const countryCode = link.split("#")[1];
if (!excludeCountries.includes(countryCode)) {
if (!groupedLinks[countryCode]) {
groupedLinks[countryCode] = [];
}
groupedLinks[countryCode].push(link);
}
});
// 按国家排序并随机选择每个国家的5个链接
const result = [];
countryOrder.forEach(country => {
if (groupedLinks[country]) {
const linksForCountry = groupedLinks[country];
// 先去重,然后随机选择每个国家的前5个链接
const uniqueLinks = Array.from(new Set(linksForCountry));
const selectedLinks = shuffleArray(uniqueLinks).slice(0, 5);
selectedLinks.forEach(link => {
if (!result.includes(link)) {
result.push(link);
}
});
}
});
return result;
}
// 洗牌算法随机打乱数组
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}