diff options
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100644 index 0000000..8557f34 --- /dev/null +++ b/index.js @@ -0,0 +1,237 @@ +const ora = require('ora'); +const chalk = require('chalk'); +const config = require('./auth.json'); +const net = require('net'); +const readline = require("readline"); +const fs = require("fs"); +global.connected = false; + +function commandline() { + rl.question(chalk.gray("bingoloto> "), function(cmd) { + if (cmd.trim() == "") { + commandline(); + return; + } + + args = cmd.split(" "); + command = args[0]; + args.shift(); + + switch (command) { + case "q": + console.log("Bye!"); + connected = false; + client.destroy(); + process.exit(); + break; + case "h": + console.log(fs.readFileSync("help.txt").toString()); + break; + case "n": + if (args[0] != undefined) { + if (args[0].trim() > 90 || isNaN(args[0].trim())) { + console.log(chalk.red("invalid number")); + commandline() + } else { + client.write("pick|" + args[0].trim()); + } + } else { + console.log(chalk.red("missing operand")); + commandline() + } + return; + case "p": + if (args[0] != undefined) { + if (args[0].trim() > 90 || isNaN(args[0].trim())) { + console.log(chalk.red("invalid number")); + commandline() + } else { + global.lmode = args[0].trim(); + client.write("list") + return; + } + } else { + console.log(chalk.red("missing operand")); + commandline() + } + return; + case "d": + if (args[0] != undefined) { + client.write("jsae|" + args.join(" ").trim()) + } else { + console.log(chalk.red("missing operand")); + commandline() + } + return; + case "l": + global.lmode = false; + client.write("list") + return; + case "g": + client.write("glog") + return; + case "x": + client.write("stop") + return; + case "s": + client.write("sysi") + return; + case "u": + client.write("undo") + return; + case "e": + client.write("stat") + return; + case "t": + if (args[0] != undefined) { + client.write("stag|" + args.join(" ").trim() + " ") + } else { + client.write("gtag") + } + return; + case "m": + if (args[0] != undefined) { + if (args[0] == "bingo" || args[0] == "loto" || args[0] == "l" || args[0] == "b") { + client.write("smod|" + args[0].trim()); + } else { + console.log(chalk.red("unknown mode")); + commandline(); + } + } else { + client.write("gmod") + } + return; + default: + console.log(chalk.red("unknown command")); + } + + commandline(); + }); +} + +process.on('uncaughtException', (err) => { + if (err.stack.includes("ECONNREFUSED")) { + console.log(chalk.red("\nUnable to connect to server, retrying")); + joinsrv(); + } else { + console.log("\n" + chalk.red(err.stack) + "\n"); + process.exit(255); + } +}) + +const spinner = ora('Connecting to ' + config.host + ":7441").start(); + +var client = new net.Socket(); +function joinsrv() { + try { + client.connect(7441, config.host, function() { + spinner.succeed() + connected = true; + console.log(chalk.green('Connected to server')); + }); + } catch (e) { + console.log(chalk.red("\nUnable to connect to server, retrying")); + joinsrv(); + } +} +joinsrv() + +authent = false; + +client.on('data', function(data) { + if (data.toString().startsWith("connected|")) { + global.ver = data.toString().split("|")[1]; + global.authspin = ora('Authenticating').start(); + authent = true; + client.write("auth|" + config.key); + return; + } + if (data.toString().startsWith("raw|")) { + console.log(data.toString().split("|")[1]); + return; + } + if (data.toString().startsWith("list|")) { + if (lmode != false) { + list = data.toString().split("|")[1].split(", "); + if (list.includes(lmode)) { + console.log(chalk.green("yes")); + } else { + console.log(chalk.yellow("no")); + } + } else { + console.log(data.toString().split("|")[1]); + } + commandline(); + return; + } + if (data.toString().startsWith("tagg|")) { + console.log(data.toString().split("|")[1]); + commandline(); + return; + } + if (data.toString().startsWith("aerp|")) { + console.log(data.toString().split("|")[1]); + commandline(); + return; + } + if (data.toString().startsWith("rlog|")) { + console.log(data.toString().split("|")[1]); + commandline(); + return; + } + if (data.toString().startsWith("srvo|")) { + parts = data.toString().split("|"); + parts.shift(); + + console.log(chalk.cyan.bold("os: ") + parts[0] + chalk.gray(" (" + parts[1] + ") ") + parts[2] + chalk.blue.bold(" @ " + parts[3])); + console.log(chalk.cyan.bold("ver: ") + chalk.magenta.bold("node: ") + parts[4] + "," + chalk.magenta.bold(" loto: ") + ver); + console.log(chalk.cyan.bold("mem: ") + chalk.magenta.bold("heap: ") + (parts[5] / 1024).toFixed(2) + "K/" + (parts[6] / 1024) + "K," + chalk.magenta.bold(" rss: ") + (parts[5] / 1024).toFixed(2) + "K"); + + commandline(); + return; + } + if (data.toString().startsWith("modc|")) { + console.log(data.toString().split("|")[1]); + commandline(); + return; + } + if (data.toString().startsWith("pok")) { + console.log(chalk.green("success")); + commandline(); + return; + } + if (data.toString().startsWith("sok")) { + connected = false; + console.log(chalk.green("Successfully stopped the server")); + process.exit(); + return; + } + if (data.toString().startsWith("stok") || data.toString().startsWith("msok") || data.toString().startsWith("stok") || data.toString().startsWith("udok")) { + console.log(chalk.green("success")); + commandline(); + return; + } + if (data.toString() == "ok" && authent) { + authent = false; + authspin.stop(); + console.log(chalk.blue.bold("I:") + " This server is running Bingoloto " + ver); + console.log("\nWelcome to Bingoloto shell!\nEnter 'h' to get help.\nEnter 'q' to close connection.\n"); + global.rl = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + commandline(); + return; + } else if (authent) { + console.log(chalk.red("\nFailed to authenticate, the server returned " + data.toString())); + process.exit(255); + return; + } +}); + +client.on('close', function() { + if (connected) { + console.log(chalk.red('\nConnection closed by remote peer')); + process.exit() + } +}); |