ChomeNS
b13456c9c8
getPlayerList will temporailly not work for now because i am too lazy to fix the output thing
76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
const { parentPort } = require('worker_threads')
|
|
const { VM } = require('vm2')
|
|
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 util = require('util')
|
|
const { stylize } = require('./colors')
|
|
|
|
const BRIDGE_PREFIX = 'function:'
|
|
|
|
let proxy
|
|
|
|
let vm
|
|
|
|
const handler = {
|
|
get (target, prop) {
|
|
if (!target[prop]) throw new Error(`Function "${prop}" not available`)
|
|
|
|
return (...args) => target[prop](...args)
|
|
}
|
|
}
|
|
|
|
let functions
|
|
|
|
parentPort.on('message', (msg) => {
|
|
switch (msg.type) {
|
|
case 'setFunctions':
|
|
const parsed = JSON.parse(msg.jsonArray)
|
|
|
|
functions = {}
|
|
|
|
for (const eachFuntion of parsed) {
|
|
functions[eachFuntion] = (...args) => {
|
|
parentPort.postMessage({ type: 'socketEmit', data: [BRIDGE_PREFIX + eachFuntion, ...args] })
|
|
|
|
// how do i make thjis work
|
|
// return new Promise((resolve) => {
|
|
// parentPort.postMessage({ type: 'socketOnce', data: [`functionOutput:${eachFuntion}`, (message, parseJSON) => {
|
|
// if (parseJSON) resolve(JSON.parse(message))
|
|
// else resolve(message)
|
|
// }]})
|
|
// })
|
|
}
|
|
}
|
|
|
|
proxy = new Proxy(functions, handler)
|
|
|
|
vm = new VM({
|
|
timeout: 100,
|
|
sandbox: {
|
|
get bridge () { return proxy },
|
|
randomstring,
|
|
ChatMessage,
|
|
mc,
|
|
moment,
|
|
crypto,
|
|
nbt
|
|
}
|
|
})
|
|
|
|
break
|
|
case 'runCode':
|
|
try {
|
|
const output = vm.run(msg.code)
|
|
|
|
parentPort.postMessage({ type: 'codeOutput', output: util.inspect(output, { stylize }), error: false })
|
|
} catch (e) {
|
|
parentPort.postMessage({ type: 'codeOutput', output: e.toString(), error: true })
|
|
}
|
|
|
|
break
|
|
}
|
|
})
|