eval-server/index.js

86 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-07-01 09:07:51 -04:00
const { VM } = require('vm2')
const { Server } = require('socket.io')
const util = require('util')
const { stylize } = require('./colors')
2023-07-01 20:13:49 -04:00
const randomstring = require('randomstring')
const ChatMessage = require('prismarine-chat')('1.20.1')
2023-07-02 00:51:22 -04:00
const mc = require('minecraft-protocol')
const moment = require('moment-timezone')
const crypto = require('crypto')
2023-07-30 21:17:29 -04:00
const nbt = require('prismarine-nbt')
2023-07-01 09:07:51 -04:00
const BRIDGE_PREFIX = 'function:'
const io = new Server(3069)
io.on('connection', (socket) => {
2023-07-02 09:11:38 -04:00
let functions
let proxy
const handler = {
get (target, prop) {
if (!target[prop]) throw new Error(`Function "${prop}" not available`)
return (...args) => target[prop](...args)
}
}
2023-07-03 05:34:34 -04:00
socket.on('setFunctions', (jsonArray) => {
const parsed = JSON.parse(jsonArray)
2023-07-02 09:11:38 -04:00
functions = {}
2023-07-03 05:34:34 -04:00
for (const eachFuntion of parsed) {
2023-07-22 22:10:01 -04:00
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)
})
})
}
2023-07-02 09:11:38 -04:00
}
proxy = new Proxy(functions, handler)
resetVM()
2023-07-02 09:11:38 -04:00
})
2023-07-01 09:07:51 -04:00
let vm
function resetVM () {
vm = new VM({
timeout: 3000,
sandbox: {
2023-08-01 22:25:49 -04:00
get bridge () { return proxy },
2023-07-01 20:13:49 -04:00
randomstring,
2023-07-02 00:51:22 -04:00
ChatMessage,
mc,
moment,
2023-07-30 21:17:29 -04:00
crypto,
nbt
2023-07-01 09:07:51 -04:00
}
})
}
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)
})
2023-07-02 02:45:11 -04:00
process.on('uncaughtException', (e) => {
console.log(`Caught an uncaught exception!\n${e.stack}`)
})