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 15:20:33 +03:00
const { VM } = require('vm2')
2022-12-26 16:24:34 +03:00
const args = process.argv.slice(2)
const port = args[0] || 8000
const host = args[1] || 'localhost'
2023-07-27 09:57:26 +03:00
const stubFunc = (_i) => {}
2022-12-26 16:24:34 +03:00
const vm = new VM({
timeout: 2000,
allowAsync: false,
sandbox: {
atob: atob,
2023-07-27 09:57:26 +03:00
setTimeout: stubFunc,
setInterval: stubFunc,
2023-06-30 09:26:04 +03:00
document: {
2023-07-27 09:57:26 +03:00
getElementById: stubFunc,
prepend: stubFunc,
append: stubFunc,
appendChild: stubFunc,
2023-06-30 09:26:04 +03:00
doctype: {},
currentScript: {},
2023-06-30 09:26:04 +03:00
},
2022-12-26 16:24:34 +03:00
},
})
2023-06-30 09:26:04 +03:00
vm.run('var window = global')
2022-12-26 16:24:34 +03: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 16:24:34 +03: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'))