biangbiang commited on
Commit
47c001d
1 Parent(s): 94319fd

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +13 -0
  2. index.js +92 -0
  3. 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,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ // ... 添加更多url
10
+ ];
11
+
12
+ // 添加1:00-6:00暂停,其他时间正常访问的url数组
13
+ const urls = [
14
+ 'https://biangbiang-uptime-kuma.hf.space', //uptime-kuma
15
+ 'https://biangbiang-keep-alive.hf.space', //keep-alive
16
+ 'https://shisb-pan.hf.space', //pan
17
+ 'https://shisb-bai.hf.space', //bingai
18
+ 'https://shisb-lobechat.hf.space' //lobechat
19
+ // ... 添加更多url
20
+ ];
21
+
22
+ // 遍历网页数组并发请求访问网页
23
+ const visit = async (url) => {
24
+ try {
25
+ const response = await axios.get(url);
26
+ console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Visited web successfully: ${url} --- Status: ${response.status}\n`);
27
+ } catch (error) {
28
+ console.error(`Failed to visit ${url}: ${error.message}\n`);
29
+ }
30
+ };
31
+ const visitAll = async () => {
32
+ for (let url of urls) {
33
+ await visit(url);
34
+ }
35
+ };
36
+
37
+ // 定判断是否在访问时间段内
38
+ const isWithinTime = () => {
39
+ const now = moment().tz('Asia/Hong_Kong');
40
+ const hour = now.hour();
41
+ if (hour >= 1 && hour < 6) {
42
+ return false;
43
+ }
44
+ return true;
45
+ };
46
+
47
+ // 创建http服务
48
+ const createServer = () => {
49
+ const server = http.createServer((req, res) => {
50
+ if (req.url === '/') {
51
+ res.writeHead(200, {'Content-Type': 'text/plain'});
52
+ res.end('Hello world!');
53
+ } else {
54
+ res.writeHead(404, {'Content-Type': 'text/plain'});
55
+ res.end('404 Not Found');
56
+ }
57
+ });
58
+ server.listen(port, () => {
59
+ console.log(`Server started on port ${port}`);
60
+ });
61
+ };
62
+
63
+ // 执行访问逻辑
64
+ const main = async () => {
65
+ createServer();
66
+ setInterval(async () => {
67
+ if (isWithinTime()) {
68
+ await visitAll();
69
+ } else {
70
+ console.log(`Stop visiting at ${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}`);
71
+ }
72
+ }, 2 * 60 * 1000); // 指定时间段访问的间隔2分钟
73
+ };
74
+ main();
75
+
76
+ //24小时不间断访问部分
77
+ async function access(url) {
78
+ try {
79
+ const response = await axios.get(url);
80
+ console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Web visited successfully: ${url} --- status:${response.status}`);
81
+ } catch (error) {
82
+ console.error(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Failed to visit ${url}, Error ${error.message}`);
83
+ }
84
+ }
85
+
86
+ async function batchVisit() {
87
+
88
+ for (let url of webpages) {
89
+ await access(url);
90
+ }
91
+ }
92
+ 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
+ }