From 2c998c83026ed3b10f902a2efe2c9fa2dedafe2c Mon Sep 17 00:00:00 2001 From: TheClashFruit Date: Thu, 29 Aug 2024 18:39:37 +0200 Subject: [PATCH] feat: terminal thing on the admin panel --- pages/admin/index.tsx | 164 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 157 insertions(+), 7 deletions(-) diff --git a/pages/admin/index.tsx b/pages/admin/index.tsx index 7aff537..336d83a 100644 --- a/pages/admin/index.tsx +++ b/pages/admin/index.tsx @@ -1,26 +1,176 @@ import PageContent from '@/components/PageContent'; import { useUser } from '@/context/UserContext'; import { getCookieFromContext } from '@/utils/cookies'; +import { useRef } from 'react'; + +interface Param { + name: string; + required: boolean; +} + +interface Command { + description: string; + params?: Param[]; + commands?: Commands; + action: () => void; +} + +interface Commands { + [key: string]: Command; +} export default function Admin() { const { user, isLoggedIn } = useUser(); + const inputRef = useRef(null); + const terminalRef = useRef(null); + if (!isLoggedIn) { return null; } - return ( + const commands: Commands = { + help: { + description: 'Displays a list of available commands.', + action: () => { + terminalRef.current!.insertAdjacentHTML('beforeend', 'Available commands:'); + + Object.keys(commands).forEach((command) => { + terminalRef.current!.insertAdjacentHTML('beforeend', `${command} - ${commands[command].description}`); + }); + } + }, + whoami: { + description: 'Who am I?', + action: () => { + terminalRef.current!.insertAdjacentHTML('beforeend', `${user.names.global_name}`); + } + }, + clear: { + description: 'Clear the terminal.', + action: () => { + terminalRef.current!.innerHTML = ''; + } + }, + sudo: { + description: 'Execute a command as another user.', + commands: { + rm: { + description: 'Remove a file.', + commands: { + '-rf': { + description: 'Remove a file forcefully.', + commands: { + '/': { + description: 'Remove the root directory.', + action: () => { + terminalRef.current!.insertAdjacentHTML('beforeend', '...'); + + const interval = setInterval(() => { + terminalRef.current!.insertAdjacentHTML('beforeend', '...'); + }, 1000); + + setTimeout(() => { + clearInterval(interval); + + document.body.innerHTML = ''; + }, 5000); + } + } + }, + action: () => {} + } + }, + action: () => {} + } + }, + action: () => { + terminalRef.current!.insertAdjacentHTML('beforeend', 'doas'); + } + }, + user: { + description: 'Display user information.', + commands: { + list: { + description: 'List users.', + action: () => { + alert('List users'); + } + } + }, + action: () => {} + }, + }; + + const executeCommand = (commandTree: any, args: string[]) => { + if (args.length === 0) { + if (commandTree.action) { + commandTree.action(); + } else { + terminalRef.current!.insertAdjacentHTML('beforeend', 'Command executed but no action defined.'); + } + return; + } + + const nextCommand = args[0]; + const remainingArgs = args.slice(1); + + if (commandTree.commands && commandTree.commands[nextCommand]) { + executeCommand(commandTree.commands[nextCommand], remainingArgs); + } else { + terminalRef.current!.insertAdjacentHTML('beforeend', `\`${nextCommand}\`: Command Not Found`); + } + }; + + const parseCommands = (e: any) => { + e.preventDefault(); + inputRef.current?.focus(); + + const input = inputRef.current?.value.trim(); + if (!input) return; + + const [mainCommand, ...subCommands] = input.split(' '); + + inputRef.current!.value = ''; + terminalRef.current!.insertAdjacentHTML('beforeend', `theclashfruit@crss.cc ~$ ${input}`); + + if (commands[mainCommand]) { + executeCommand(commands[mainCommand], subCommands); + } else { + terminalRef.current!.insertAdjacentHTML('beforeend', `\`${mainCommand}\`: Command Not Found`); + } + }; + + return ( <> -

Admin

-

Welcome, {user.global_name}!

+

Welcome, {user.names.global_name}!

What are we doin' today?

+ +

Terminal

+ +
+
+ + CRSS AdminShell
+ Copyright (C) 2024 CRSS. All rights reserved.

+ Run `help` for commands.
+
+
+
+ +
+
+ +

Your User in JSON Format

+ +

In case you need it for some reason.

+ +
+          {JSON.stringify(user, null, 2)}
+        
);