This repository has been archived on 2024-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
python-aternos/python_aternos/data/server.js

50 lines
1.1 KiB
JavaScript
Raw Normal View History

const http = require('http')
const process = require('process')
2022-12-26 16:20:33 +04:00
const { VM } = require('vm2')
2022-12-26 17:24:34 +04:00
const args = process.argv.slice(2)
const port = args[0] || 8000
const host = args[1] || 'localhost'
2023-07-27 10:57:26 +04:00
const stubFunc = (_i) => {}
2022-12-26 17:24:34 +04:00
const vm = new VM({
timeout: 2000,
allowAsync: false,
sandbox: {
atob: atob,
2023-07-27 10:57:26 +04:00
setTimeout: stubFunc,
setInterval: stubFunc,
2023-06-30 10:26:04 +04:00
document: {
2023-07-27 10:57:26 +04:00
getElementById: stubFunc,
prepend: stubFunc,
append: stubFunc,
appendChild: stubFunc,
2023-06-30 10:26:04 +04:00
doctype: {},
currentScript: {},
2023-06-30 10:26:04 +04:00
},
2022-12-26 17:24:34 +04:00
},
})
2023-06-30 10:26:04 +04:00
vm.run('var window = global')
2022-12-26 17:24:34 +04:00
const listener = (req, res) => {
if (req.method != 'POST')
res.writeHead(405) & res.end()
let body = ''
req.on('data', chunk => (body += chunk))
req.on('end', () => {
let resp
2022-12-26 17:24:34 +04:00
try { resp = JSON.stringify(vm.run(body)) }
catch (ex) { resp = ex.message }
res.writeHead(200)
res.end(resp)
})
}
const server = http.createServer(listener)
server.listen(port, host, () => console.log('OK'))