2024-10-18 22:18:01 -04:00
|
|
|
const { VM } = require('@n8n/vm2')
|
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')
|
2024-10-20 01:28:02 -04:00
|
|
|
const mineflayer = require('mineflayer')
|
2023-09-15 05:59:58 -04:00
|
|
|
const moment = require('moment-timezone')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const nbt = require('prismarine-nbt')
|
2024-10-19 00:12:34 -04:00
|
|
|
const net = require('net')
|
|
|
|
const axios = require('axios')
|
2023-09-15 05:59:58 -04:00
|
|
|
|
|
|
|
const BRIDGE_PREFIX = 'function:'
|
2023-07-01 09:07:51 -04:00
|
|
|
|
|
|
|
const io = new Server(3069)
|
|
|
|
|
2024-10-18 22:18:01 -04:00
|
|
|
io.on('connection', async (socket) => {
|
2023-09-15 05:59:58 -04:00
|
|
|
let functions
|
|
|
|
let proxy
|
2024-10-18 22:18:01 -04:00
|
|
|
let vm
|
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-08-01 22:29:48 -04:00
|
|
|
|
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
|
|
|
async function resetVM () {
|
2024-10-18 22:18:01 -04:00
|
|
|
vm = new VM({
|
|
|
|
timeout: 1000,
|
|
|
|
sandbox: {
|
|
|
|
get bridge () { return proxy },
|
|
|
|
randomstring,
|
|
|
|
ChatMessage,
|
|
|
|
mc,
|
2024-10-20 01:28:02 -04:00
|
|
|
mineflayer,
|
2024-10-18 22:18:01 -04:00
|
|
|
moment,
|
|
|
|
crypto,
|
2024-10-19 00:12:34 -04:00
|
|
|
nbt,
|
|
|
|
net,
|
|
|
|
axios
|
2024-10-18 22:18:01 -04:00
|
|
|
}
|
|
|
|
})
|
2023-09-15 05:59:58 -04:00
|
|
|
}
|
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) => {
|
2024-10-19 06:59:11 -04:00
|
|
|
new Promise(() => {
|
|
|
|
try {
|
|
|
|
const output = vm.run(code)
|
2023-08-17 21:58:54 -04:00
|
|
|
|
2024-10-19 06:59:11 -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}`)
|
|
|
|
})
|