Compare commits

..

8 Commits

Author SHA1 Message Date
b1ek fc10dcfe1b
add su command 2023-05-06 11:19:08 +10:00
b1ek 7cf1d45283
remove % at end of command if no new line 2023-05-06 11:18:58 +10:00
b1ek 60ad80616b
change var to let to match ES6 standart 2023-05-05 14:37:00 +10:00
b1ek 33cea94f6d
add stdin and half-baked su command 2023-05-05 14:36:47 +10:00
b1ek ec105acfb8
disable settin hostname to blek.codes 2023-05-05 14:36:22 +10:00
b1ek 3db714b153
add read permission check to cat command 2023-05-05 01:26:14 +10:00
b1ek 2f3ccae2f4
fix set-hostname 2023-05-05 01:25:55 +10:00
b1ek 04133aa313
add fs.constants 2023-05-05 01:25:36 +10:00
7 changed files with 167 additions and 21 deletions

View File

@ -23,6 +23,9 @@ module.exports = (argv, terminal) => {
terminal.writeln(`${argv[0]}: ${file}: no such file or directory`);
return;
}
if (!fs.accessSync(file, fs.constants.R_OK)) {
terminal.writeln(`${argv[0]}: read ${file}: access denied`);
}
const lines = fs.readFileSync(file).toString().split('\n');
if (numbers) {

View File

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

View File

@ -1,7 +1,6 @@
const { Terminal } = require("xterm");
const original_data = Object.freeze(data);
global.orig = original_data;
const orig_ip = data.ip;
/**
*
@ -9,22 +8,26 @@ global.orig = original_data;
* @param {Terminal} terminal
*/
module.exports = (argv, terminal, zsh) => {
if (argv.indexOf('--help') !== -1) {
terminal.writeln(`
Usage: ${argv[0]} [hostname] [-R] [--help]
-R Reset it to original IP (${original_data.ip})
if (argv.indexOf('--help') !== -1 | (!argv[1])) {
terminal.writeln(`Usage: ${argv[0]} [hostname] [-R] [--help]
-R Reset it to original IP (${orig_ip})
--help Show this page`
);
return 0;
}
if (argv.indexOf('-R') !== -1) {
data.ip = original_data.ip;
data.ip = orig_ip;
zsh.update_prompt();
return 0;
}
data.ip = argv[1] || 'resume.js';
if (argv[1] == 'blek.codes') {
terminal.writeln(`${argv[0]}: cannot set hostname to ${argv[1]}: permission denied (you are not on blek.codes you moron)`);
return 1;
}
data.ip = argv[1];
zsh.update_prompt();
return 0;
}

View File

@ -0,0 +1,62 @@
const { Terminal } = require("xterm");
const sleep = require("../../lib/sleep");
/**
* @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: ');
/** @type {string} */
const pass = await zsh.get_stdin({hide: true});
const some_random_array = new Uint16Array([12319, 14486, 12589, 13232]);
let checksum = new Uint32Array(pass.length);
let i = 0;
for (const letter of pass) {
checksum[i] = letter.charCodeAt(0) * 128;
checksum[i] += (i*8) - letter.charCodeAt(0);
i++;
}
i = undefined;
console.log(checksum, some_random_array);
let valid = true;
if (checksum.length == some_random_array.length)
for (let i = 0; i != some_random_array.length; i++) {
if (checksum[i] != some_random_array[i])
valid = false;
}
else
valid = false;
console.log(valid);
if (valid) {
terminal.writeln('yay, you did it!! congratulations :3');
} else {
await sleep(3000);
terminal.writeln('su: Authentication failure');
return 1;
}
}

View File

@ -19,4 +19,6 @@ if (!fs.existsSync('dev'))
}
})();
ufs.constants = fs.constants;
module.exports = ufs;

View File

@ -8,13 +8,6 @@ global.fs = fs;
const cmds = require('./commands');
const sleep = require('../lib/sleep');
// ZSH api to be used in commands (see references)
const zshapi = {
update_prompt,
text_prompt,
pr_char
};
/**
* @type { Terminal }
*/
@ -25,6 +18,16 @@ let terminal;
*/
let dom;
// ZSH api to be used in commands (see references)
const zshapi = {
update_prompt,
text_prompt,
pr_char,
get_stdin,
terminal,
dom,
};
let prompt = `\x1b[1;32muser@${data.ip} \x1b[36m~ $ \x1b[0m`;
let cmd = '';
let lastcmd = window.sessionStorage.getItem('last_cmd') || '';
@ -37,6 +40,69 @@ function text_prompt() {
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 && (!opt.hide)) break;
if (!opt.hide) terminal.write('\b \b');
input = input.substring(0, input.length - 1);
break;
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
@ -90,13 +156,10 @@ async function exec_cmd() {
}
if (cmds[command] != undefined) {
const startY = terminal.buffer.normal.cursorY
// execute command
await cmds[command](c.split(' '), terminal, zshapi);
if (terminal.buffer.active.cursorX != 0 && startY != terminal.buffer.active.cursorY) {
terminal.write('\033[30;47m%\033[0m\n');
}
print_prompt();
return;
}
@ -169,6 +232,13 @@ async function control_char(id, dom) {
terminal.write(lastcmd);
break;
case 40:
if (cmd == '') break;
lastcmd = cmd;
reprint_prompt();
cmd = '';
break;
// Ctrl+c
case 67:
if (dom.ctrlKey) {
@ -194,6 +264,7 @@ async function control_char(id, dom) {
case 68:
if (dom.ctrlKey && dom.key.toLowerCase() == 'd') {
alert('you are a moron. zsh runs with PID 1');
terminal.writeln('');
pr_char = () => {};
exec_cmd = pr_char;
@ -221,6 +292,8 @@ async function control_char(id, dom) {
}
function key(e) {
if (stdin_open) return;
/** @type {KeyboardEvent} */
const dom = e.domEvent;
if (dom.key.length == 1 && !(dom.ctrlKey || dom.altKey)) {

View File

@ -5,7 +5,7 @@
<meta charset="utf-8" />
<link rel="stylesheet" href="standalone_page.css" />
<script>
var data = {
let data = {
ip: '127.0.0.1'
}
</script>