83 lines
2.1 KiB
JavaScript
83 lines
2.1 KiB
JavaScript
const { VM } = require('vm2')
|
|
const { Server } = require('socket.io')
|
|
const util = require('util')
|
|
const { stylize } = require('./colors')
|
|
const randomstring = require('randomstring')
|
|
const ChatMessage = require('prismarine-chat')('1.20.1')
|
|
const mc = require('minecraft-protocol')
|
|
const moment = require('moment-timezone')
|
|
const crypto = require('crypto')
|
|
const nbt = require('prismarine-nbt')
|
|
|
|
const BRIDGE_PREFIX = 'function:'
|
|
|
|
const io = new Server(3069)
|
|
|
|
io.on('connection', (socket) => {
|
|
let functions
|
|
|
|
let proxy
|
|
|
|
const handler = {
|
|
get (target, prop) {
|
|
if (!target[prop]) throw new Error(`Function "${prop}" not available`)
|
|
|
|
return (...args) => target[prop](...args)
|
|
}
|
|
}
|
|
|
|
socket.on('setFunctions', (jsonArray) => {
|
|
const parsed = JSON.parse(jsonArray)
|
|
|
|
functions = {}
|
|
|
|
for (const eachFuntion of parsed) {
|
|
functions[eachFuntion] = (...args) => {
|
|
socket.emit(BRIDGE_PREFIX + eachFuntion, ...args)
|
|
|
|
return new Promise((resolve) => {
|
|
socket.once(`functionOutput:${eachFuntion}`, (message, parseJSON) => {
|
|
if (parseJSON) resolve(JSON.parse(message))
|
|
else resolve(message)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
proxy = new Proxy(functions, handler)
|
|
})
|
|
|
|
let vm
|
|
function resetVM () {
|
|
vm = new VM({
|
|
timeout: 3000,
|
|
sandbox: {
|
|
bridge: () => proxy,
|
|
randomstring,
|
|
ChatMessage,
|
|
mc,
|
|
moment,
|
|
crypto,
|
|
nbt
|
|
}
|
|
})
|
|
}
|
|
|
|
resetVM()
|
|
|
|
socket.on('runCode', (transactionId, code) => {
|
|
try {
|
|
const output = vm.run(code)
|
|
|
|
socket.emit('codeOutput', transactionId, false, util.inspect(output, { stylize }))
|
|
} catch (e) {
|
|
socket.emit('codeOutput', transactionId, true, e.toString())
|
|
}
|
|
})
|
|
|
|
socket.on('reset', resetVM)
|
|
})
|
|
|
|
process.on('uncaughtException', (e) => {
|
|
console.log(`Caught an uncaught exception!\n${e.stack}`)
|
|
})
|