-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
119 lines (108 loc) · 3.36 KB
/
Copy pathserver.js
File metadata and controls
119 lines (108 loc) · 3.36 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const express = require('express');
const cors = require('cors');
const path = require('path');
const { status } = require('minecraft-server-util');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// Popular servers to monitor
const popularServers = [
{ name: 'Hypixel Network', host: 'mc.hypixel.net', port: 25565 },
{ name: 'Mineplex', host: 'us.mineplex.com', port: 25565 },
{ name: 'CubeCraft Games', host: 'play.cubecraft.net', port: 25565 },
{ name: 'The Hive', host: 'play.hivemc.com', port: 25565 }
];
// API Routes
app.get('/api/servers', async (req, res) => {
try {
const serverStatuses = await Promise.all(
popularServers.map(async (server) => {
try {
const result = await status(server.host, { port: server.port, timeout: 5000 });
return {
name: server.name,
host: server.host,
port: server.port,
online: true,
players: result.players.online,
maxPlayers: result.players.max,
version: result.version.name,
motd: result.motd.clean,
ping: result.roundTripLatency
};
} catch (error) {
return {
name: server.name,
host: server.host,
port: server.port,
online: false,
players: 0,
maxPlayers: 0,
version: 'Unknown',
motd: 'Server offline',
ping: null,
error: error.message
};
}
})
);
res.json(serverStatuses);
} catch (error) {
res.status(500).json({ error: 'Failed to fetch server statuses' });
}
});
app.post('/api/server/check', async (req, res) => {
try {
const { address } = req.body;
if (!address) {
return res.status(400).json({ error: 'Server address is required' });
}
// Parse address (could be domain:port or just domain)
const [host, portStr] = address.split(':');
const port = portStr ? parseInt(portStr) : 25565;
if (!host) {
return res.status(400).json({ error: 'Invalid server address' });
}
try {
const result = await status(host, { port: port, timeout: 10000 });
res.json({
name: host,
host: host,
port: port,
online: true,
players: result.players.online,
maxPlayers: result.players.max,
version: result.version.name,
motd: result.motd.clean,
ping: result.roundTripLatency,
favicon: result.favicon || null
});
} catch (error) {
res.json({
name: host,
host: host,
port: port,
online: false,
players: 0,
maxPlayers: 0,
version: 'Unknown',
motd: 'Server offline or unreachable',
ping: null,
error: error.message
});
}
} catch (error) {
res.status(500).json({ error: 'Server error occurred' });
}
});
// Serve the main HTML file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`ServerWatch is running on port ${PORT}`);
console.log(`Visit http://localhost:${PORT} to view the application`);
});