add stdin and half-baked su command

This commit is contained in:
b1ek 2023-05-05 14:36:47 +10:00
parent ec105acfb8
commit 33cea94f6d
Signed by: blek
GPG Key ID: 14546221E3595D0C
3 changed files with 109 additions and 10 deletions

View File

@ -8,7 +8,6 @@ let cmds = {
'wget': require('./wget'), 'wget': require('./wget'),
'export_file': require('./export_file'), 'export_file': require('./export_file'),
'import_file': require('./import_file'), 'import_file': require('./import_file'),
'zsh': require('./zsh'),
'ps': require('./ps'), 'ps': require('./ps'),
'clear': require('./clear'), 'clear': require('./clear'),
'rm': require('./rm'), 'rm': require('./rm'),
@ -17,6 +16,10 @@ let cmds = {
'systemctl': require('./systemctl'), 'systemctl': require('./systemctl'),
'set-hostname': require('./set-hostname.js'), 'set-hostname': require('./set-hostname.js'),
// funny stuff
'su': require('./su'),
'zsh': require('./zsh'),
'guide': require('./guide'), 'guide': require('./guide'),
'skills': require('./skills'), 'skills': require('./skills'),

View File

@ -0,0 +1,31 @@
const { Terminal } = require("xterm");
/**
* @param {string[]} argv
* @param {Terminal} terminal
*/
module.exports = async (argv, terminal, zsh) => {
if (argv.indexOf('--help') !== -1) {
terminal.writeln(
`Usage: ${argv[0]} [options] [<user>]` + '\n' +
'Change the user ID and group ID to <user>\'s.' + '\n' +
'\n' +
'If <user> is not giver, root is assumed.' + '\n' +
'This command is an easter egg. Try to guess the root\'s password!'
);
return;
}
const user = argv[1] || 'root';
const users = Object.freeze(['root', 'daemon', 'bin', 'sys', 'adm', 'nobody', 'lpd', 'lp', 'ipsec', 'user']);
if (users.indexOf(user) == -1) {
terminal.writeln(`${argv[0]}: user ${user} does not exist or the user entry does not contain all the required fields`);
return 2;
}
terminal.write('Password: ');
const pass = await zsh.get_stdin({hide: true});
terminal.write(pass);
}

View File

@ -23,8 +23,9 @@ const zshapi = {
update_prompt, update_prompt,
text_prompt, text_prompt,
pr_char, pr_char,
get_stdin,
terminal, terminal,
dom dom,
}; };
let prompt = `\x1b[1;32muser@${data.ip} \x1b[36m~ $ \x1b[0m`; let prompt = `\x1b[1;32muser@${data.ip} \x1b[36m~ $ \x1b[0m`;
@ -39,17 +40,71 @@ function text_prompt() {
return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); return prompt.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
} }
let stdin_open = false;
/**
*
* @param {{hide: boolean}} opt
* @returns
*/
async function get_stdin(opt) {
if (opt == undefined) opt = {};
if (stdin_open) {
return 0;
}
stdin_open = true;
let input = '';
const get_char = function(e) {
if (break_loop) return;
/** @type {KeyboardEvent} */
const dom = e.domEvent;
const startX = terminal.buffer.active.cursorX;
if ((dom.key.length == 1 && !(dom.ctrlKey || dom.altKey))) {
// non-special key
if (!opt.hide) terminal.write(dom.key);
input += dom.key;
} else {
// special key
switch (dom.keyCode) {
case 8:
if (terminal.buffer.active.cursorX <= startX) return;
terminal.write('\b \b');
input = input.substring(0, input.length - 1);
case 13:
break_loop = true;
terminal.write('\n');
break;
case 68:
if (dom.ctrlKey && dom.key.toLowerCase() == 'd') {
terminal.write('\x1b[0m\x1b[49m^D\x1b[0m');
break_loop = true;
}
}
}
}
let break_loop = false
terminal.onKey(get_char)
while (1) {
await sleep(5);
if (break_loop) break;
}
stdin_open = false;
return input;
}
/** /**
* *
* @param { string } char * @param { string } char
// ZSH api to be used in commands (see references)
const zshapi = {
update_prompt,
text_prompt,
pr_char,
terminal,
dom
};
* @param { KeyboardEvent } dom * @param { KeyboardEvent } dom
*/ */
function pr_char(char, dom) { function pr_char(char, dom) {
@ -179,6 +234,13 @@ async function control_char(id, dom) {
terminal.write(lastcmd); terminal.write(lastcmd);
break; break;
case 40:
if (cmd == '') break;
lastcmd = cmd;
reprint_prompt();
cmd = '';
break;
// Ctrl+c // Ctrl+c
case 67: case 67:
if (dom.ctrlKey) { if (dom.ctrlKey) {
@ -204,6 +266,7 @@ async function control_char(id, dom) {
case 68: case 68:
if (dom.ctrlKey && dom.key.toLowerCase() == 'd') { if (dom.ctrlKey && dom.key.toLowerCase() == 'd') {
alert('you are a moron. zsh runs with PID 1');
terminal.writeln(''); terminal.writeln('');
pr_char = () => {}; pr_char = () => {};
exec_cmd = pr_char; exec_cmd = pr_char;
@ -231,6 +294,8 @@ async function control_char(id, dom) {
} }
function key(e) { function key(e) {
if (stdin_open) return;
/** @type {KeyboardEvent} */ /** @type {KeyboardEvent} */
const dom = e.domEvent; const dom = e.domEvent;
if (dom.key.length == 1 && !(dom.ctrlKey || dom.altKey)) { if (dom.key.length == 1 && !(dom.ctrlKey || dom.altKey)) {