Skip to content

Commit 96bfc00

Browse files
authored
feat: update workers demo (#3)
1 parent 401de6e commit 96bfc00

5 files changed

Lines changed: 472 additions & 0 deletions

File tree

cloudflareworkers/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Cloudflare workers Example
2+
3+
## How to Run
4+
5+
First ensure that your Wrangler version is up to date.
6+
7+
```bash
8+
$ wrangler -v
9+
```
10+
11+
Now, if you run `wrangler dev` within this directory, it should use the config
12+
in `wrangler.toml` to run the example.
13+
14+
You can also run `wrangler deploy` to deploy the example.

cloudflareworkers/src/nodeseek.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
let message = "";
2+
3+
// Cloudflare Worker
4+
export default {
5+
async fetch(request, env, ctx) {
6+
console.log("🚀 NodeSeek Worker started - fetch handler called");
7+
8+
const url = new URL(request.url);
9+
10+
if (url.pathname === '/checkin') {
11+
return await handleNodeSeekCheckin(env);
12+
}
13+
14+
// Other paths return simple information
15+
return new Response(
16+
JSON.stringify({ message: "NodeSeek Worker is running. Visit /checkin to trigger check-in." }),
17+
{ headers: { "Content-Type": "application/json" } }
18+
);
19+
},
20+
21+
async scheduled(event, env, ctx) {
22+
console.log("⏰ NodeSeek Scheduled handler called");
23+
const result = await handleNodeSeekCheckin(env);
24+
const resultText = await result.text();
25+
console.log(`📊 NodeSeek Scheduled check-in result: ${resultText}`);
26+
}
27+
};
28+
29+
async function handleNodeSeekCheckin(env) {
30+
console.log("🔧 Starting NodeSeek handleCheckin");
31+
32+
// Get cookie from environment variable
33+
const cookie = env.NODESEEK_COOKIE;
34+
35+
if (!cookie) {
36+
console.log("❌ No NODESEEK_COOKIE found in environment");
37+
return new Response(
38+
JSON.stringify({ error: "Environment variable NODESEEK_COOKIE is not set" }),
39+
{
40+
status: 400,
41+
headers: { "Content-Type": "application/json" }
42+
}
43+
);
44+
}
45+
46+
console.log(`✅ cookie found, length: ${cookie.length}`);
47+
48+
// Initialize message time (UTC+8)
49+
const now = new Date();
50+
const utc8Time = new Date(now.getTime() + 8 * 60 * 60 * 1000);
51+
message = utc8Time.toLocaleString('zh-CN', {
52+
year: 'numeric',
53+
month: '2-digit',
54+
day: '2-digit',
55+
hour: '2-digit',
56+
minute: '2-digit',
57+
second: '2-digit',
58+
hour12: false
59+
}) + " from NodeSeek \n";
60+
61+
console.log(`🕐 Current time (UTC+8): ${utc8Time.toLocaleString('zh-CN')}`);
62+
63+
console.log(`🔄 Processing single account...`);
64+
65+
const result = await checkInAccount(cookie);
66+
message += `Account: ${result.message}\n`;
67+
68+
console.log(`🎉 NodeSeek check-in completed: ${result.success ? 'successful' : 'failed'}`);
69+
70+
return new Response(
71+
JSON.stringify({
72+
success: result.success,
73+
message: message,
74+
result: result,
75+
summary: `Account checked in ${result.success ? 'successfully' : 'with failure'}`
76+
}),
77+
{
78+
headers: { "Content-Type": "application/json" }
79+
}
80+
);
81+
}
82+
83+
async function checkInAccount(cookie) {
84+
const headers = {
85+
'Accept': '*/*',
86+
'Accept-Encoding': 'gzip, deflate, br, zstd',
87+
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
88+
'Content-Length': '0',
89+
'Origin': 'https://www.nodeseek.com',
90+
'Referer': 'https://www.nodeseek.com/board',
91+
'Sec-CH-UA': '"Chromium";v="134", "Not:A-Brand";v="24", "Google Chrome";v="134"',
92+
'Sec-CH-UA-Mobile': '?0',
93+
'Sec-CH-UA-Platform': '"Windows"',
94+
'Sec-Fetch-Dest': 'empty',
95+
'Sec-Fetch-Mode': 'cors',
96+
'Sec-Fetch-Site': 'same-origin',
97+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
98+
'Cookie': cookie
99+
};
100+
101+
try {
102+
// random=true means get random reward
103+
const url = 'https://www.nodeseek.com/api/attendance?random=true';
104+
console.log(`🌐 Account checking in at: ${url}`);
105+
106+
const response = await fetch(url, {
107+
method: 'POST',
108+
headers: headers,
109+
cf: {
110+
// Force IPv4
111+
resolveOverride: 'ipv4'
112+
}
113+
});
114+
115+
console.log(`📡 Account Status Code: ${response.status}`);
116+
const responseText = await response.text();
117+
console.log(`📄 Account Response Content: ${responseText}`);
118+
119+
// Check if the check-in is successful
120+
if (response.status === 200) {
121+
const successMessage = `✅ Account check-in successful`;
122+
console.log(successMessage);
123+
124+
// Try to parse the response content to get more information
125+
try {
126+
const responseData = JSON.parse(responseText);
127+
let detailMessage = successMessage;
128+
if (responseData.message) {
129+
detailMessage += ` - ${responseData.message}`;
130+
}
131+
if (responseData.data && responseData.data.reward) {
132+
detailMessage += ` - Reward: ${responseData.data.reward}`;
133+
}
134+
return { success: true, message: detailMessage, response: responseData };
135+
} catch (parseError) {
136+
return { success: true, message: successMessage, response: responseText };
137+
}
138+
} else {
139+
const failMessage = `❌ Account check-in failed, status: ${response.status}, response: ${responseText}`;
140+
console.log(failMessage);
141+
return { success: false, message: failMessage, response: responseText };
142+
}
143+
144+
} catch (error) {
145+
const errorMessage = `💥 Account check-in process error: ${error.message}`;
146+
console.log(errorMessage);
147+
return { success: false, message: errorMessage, error: error.message };
148+
}
149+
}
150+
151+
// Helper function: delay execution
152+
function sleep(ms) {
153+
return new Promise(resolve => setTimeout(resolve, ms));
154+
}

0 commit comments

Comments
 (0)