feat: execute files check via docker cli

WARNING: possible command injection vunerability.
This commit is contained in:
Lin, Hung-Shin authored and wancat committed 2023-10-29 17:41:38 +08:00
1 parent 38f3dbcd65
commit 50b1fe5124
4 files changed
+17 -53

No files matched your search

-46
View File
@@ -1,6 +1,4 @@
import { PassThrough } from 'stream'
import Docker from 'dockerode'
import { buildFileCheckCmd } from '@linux-odyssey/utils'
import config from '../config.js'
const engine = new Docker()
@@ -79,47 +77,3 @@ export async function deleteContainer(id) {
}
await container.remove()
}
// Utility function to convert a stream to a string
function streamToString(stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
setTimeout(() => resolve(Buffer.concat(chunks).toString('utf8')), 100)
})
}
export async function checkFiles(id, files) {
const container = engine.getContainer(id)
if (!container) {
throw new Error('Container not found')
}
const cmd = buildFileCheckCmd(files)
console.log(cmd)
const exec = await container.exec({
AttachStdout: true,
AttachStderr: true,
Cmd: ['sh', '-c', cmd],
})
const execStream = await exec.start()
const outStream = new PassThrough()
const errStream = new PassThrough()
console.log('exec started')
exec.modem.demuxStream(execStream, outStream, errStream)
const [output, errorOutput] = await Promise.all([
streamToString(outStream),
streamToString(errStream),
])
console.log('output', output, 'errorOutput', errorOutput)
const execOutput = await exec.inspect()
console.log('exit code', execOutput.ExitCode)
console.log('exit code === 0', execOutput.ExitCode === 0)
return execOutput.ExitCode === 0
}