-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy path_worker.js
More file actions
322 lines (274 loc) · 10.3 KB
/
Copy path_worker.js
File metadata and controls
322 lines (274 loc) · 10.3 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
let domain = "Enter your domain here";
let username = "Enter your email here";
let password = "Enter your password here";
let token;
let botToken = '';
let chatId = '';
let checkInResult;
let jcType = '69yun69';
// 初始化变量
let fetch, Response;
// 判断当前环境是否是 Node.js 环境
if (typeof globalThis.fetch === "undefined") {
import('node-fetch').then(module => {
fetch = module.default;
Response = module.Response;
console.log("在 Node.js 环境中,已导入 node-fetch");
const env = {
JC_TYPE: process.env.JC_TYPE,
DOMAIN: process.env.DOMAIN,
USERNAME: process.env.USERNAME,
PASSWORD: process.env.PASSWORD,
TOKEN: process.env.TOKEN,
TG_TOKEN: process.env.TG_TOKEN,
TG_ID: process.env.TG_ID
};
//console.log("在 Node.js 环境中env",env);
const handler = {
async scheduled(controller, env) {
console.log("定时任务开始");
try {
await initConfig(env);
await handleCheckIn();
console.log("定时任务成功完成");
} catch (error) {
console.error("定时任务失败:", error);
await sendMessage(`${jcType}定时任务失败: ${error.message}`);
}
}
};
handler.scheduled(null, env);
}).catch(error => {
console.error("导入 node-fetch 失败:", error);
});
} else {
fetch = globalThis.fetch;
Response = globalThis.Response;
console.log("在 Cloudflare Worker 环境中,已使用内置 fetch");
}
export default {
async fetch(request, env) {
await initConfig(env);
const url = new URL(request.url);
if (url.pathname === "/tg") {
return await handleTgMsg();
} else if (url.pathname === `/${token}`) {
return await handleCheckIn();
}
return new Response(checkInResult, {
headers: { 'Content-Type': 'text/plain;charset=UTF-8' },
status: 200
});
},
async scheduled(controller, env) {
console.log("定时任务开始");
try {
await initConfig(env);
await handleCheckIn();
console.log("定时任务成功完成");
} catch (error) {
console.error("定时任务失败:", error);
await sendMessage(`${jcType}定时任务失败: ${error.message}`);
}
},
};
function decodeBase64Utf8(b64) {
const binary = atob(b64);
const bytes = Uint8Array.from(binary, c => c.charCodeAt(0));
return new TextDecoder("utf-8").decode(bytes);
}
async function handleCheckIn() {
try {
validateConfig();
if (jcType === "hongxingdl") {
checkInResult = await hongxingdlCheckIn();
} else {
const cookies = await loginAndGetCookies();
checkInResult = await performCheckIn(cookies);
}
await sendMessage(checkInResult);
return new Response(checkInResult, { status: 200 });
} catch (error) {
console.error("签到失败:", error);
const errorMsg = `${checkInResult}\n🎁${error.message}`;
await sendMessage(errorMsg);
return new Response(errorMsg, { status: 500 });
}
}
function validateConfig() {
if (!domain || !username || !password) {
throw new Error("缺少必要的配置参数");
}
}
async function loginAndGetCookies() {
const loginUrl = `${domain}/auth/login`;
const response = await fetch(loginUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36",
"Accept": "application/json, text/plain, */*",
"Origin": domain,
"Referer": `${domain}/auth/login`
},
body: JSON.stringify({ email: username , passwd: password, remember_me: "on", code: "" }),
});
if (!response.ok) {
throw new Error(`${jcType}登录失败: ${await response.text()}`);
}
const jsonResponse = await response.json();
if (jsonResponse.ret !== 1) {
throw new Error(`${jcType}登录失败: ${jsonResponse.msg || "未知错误"}`);
}
const cookieHeader = response.headers.get("set-cookie");
if (!cookieHeader) {
throw new Error("${jcType}登录成功但未收到 Cookies");
}
return cookieHeader.split(',').map(cookie => cookie.split(';')[0]).join("; ");
}
async function performCheckIn(cookies) {
const checkInUrl = `${domain}/user/checkin`;
const response = await fetch(checkInUrl, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'Origin': domain,
'Referer': `${domain}/user/panel`,
'Cookie': cookies,
'X-Requested-With': 'XMLHttpRequest'
},
});
if (!response.ok) {
throw new Error(`${jcType}签到请求失败: ${await response.text()}`);
}
const jsonResponse = await response.json();
console.log("签到信息:", jsonResponse);
if (jsonResponse.ret !== 1 && jsonResponse.ret !== 0) {
throw new Error(`${jcType}签到失败: ${jsonResponse.msg || "未知错误"}`);
}
return `🎉 ${jcType}签到结果 🎉\n${jsonResponse.msg || "签到完成"}`;
}
async function hongxingdlCheckIn() {
const checkInUrl = atob("aHR0cHM6Ly9zaWduLmhvbmd4aW5nLm9uZS9zaWdu");
const response = await fetch(checkInUrl, {
method: "POST",
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
'Accept': 'application/json, text/plain, */*',
'X-Requested-With': 'XMLHttpRequest'
},
body: JSON.stringify({ email: username , password: password }),
});
if (!response.ok) {
throw new Error(`${jcType}签到请求失败: ${await response.text()}`);
}
const jsonResponse = await response.json();
console.log("签到信息:", jsonResponse);
if (jsonResponse.status !== 200) {
throw new Error(`${jcType}签到失败: ${jsonResponse.data?.mag ?? "未知错误"}`);
}
const bytesToMB = jsonResponse.data?.bytes ? jsonResponse.data.bytes / (1024 * 1024) : null;
const str = bytesToMB ? (
bytesToMB >= 1024
? `,您获得了 ${(bytesToMB / 1024).toFixed(3)} GB 流量.`
: `,您获得了 ${bytesToMB.toFixed(3)} MB 流量.`
) : '';
return `🎉 ${jcType}签到结果 🎉\n${jsonResponse.data?.mag ?? "签到完成"}${str}`;
}
const jcButtons = {
"69yun69": [
[
{
text: decodeBase64Utf8('44CQNjnkupHjgJHkuK3ovazpq5jpgJ/mnLrlnLos5YWo5rWB5aqS5L2T6Kej6ZSBLDEwLjg55YWDNDAwRw=='),
url: decodeBase64Utf8('aHR0cHM6Ly82OXl1bjY5LmNvbS9hdXRoL3JlZ2lzdGVyP2NvZGU9eWY4Z1Br')
}
]
],
"hongxingdl": [
[
{
text: decodeBase64Utf8('44CQOOaKmOegge+8mkFN56eR5oqA44CRW+e6ouadj+S6kV3kuK3ovazpq5jpgJ/mnLrlnLos6Kej6ZSB5YWo5rWB54Wk5L2T5ZKMR1BU'),
url: decodeBase64Utf8('aHR0cHM6Ly9ob25neGluZ3l1bjMudmlwL3dlYi8jL2xvZ2luP2NvZGU9bW41VHVpcGY=')
}
]
]
};
async function sendMessage(msg) {
if (!botToken || !chatId) {
console.log("Telegram 推送未启用. 消息内容:", msg);
return;
}
const now = new Date();
const formattedTime = new Date(now.getTime() + 8 * 60 * 60 * 1000)
.toISOString()
.slice(0, 19)
.replace("T", " ");
const messageText = `执行时间: ${formattedTime}\n${msg}`;
const inline_keyboard = jcButtons[jcType] || [];
const payload = {
chat_id: chatId,
text: messageText,
parse_mode: "HTML",
reply_markup: {
inline_keyboard
}
};
try {
const response = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
const data = await response.json();
if (!response.ok || !data.ok) {
console.error("Telegram 消息发送失败:", data);
return `Telegram 消息发送失败: ${data.description || '未知错误'}`;
}
console.log("Telegram 消息发送成功:", data);
return messageText;
} catch (error) {
console.error("发送 Telegram 消息失败:", error);
return `发送 Telegram 消息失败: ${error.message}`;
}
}
function formatDomain(domain) {
return domain.includes("//") ? domain : `https://${domain}`;
}
async function handleTgMsg() {
const message = `${checkInResult}`;
const sendResult = await sendMessage(message);
return new Response(sendResult, { status: 200 });
}
function maskSensitiveData(str, type = 'default') {
if (!str) return "N/A";
const urlPattern = /^(https?:\/\/)([^\/]+)(.*)$/;
if (type === 'url' && urlPattern.test(str)) {
return str.replace(/(https:\/\/)(\w)(\w+)(\w)(\.\w+)/, '$1$2****$4$5');;
}
const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (type === 'email' && emailPattern.test(str)) {
return str.replace(/^(\w)(\w+)(\@)(\w)(\w+)(\.\w+)$/, '$1****$3$4****$6');
}
return `${str[0]}****${str[str.length - 1]}`;
}
async function initConfig(env) {
domain = formatDomain(env.DOMAIN || domain);
username = env.USERNAME || username ;
password = env.PASSWORD || password;
token = env.TOKEN || token;
botToken = env.TG_TOKEN || botToken;
chatId = env.TG_ID || chatId;
jcType = env.JC_TYPE || jcType;
checkInResult = `配置信息:
机场类型: ${jcType}
登录地址: ${maskSensitiveData(domain, 'url')}
登录账号: ${maskSensitiveData(username, 'email')}
登录密码: ${maskSensitiveData(password)}
TG 推送: ${botToken && chatId ? "已启用" : "未启用"} `;
//console.log("initConfig-->", checkInResult);
}