一些机场可以通过签到来领取流量,本脚本可以实现定时自动化签到来实现每天签到的需求,可以通过cloudflare Workers部署,简单方便。
let sites = [];
let BotToken = '';
let ChatID = '';
export default {
async fetch(request, env, ctx) {
await initializeVariables(env);
const url = new URL(request.url);
let output = "";
// 新增 Telegram webhook 接收,路径改为 /telegram,且只接受 POST
if (url.pathname === "/telegram" && request.method === "POST") {
const update = await request.json();
await handleTelegramWebhook(update);
return new Response("OK", { status: 200 });
}
// 保留 /tg 测试推送,GET 请求
if (url.pathname === "/tg" && request.method === "GET") {
output = "✅ TG 推送测试成功";
await sendMessage("测试消息", { domain: "", user: "", pass: "" });
} else if (url.pathname === "/checkin" && request.method === "GET") {
output = await runAllCheckins();
} else {
output = "❌ 路径错误,请使用 /checkin、/tg 或 POST /telegram";
}
return new Response(output, {
status: 200,
headers: { 'Content-Type': 'text/plain;charset=UTF-8' }
});
},
async scheduled(controller, env, ctx) {
await initializeVariables(env);
await runAllCheckins();
await registerTelegramWebhook(env); //定期注册 Webhook
}
};
async function initializeVariables(env) {
const configRaw = env.CONFIG || "";
try {
sites = JSON.parse(configRaw);
if (!Array.isArray(sites)) throw new Error("CONFIG 应为数组格式");
} catch (e) {
throw new Error("CONFIG 环境变量解析失败: 请确认是合法 JSON 数组");
}
BotToken = env.TGTOKEN || "";
ChatID = env.TGID || "";
}
async function runAllCheckins() {
const results = [];
for (const site of sites) {
const result = await checkin(site);
results.push(result);
}
return results.join('\n\n');
}
async function checkin({ domain, user, pass }) {
let result = "";
try {
if (!domain.includes("//")) domain = `https://${domain}`;
const loginRes = await fetch(`${domain}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
email: user,
passwd: pass,
remember_me: 'on',
code: ""
})
});
if (!loginRes.ok) {
const errText = await loginRes.text();
throw new Error(`登录请求失败: ${errText}`);
}
const loginJson = await loginRes.json();
if (loginJson.ret !== 1) {
throw new Error(`登录失败: ${loginJson.msg || '未知错误'}`);
}
const cookieHeader = loginRes.headers.get('set-cookie');
if (!cookieHeader) throw new Error("登录成功但未获取到 Cookie");
const cookies = cookieHeader.split(',').map(c => c.split(';')[0]).join('; ');
await new Promise(r => setTimeout(r, 1000)); // 等待 1 秒
const checkinRes = await fetch(`${domain}/user/checkin`, {
method: 'POST',
headers: {
'Cookie': cookies,
'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
});
const checkinText = await checkinRes.text();
let checkinJson = {};
try {
checkinJson = JSON.parse(checkinText);
} catch (e) {
throw new Error(`签到响应非 JSON: ${checkinText}`);
}
result = `✅ [${domain}] 签到成功: ${checkinJson.msg}`;
} catch (err) {
result = `❌ [${domain}] 签到失败: ${err.message}`;
}
await sendMessage(result, { domain, user, pass });
return result;
}
async function sendMessage(msg, { domain, user, pass }) {
const time = new Date(Date.now() + 8 * 3600 * 1000).toISOString().replace("T", " ").slice(0, 19);
const info = `
🕒 执行时间: ${time}
🌐 域名: ${domain || '-'}
📧 账号: ${user || '-'}
🔑 密码: <tg-spoiler>${pass || '-'}</tg-spoiler>
📢 签到结果: ${msg}
`.trim();
if (BotToken && ChatID) {
const url = `https://api.telegram.org/bot${BotToken}/sendMessage`;
await fetch(`${url}?chat_id=${ChatID}&parse_mode=HTML&text=${encodeURIComponent(info)}`);
}
}
// 新增:处理 Telegram Bot 消息 webhook
async function handleTelegramWebhook(update) {
if (!update.message || !update.message.text) return;
const text = update.message.text.trim();
const chatId = update.message.chat.id;
if (text === "/checkin") {
const result = await runAllCheckins();
} else {
await sendPlainText(chatId, "🤖 发送 /checkin 来执行签到操作");
}
}
async function sendPlainText(chatId, msg) {
if (!BotToken) return;
const url = `https://api.telegram.org/bot${BotToken}/sendMessage`;
await fetch(`${url}?chat_id=${chatId}&text=${encodeURIComponent(msg)}`);
}
async function registerTelegramWebhook(env) {
if (!BotToken || !env.TGURL) return;
if (!env.TGURL.includes("//")) {
env.TGURL = `https://${env.TGURL}`;
}
const url = `https://api.telegram.org/bot${BotToken}/setWebhook?url=${encodeURIComponent(env.TGURL)}`;
try {
const res = await fetch(url);
const result = await res.json();
if (!result.ok) {
console.warn("❌ 设置 Telegram Webhook 失败:", result);
}
} catch (e) {
console.error("❌ 设置 Telegram Webhook 出现异常:", e.message);
}
}
cloudflare设置相关变量
变量名称 | 示例值 | 描述 |
CONFIG | [{“domain”:”机场域名”,”user”:”注册机场的邮箱”,”pass”:”密码”},{“domain”:”机场域名”,”user”:”注册机场的邮箱”,”pass”:”密码”}] | 支持多机场签到 |
TGID (可选) | 123456 | 你的telegram ID |
TGTOKEN (可选) | 7520123456:AAEWbcLnzbcpHSozTjcydffrMRmDyHkjgXA | 你的telegram 机器人的Token |
TGURL (可选) | https://自定义域名/telegram | telegram hooks访问的url |
所有变量类型为 文本。
自动签到
在项目里面设置一个触发事件就可以了实现每天定时签到了。
手动签到
方法一:
打开浏览器访问:
https://自定义域名/checkin
方法二:
打开浏览器访问:https://api.telegram.org/bot<你的telegram 机器人的Token>/setWebhook?url=https://自定义域名/telegram (仅一次即可)
后续 telegram机器人发送
/checkin
即可实现手动签到