-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadminserver.js
More file actions
executable file
·609 lines (537 loc) · 16.1 KB
/
adminserver.js
File metadata and controls
executable file
·609 lines (537 loc) · 16.1 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
//
// adminserver.js
//
import express from "express";
import axios from "axios";
import dotenv from "dotenv";
import crypto from "node:crypto";
dotenv.config();
const app = express();
const PORT = process.env.ADMIN_PANEL_PORT || 4001;
const SESSION_COOKIE = "panel_session";
const SESSION_TTL_MS = 1000 * 60 * 60 * 12;
const STATE_TTL_MS = 1000 * 60 * 5;
const sessions = new Map();
const pendingStates = new Map();
const REQUIRED_ENV = [
"DISCORD_TOKEN",
"DISCORD_CLIENT_ID",
"DISCORD_CLIENT_SECRET",
"DISCORD_REDIRECT_URI",
"GUILD_ID",
"PANEL_ADMIN_ROLE_ID"
];
const missingEnv = REQUIRED_ENV.filter(key => !process.env[key]);
if (missingEnv.length) {
console.warn("Missing admin panel environment variables:", missingEnv.join(", "));
}
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const discordApi = axios.create({
baseURL: "https://discord.com/api",
headers: { Authorization: `Bot ${process.env.DISCORD_TOKEN}` }
});
const ROLE_EMBED_DESCRIPTION = `**@everyone** – Very important global notifications.
(Major issues/changes affecting all users)
**@here** – Urgent short-term notifications (max 6h)
---
### React For Roles
🌐 = **@site** – Site updates
🖥️ = **@monitor** – Tarkov Monitor updates
📋 = **@polls** – Community polls
📰 = **@news** – News & updates
🔔 = **@notifs** – All notifications`;
const ROLE_COMPONENTS = [
{
type: 1,
components: [
{ type: 2, style: 1, custom_id: "role_site", label: "🌐 Site" },
{ type: 2, style: 1, custom_id: "role_monitor", label: "🖥️ Monitor" },
{ type: 2, style: 1, custom_id: "role_polls", label: "📋 Polls" },
{ type: 2, style: 1, custom_id: "role_news", label: "📰 News" },
{ type: 2, style: 1, custom_id: "role_notifs", label: "🔔 Notifs" }
]
}
];
app.get("/", (req, res) => {
const session = getSession(req);
if (!session) return res.send(renderLanding());
return res.send(renderPanel(session.user));
});
app.get("/auth/discord", (req, res) => {
const state = createState();
const authorizeUrl = new URL("https://discord.com/api/oauth2/authorize");
authorizeUrl.searchParams.set("client_id", process.env.DISCORD_CLIENT_ID || "");
authorizeUrl.searchParams.set("redirect_uri", process.env.DISCORD_REDIRECT_URI || "");
authorizeUrl.searchParams.set("response_type", "code");
authorizeUrl.searchParams.set("scope", "identify");
authorizeUrl.searchParams.set("state", state);
res.redirect(authorizeUrl.toString());
});
app.get("/auth/discord/callback", async (req, res) => {
try {
const { code, state } = req.query;
if (!code || typeof code !== "string") return res.status(400).send("Missing code");
if (!consumeState(state)) return res.status(400).send("Invalid state");
const token = await exchangeCodeForToken(code);
const user = await fetchDiscordUser(token.access_token);
await assertUserIsAdmin(user.id);
const sessionId = createSession(user);
attachSession(res, sessionId);
res.redirect("/");
} catch (err) {
console.error("OAuth callback failed", err.response?.data || err.message);
res.status(401).send("Authentication failed. Ensure you have the Admin role.");
}
});
app.post("/logout", (req, res) => {
const session = getSession(req);
if (session) {
sessions.delete(session.id);
res.clearCookie(SESSION_COOKIE);
}
res.redirect("/");
});
app.post("/api/send-embed", requireAuth, async (req, res) => {
try {
await assertUserIsAdmin(req.user.id);
const { channelId, title, description, color } = req.body;
if (!channelId || !/^\d+$/.test(channelId)) {
return res.status(400).json({ error: "Invalid channel ID." });
}
if (!description || typeof description !== "string") {
return res.status(400).json({ error: "Description is required." });
}
const embed = buildEmbed({ title, description, color, author: req.user });
await discordApi.post(`/channels/${channelId}/messages`, { embeds: [embed] });
res.json({ ok: true });
} catch (err) {
console.error("Failed to send embed", err.response?.data || err.message);
res.status(500).json({ error: "Request failed." });
}
});
app.post("/api/send-role-message", requireAuth, async (req, res) => {
try {
await assertUserIsAdmin(req.user.id);
const { channelId } = req.body;
if (!channelId || !/^\d+$/.test(channelId)) {
return res.status(400).json({ error: "Invalid channel ID." });
}
const embed = {
title: "Reaction Roles",
description: ROLE_EMBED_DESCRIPTION,
color: 0x0099ff
};
await discordApi.post(`/channels/${channelId}/messages`, {
embeds: [embed],
components: ROLE_COMPONENTS
});
res.json({ ok: true });
} catch (err) {
console.error("Failed to send role message", err.response?.data || err.message);
res.status(500).json({ error: "Request failed." });
}
});
app.get("/health", (req, res) => res.json({ status: "ok" }));
app.listen(PORT, () => {
console.log(`Admin panel running on port ${PORT}`);
});
function buildEmbed({ title, description, color, author }) {
const trimmedTitle = typeof title === "string" && title.trim().length ? title.trim().slice(0, 256) : "Annonce";
const trimmedDescription = description.trim().slice(0, 2000);
const parsedColor = parseColor(color) ?? 0x5865f2;
return {
title: trimmedTitle,
description: trimmedDescription,
color: parsedColor,
timestamp: new Date().toISOString(),
footer: { text: `Posted by ${author.username}` }
};
}
function parseColor(value) {
if (typeof value !== "string") return null;
const hex = value.trim().replace("#", "");
if (!/^[0-9a-fA-F]{6}$/.test(hex)) return null;
return parseInt(hex, 16);
}
function renderLanding() {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tracker Admin Panel</title>
<style>${baseStyles()}</style>
</head>
<body class="landing">
<div class="shell fade">
<div class="hero-card card">
<p class="badge">Tracker Admin</p>
<h1>Sign in to manage announcements</h1>
<p class="lead">
Authenticate with your Discord account that has the admin role inside the TarkovTracker guild.
Once approved you can post embeds and rebuild the reaction-role panel directly from this dashboard.
</p>
<a class="button primary big" href="/auth/discord">Sign in with Discord</a>
</div>
</div>
</body>
</html>`;
}
function renderPanel(user) {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Tracker Admin Panel</title>
<style>${baseStyles()}</style>
</head>
<body>
<div class="shell fade">
<header class="toolbar">
<div>
<p class="badge">Tracker Admin</p>
<h1>Control Center</h1>
<span class="subtitle">Signed in as <strong>${escapeHtml(user.username)}</strong></span>
</div>
<form method="post" action="/logout">
<button class="button ghost" type="submit">Log out</button>
</form>
</header>
<section class="grid">
<article class="card form-card">
<header>
<h2>Custom Embed</h2>
<p>Push an embed to any text channel using the bot identity.</p>
</header>
<form id="customMessageForm">
<label>Channel ID
<input name="channelId" placeholder="123456789" required />
</label>
<label>Title
<input name="title" placeholder="Leave empty for a default title" />
</label>
<label>Description
<textarea name="description" rows="5" placeholder="Message body..." required></textarea>
</label>
<label>Embed color
<input type="color" name="color" value="#5865f2" />
</label>
<button class="button primary" type="submit">Send Embed</button>
</form>
</article>
<article class="card form-card">
<header>
<h2>Reaction Roles</h2>
<p>Re-post the full reaction role board after a cleanup.</p>
</header>
<form id="roleMessageForm">
<label>Channel ID
<input name="channelId" placeholder="123456789" required />
</label>
<button class="button primary" type="submit">Post Roles</button>
</form>
</article>
</section>
<div id="status" class="status"></div>
</div>
<script>
const statusBox = document.getElementById('status');
function setStatus(text, isError) {
statusBox.textContent = text;
statusBox.className = isError ? 'status error' : 'status success';
}
const customForm = document.getElementById('customMessageForm');
customForm.addEventListener('submit', async (event) => {
event.preventDefault();
const payload = Object.fromEntries(new FormData(customForm).entries());
try {
const res = await fetch('/api/send-embed', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!res.ok) throw new Error();
setStatus('Embed sent successfully ✅');
customForm.reset();
} catch {
setStatus('Failed to send embed', true);
}
});
const roleForm = document.getElementById('roleMessageForm');
roleForm.addEventListener('submit', async (event) => {
event.preventDefault();
const payload = Object.fromEntries(new FormData(roleForm).entries());
try {
const res = await fetch('/api/send-role-message', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!res.ok) throw new Error();
setStatus('Reaction role board sent ✅');
roleForm.reset();
} catch {
setStatus('Failed to post reaction roles', true);
}
});
</script>
</body>
</html>`;
}
function baseStyles() {
return `
:root {
font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
--bg: radial-gradient(circle at top, #1f2a44, #0c111c 60%);
--card: rgba(18, 23, 35, 0.95);
--border: rgba(255, 255, 255, 0.08);
--accent: #7f8cff;
--accent-hover: #6c75ff;
--ghost: rgba(255, 255, 255, 0.1);
--text: #f7f8fc;
--muted: rgba(247, 248, 252, 0.7);
--radius: 18px;
--shadow: 0 25px 60px rgba(0, 0, 0, 0.45);
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
background: var(--bg);
color: var(--text);
}
.shell {
max-width: 960px;
margin: 0 auto;
padding: 40px 24px 80px;
}
.fade {
animation: fadeIn 0.35s ease;
}
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 28px;
box-shadow: var(--shadow);
backdrop-filter: blur(10px);
}
.hero-card h1 {
margin: 12px 0 8px;
}
.lead {
color: var(--muted);
line-height: 1.6;
margin-bottom: 28px;
}
.badge {
display: inline-flex;
align-items: center;
gap: 6px;
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.08em;
color: var(--muted);
}
.toolbar {
display: flex;
justify-content: space-between;
align-items: flex-start;
margin-bottom: 28px;
gap: 20px;
}
.subtitle {
color: var(--muted);
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
}
.form-card header p {
color: var(--muted);
margin: 4px 0 0;
}
label {
display: block;
margin-top: 18px;
font-weight: 600;
font-size: 0.95rem;
}
input,
textarea {
width: 100%;
margin-top: 8px;
padding: 12px 14px;
border-radius: 12px;
border: 1px solid var(--border);
background: rgba(255, 255, 255, 0.04);
color: var(--text);
font-size: 1rem;
transition: border 0.2s ease, box-shadow 0.2s ease;
}
input:focus,
textarea:focus {
border-color: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
outline: none;
}
textarea {
resize: vertical;
}
.button {
margin-top: 22px;
padding: 13px 22px;
border-radius: 999px;
border: none;
font-weight: 600;
font-size: 1rem;
cursor: pointer;
color: #fff;
transition: background 0.2s ease, transform 0.2s ease;
}
.button.primary {
background: linear-gradient(120deg, #7f8cff, #a77bff);
}
.button.primary:hover {
background: linear-gradient(120deg, #6c75ff, #9665ff);
transform: translateY(-1px);
}
.button.big {
width: fit-content;
padding-inline: 32px;
font-size: 1.05rem;
}
.button.ghost {
background: var(--ghost);
color: var(--text);
}
.button.ghost:hover {
background: rgba(255, 255, 255, 0.2);
transform: translateY(-1px);
}
.status {
margin-top: 28px;
padding: 14px 18px;
border-radius: 12px;
font-weight: 600;
text-align: center;
display: none;
}
.status.success {
display: block;
background: rgba(74, 222, 128, 0.12);
color: #86efac;
border: 1px solid rgba(74, 222, 128, 0.4);
}
.status.error {
display: block;
background: rgba(248, 113, 113, 0.12);
color: #fca5a5;
border: 1px solid rgba(248, 113, 113, 0.4);
}
@media (max-width: 640px) {
.toolbar {
flex-direction: column;
align-items: stretch;
}
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(12px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
`;
}
function getSession(req) {
const cookieHeader = req.headers.cookie;
if (!cookieHeader) return null;
const cookies = cookieHeader.split(";").map(part => part.trim());
const target = cookies.find(entry => entry.startsWith(`${SESSION_COOKIE}=`));
if (!target) return null;
const value = decodeURIComponent(target.substring(SESSION_COOKIE.length + 1));
const session = sessions.get(value);
if (!session) return null;
if (Date.now() - session.createdAt > SESSION_TTL_MS) {
sessions.delete(value);
return null;
}
return { id: value, ...session };
}
function createSession(user) {
const id = crypto.randomBytes(24).toString("hex");
sessions.set(id, { user, createdAt: Date.now() });
return id;
}
function attachSession(res, id) {
res.cookie(SESSION_COOKIE, id, {
httpOnly: true,
sameSite: "lax",
secure: (process.env.NODE_ENV === "production"),
maxAge: SESSION_TTL_MS
});
}
function requireAuth(req, res, next) {
const session = getSession(req);
if (!session) return res.status(401).json({ error: "Not authenticated" });
req.user = session.user;
return next();
}
function createState() {
const state = crypto.randomBytes(16).toString("hex");
pendingStates.set(state, Date.now());
return state;
}
function consumeState(state) {
if (!state || typeof state !== "string") return false;
const createdAt = pendingStates.get(state);
pendingStates.delete(state);
return createdAt && Date.now() - createdAt <= STATE_TTL_MS;
}
async function exchangeCodeForToken(code) {
const data = new URLSearchParams({
client_id: process.env.DISCORD_CLIENT_ID,
client_secret: process.env.DISCORD_CLIENT_SECRET,
grant_type: "authorization_code",
code,
redirect_uri: process.env.DISCORD_REDIRECT_URI,
scope: "identify"
});
const response = await axios.post("https://discord.com/api/oauth2/token", data.toString(), {
headers: { "Content-Type": "application/x-www-form-urlencoded" }
});
return response.data;
}
async function fetchDiscordUser(accessToken) {
const response = await axios.get("https://discord.com/api/users/@me", {
headers: { Authorization: `Bearer ${accessToken}` }
});
return response.data;
}
async function assertUserIsAdmin(userId) {
const guildId = process.env.GUILD_ID;
const adminRoleId = process.env.PANEL_ADMIN_ROLE_ID;
const response = await discordApi.get(`/guilds/${guildId}/members/${userId}`);
const roles = response.data?.roles || [];
if (!roles.includes(adminRoleId)) {
throw new Error("User missing admin role");
}
}
function escapeHtml(str) {
return String(str).replace(/[&<>"']/g, match => ({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
})[match]);
}