Last active
March 7, 2022 10:42
-
-
Save Innei/865b40849d61c2200f1c6ec99c48f716 to your computer and use it in GitHub Desktop.
test nodejs vm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(){ | |
return 'bar' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('https'), | |
vm = require('vm') | |
;[ | |
'https://gist.githubusercontent.com/Innei/865b40849d61c2200f1c6ec99c48f716/raw/b4ceb3af6b5a52040a1f31594e5ee53154b8b6d5/case-1.js', | |
].forEach((url) => { | |
http.get(url, (res) => { | |
if (res.statusCode === 200) { | |
let rawData = '' | |
res.setEncoding('utf8') | |
res.on('data', (chunk) => { | |
rawData += chunk | |
}) | |
res.on('end', () => { | |
vm.runInThisContext(`${rawData}; let res = foo();console.log(res)`, url) | |
}) | |
} | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { default: axios } = require('axios') | |
const express = require('express') | |
const vm = require('vm') | |
const app = express() | |
app.listen(2000, () => { | |
console.log('http://localhost:2000') | |
}) | |
app.get('/', async (req, res) => { | |
const url = | |
'https://gist.githubusercontent.com/Innei/865b40849d61c2200f1c6ec99c48f716/raw/b4ceb3af6b5a52040a1f31594e5ee53154b8b6d5/case-1.js' | |
const { data } = await axios.get(url) | |
const vmRes = vm.runInThisContext( | |
`${data};function main() { let res = foo();return res}; main(); `, | |
url, | |
) | |
console.log('vm:', vmRes) | |
res.send(vmRes) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment