File size: 2,943 Bytes
fcf0093
 
 
 
 
 
 
 
3bb5e1f
 
f19cb85
ebe98de
3bb5e1f
 
fcf0093
 
 
 
 
fcdfb59
6c4a5be
fcf0093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01f0c44
fcf0093
 
 
 
 
 
 
 
 
 
 
 
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
const axios = require('axios');
const moment = require('moment-timezone');
const http = require('http');
const cron = require('node-cron');
const port = process.env.PORT || 7860;           

// 添加24小时是不间断访问的url数组
const webpages = [
  'https://yuehe-glitch.glitch.me',                      // Glitch
  'https://student00a.zeabur.app',                       // Zeabur
  'https://27722.zeabur.app',                            // Zeabur
  'https://ar-ren.onrender.com',                         // render
  'https://wakeful-complex-smelt.glitch.me',             // glitch保活
  'https://huggingface.co/spaces/heyue572/keep-alive',   // huggingface保活
  // ... 添加更多url
];

// 添加1:00-6:00暂停,其他时间正常访问的url数组
const urls = [
  'https://back4app1-q920aj4t.b4a.run',                  // back4app
  'https://www.baidu.com',
  // ... 添加更多url
];

// 遍历网页数组并发请求访问网页
const visit = async (url) => {
  try {
    const response = await axios.get(url);
    console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Visited web successfully: ${url} --- Status: ${response.status}\n`);
  } catch (error) {
    console.error(`Failed to visit ${url}: ${error.message}\n`);
  }
};
const visitAll = async () => {
  for (let url of urls) {
    await visit(url);
  }
};

// 定判断是否在访问时间段内
const isWithinTime = () => {
  const now = moment().tz('Asia/Hong_Kong');
  const hour = now.hour();
  if (hour >= 1 && hour < 6) {
    return false;
  }
  return true;
};

// 创建http服务
const createServer = () => {
  const server = http.createServer((req, res) => {
    if (req.url === '/') {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello world!');
    } else {
      res.writeHead(404, {'Content-Type': 'text/plain'});
      res.end('404 Not Found');
    }
  });
  server.listen(port, () => {
    console.log(`Server started on port ${port}`);
  });
};

// 执行访问逻辑
const main = async () => {
  createServer();
  setInterval(async () => {
    if (isWithinTime()) {
      await visitAll();
    } else {
      console.log(`Stop visiting at ${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}`);
    }
  }, 2 * 60 * 1000); // 指定时间段访问的间隔2分钟
};
main();

//24小时不间断访问部分
async function access(url) {
  try {
    const response = await axios.get(url);
    console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Web visited successfully: ${url} --- status:${response.status}`);
  } catch (error) {
    console.error(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Failed to visit ${url}, Error ${error.message}`);
  }
}

async function batchVisit() {

  for (let url of webpages) {
    await access(url);
  }
}
cron.schedule('*/2 * * * *', batchVisit); // 24小时访问任务间隔周期,默认2分钟