hfs
Version:
HTTP File Server
161 lines (160 loc) • 5.77 kB
JavaScript
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const perm_1 = require("./perm");
const config_1 = require("./config");
const lodash_1 = __importDefault(require("lodash"));
const update_1 = require("./update");
const listen_1 = require("./listen");
const yaml_1 = __importDefault(require("yaml"));
const const_1 = require("./const");
const readline_1 = require("readline");
const plugins_1 = require("./plugins");
const fileAttr_1 = require("./fileAttr");
const github_1 = require("./github");
const cross_1 = require("./cross");
const api_monitor_1 = __importDefault(require("./api.monitor"));
const argv_1 = require("./argv");
if (!argv_1.argv.updating && !config_1.showHelp)
try {
/*
is this try-block useful in case the stdin is unavailable?
Not sure, but someone reported a problem using nohup https://github.com/rejetto/hfs/issues/74
and I've found this example try-catching https://github.com/DefinitelyTyped/DefinitelyTyped/blob/dda83a906914489e09ca28afea12948529015d4a/types/node/readline.d.ts#L489
*/
(0, readline_1.createInterface)({ input: process.stdin }).on('line', parseCommandLine);
console.log(`HINT: type "help" for help`);
}
catch (_a) {
console.log("console commands not available");
}
function parseCommandLine(line) {
if (!line)
return;
let [name, ...params] = line.trim().split(/ +/);
name = aliases[name] || name;
let cmd = commands[name];
if (cmd === null || cmd === void 0 ? void 0 : cmd.alias)
cmd = commands[cmd.alias];
if (!cmd)
return console.error("cannot understand entered command, try 'help'");
if (cmd.cb.length > params.length)
return console.error("insufficient parameters, expected: " + cmd.params);
Promise.resolve(cmd.cb(...params)).then(() => console.log("+++ command executed"), (err) => {
if (typeof err !== 'string' && !(err === null || err === void 0 ? void 0 : err.message))
throw err;
console.error("command failed:", err.message || err);
});
}
const aliases = { ver: 'version', exit: 'quit' };
const commands = {
help: {
params: '',
cb() {
console.log("supported commands:", ...lodash_1.default.map(commands, ({ params }, name) => '\n - ' + name + ' ' + params));
}
},
'show-admin': {
params: '',
cb() {
(0, listen_1.openAdmin)();
}
},
'create-admin': {
params: '<password> [<username>=admin]',
cb: perm_1.createAdmin
},
'change-password': {
params: '<user> <password>',
async cb(user, password) {
const acc = (0, perm_1.getAccount)(user);
if (!acc)
throw "user doesn't exist";
await (0, perm_1.updateAccount)(acc, { password });
}
},
config: {
params: '<key> <value>',
cb(key, value) {
if (!(0, config_1.configKeyExists)(key))
throw "specified key doesn't exist";
let v = value;
try {
v = JSON.parse(v);
}
catch (_a) { }
(0, config_1.setConfig)({ [key]: v });
}
},
'get-config': {
params: '[<key-mask>]',
cb(key = '*') {
const matcher = (0, cross_1.makeMatcher)(key);
const filtered = lodash_1.default.pickBy((0, config_1.getWholeConfig)({}), (v, k) => matcher(k));
console.log('\n' + yaml_1.default.stringify(filtered, { lineWidth: 1000 }).trim());
}
},
quit: {
params: '',
cb() {
process.emit('SIGTERM');
}
},
update: {
params: '[<version>=latest]',
cb: update_1.update,
},
'check-update': {
params: '',
async cb() {
const update = (await (0, update_1.getUpdates)(true))[0];
if (!update)
throw "you already have the latest version: " + const_1.VERSION;
console.log("new version available", update.name);
}
},
version: {
params: '',
cb() {
console.log(const_1.VERSION, 'build', const_1.BUILD_TIMESTAMP);
}
},
'start-plugin': {
params: '<name>',
cb: plugins_1.startPlugin,
},
'stop-plugin': {
params: '<name>',
cb: plugins_1.stopPlugin,
},
'download-plugin': {
params: '<githubUser/repo>',
cb: github_1.downloadPlugin,
},
'list-plugins': {
params: '',
cb() {
(0, plugins_1.mapPlugins)(p => console.log('ON:', p.id), false);
(0, plugins_1.getAvailablePlugins)().map(p => console.log('OFF:', p.id));
}
},
'purge-file-attr': {
params: '',
cb: fileAttr_1.purgeFileAttr,
},
status: {
params: '',
async cb() {
const conn = (await api_monitor_1.default.get_connection_stats().next()).value;
if (conn) {
const { sent_got: sg } = conn;
console.log(`Speed ↑ ${(0, cross_1.formatSpeed)(conn.outSpeed)} ↓ ${(0, cross_1.formatSpeed)(conn.inSpeed)}`);
console.log(`Transfered ↑ ${(0, cross_1.formatBytes)(sg[0])} ↓ ${(0, cross_1.formatBytes)(sg[1])} since ${(0, cross_1.formatTimestamp)(sg[2])}`);
console.log(`Connections ${conn.connections} (${conn.ips} IPs)`);
}
}
}
};
;