neoss
Version:
<div align="center"> <h1>neoss</h1> <i>Socket statistics, with a UI.</i> </div> <p align="center"> <a href="https://img.shields.io/github/v/release/pablolec/neoss" target="_blank"> <img src="https://img.shields.io/github/v/release/pablolec/neo
153 lines (152 loc) • 4.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setDefaultScreen = setDefaultScreen;
exports.focusPopup = focusPopup;
exports.removePopup = removePopup;
exports.handlePopup = handlePopup;
exports.loadingPopup = loadingPopup;
const neo_blessed_1 = require("neo-blessed");
const fs_1 = require("fs");
const path_1 = require("path");
const whois_1 = require("../utils/whois");
let screen;
let currentBox = null;
const strings = {};
const stringsNames = ["ports", "protocols", "queues", "states"];
stringsNames.forEach(function (name) {
(0, fs_1.readFile)((0, path_1.join)(__dirname, "..", "..", "src", "strings", name + ".json"), (err, data) => {
if (err)
throw err;
strings[name] = JSON.parse(data);
});
});
function setDefaultScreen(defaultScreen) {
screen = defaultScreen;
}
function focusPopup() {
if (currentBox != null) {
currentBox.focus();
}
}
function removePopup() {
if (currentBox == null) {
return;
}
screen.remove(currentBox);
currentBox = null;
screen.rewindFocus();
screen.render();
}
function createPopup(content, escapable = true) {
if (content == null) {
return;
}
currentBox = createBox(content);
if (escapable) {
currentBox.key(["enter", "escape"], function () {
removePopup();
});
}
screen.append(currentBox);
screen.render();
currentBox.focus();
}
function handlePopup(column, content) {
switch (column) {
case 0:
createPopup(strings["protocols"][content]);
break;
case 1:
createPopup(strings["states"][content]);
break;
case 2:
createPopup(strings["queues"]["receiveQueue"]);
break;
case 3:
createPopup(strings["queues"]["sendQueue"]);
break;
case 4:
break;
case 5:
createPopup(getPortText(content));
break;
case 6:
createPopup("Wait for Whois...", false);
(0, whois_1.whois)(content).then(function (response) {
removePopup();
createPopup(response);
});
break;
case 7:
createPopup(getPortText(content));
break;
case 8:
createPopup(getUsersText(content));
break;
}
}
function canShrink(content) {
let maxShrinkSize = Math.floor((Math.floor(screen.lines.length / 2) - 1) / 2);
let numberOfLines = content.split(/\r\n|\r|\n/).length;
return numberOfLines < maxShrinkSize;
}
function createBox(content) {
let height = "50%";
let scrollable = true;
if (canShrink(content)) {
height = "shrink";
scrollable = false;
}
return (0, neo_blessed_1.Box)({
top: "center",
left: "center",
width: "shrink",
height: height,
scrollable: scrollable,
alwaysScroll: true,
keys: true,
content: content.trim(),
tags: true,
border: {
type: "line",
},
style: {
fg: "white",
border: {
fg: "#f0f0f0",
},
},
});
}
function getPortText(port) {
let assignment = strings["ports"][port];
if (assignment == undefined) {
return "This port is not assigned";
}
let text = "{bold}This port is assigned to:{/bold} " + assignment + "\n";
text += "Note that regardless of a port assignment, it can be used in any way.";
return text;
}
function getUsersText(users) {
if (users.text == "/") {
return null;
}
let text = "";
let length = Object.keys(users).length - 1;
for (let i = 0; i < length; i++) {
if (length > 1) {
text += "{bold} - - User " + (i + 1) + " - -{/bold}\n";
}
text += "{bold}Name:{/bold} " + users[i].name + "\n";
text += "{bold}PID:{/bold} " + users[i].pid + "\n";
text += "{bold}Owner:{/bold} " + users[i].owner + "\n";
text += "{bold}Command:{/bold} " + users[i].cmdline.trim() + "\n";
if (i < length - 1) {
text += "\n\n";
}
}
return text;
}
function loadingPopup() {
createPopup("Loading{blink}...{/blink}", false);
}