improve by using proxy !!

This commit is contained in:
Chayapak 2023-07-02 20:11:38 +07:00
parent dd037643aa
commit e2c8374431

View file

@ -12,24 +12,35 @@ const BRIDGE_PREFIX = 'function:'
const io = new Server(3069) const io = new Server(3069)
function checkString (string) { io.on('connection', (socket) => {
if (typeof(string) !== 'string') throw new Error('Must be a string') let functions
let proxy
const handler = {
get (target, prop) {
if (!target[prop]) throw new Error(`Function "${prop}" not available`)
return (...args) => target[prop](...args)
}
} }
io.on('connection', (socket) => { socket.on('setFunctions', (...args) => {
functions = {}
for (const eachFuntion of args) {
functions[eachFuntion] = (...args) => socket.emit(BRIDGE_PREFIX + eachFuntion, ...args)
}
proxy = new Proxy(functions, handler)
})
let vm let vm
function resetVM () { function resetVM () {
vm = new VM({ vm = new VM({
timeout: 3000, timeout: 3000,
sandbox: { sandbox: {
chat: (message) => { bridge: () => proxy,
checkString(message)
socket.emit(BRIDGE_PREFIX + 'chat', message)
},
core: (command) => {
checkString(command)
socket.emit(BRIDGE_PREFIX + 'core', command)
},
randomstring, randomstring,
ChatMessage, ChatMessage,
mc, mc,