Spaces:
Running
Running
Upload index.js
Browse files
index.js
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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://nodejs-te5sbtg5.b4a.run', // Back4App
|
10 |
+
'https://0240b722-458d-4957-8977-5631b5ad1e59-00-26dr16bsciyin.sisko.replit.dev', //Replit 保活有问题
|
11 |
+
'http://fi3.bot-hosting.net:21286', //Bot-hosting
|
12 |
+
'http://45.140.165.216:1070', //Waifly
|
13 |
+
'http://sgp5.as.blare.host:25785', //Blare
|
14 |
+
'http://node4.lunes.host:1120', //Lunes
|
15 |
+
// ... 添加更多url
|
16 |
+
];
|
17 |
+
|
18 |
+
// 添加1:00-6:00暂停,其他时间正常访问的url数组
|
19 |
+
const urls = [
|
20 |
+
'https://www.google.com', // 备注名称
|
21 |
+
'https://www.baidu.com',
|
22 |
+
// ... 添加更多url
|
23 |
+
];
|
24 |
+
|
25 |
+
// 遍历网页数组并发请求访问网页
|
26 |
+
const visit = async (url) => {
|
27 |
+
try {
|
28 |
+
const response = await axios.get(url);
|
29 |
+
console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Visited web successfully: ${url} --- Status: ${response.status}\n`);
|
30 |
+
} catch (error) {
|
31 |
+
console.error(`Failed to visit ${url}: ${error.message}\n`);
|
32 |
+
}
|
33 |
+
};
|
34 |
+
const visitAll = async () => {
|
35 |
+
for (let url of urls) {
|
36 |
+
await visit(url);
|
37 |
+
}
|
38 |
+
};
|
39 |
+
|
40 |
+
// 定判断是否在访问时间段内
|
41 |
+
const isWithinTime = () => {
|
42 |
+
const now = moment().tz('Asia/Hong_Kong');
|
43 |
+
const hour = now.hour();
|
44 |
+
if (hour >= 1 && hour < 6) {
|
45 |
+
return false;
|
46 |
+
}
|
47 |
+
return true;
|
48 |
+
};
|
49 |
+
|
50 |
+
// 创建http服务
|
51 |
+
const createServer = () => {
|
52 |
+
const server = http.createServer((req, res) => {
|
53 |
+
if (req.url === '/') {
|
54 |
+
res.writeHead(200, {'Content-Type': 'text/plain'});
|
55 |
+
res.end('Hello world!');
|
56 |
+
} else {
|
57 |
+
res.writeHead(404, {'Content-Type': 'text/plain'});
|
58 |
+
res.end('404 Not Found');
|
59 |
+
}
|
60 |
+
});
|
61 |
+
server.listen(port, () => {
|
62 |
+
console.log(`Server started on port ${port}`);
|
63 |
+
});
|
64 |
+
};
|
65 |
+
|
66 |
+
// 执行访问逻辑
|
67 |
+
const main = async () => {
|
68 |
+
createServer();
|
69 |
+
setInterval(async () => {
|
70 |
+
if (isWithinTime()) {
|
71 |
+
await visitAll();
|
72 |
+
} else {
|
73 |
+
console.log(`Stop visiting at ${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')}`);
|
74 |
+
}
|
75 |
+
}, 2 * 60 * 1000); // 指定时间段访问的间隔2分钟
|
76 |
+
};
|
77 |
+
main();
|
78 |
+
|
79 |
+
//24小时不间断访问部分
|
80 |
+
async function access(url) {
|
81 |
+
try {
|
82 |
+
const response = await axios.get(url);
|
83 |
+
console.log(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Web visited successfully: ${url} --- status:${response.status}`);
|
84 |
+
} catch (error) {
|
85 |
+
console.error(`${moment().tz('Asia/Hong_Kong').format('YYYY-MM-DD HH:mm:ss')} Failed to visit ${url}, Error ${error.message}`);
|
86 |
+
}
|
87 |
+
}
|
88 |
+
|
89 |
+
async function batchVisit() {
|
90 |
+
|
91 |
+
for (let url of webpages) {
|
92 |
+
await access(url);
|
93 |
+
}
|
94 |
+
}
|
95 |
+
cron.schedule('*/2 * * * *', batchVisit); // 24小时访问任务间隔周期,默认2分钟
|