Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +13 -0
- index.js +90 -0
- package.json +21 -0
Dockerfile
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM node:alpine
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY . .
|
6 |
+
|
7 |
+
EXPOSE 7860
|
8 |
+
|
9 |
+
RUN apk update && apk add --no-cache openssl curl &&\
|
10 |
+
chmod +x index.js &&\
|
11 |
+
npm install
|
12 |
+
|
13 |
+
CMD ["node", "index.js"]
|
index.js
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const axios = require('axios');
|
2 |
+
const moment = require('moment-timezone');
|
3 |
+
const http = require('http');
|
4 |
+
const cron = require('node-cron');
|
5 |
+
const port = process.env.PORT || 7860;
|
6 |
+
|
7 |
+
// 添加24小时是不间断访问的url数组
|
8 |
+
const webpages = [
|
9 |
+
'https://tabby-tundra-gaura.glitch.me/sub', // Glitch-688
|
10 |
+
// ... 添加更多url
|
11 |
+
];
|
12 |
+
|
13 |
+
// 添加1:00-6:00暂停,其他时间正常访问的url数组
|
14 |
+
const urls = [
|
15 |
+
'https://www.google.com', // 备注名称
|
16 |
+
'https://www.baidu.com'
|
17 |
+
// ... 添加更多url
|
18 |
+
];
|
19 |
+
|
20 |
+
// 遍历网页数组并发请求访问网页
|
21 |
+
const visit = async (url) => {
|
22 |
+
try {
|
23 |
+
const response = await axios.get(url);
|
24 |
+
console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Visited web successfully: ${url} --- Status: ${response.status}\n`);
|
25 |
+
} catch (error) {
|
26 |
+
console.error(`Failed to visit ${url}: ${error.message}\n`);
|
27 |
+
}
|
28 |
+
};
|
29 |
+
const visitAll = async () => {
|
30 |
+
for (let url of urls) {
|
31 |
+
await visit(url);
|
32 |
+
}
|
33 |
+
};
|
34 |
+
|
35 |
+
// 定判断是否在访问时间段内
|
36 |
+
const isWithinTime = () => {
|
37 |
+
const now = moment().tz('Asia/Hong_Kong');
|
38 |
+
const hour = now.hour();
|
39 |
+
if (hour >= 1 && hour < 6) {
|
40 |
+
return false;
|
41 |
+
}
|
42 |
+
return true;
|
43 |
+
};
|
44 |
+
|
45 |
+
// 创建http服务
|
46 |
+
const createServer = () => {
|
47 |
+
const server = http.createServer((req, res) => {
|
48 |
+
if (req.url === '/') {
|
49 |
+
res.writeHead(200, {'Content-Type': 'text/plain'});
|
50 |
+
res.end('Hello world!');
|
51 |
+
} else {
|
52 |
+
res.writeHead(404, {'Content-Type': 'text/plain'});
|
53 |
+
res.end('404 Not Found');
|
54 |
+
}
|
55 |
+
});
|
56 |
+
server.listen(port, () => {
|
57 |
+
console.log(`Server started on port ${port}`);
|
58 |
+
});
|
59 |
+
};
|
60 |
+
|
61 |
+
// 执行访问逻辑
|
62 |
+
const main = async () => {
|
63 |
+
createServer();
|
64 |
+
setInterval(async () => {
|
65 |
+
if (isWithinTime()) {
|
66 |
+
await visitAll();
|
67 |
+
} else {
|
68 |
+
console.log(`Stop visiting at ${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}`);
|
69 |
+
}
|
70 |
+
}, 2 * 60 * 1000); // 指定时间段访问的间隔2分钟
|
71 |
+
};
|
72 |
+
main();
|
73 |
+
|
74 |
+
//24小时不间断访问部分
|
75 |
+
async function access(url) {
|
76 |
+
try {
|
77 |
+
const response = await axios.get(url);
|
78 |
+
console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Web visited successfully: ${url} --- status code is ${response.status}`);
|
79 |
+
} catch (error) {
|
80 |
+
console.error(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Failed to visit ${url}, Error ${error.message}`);
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
async function batchVisit() {
|
85 |
+
|
86 |
+
for (let url of webpages) {
|
87 |
+
await access(url);
|
88 |
+
}
|
89 |
+
}
|
90 |
+
cron.schedule('*/2 * * * *', batchVisit); // 24小时访问任务间隔周期,默认2分钟
|
package.json
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "keep-alive",
|
3 |
+
"version": "1.0.0",
|
4 |
+
"description": "keep-web-alive",
|
5 |
+
"main": "index.js",
|
6 |
+
"author": "eoovve",
|
7 |
+
"repository": "",
|
8 |
+
"license": "MIT",
|
9 |
+
"private": false,
|
10 |
+
"scripts": {
|
11 |
+
"start": "node index.js"
|
12 |
+
},
|
13 |
+
"dependencies": {
|
14 |
+
"axios": "^1.6.2",
|
15 |
+
"node-cron": "^2.0.1",
|
16 |
+
"moment-timezone": "latest"
|
17 |
+
},
|
18 |
+
"engines": {
|
19 |
+
"node": ">=14"
|
20 |
+
}
|
21 |
+
}
|