-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsub.js
99 lines (78 loc) · 3.36 KB
/
sub.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
// 部署完成后在网址后面加上这个,获取自建节点和机场聚合节点,/?token=16516a90&tag=jichang
// 部署完成后在网址后面加上这个,只获取自建节点,/?token=16516a90
const mytoken = '16516a90'; //可以随便取,不能特殊字符,可以uuid生成,
const tgbottoken =''; //可以为空,或者@BotFather中输入/start,/newbot,并关注机器人
const tgchatid =''; //可以为空,或者@userinfobot中获取,/start
//自建节点
const MainData = `
trojan://cafffbbd-fa7e-e3ca-
vless://6d885d9d-79ca-4f5b-bf
trojan://a1fdb16c-e0db-4cc2-aac
vless://ae03a8c1-084c-4080-b42d-0a
`
//机场信息,可多个,也可为0
const urls = [
'https://mareep.netlify.app/sub/shadowrocket_base64.txt',
'https://mareep.netlify.app/sub/shadowrocket_base64.txt'
// 添加更多订阅,支持base64
];
addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) })
async function handleRequest(request) {
const url = new URL(request.url);
const tag = url.searchParams.get('tag');
const token = url.searchParams.get('token'); // Get the token from the URL
if (token !== mytoken) {
//await sendMessage("#Token错误信息", request.headers.get('CF-Connecting-IP'), `Invalid Token: ${token}`);
return new Response('Invalid token???', { status: 403 });
}
let req_data = "";
req_data += MainData
if (tag) {
switch (tag) {
case 'jichang':
const responses = await Promise.all(urls.map(url => fetch(url)));
for (const response of responses) {
if (response.ok) {
const content = await response.text();
req_data += atob(content) + '\n';
}
}
break;
default:
break;
}
}
await sendMessage("#访问信息", request.headers.get('CF-Connecting-IP'), `Tag: ${tag}`);
//修复中文错误
const utf8Encoder = new TextEncoder();
const encodedData = utf8Encoder.encode(req_data);
const base64Data = btoa(String.fromCharCode.apply(null, encodedData));
return new Response(base64Data);
}
// 代码参考:https://azhuge233.com/cloudflare-workers-%E8%BD%AC%E5%8F%91-telegram-bot-%E6%8E%A8%E9%80%81%E4%BF%A1%E6%81%AF/
async function sendMessage(type, ip, add_data = "") {
const OPT = {
BotToken: tgbottoken, // Telegram Bot API
ChatID: tgchatid, // User 或者 ChatID,电报用户名
}
let msg = "";
const response = await fetch(`http://ip-api.com/json/${ip}`);
if (response.status == 200) { // 查询 IP 来源信息,使用方法参考:https://ip-api.com/docs/api:json
const ipInfo = await response.json();
msg = `${type}\nIP: ${ip}\nCountry: ${ipInfo.country}\nCity: ${ipInfo.city}\n${add_data}`;
} else {
msg = `${type}\nIP: ${ip}\n${add_data}`;
}
let url = "https://api.telegram.org/";
url += "bot" + OPT.BotToken + "/sendMessage?";
url += "chat_id=" + OPT.ChatID + "&";
url += "text=" + encodeURIComponent(msg);
return fetch(url, {
method: 'get',
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;',
'Accept-Encoding': 'gzip, deflate, br',
'User-Agent': 'Mozilla/5.0 Chrome/90.0.4430.72'
}
});
}