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; } 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.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)}
        
); } export async function getServerSideProps(context: any) { const { req } = context; const cookie = req.headers.cookie; let isLoggedIn = false; if (cookie) { const sessionCookie = getCookieFromContext('session', cookie); if (sessionCookie) { isLoggedIn = true; } } if (!isLoggedIn) { return { notFound: true, }; } return { props: {}, }; }