eval-server/index.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-09-15 05:59:58 -04:00
const { Isolate } = require('isolated-vm')
2023-07-01 09:07:51 -04:00
const { Server } = require('socket.io')
2023-09-15 05:59:58 -04:00
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:'
2023-07-01 09:07:51 -04:00
const io = new Server(3069)
io.on('connection', (socket) => {
2023-09-15 05:59:58 -04:00
let functions
2023-07-22 22:10:01 -04:00
2023-09-15 05:59:58 -04:00
let proxy
2023-08-18 23:43:53 -04:00
2023-09-15 05:59:58 -04:00
const handler = {
get (target, prop) {
if (!target[prop]) throw new Error(`Function "${prop}" not available`)
2023-08-18 23:48:17 -04:00
2023-09-15 05:59:58 -04:00
return (...args) => target[prop](...args)
}
}
2023-07-02 09:11:38 -04:00
2023-09-15 05:59:58 -04:00
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)
})
})
}
}
2023-09-15 05:59:58 -04:00
proxy = new Proxy(functions, handler)
2023-07-02 09:11:38 -04:00
2023-09-15 05:59:58 -04:00
resetVM()
})
2023-08-18 23:43:53 -04:00
2023-09-15 05:59:58 -04:00
const isolate = new Isolate({ memoryLimit: 256 })
2023-07-01 09:07:51 -04:00
2023-09-15 05:59:58 -04:00
let context
async function resetVM () {
context = await isolate.createContext()
2023-09-15 05:59:58 -04:00
// TODO: fix
2023-08-18 23:48:17 -04:00
2023-09-15 05:59:58 -04:00
// const global = context.global
// await global.set('this', global.derefInto())
// await global.set('global', global.derefInto())
// await global.set('bridge', () => { return proxy })
// await global.set('randomstring', () => randomstring)
// await global.set('ChatMessage', () => ChatMessage)
// await global.set('mc', () => mc)
// await global.set('moment', () => moment)
// await global.set('crypto', () => crypto)
// await global.set('nbt', () => nbt)
}
2023-07-01 09:07:51 -04:00
2023-09-15 05:59:58 -04:00
resetVM()
2023-07-01 09:07:51 -04:00
2023-09-15 05:59:58 -04:00
socket.on('runCode', async (transactionId, code) => {
try {
2023-09-16 07:02:53 -04:00
const output = await context.eval(code, { timeout: 1000 })
2023-09-15 05:59:58 -04:00
socket.emit('codeOutput', transactionId, false, util.inspect(output, { stylize }))
} catch (e) {
socket.emit('codeOutput', transactionId, true, e.toString())
}
2023-07-01 09:07:51 -04:00
})
2023-09-15 05:59:58 -04:00
socket.on('reset', resetVM)
2023-07-01 09:07:51 -04:00
})
2023-07-02 02:45:11 -04:00
process.on('uncaughtException', (e) => {
console.log(`Caught an uncaught exception!\n${e.stack}`)
})