Files
gornodox.dev/src/lib/stores/bash/bash.ts
T

243 lines
6.2 KiB
TypeScript
Executable File

import { Stack } from '../stack';
import { Terminal, type PrintData } from '../terminal/terminal';
import {
COMMANDS,
type CommandArgs,
type CommandResultData,
type ICommand
} from './commandRegistry';
import { GROUP, type Group } from './etc/groupData';
import { PASSWD, type User } from './etc/userData';
import { VirtualFS, type TreeNode } from './fs';
import type { KeybindInhibitor } from './keybindInhibitor';
import { ExitCode } from './metadata';
import { Process } from './process';
export enum Signal {
SIGINT,
SIGKILL
}
export type BashInitArgs = {
io?: Terminal;
fs: Map<number, TreeNode>;
user: { username: string; password: string };
};
export type Result = {
exitCode: ExitCode;
path: number; //the inode of the place that the command was executed in
resultData?: CommandResultData;
};
export class Bash {
private readonly vfs: VirtualFS;
private readonly _passwd: User[];
private readonly _group: Group[];
private readonly _terminal!: Terminal;
private readonly _commands: Record<string, ICommand>;
private _userInstances: Stack<User>;
private user: User;
private _process?: Process;
constructor(args: BashInitArgs) {
this._commands = COMMANDS;
this._passwd = PASSWD;
this._group = GROUP;
this._userInstances = new Stack<User>();
this._terminal = args.io!;
const loginResult = this.userLogin(args.user.username, args.user.password);
if (loginResult == ExitCode.ERROR)
this._terminal.throwExeption(
`Failed to initialize bash instance - access denied for user ${args.user.username}`,
ExitCode.ERROR
);
this.user = this._userInstances.peek()!;
this.vfs = new VirtualFS({ fs: args.fs, user: this.user });
}
private _initNewUserSession(user: User): User {
if (!this._passwd.includes(user))
this._terminal.throwExeption(
`user not found under the name ${user.username}`,
ExitCode.ERROR
);
this._userInstances.push(user);
return user;
}
private async _initNewProcess(command: ICommand, args: CommandArgs) {
this._process = new Process(this, this.user.uid, 'TEMP PROCESS NAME', command, args);
//this._terminal.isProcessActive = true;
}
private _appendNewResult(inode: number, output: any, cmd: string) {
const data: PrintData = {
path: this.vfs.formatPath(this.vfs.getPathByInode(inode)),
output: output,
cmd: cmd
};
console.log(this.vfs.formatPath(this.vfs.getPathByInode(inode)), 'TEST TEST !@#');
this._terminal.PrintOutput(data);
}
updateHistory(input: string): void {
if ((this.user.history.length = 255)) {
this.user.history.unshift(...this.user.history.splice(-1));
this.user.history[0] = input;
} else {
this.user.history.push(input);
this.user.history.unshift(...this.user.history.splice(-1));
}
}
clearTerminal(): void {
this._terminal.clearTerminal();
}
getCwd(): number {
return this.vfs.cwd;
}
getPwd(): number {
return this.vfs.pwd;
}
getUser(): User {
return this.user;
}
getFs(): VirtualFS {
return this.vfs;
}
getProcess(): Process | ExitCode.ERROR {
if (this._process) {
return this._process;
}
return ExitCode.ERROR;
}
getProcessKeybindInhibitor(): KeybindInhibitor | null {
if (this._process) {
return this._process.getKeybindInhibitor();
} else return null;
}
getTerminalWidth(): number {
return this._terminal.getTerminalWidth();
}
getTerminalFontSize(): number {
return this._terminal.getFontSize();
}
hasSudoPerms(uid: number): boolean {
return this._group[0].members.includes(uid);
}
async executeCommand(commandName: string, args: CommandArgs): Promise<Result> {
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
const command: ICommand = this._commands[commandName];
if (!command) this.throwError(result);
if (command.root) {
if (this.hasSudoPerms(this.user.uid)) {
await this._initNewProcess(command, args);
if (!this._process) throw new Error('failed to init new process');
const out: Result = await this._process.getResult();
this._appendNewResult(this.getCwd(), out, this.user.history[0]);
}
this.throwError(result);
}
await this._initNewProcess(command, args);
if (!this._process) throw new Error('failed to init new process');
const out: Result = await this._process.getResult();
console.log(out);
this._appendNewResult(out.path, out.resultData?.data, this.user.history[0]);
return out;
}
static testMethod() {
//
}
throwError(result: Result): void {
switch (result.exitCode) {
default: {
throw new Error(`Error, dont know where, just look for it;`);
}
}
}
userLogout() {
this._userInstances.pop();
if (this._userInstances.size() === 0) {
//TODO: Implement system logout
} else {
//this.changeUser(this._instances.peek()!);
}
}
userLogin(username: string, passwd: string): ExitCode {
const user: User | undefined = this._passwd.find((user) => user.username === username);
if (user && user.passwd === passwd) {
this._initNewUserSession(user);
return ExitCode.SUCCESS;
}
return ExitCode.ERROR;
}
formatBytes(bytes: number, dPoint?: number, pow: 1024 | 1000 = 1024): string {
if (!+bytes) return '0';
const k: number = pow;
const dp: number = dPoint ? (dPoint < 0 ? 0 : dPoint) : 1;
const units: string[] = ['', 'K', 'M', 'G', 'T', 'P'];
const i: number = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(dp)}${units[i]}`;
}
getGroupByName(name: string): Group {
const out: Group | undefined = this._group.find((group) => group.groupname === name);
if (out) return out;
else throw new Error(`Cannot find a user group named ${name}`);
}
getUserByName(name: string): User {
const out: User | undefined = this._passwd.find((user) => user.username === name);
if (out) return out;
else throw new Error(`Cannot find a user named ${name}`);
}
getGroupByGid(gid: number): Group {
const out: Group | undefined = this._group.find((group) => group.gid === gid);
if (out) return out;
else throw new Error(`Cannot find a user group with id of ${gid}`);
}
getUserByUid(uid: number): User {
const out: User | undefined = this._passwd.find((user) => user.uid === uid);
if (out) return out;
else throw new Error(`Cannot find a user with id of ${uid}`);
}
}