sshChat-CLI/new.js

172 lines
3.9 KiB
JavaScript

const blessed = require('neo-blessed');
const Hyperswarm = require('hyperswarm');
const crypto = require('hypercore-crypto');
const b4a = require('b4a');
const readline = require('readline');
const fs = require("fs");
const commands = ['login', 'exec', 'stop', 'start', 'restart', 'stats', 'cd', 'AI'];
const commandModules = {};
for (const command of commands) {
commandModules[command] = require(`./commands/${command}`);
}
const USERPWD = '/';
const DAPI_KEY = '';
const LOGGEDIN = false;
const MYKEY = [];
const conns = [];
const connectedUsers = new Map();
const USERNAME = ['annon'];
const screen = blessed.screen({ smartCSR: true, fastCSR: true });
let mainBox = blessed.box({
parent: screen,
top: 0,
left: 0,
width: '80%',
height: '80%',
border: {
type: 'line'
},
style: {
fg: 'white',
bg: 'black'
},
keys: true,
vi: true,
alwaysScroll: true,
scrollable: true,
scrollbar: {
style: {
bg: 'yellow'
}
}
});
async function updateScroll() {
mainBox.scrollTo(mainBox.getScrollHeight());
}
// Create the STDIN box for chat input and command input
const stdinBox = blessed.textbox({
bottom: '0',
left: '0',
width: '80%',
height: '21%',
border: {
type: 'line'
},
style: {
fg: 'white',
bg: 'black',
border: {
fg: '#f0f0f0'
},
},
inputOnFocus: true,
input: true
});
// Create the sidebar box for connected peers
const sidebarBox = blessed.box({
top: '0',
right: '0',
width: '20%',
height: '100%',
content: '',
border: {
type: 'line'
},
style: {
fg: 'white',
bg: 'black',
border: {
fg: '#f0f0f0'
},
}
});
const handleUser = (user, peerId) => {
if (connectedUsers.has(peerId)) {
connectedUsers.delete(peerId);
} else {
connectedUsers.set(peerId, user);
}
sidebarBox.setContent("Connected Peers: \n" + [...connectedUsers].map(([peerId, user]) => `${user} - ${peerId}`).join("\n"));
screen.render();
}
// Generate the keys
const myKey = crypto.keyPair()
const myId = crypto.randomBytes(32)
MYKEY.push(myId)
// Create a new Hyperswarm instance
const swarm = new Hyperswarm()
// Join the chat room
swarm.join(myId, {
announce: true,
lookup: true
})
swarm.on('connection', (socket, details) => {
console.log("Incoming Connection!")
console.log("Peer ID: " + details.peer.toString('hex'))
conns.push(socket)
// Request username
socket.write("Please enter a username: ")
let data = ''
socket.on('data', async (d) => {
data += d.toString()
if (data.endsWith('\n')) {
// handle the data
const [command, ...args] = data.split(' ');
if (command === 'username') {
const peerId = details.peer.toString('hex');
const user = args.join(' ').trim();
handleUser(user, peerId);
socket.write("Welcome " + user + "!\n")
} else if (commandModules[command]) {
commandModules[command].execute(args, socket);
} else {
socket.write("Unknown command.\n");
}
data = '';
}
});
socket.on('close', () => {
console.log("Closing Connection!")
conns = conns.filter(conn => conn !== socket);
});
});
stdinBox.on('submit', (data) => {
const [command, ...args] = data.split(' ');
if (commandModules[command]) {
commandModules[command].execute(args);
} else {
console.log("Unknown command.");
}
});
stdinBox.key('C-c', () => process.exit(0));
screen.append(mainBox);
screen.append(stdinBox);
screen.append(sidebarBox);
stdinBox.focus()
screen.key(['escape', 'q', 'C-c'], (ch, key) => {
return process.exit(0);
});
screen.render();