-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
749 lines (644 loc) · 23.4 KB
/
server.js
File metadata and controls
749 lines (644 loc) · 23.4 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
import os from 'os';
import fs from 'fs';
import path from 'path';
import si from 'systeminformation';
import axios from 'axios';
import bcrypt from 'bcrypt';
import Parser from 'rss-parser';
import QRCode from 'qrcode';
import osu from 'os-utils';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const parser = new Parser();
const fastify = Fastify({ logger: { level: 'error' } });
const PORT = 6969;
let networkCache = null;
let networkCacheTime = 0;
const NETWORK_CACHE_TTL = 5 * 60 * 1000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'dist'),
prefix: '/',
});
fastify.addContentTypeParser('application/json', { parseAs: 'string' }, (req, body, done) => {
try {
const json = JSON.parse(body);
done(null, json);
} catch (err) {
done(err, undefined);
}
});
const linksFile = path.join(__dirname, "json/links.json");
const settingsFile = path.join(__dirname, "json/settings.json");
const serverLog = path.join(__dirname, "server.log");
const networkLog = path.join(__dirname, "network.log");
const notesFile = path.join(__dirname, "json/notes.json");
let settings = {};
let links = [];
let notes = {};
function initFiles() {
const jsonDir = path.join(__dirname, "json");
if (!fs.existsSync(jsonDir)) {
fs.mkdirSync(jsonDir, { recursive: true });
}
if (!fs.existsSync(linksFile)) {
const defaultLinks = [
{
name: "msehic",
url: "https://msehic.com",
icon: "bi-link-45deg",
category: "External",
opened: 0,
},
];
fs.writeFileSync(linksFile, JSON.stringify(defaultLinks, null, 2), "utf8");
links = defaultLinks;
} else {
links = JSON.parse(fs.readFileSync(linksFile, "utf8"));
}
if (!fs.existsSync(settingsFile)) {
const defaultSettings = {
server: "Server name",
name: "Nickname",
refreshInterval: 30,
welcome: false,
bgPath: "./assets/background.jpg",
rss: "https://hnrss.org/frontpage",
login: "",
diskPaths: ["/"],
tools: {
Process: true,
Crypto: true,
Notes: true,
RSS: true,
Hardware: true,
QR: true
},
};
fs.writeFileSync(settingsFile, JSON.stringify(defaultSettings, null, 2), "utf8");
settings = defaultSettings;
} else {
settings = JSON.parse(fs.readFileSync(settingsFile, "utf8"));
}
if (!fs.existsSync(serverLog)) {
fs.writeFileSync(serverLog, `[${new Date().toISOString()}] Server log initialized\n`, "utf8");
}
if (!fs.existsSync(networkLog)) {
fs.writeFileSync(networkLog, `[${new Date().toISOString()}] Network log initialized\n`, "utf8");
}
if (!fs.existsSync(notesFile)) {
const defaultNotes = {
notes: "Welcome to Server Homepage Notes!\nThis is your personal space for quick notes and reminders.\nLooking for a more advanced notes app for your server?\nCheck out: https://github.com/MuxBH28/brahke-pisar",
lastEdited: new Date().toISOString(),
};
fs.writeFileSync(notesFile, JSON.stringify(defaultNotes, null, 2), "utf8");
notes = defaultNotes;
} else {
notes = JSON.parse(fs.readFileSync(notesFile, "utf8"));
}
}
async function logSystemStats() {
try {
const cpuLoad = await getCpuUsage();
const cpuTemp = await getCpuTemp();
const totalMem = os.totalmem();
const usedMem = totalMem - os.freemem();
const memPercent = ((usedMem / totalMem) * 100).toFixed(2);
const cpuLoadNum = Number(cpuLoad) || 0;
const now = new Date();
const timestamp = now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
const logLine = `[${timestamp}] CPU: ${cpuLoadNum.toFixed(2)}% | RAM: ${memPercent}% | TEMP: ${cpuTemp || 'N/A'}°C`;
let systemLines = [];
if (fs.existsSync(serverLog)) {
const data = fs.readFileSync(serverLog, "utf8");
systemLines = data.trim().split("\n");
}
systemLines.push(logLine);
const MAX_LINES = 60 * 8;
if (systemLines.length > MAX_LINES) {
systemLines = systemLines.slice(systemLines.length - MAX_LINES);
}
fs.writeFileSync(serverLog, systemLines.join("\n") + "\n", "utf8");
const netStats = await si.networkStats();
const downloadSpeed = (netStats[0].rx_sec / 1024 / 1024).toFixed(2) + " MB/s";
const uploadSpeed = (netStats[0].tx_sec / 1024 / 1024).toFixed(2) + " MB/s";
const netLogLine = `[${timestamp}] UP: ${uploadSpeed} | DOWN: ${downloadSpeed}`;
let netLines = [];
if (fs.existsSync(networkLog)) {
const netData = fs.readFileSync(networkLog, "utf8");
netLines = netData.trim().split("\n");
}
netLines.push(netLogLine);
if (netLines.length > MAX_LINES) {
netLines = netLines.slice(netLines.length - MAX_LINES);
}
fs.writeFileSync(networkLog, netLines.join("\n") + "\n", "utf8");
} catch (err) {
console.error("Error logging system or network stats:", err.message);
}
}
fastify.get('/api/links', async (request, reply) => {
try {
const data = await fs.promises.readFile(linksFile, 'utf8');
reply.send(JSON.parse(data));
} catch (err) {
reply.status(500).send({ error: 'Failed to read links' });
}
});
fastify.get('/api/system', async (request, reply) => {
try {
const cpuLoad = await getCpuUsage();
const mem = process.memoryUsage();
const uptime = process.uptime();
const fsData = await si.fsSize();
const diskInfo = {};
for (const p of settings.diskPaths) {
const match = fsData.find(d => d.mount === p || d.fs === p);
if (match) {
diskInfo[p] = {
total: match.size,
free: match.size - match.used,
used: match.used,
usedPercent: ((match.used / match.size) * 100).toFixed(2)
};
} else {
diskInfo[p] = null;
}
}
const cpuTemp = await getCpuTemp();
reply.send({
cpu_percent: cpuLoad,
memory: {
rss: mem.rss,
heapTotal: mem.heapTotal,
heapUsed: mem.heapUsed,
external: mem.external,
freeMem: os.freemem(),
totalMem: os.totalmem()
},
uptime: formatUptime(uptime),
cpu_temp: cpuTemp,
disk: diskInfo
});
} catch (e) {
reply.status(500).send({ error: e.message });
}
});
fastify.post('/api/links', async (request, reply) => {
const { name, url, icon = 'bi-link-45deg', category = 'Other', sidebar = false } = request.body;
if (!name || !url)
return reply.status(400).send({ error: "Name and URL are required" });
try {
const data = await fs.promises.readFile(linksFile, 'utf8');
let links = [];
try {
links = JSON.parse(data);
} catch (e) {
return reply.status(500).send({ error: 'Corrupt links data' });
}
links.push({ name, url, icon, category, sidebar, opened: 0 });
await fs.promises.writeFile(linksFile, JSON.stringify(links, null, 2), 'utf8');
reply.send({ success: true });
} catch (err) {
reply.status(500).send({ error: 'Failed to save link' });
}
});
fastify.delete('/api/links/:index', async (request, reply) => {
const index = parseInt(request.params.index);
try {
const data = await fs.promises.readFile(linksFile, 'utf8');
let links = JSON.parse(data);
if (index < 0 || index >= links.length) {
return reply.status(400).send({ error: 'Invalid index' });
}
links.splice(index, 1);
await fs.promises.writeFile(linksFile, JSON.stringify(links, null, 2), 'utf8');
reply.send({ success: true });
} catch (err) {
reply.status(500).send({ error: 'Failed to delete link' });
}
});
fastify.put('/api/links/:index', async (request, reply) => {
const index = parseInt(request.params.index);
const { name, url, icon = 'bi-link-45deg', category = 'Other', sidebar = false } = request.body;
if (!name || !url) {
return reply.status(400).send({ error: "Name and URL are required" });
}
try {
const data = await fs.promises.readFile(linksFile, 'utf8');
let links = JSON.parse(data);
if (index < 0 || index >= links.length) {
return reply.status(400).send({ error: 'Invalid index' });
}
links[index] = { ...links[index], name, url, icon, category, sidebar };
await fs.promises.writeFile(linksFile, JSON.stringify(links, null, 2), 'utf8');
reply.send({ success: true });
} catch (err) {
reply.status(500).send({ error: 'Failed to update link' });
}
});
fastify.get('/api/process', async (request, reply) => {
try {
const data = await si.processes();
const sorted = data.list.sort((a, b) => b.cpu - a.cpu);
const topProcesses = sorted.slice(0, 15).map(proc => ({
pid: proc.pid,
name: proc.name,
cpu: proc.cpu.toFixed(1),
memory: proc.memRss
}));
reply.send(topProcesses);
} catch (err) {
reply.status(500).send({ error: err.message });
}
});
fastify.post('/api/links/:encodedUrl/increment', async (request, reply) => {
const url = decodeURIComponent(request.params.encodedUrl);
try {
const data = await fs.promises.readFile(linksFile, 'utf8');
let links = [];
try {
links = JSON.parse(data);
} catch {
return reply.status(500).send({ error: 'Corrupt links data' });
}
const link = links.find(l => l.url === url);
if (!link) return reply.status(400).send({ error: 'Invalid link URL' });
link.opened = (link.opened || 0) + 1;
await fs.promises.writeFile(linksFile, JSON.stringify(links, null, 2), 'utf8');
reply.send({ success: true, opened: link.opened });
} catch (err) {
reply.status(500).send({ error: 'Failed to save link' });
}
});
fastify.get("/api/settings", async (request, reply) => {
try {
const data = await fs.promises.readFile(settingsFile, "utf8");
const settings = JSON.parse(data);
const appVersions = await getVersion();
settings.appVersions = appVersions;
reply.send(settings);
} catch (err) {
console.error("Error reading settings or versions:", err);
reply.status(500).send({ error: "Failed to load settings or versions." });
}
});
fastify.post("/api/settings", async (request, reply) => {
try {
await fs.promises.writeFile(settingsFile, JSON.stringify(request.body, null, 2), "utf8");
reply.send({ success: true });
} catch (err) {
console.error("Error writing settings.json:", err);
reply.status(500).send({ error: "Error saving settings." });
}
});
fastify.get("/api/logs", async (request, reply) => {
try {
if (!fs.existsSync(serverLog)) return reply.send([]);
const data = await fs.promises.readFile(serverLog, "utf8");
const lines = data
.split("\n")
.filter(line => line.trim() !== "")
.map(line => {
const match = line.match(/\[(.*?)\] CPU: ([\d.]+)% \| RAM: ([\d.]+)% \| TEMP: (.*)°C/);
if (match) {
return {
timestamp: match[1],
cpu: parseFloat(match[2]),
ram: parseFloat(match[3]),
temp: match[4] === "N/A" ? null : parseFloat(match[4])
};
} else {
return { raw: line };
}
});
reply.send(lines);
} catch (err) {
console.error("Error reading server log:", err.message);
reply.status(500).send({ error: "Failed to read server log." });
}
});
fastify.get("/api/network-logs", async (request, reply) => {
try {
if (!fs.existsSync(networkLog)) return reply.send([]);
const data = await fs.promises.readFile(networkLog, "utf8");
const lines = data
.split("\n")
.filter(line => line.trim() !== "")
.map(line => {
const match = line.match(/\[(.*?)\] UP: ([\d.]+) MB\/s \| DOWN: ([\d.]+) MB\/s/);
if (match) {
return {
timestamp: match[1],
upload: parseFloat(match[2]),
download: parseFloat(match[3])
};
} else {
return { raw: line };
}
});
reply.send(lines);
} catch (err) {
console.error("Error reading network log:", err.message);
reply.status(500).send({ error: "Failed to read network log." });
}
});
fastify.get("/api/info", async (request, reply) => {
try {
const cpu = await si.cpu();
const mem = os.totalmem();
const osInfo = await si.osInfo();
const disk = await si.diskLayout();
const graphics = await si.graphics();
const network = await si.networkInterfaces();
const bytesToGB = (bytes) => +(bytes / 1024 / 1024 / 1024).toFixed(2);
reply.send({
cpu: {
manufacturer: cpu.manufacturer,
brand: cpu.brand,
speed: cpu.speed,
cores: cpu.cores,
physicalCores: cpu.physicalCores,
},
memory: {
totalGB: bytesToGB(mem),
},
os: {
platform: osInfo.platform,
distro: osInfo.distro,
release: osInfo.release,
kernel: osInfo.kernel,
arch: osInfo.arch,
hostname: osInfo.hostname,
},
disk: disk.map(d => ({
device: d.device,
type: d.type,
sizeGB: bytesToGB(d.size),
name: d.name,
vendor: d.vendor
})),
graphics: graphics.controllers.map(g => ({
model: g.model,
vendor: g.vendor,
vramGB: bytesToGB(g.vram * 1024 * 1024)
})),
network: network.map(n => ({
iface: n.iface,
ip4: n.ip4,
ip6: n.ip6,
mac: n.mac,
internal: n.internal
}))
});
} catch (err) {
console.error("Error fetching system info:", err);
reply.status(500).send({ error: "Failed to fetch system info" });
}
});
fastify.get("/api/crypto", async (request, reply) => {
try {
const resp = await axios.get("https://api.coingecko.com/api/v3/simple/price", {
params: {
ids: "bitcoin,ethereum,dogecoin,litecoin,cardano,solana,polkadot",
vs_currencies: "usd"
}
});
reply.send(resp.data);
} catch (err) {
console.error("Error fetching crypto:", err.message);
reply.status(500).send({ error: "Failed to fetch crypto data." });
}
});
fastify.get("/api/notes", async (request, reply) => {
try {
if (!fs.existsSync(notesFile)) return reply.send([]);
const data = JSON.parse(fs.readFileSync(notesFile, "utf8"));
reply.send(data);
} catch (err) {
console.error("Error reading notes:", err.message);
reply.status(500).send({ error: "Failed to read notes." });
}
});
fastify.post("/api/notes", async (request, reply) => {
try {
const notes = request.body || [];
fs.writeFileSync(notesFile, JSON.stringify(notes, null, 2), "utf8");
reply.send({ success: true, data: notes });
} catch (err) {
console.error("Error saving notes:", err.message);
reply.status(500).send({ error: "Failed to save notes." });
}
});
fastify.get("/api/rss", async (request, reply) => {
try {
const feedUrl = settings.rss || "https://hnrss.org/frontpage";
const feed = await parser.parseURL(feedUrl);
const items = feed.items.slice(0, 12).map((item) => ({
title: item.title,
link: item.link,
pubDate: item.pubDate
}));
reply.send(items);
} catch (err) {
console.error("Error fetching RSS:", err.message);
reply.status(500).send({ error: "Failed to fetch RSS feed." });
}
});
fastify.post("/api/set-login", async (request, reply) => {
try {
const pin = request.body.pin || "";
if (!pin || pin.length < 4) {
return reply
.status(400)
.send({ error: "PIN must be at least 4 characters long." });
}
const hashedPin = await bcrypt.hash(pin, 10);
if (!fs.existsSync(settingsFile)) {
return reply.status(500).send({ error: "Settings file not found." });
}
const settingsData = JSON.parse(fs.readFileSync(settingsFile, "utf8"));
settingsData.login = hashedPin;
fs.writeFileSync(settingsFile, JSON.stringify(settingsData, null, 2), "utf8");
settings.login = hashedPin;
reply.send({ success: true, message: "PIN saved successfully." });
} catch (err) {
console.error("Error setting login PIN:", err.message);
reply.status(500).send({ error: "Failed to set login PIN." });
}
});
fastify.post("/api/check-login", async (request, reply) => {
try {
const inputPin = request.body.pin;
if (!settings.login || !inputPin) {
return reply.send({ success: false });
}
bcrypt.compare(inputPin, settings.login, (err, result) => {
if (err) {
console.error("Error comparing PIN:", err.message);
return reply.status(500).send({ success: false, error: "Server error" });
}
reply.send({ success: result });
});
} catch (err) {
console.error("Error in check-login:", err.message);
reply.status(500).send({ success: false, error: "Server error" });
}
});
fastify.post("/api/linksFile", async (request, reply) => {
const newLinks = request.body;
if (!newLinks) {
return reply.status(400).send({ error: "No data provided" });
}
try {
fs.writeFileSync(linksFile, JSON.stringify(newLinks, null, 2), "utf8");
reply.send({ message: "Links updated successfully" });
} catch (err) {
reply.status(500).send({ error: "Failed to write links" });
}
});
fastify.post("/api/urltoqrl", async (request, reply) => {
const { text, type, width, color, bgColor } = request.body;
const qrPath = path.join(__dirname, "dist", "assets", `qrcode.${type}`);
try {
await QRCode.toFile(qrPath, text, {
type,
width,
margin: 2,
color: {
dark: color,
light: bgColor,
},
});
reply.send({ success: true });
} catch (err) {
console.error("QR code generation error:", err.message);
reply.status(500).send({ error: "Failed to generate QR code" });
}
});
fastify.get("/api/network", async (request, reply) => {
const now = Date.now();
if (!networkCache || now - networkCacheTime >= NETWORK_CACHE_TTL) {
const nets = os.networkInterfaces();
let localIP = null;
let ifaceName = null;
let state = null;
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
if (net.family === "IPv4" && !net.internal) {
localIP = net.address;
ifaceName = name;
state = net.mac ? "up" : "down";
break;
}
}
if (localIP) break;
}
let publicIP = "N/A";
let city = "N/A";
let country = "N/A";
let loc = "N/A";
try {
const response = await axios.get("https://ipinfo.io/json");
const data = response.data;
publicIP = data.ip || publicIP;
city = data.city || city;
country = data.country || country;
loc = data.loc || loc;
} catch (err) {
fastify.log.error("Failed to fetch public IP info:", err.message);
}
networkCache = {
ifaceName,
state,
local_ip: localIP,
public_ip: publicIP,
city,
country,
loc,
};
networkCacheTime = now;
}
const netStats = await si.networkStats();
const downloadSpeed =
(netStats[0].rx_sec / 1024 / 1024).toFixed(2) + " MB/s";
const uploadSpeed =
(netStats[0].tx_sec / 1024 / 1024).toFixed(2) + " MB/s";
reply.send({
...networkCache,
download_speed: downloadSpeed,
upload_speed: uploadSpeed,
});
});
fastify.get("/api/temperature", async (request, reply) => {
try {
const temp = await getCpuTemp();
const hours = new Date().getHours();
reply.send({
temperature: temp,
hour: hours,
});
} catch (err) {
reply.status(500).send({ error: "Failed to get CPU temperature" });
}
});
async function getVersion() {
const appVersions = {
local: "N/A",
github: "N/A",
};
try {
const packagePath = path.join(__dirname, "package.json");
const packageData = JSON.parse(fs.readFileSync(packagePath, "utf8"));
appVersions.local = packageData.version || "N/A";
const githubUrl = "https://raw.githubusercontent.com/MuxBH28/server-homepage/main/package.json";
const response = await axios.get(githubUrl);
const githubData = response.data;
appVersions.github = githubData.version || "N/A";
} catch (err) {
console.error("Failed to get versions:", err.message);
}
return appVersions;
}
function getCpuUsage() {
return new Promise((resolve) => {
osu.cpuUsage((v) => {
resolve((v * 100).toFixed(2));
});
});
}
async function getCpuTemp() {
try {
const temp = await si.cpuTemperature();
return temp.main ? temp.main.toFixed(1) : null;
} catch (err) {
console.error(err);
return null;
}
}
function formatUptime(seconds) {
const d = Math.floor(seconds / (3600 * 24));
const h = Math.floor((seconds % (3600 * 24)) / 3600);
const m = Math.floor((seconds % 3600) / 60);
const s = Math.floor(seconds % 60);
return `${d}d ${h}h ${m}m ${s}s`;
}
fastify.setNotFoundHandler((request, reply) => {
reply.sendFile("index.html");
});
const start = async () => {
try {
initFiles();
logSystemStats();
setInterval(logSystemStats, 60 * 1000);
await fastify.listen({ port: PORT, host: "0.0.0.0" });
console.log(`Server running at http://0.0.0.0:${PORT}`);
} catch (err) {
console.error("Error starting server:", err);
process.exit(1);
}
};
start();