部署自动签到

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
let domain = "69yun69.com";
let user = "你的注册邮箱";
let pass = "你的密码";
let 签到结果;
let BotToken ='';
let ChatID ='';

export default {
// HTTP 请求处理函数保持不变
async fetch(request, env, ctx) {
await initializeVariables(env);
const url = new URL(request.url);
if(url.pathname == "/tg") {
await sendMessage();
} else if (url.pathname == `/${pass}`){
await checkin();
}
return new Response(签到结果, {
status: 200,
headers: { 'Content-Type': 'text/plain;charset=UTF-8' }
});
},

// 定时任务处理函数
async scheduled(controller, env, ctx) {
console.log('Cron job started');
try {
await initializeVariables(env);
await checkin();
console.log('Cron job completed successfully');
} catch (error) {
console.error('Cron job failed:', error);
签到结果 = `定时任务执行失败: ${error.message}`;
await sendMessage(签到结果);
}
},
};

async function initializeVariables(env) {
domain = env.JC || env.DOMAIN || domain;
user = env.ZH || env.USER || user;
pass = env.MM || env.PASS || pass;
if(!domain.includes("//")) domain = `https://${domain}`;
BotToken = env.TGTOKEN || BotToken;
ChatID = env.TGID || ChatID;
签到结果 = `地址: ${domain.substring(0, 9)}****${domain.substring(domain.length - 5)}\n账号: ${user.substring(0, 1)}****${user.substring(user.length - 5)}\n密码: ${pass.substring(0, 1)}****${pass.substring(pass.length - 1)}\n\nTG推送: ${BotToken !== '' && ChatID !== ''}`;
}

async function sendMessage(msg = "") {
const 账号信息 = `地址: ${domain}\n账号: ${user}\n密码: <tg-spoiler>${pass}</tg-spoiler>`;
const now = new Date();
const beijingTime = new Date(now.getTime() + 8 * 60 * 60 * 1000);
const formattedTime = beijingTime.toISOString().slice(0, 19).replace('T', ' ');
console.log(msg);
if (BotToken !== '' && ChatID !== '') {
const url = `https://api.telegram.org/bot${BotToken}/sendMessage?chat_id=${ChatID}&parse_mode=HTML&text=${encodeURIComponent("执行时间: " + formattedTime + "\n" + 账号信息 + "\n\n" + 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'
}
});
}
}

// checkin 函数修改
async function checkin() {
try {
if (!domain || !user || !pass) {
throw new Error('必需的配置参数缺失');
}

// 登录请求
const loginResponse = await fetch(`${domain}/auth/login`, {
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: user,
passwd: pass,
remember_me: 'on',
code: "",
}),
});

console.log('Login Response Status:', loginResponse.status);

if (!loginResponse.ok) {
const errorText = await loginResponse.text();
throw new Error(`登录请求失败: ${errorText}`);
}

const loginJson = await loginResponse.json();
console.log('Login Response:', loginJson);

if (loginJson.ret !== 1) {
throw new Error(`登录失败: ${loginJson.msg || '未知错误'}`);
}

// 获取 Cookie
const cookieHeader = loginResponse.headers.get('set-cookie');
if (!cookieHeader) {
throw new Error('登录成功但未收到Cookie');
}

console.log('Received cookies:', cookieHeader);
const cookies = cookieHeader.split(',').map(cookie => cookie.split(';')[0]).join('; ');

// 等待确保登录状态
await new Promise(resolve => setTimeout(resolve, 1000));

// 签到请求
const checkinResponse = await fetch(`${domain}/user/checkin`, {
method: 'POST',
headers: {
'Cookie': cookies,
'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, */*',
'Content-Type': 'application/json',
'Origin': domain,
'Referer': `${domain}/user/panel`,
'X-Requested-With': 'XMLHttpRequest'
},
});

console.log('Checkin Response Status:', checkinResponse.status);

const responseText = await checkinResponse.text();
console.log('Checkin Raw Response:', responseText);

try {
const checkinResult = JSON.parse(responseText);
console.log('Checkin Result:', checkinResult);

if (checkinResult.ret === 1 || checkinResult.ret === 0) {
签到结果 = `🎉 签到结果 🎉\n ${checkinResult.msg || (checkinResult.ret === 1 ? '签到成功' : '签到失败')}`;
} else {
签到结果 = `🎉 签到结果 🎉\n ${checkinResult.msg || '签到结果未知'}`;
}
} catch (e) {
if (responseText.includes('登录')) {
throw new Error('登录状态无效,请检查Cookie处理');
}
throw new Error(`解析签到响应失败: ${e.message}\n\n原始响应: ${responseText}`);
}

await sendMessage(签到结果);
return 签到结果;

} catch (error) {
console.error('Checkin Error:', error);
签到结果 = `签到过程发生错误: ${error.message}`;
await sendMessage(签到结果);
return 签到结果;
}
}