-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
77 lines (62 loc) · 1.69 KB
/
server.js
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
const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const url = require('url');
const MAX_TXT_URL = 'https://raw.githubusercontent.com/LovelyO0Sam/pgm-api/main/max.txt';
const MIN_TXT_URL = 'https://raw.githubusercontent.com/LovelyO0Sam/pgm-api/main/min.txt';
const MAX_TXT_PATH = path.join(__dirname, 'api-max.txt');
const MIN_TXT_PATH = path.join(__dirname, 'api-min.txt');
const PORT = 443;
fs.unlink(MAX_TXT_PATH, () => {});
fs.unlink(MIN_TXT_PATH, () => {});
let maxLines = [];
let minLines = [];
function downloadFiles() {
https.get(MAX_TXT_URL, (res) => {
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
maxLines = data.split('\n');
});
}).on('error', (err) => {
console.error(err);
});
https.get(MIN_TXT_URL, (res) => {
let data = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
minLines = data.split('\n');
});
}).on('error', (err) => {
console.error(err);
});
}
downloadFiles();
setInterval(downloadFiles, 24 * 60 * 60 * 1000);
const server = http.createServer((req, res) => {
const reqUrl = url.parse(req.url);
if (reqUrl.pathname === '/favicon.ico') {
res.statusCode = 404;
res.end();
return;
}
let lines;
if (reqUrl.pathname === '/api-min') {
lines = minLines;
} else {
lines = maxLines;
}
const line = lines[Math.floor(Math.random() * lines.length)];
res.setHeader('Content-Type', 'text/plain; charset=utf-8');
res.end(line);
});
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});