Merge branch 'master' of ssh://github.com/engineer-man/piston

This commit is contained in:
Brian Seymour 2021-01-21 14:11:46 -06:00
commit 14e0ace2a0
38 changed files with 461 additions and 297 deletions

View File

@ -1,48 +0,0 @@
const { writeFile } = require('fs/promises');
const { spawn } = require('child_process');
async function execute(res, language, body) {
const stamp = new Date().getTime();
const sourceFile = `/tmp/${stamp}.code`;
await writeFile(sourceFile, body.source);
const process = spawn(__dirname + '/../../lxc/execute', [
language.name,
sourceFile,
body.args?.join('\n') ?? '',
]);
const result = {
ran: true,
language: language.name,
stderr: '',
stdout: '',
output: '',
};
if (language.version)
result.version = language.version;
process.stderr.on('data', chunk => {
result.stderr += chunk;
result.output += chunk;
});
process.stdout.on('data', chunk => {
result.stdout += chunk;
result.output += chunk;
});
process.on('exit', () => {
result.stderr = result.stderr.trim().substring(0, 65535);
result.stdout = result.stdout.trim().substring(0, 65535);
result.output = result.output.trim().substring(0, 65535);
res.json(result);
});
}
module.exports = {
execute,
};

View File

@ -1,5 +1,5 @@
const express = require('express'); const express = require('express');
const { execute } = require('./execute'); const { execute } = require('../../shared/execute');
const { languages } = require('./languages'); const { languages } = require('./languages');
const { checkSchema, validationResult } = require('express-validator'); const { checkSchema, validationResult } = require('express-validator');
@ -20,7 +20,7 @@ app.post(
errorMessage: 'Supplied language is not a string', errorMessage: 'Supplied language is not a string',
}, },
custom: { custom: {
options: value => languages.find(language => language.aliases.includes(value?.toLowerCase())), options: value => value && languages.find(language => language.aliases.includes(value.toLowerCase())),
errorMessage: 'Supplied language is not supported by Piston', errorMessage: 'Supplied language is not supported by Piston',
}, },
}, },
@ -39,9 +39,16 @@ app.post(
isArray: { isArray: {
errorMessage: 'Supplied args is not an array', errorMessage: 'Supplied args is not an array',
}, },
},
stdin: {
in: 'body',
optional: true,
isString: {
errorMessage: 'Supplied stdin is not a string',
},
} }
}), }),
(req, res) => { async (req, res) => {
const errors = validationResult(req).array(); const errors = validationResult(req).array();
if (errors.length === 0) { if (errors.length === 0) {
@ -49,7 +56,16 @@ app.post(
language.aliases.includes(req.body.language.toLowerCase()) language.aliases.includes(req.body.language.toLowerCase())
); );
execute(res, language, req.body); const { stdout, stderr, output, ran } = await execute(language, req.body.source, req.body.stdin, req.body.args);
res.status(200).json({
ran,
language: language.name,
version: language.version,
stdout,
stderr,
output,
});
} else { } else {
res.status(400).json({ res.status(400).json({
message: errors[0].msg, message: errors[0].msg,

View File

@ -1,119 +1,5 @@
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const languages = require('../../shared/languages.json');
const languages = [
{
name: 'nasm',
aliases: ['asm', 'nasm'],
},
{
name: 'nasm64',
aliases: ['asm64', 'nasm64'],
},
{
name: 'awk',
aliases: ['awk'],
},
{
name: 'bash',
aliases: ['bash'],
},
{
name: 'brainfuck',
aliases: ['bf', 'brainfuck'],
},
{
name: 'c',
aliases: ['c'],
},
{
name: 'csharp',
aliases: ['c#', 'cs', 'csharp'],
},
{
name: 'cpp',
aliases: ['c++', 'cpp'],
},
{
name: 'deno',
aliases: ['deno', 'denojs', 'denots'],
},
{
name: 'ruby',
aliases: ['duby', 'rb', 'ruby'],
},
{
name: 'emacs',
aliases: ['el', 'elisp', 'emacs'],
},
{
name: 'elixir',
aliases: ['elixir'],
},
{
name: 'haskell',
aliases: ['haskell', 'hs'],
},
{
name: 'go',
aliases: ['go'],
},
{
name: 'java',
aliases: ['java'],
},
{
name: 'node',
aliases: ['javascript', 'js', 'node'],
},
{
name: 'jelly',
aliases: ['jelly'],
},
{
name: 'julia',
aliases: ['jl', 'julia'],
},
{
name: 'kotlin',
aliases: ['kotlin'],
},
{
name: 'lua',
aliases: ['lua'],
},
{
name: 'paradoc',
aliases: ['paradoc'],
},
{
name: 'perl',
aliases: ['perl'],
},
{
name: 'php',
aliases: ['php', 'php3', 'php4', 'php5'],
},
{
name: 'python3',
aliases: ['py', 'py3', 'python', 'python3'],
},
{
name: 'python2',
aliases: ['python2'],
},
{
name: 'rust',
aliases: ['rs', 'rust'],
},
{
name: 'swift',
aliases: ['swift'],
},
{
name: 'typescript',
aliases: ['ts', 'typescript'],
},
];
{ {
const process = spawn(__dirname + '/../../lxc/versions'); const process = spawn(__dirname + '/../../lxc/versions');

32
cli/execute Executable file
View File

@ -0,0 +1,32 @@
#!/usr/bin/env node
const { execute } = require('../shared/execute');
const { readFileSync } = require('fs');
const languages = require('../shared/languages.json');
const [languageName, sourceFile, ...args] = process.argv.slice(2);
(async () => {
if (!languageName) {
console.error('Provide a language name');
return;
}
if (!sourceFile) {
console.error('Provide a source file');
return;
}
const source = readFileSync(sourceFile).toString();
const language = languages.find(language => language.aliases.includes(languageName.toLowerCase()));
if (!language) {
console.error(`${languageName} is not supported by Piston`);
return;
}
const { output } = await execute(language, source, '', args);
console.log(output);
})();

View File

@ -14,13 +14,15 @@ epoch=$(date +%s%N)
basepath="/var/lib/lxc/piston/rootfs" basepath="/var/lib/lxc/piston/rootfs"
filepath="/tmp/$epoch/code.code" filepath="/tmp/$epoch/code.code"
argpath="/tmp/$epoch/args.args" argpath="/tmp/$epoch/args.args"
stdinpath="/tmp/$epoch/stdin.stdin"
arg=$(basename $argpath) arg=$(basename $argpath)
# write arg file # write arg file
mkdir -p $basepath/tmp/$epoch mkdir -p $basepath/tmp/$epoch
chmod 777 $basepath/tmp/$epoch chmod 777 $basepath/tmp/$epoch
cat $2 > $basepath$filepath cat $2 > $basepath$filepath
echo "${@:3}" > $basepath$argpath echo $3 > $basepath$stdinpath
echo "${@:4}" > $basepath$argpath
# process incrementor # process incrementor
exec 200>$dir/lockfile exec 200>$dir/lockfile
@ -39,17 +41,21 @@ exec 200>&-
# runner # runner
timeout -s KILL 20 \ timeout -s KILL 20 \
lxc-attach --clear-env -n piston -- \ lxc-attach --clear-env -n piston -- \
/bin/bash -l -c "bash /exec/$lang $newinc $epoch" /bin/bash -l -c "runuser runner$newinc /exec/$lang $newinc $epoch"
# process janitor # process janitor
lxc-attach --clear-env -n piston -- \ lxc-attach --clear-env -n piston -- \
/bin/bash -l -c "\ /bin/bash -l -c "
for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\ for i in {1..100}
find /tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\ do
find /var/tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\ pkill -u runner$newinc --signal SIGKILL
find /var/lock -user runner$newinc -exec /bin/rm -rf {} \; ;\ done
find /dev/shm -user runner$newinc -exec /bin/rm -rf {} \; ;\
find /run/lock -user runner$newinc -exec /bin/rm -rf {} \; ;\ find /tmp -user runner$newinc -delete
" > /dev/null 2>&1 & find /var/tmp -user runner$newinc -delete
find /var/lock -user runner$newinc -delete
find /dev/shm -user runner$newinc -delete
find /run/lock -user runner$newinc -delete
" > /dev/null 2>&1 &
rm -rf $basepath/tmp/$epoch rm -rf $basepath/tmp/$epoch

View File

@ -1,4 +1,6 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
timeout -s KILL 2 sed "/___code___/Q" code.code > code.stdin timeout -s KILL 2 sed "/___code___/Q" code.code > code.stdin
timeout -s KILL 2 sed "1,/___code___/d" code.code > code.awk timeout -s KILL 2 sed "1,/___code___/d" code.code > code.awk
runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 awk -f code.awk < code.stdin" timeout -s KILL 3 awk -f code.awk < code.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 bash code.code" xargs -a args.args -d '\n' timeout -s KILL 3 bash code.code < stdin.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 brainfuck code.code" timeout -s KILL 3 xargs -a args.args -d '\n' brainfuck code.code < stdin.stdin

View File

@ -1,4 +1,5 @@
runuser runner$1 -c "\ #!/usr/bin/bash
cd /tmp/$2 ; \
gcc -std=c11 -o binary -x c code.code ; \ cd /tmp/$2
cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" gcc -std=c11 -o binary -x c code.code
timeout -s KILL 3 xargs -a args.args ./binary < stdin.stdin

View File

@ -1,4 +1,5 @@
runuser runner$1 -c "\ #!/bin/bash
cd /tmp/$2 ; \
g++ -std=c++17 -o binary -x c++ code.code ; \ cd /tmp/$2
cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" g++ -std=c++17 -o binary -x c++ code.code
timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin

View File

@ -1,4 +1,5 @@
runuser runner$1 -c "\ #!/bin/bash
cd /tmp/$2 ; \
mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary ; \ cd /tmp/$2
cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary" mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary
timeout -s KILL 3 xargs -a args.args -d '\n' mono binary < stdin.stdin

View File

@ -1,7 +1,4 @@
cd /tmp/$2 #!/bin/bash
if [[ -z $(grep '[^[:space:]]' args.args) ]]; then cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 deno run code.code" timeout -s KILL 3 xargs -a args.args -d '\n' deno run code.code < stdin.stdin
else
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 deno run code.code"
fi

View File

@ -1,7 +1,4 @@
cd /tmp/$2 #!/bin/bash
if [[ -z $(grep '[^[:space:]]' args.args) ]]; then cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 elixir code.code" timeout -s KILL 3 xargs -a args.args -d '\n' elixir code.code < stdin.stdin
else
runuser runner$1 -c "cd /tmp/$2 ; cat args.args ; xargs -d '\n' timeout -s KILL 3 elixir code.code"
fi

View File

@ -1,7 +1,4 @@
cd /tmp/$2 #!/bin/bash
if [[ -z $(grep '[^[:space:]]' args.args) ]]; then cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 emacs -Q --script code.code" timeout -s KILL 3 xargs -a args.args -d '\n' emacs -Q --script code.code < stdin.stdin
else
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 emacs -Q --script code.code"
fi

View File

@ -1,6 +1,6 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
cp code.code interim.go cp code.code interim.go
file="interim.go" go build interim.go
go build $file timeout -s KILL 3 xargs -a args.args -d '\n' ./interim < stdin.stdin
file=${file%%.*}
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./$file"

View File

@ -1,7 +1,6 @@
cd /tmp/$2 #!/bin/bash
mv code.code code.hs
runuser runner$1 -c "\ cd /tmp/$2
cd /tmp/$2 ; \ cp code.code code.hs
ghc -dynamic -o binary code.hs > /dev/null 2>&1 ; \ ghc -dynamic -o binary code.hs > /dev/null 2>&1
cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin

View File

@ -1,9 +1,7 @@
cd /tmp/$2 #!/bin/bash
cp code.code interim.java
name=$(grep -Po "(?<=\n|\A)\s*(public\s+)?(class|interface)\s+\K([^\/\\\\\n\s{]+)" interim.java)
mv interim.java $name.java
runuser runner$1 -c "\ cd /tmp/$2
cd /tmp/$2 ; \ name=$(grep -Po "(?<=\n|\A)\s*(public\s+)?(class|interface)\s+\K([^\/\\\\\n\s{]+)" code.code)
javac $name.java ; \ cp code.code $name.java
cat args.args | xargs -d '\n' timeout -s KILL 3 java $name" javac $name.java
timeout -s KILL 3 xargs -a args.args -d '\n' java $name < stdin.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 jelly fu code.code" timeout -s KILL 3 xargs -a args.args -d '\n' jelly fu code.code < stdin.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 julia code.code" timeout -s KILL 3 xargs -a args.args -d '\n' julia code.code < stdin.stdin

View File

@ -1,6 +1,6 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
cp code.code code.kt cp code.code code.kt
runuser runner$1 -c "\ kotlinc code.kt -include-runtime -d code.jar
cd /tmp/$2 ; \ timeout -s KILL 3 xargs -a args.args -d '\n' java -jar code.jar < stdin.stdin
kotlinc code.kt -include-runtime -d code.jar ; \
cat args.args | xargs -d '\n' timeout -s KILL 3 java -jar code.jar"

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 lua code.code" timeout -s KILL 3 xargs -a args.args -d '\n' lua code.code < stdin.stdin

View File

@ -1,5 +1,6 @@
runuser runner$1 -c "\ #!/bin/bash
cd /tmp/$2 ; \
nasm -f elf32 -o binary.o code.code ; \ cd /tmp/$2
ld -m elf_i386 binary.o -o binary ; \ nasm -f elf32 -o binary.o code.code
cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" ld -m elf_i386 binary.o -o binary
timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin

View File

@ -1,5 +1,6 @@
runuser runner$1 -c "\ #!/bin/bash
cd /tmp/$2 ; \
nasm -f elf64 -o binary.o code.code ; \ cd /tmp/$2
ld -m elf_x86_64 binary.o -o binary ; \ nasm -f elf64 -o binary.o code.code
cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" ld -m elf_x86_64 binary.o -o binary
timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin

View File

@ -1,7 +1,4 @@
cd /tmp/$2 #!/bin/bash
if [[ -z $(grep '[^[:space:]]' args.args) ]]; then cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 node code.code" timeout -s KILL 3 xargs -a args.args -d '\n' node code.code < stdin.stdin
else
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code"
fi

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 python3 -m paradoc code.code <<< args.args" timeout -s KILL 3 python3 -m paradoc code.code < args.args

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 perl code.code" timeout -s KILL 3 xargs -a args.args -d '\n' perl code.code < stdin.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 php code.code" timeout -s KILL 3 xargs -a args.args -d '\n' php code.code < stdin.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python code.code" timeout -s KILL 3 xargs -a args.args -d '\n' python code.code < stdin.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 python3.8 code.code" timeout -s KILL 3 xargs -a args.args -d '\n' python3.8 code.code < stdin.stdin

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ruby code.code" timeout -s KILL 3 xargs -a args.args -d '\n' ruby code.code < stdin.stdin

View File

@ -1,5 +1,5 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
rustc -o binary code.code rustc -o binary code.code
runuser runner$1 -c "\ timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin
cd /tmp/$2 ; \
cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary"

View File

@ -1,2 +1,4 @@
#!/bin/bash
cd /tmp/$2 cd /tmp/$2
runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 swift code.code" timeout -s KILL 3 xargs -a args.args -d '\n' swift code.code < stdin.stdin

View File

@ -1,7 +1,8 @@
runuser runner$1 -c "\ #!/bin/bash
cd /tmp/$2 ; \
mv code.code interim.ts ; \ cd /tmp/$2
tsc interim.ts ; \ mv code.code interim.ts
rm -f interim.ts ; \ tsc interim.ts
mv interim.js code.code ; \ rm -f interim.ts
cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code" mv interim.js code.code
timeout -s KILL 3 xargs -a args.args -d '\n' node code.code < stdin.stdin

View File

@ -3,7 +3,7 @@
mkdir -p /var/lib/lxc/piston/rootfs/exec mkdir -p /var/lib/lxc/piston/rootfs/exec
rm -f /var/lib/lxc/piston/rootfs/exec/* rm -f /var/lib/lxc/piston/rootfs/exec/*
cp -f executors/* /var/lib/lxc/piston/rootfs/exec cp -f executors/* /var/lib/lxc/piston/rootfs/exec
chmod 700 /var/lib/lxc/piston/rootfs/exec/* chmod 555 /var/lib/lxc/piston/rootfs/exec/*
chown -R root:root /var/lib/lxc/piston/rootfs/exec chown -R root:root /var/lib/lxc/piston/rootfs/exec
lxc-start -n piston -d lxc-start -n piston -d

View File

@ -2,54 +2,54 @@
cd tests cd tests
echo 'testing awk' echo 'testing awk'
../execute awk test.awk ../../cli/execute awk test.awk
echo 'testing c' echo 'testing c'
../execute c test.c ../../cli/execute c test.c
echo 'testing cpp' echo 'testing cpp'
../execute cpp test.cpp ../../cli/execute cpp test.cpp
echo 'testing cs' echo 'testing cs'
../execute cs test.cs ../../cli/execute cs test.cs
echo 'testing deno' echo 'testing deno'
../execute deno testdeno.ts ../../cli/execute deno testdeno.ts
echo 'testing elisp' echo 'testing elisp'
../execute elisp test.el ../../cli/execute elisp test.el
echo 'testing elixir' echo 'testing elixir'
../execute exs test.exs ../../cli/execute exs test.exs
echo 'testing go' echo 'testing go'
../execute go test.go ../../cli/execute go test.go
echo 'testing haskell' echo 'testing haskell'
../execute haskell test.hs ../../cli/execute haskell test.hs
echo 'testing java' echo 'testing java'
../execute java test.java ../../cli/execute java test.java
echo 'testing jelly' echo 'testing jelly'
../execute jelly test.jelly ../../cli/execute jelly test.jelly
echo 'testing jl' echo 'testing jl'
../execute jl test.jl ../../cli/execute jl test.jl
echo 'testing js' echo 'testing js'
../execute js test.js ../../cli/execute js test.js
echo 'testing kotlin' echo 'testing kotlin'
../execute kotlin test.kt ../../cli/execute kotlin test.kt
echo 'testing asm 32 bit' echo 'testing asm 32 bit'
../execute asm test.nasm ../../cli/execute asm test.nasm
echo 'testing asm 64 bit' echo 'testing asm 64 bit'
../execute asm64 test64.nasm ../../cli/execute asm64 test64.nasm
echo 'testing php' echo 'testing php'
../execute php test.php ../../cli/execute php test.php
echo 'testing perl' echo 'testing perl'
../execute perl test.pl ../../cli/execute perl test.pl
echo 'testing ruby' echo 'testing ruby'
../execute ruby test.rb ../../cli/execute ruby test.rb
echo 'testing rust' echo 'testing rust'
../execute rust test.rs ../../cli/execute rust test.rs
echo 'testing bash' echo 'testing bash'
../execute bash test.sh ../../cli/execute bash test.sh
echo 'testing swift' echo 'testing swift'
../execute swift test.swift ../../cli/execute swift test.swift
echo 'testing typescript' echo 'testing typescript'
../execute typescript test.ts ../../cli/execute typescript test.ts
echo 'testing python2' echo 'testing python2'
../execute python2 test2.py ../../cli/execute python2 test2.py
echo 'testing python3' echo 'testing python3'
../execute python3 test3.py ../../cli/execute python3 test3.py
echo 'testing paradoc' echo 'testing paradoc'
../execute python3 test_paradoc.py ../../cli/execute python3 test_paradoc.py

View File

@ -148,6 +148,8 @@ cd piston/lxc
- See `var/install.txt` for how to create a new LXC container and install all of the required - See `var/install.txt` for how to create a new LXC container and install all of the required
software. software.
#### CLI Usage
- `cli/execute [language] [file path] [args]`
<br> <br>
# Usage # Usage
@ -185,14 +187,17 @@ Content-Type: application/json
[ [
{ {
"name": "awk", "name": "awk",
"aliases": ["awk"],
"version": "1.3.3" "version": "1.3.3"
}, },
{ {
"name": "bash", "name": "bash",
"aliases": ["bash"],
"version": "4.4.20" "version": "4.4.20"
}, },
{ {
"name": "c", "name": "c",
"aliases": ["c"],
"version": "7.5.0" "version": "7.5.0"
} }
] ]
@ -236,8 +241,7 @@ HTTP/1.1 400 Bad Request
Content-Type: application/json Content-Type: application/json
{ {
"code": "unsupported_language", "message": "Provided language is not supported by Piston"
"message": "whatever is not supported by Piston"
} }
``` ```

49
shared/execute.js Normal file
View File

@ -0,0 +1,49 @@
const { writeFileSync } = require('fs');
const { spawn } = require('child_process');
function execute(language, source, stdin = '', args = []) {
return new Promise(resolve => {
const stamp = new Date().getTime();
const sourceFile = `/tmp/${stamp}.code`;
writeFileSync(sourceFile, source);
const process = spawn(__dirname + '/../lxc/execute', [
language.name,
sourceFile,
stdin,
args.join('\n'),
]);
let stdout = '';
let stderr = '';
let output = '';
process.stderr.on('data', chunk => {
stderr += chunk;
output += chunk;
});
process.stdout.on('data', chunk => {
stdout += chunk;
output += chunk;
});
process.on('exit', code => {
stderr = stderr.trim().substring(0, 65535);
stdout = stdout.trim().substring(0, 65535);
output = output.trim().substring(0, 65535);
resolve({
stdout,
stderr,
output,
ran: code === 0,
});
});
});
}
module.exports = {
execute,
};

202
shared/languages.json Normal file
View File

@ -0,0 +1,202 @@
[
{
"name": "nasm",
"aliases": [
"asm",
"nasm"
]
},
{
"name": "nasm64",
"aliases": [
"asm64",
"nasm64"
]
},
{
"name": "awk",
"aliases": [
"awk"
]
},
{
"name": "bash",
"aliases": [
"bash"
]
},
{
"name": "brainfuck",
"aliases": [
"bf",
"brainfuck"
]
},
{
"name": "c",
"aliases": [
"c"
]
},
{
"name": "csharp",
"aliases": [
"c#",
"cs",
"csharp"
]
},
{
"name": "cpp",
"aliases": [
"c++",
"cpp",
"cc",
"cxx"
]
},
{
"name": "deno",
"aliases": [
"deno",
"denojs",
"denots"
]
},
{
"name": "ruby",
"aliases": [
"duby",
"rb",
"ruby"
]
},
{
"name": "emacs",
"aliases": [
"el",
"elisp",
"emacs"
]
},
{
"name": "elixir",
"aliases": [
"elixir",
"exs"
]
},
{
"name": "haskell",
"aliases": [
"haskell",
"hs"
]
},
{
"name": "go",
"aliases": [
"go",
"golang"
]
},
{
"name": "java",
"aliases": [
"java"
]
},
{
"name": "node",
"aliases": [
"javascript",
"js",
"node",
"node.js"
]
},
{
"name": "jelly",
"aliases": [
"jelly"
]
},
{
"name": "julia",
"aliases": [
"jl",
"julia"
]
},
{
"name": "kotlin",
"aliases": [
"kotlin",
"kt"
]
},
{
"name": "lua",
"aliases": [
"lua"
]
},
{
"name": "paradoc",
"aliases": [
"paradoc"
]
},
{
"name": "perl",
"aliases": [
"perl",
"pl"
]
},
{
"name": "php",
"aliases": [
"php",
"php3",
"php4",
"php5"
]
},
{
"name": "python3",
"aliases": [
"py",
"py3",
"python",
"python3"
]
},
{
"name": "python2",
"aliases": [
"python2",
"py2"
]
},
{
"name": "rust",
"aliases": [
"rs",
"rust"
]
},
{
"name": "swift",
"aliases": [
"swift"
]
},
{
"name": "typescript",
"aliases": [
"ts",
"typescript"
]
}
]