Update to version 2
This commit is contained in:
parent
325ffb1855
commit
bfea6615a8
3 changed files with 112 additions and 15 deletions
97
index.js
97
index.js
|
@ -1,16 +1,25 @@
|
|||
const mcp = require("minecraft-protocol")
|
||||
const { Worker } = require("worker_threads")
|
||||
const fs = require("fs")
|
||||
const path = require("path")
|
||||
const child_process = require("child_process")
|
||||
const config = require("./config.json")
|
||||
var globalTerm = null;
|
||||
var curWorker = null;
|
||||
child_process.execSync("chmod 760 *")
|
||||
|
||||
var client = mcp.createClient({
|
||||
username:"MCTerminal",
|
||||
host:"raccoon.pw"
|
||||
host:process.argv[2]
|
||||
})
|
||||
client.queue = []
|
||||
|
||||
function endWorker() {
|
||||
if(curWorker == null) return;
|
||||
curWorker.terminate()
|
||||
curWorker = null
|
||||
}
|
||||
|
||||
let plugins = []; //NOTE: DO NOT CHANGE, PLUGINS ARE LOADED AUTOMATICALLY
|
||||
fs.readdirSync(
|
||||
path.join(__dirname, "plugins")
|
||||
|
@ -37,28 +46,86 @@ setInterval(function() {
|
|||
client.write("chat",{message:client.queue[0]})
|
||||
client.queue.shift()
|
||||
}
|
||||
},100)
|
||||
},200)
|
||||
|
||||
client.on("message", function(username, message) {
|
||||
if(globalTerm == null) return;
|
||||
console.log(username, message)
|
||||
|
||||
if(message.startsWith(">")) {
|
||||
globalTerm.stdin.write(message.substr(1) + "\n")
|
||||
switch(message.split(">")[1].split(" ")[0]) {
|
||||
case "pkg":
|
||||
if(message.split(">")[1].split(" ")[1] == "add") {
|
||||
if(curWorker == null) {
|
||||
client.queue.push(`&aAttempting to install &e${message.split(">")[1].split(" ")[2]}`)
|
||||
curWorker = new Worker("./workers/pkg.js",{workerData:{pkg: message.split(">")[1].split(" ")[2]}})
|
||||
curWorker.on("message", function(msg) {
|
||||
switch(msg) {
|
||||
case "NOTFOUND":
|
||||
client.queue.push("&cPackage not found!")
|
||||
endWorker()
|
||||
break;
|
||||
case "ERR":
|
||||
client.queue.push("&cAn error occured while installing the package.")
|
||||
endWorker()
|
||||
break;
|
||||
case "INSTALLED":
|
||||
client.queue.push("&aPackage successfully installed!")
|
||||
endWorker()
|
||||
break;
|
||||
}
|
||||
})
|
||||
} else {
|
||||
client.queue.push("&cThere is already a package installing!")
|
||||
}
|
||||
}
|
||||
break;
|
||||
case "break":
|
||||
|
||||
default:
|
||||
globalTerm.stdin.write(message.substr(1) + "\n")
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
client.on("login", function(){
|
||||
client.queue.push("&eMCTerminal &astarted! Prefix your messages with &e>&a to execute them in the terminal!")
|
||||
var term = child_process.exec(`bash`, function(err, stdout, stderr) {
|
||||
console.log("Process exited.")
|
||||
})
|
||||
globalTerm = term
|
||||
client.queue.push("&eMCTerminal &astarted! Prefix your messages with &e>&a to execute them in the terminal! You can also install packages with &e>pkg add&a!")
|
||||
client.queue.push("&aThe terminal is starting, please wait...")
|
||||
setTimeout(function(){
|
||||
child_process.execSync("chmod 760 *")
|
||||
var term = child_process.exec(`su ${config.user}`, function(err, stdout, stderr) {
|
||||
console.log("Process exited.")
|
||||
process.exit(0)
|
||||
})
|
||||
globalTerm = term
|
||||
setTimeout(function(){
|
||||
term.stdin.write("${config.password}\n")
|
||||
setTimeout(function(){
|
||||
client.queue.push("&aAuthenticated user, giving input!")
|
||||
term.stdout.on("data", function(chunk){
|
||||
client.queue = [].concat(client.queue,chunk.toString().replace(/\n/gm," ").match(/.{1,99}/g))
|
||||
})
|
||||
|
||||
term.stderr.on("data",function(chunk){
|
||||
client.queue = [].concat(client.queue,chunk.toString().replace(/\n/gm," ").match(/.{1,99}/g))
|
||||
})
|
||||
},1000)
|
||||
},200)
|
||||
},1000)
|
||||
})
|
||||
|
||||
term.stdout.on("data", function(chunk){
|
||||
client.queue.push(chunk.toString().replace(/\n/gm,""))
|
||||
})
|
||||
client.on("end", function(reason){
|
||||
console.log(reason)
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
term.stderr.on("data",function(chunk){
|
||||
client.queue.push(chunk.toString().replace(/\n/gm,""))
|
||||
})
|
||||
client.on("error", function(err){
|
||||
console.log(err)
|
||||
process.exit(0)
|
||||
})
|
||||
|
||||
process.on("uncaughtException", function(err){
|
||||
console.log(err)
|
||||
process.exit(0)
|
||||
})
|
14
run.js
Executable file
14
run.js
Executable file
|
@ -0,0 +1,14 @@
|
|||
var process = require("child_process")
|
||||
|
||||
function start(){
|
||||
var proc = process.exec("node . " + require("process").argv[2])
|
||||
proc.on("close",function(){
|
||||
proc.kill()
|
||||
start()
|
||||
})
|
||||
proc.stdout.on("data",function(chunk){
|
||||
require("process").stdout.write(chunk)
|
||||
})
|
||||
}
|
||||
|
||||
start()
|
16
workers/pkg.js
Normal file
16
workers/pkg.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
const { workerData, parentPort } = require("worker_threads")
|
||||
const child_process = require("child_process")
|
||||
|
||||
child_process.exec(`apt-cache search --names-only '${workerData.pkg}'`, function(err,stdout,stderr) {
|
||||
if(stdout.trim() == "") {
|
||||
parentPort.postMessage("NOTFOUND")
|
||||
} else {
|
||||
child_process.exec(`sudo apt-get install ${workerData.pkg} -y`, function(err,stdout,stderr) {
|
||||
if(stderr.trim() == "" || err) {
|
||||
parentPort.postMessage("ERR")
|
||||
return;
|
||||
}
|
||||
parentPort.postMessage("INSTALLED")
|
||||
})
|
||||
}
|
||||
})
|
Loading…
Reference in a new issue