diff --git a/api/src/execute.js b/api/src/execute.js deleted file mode 100644 index 1409a7b..0000000 --- a/api/src/execute.js +++ /dev/null @@ -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, -}; diff --git a/api/src/index.js b/api/src/index.js index c29a461..d9a5cae 100644 --- a/api/src/index.js +++ b/api/src/index.js @@ -1,5 +1,5 @@ const express = require('express'); -const { execute } = require('./execute'); +const { execute } = require('../../shared/execute'); const { languages } = require('./languages'); const { checkSchema, validationResult } = require('express-validator'); @@ -20,7 +20,7 @@ app.post( errorMessage: 'Supplied language is not a string', }, 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', }, }, @@ -39,9 +39,16 @@ app.post( isArray: { 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(); if (errors.length === 0) { @@ -49,7 +56,16 @@ app.post( 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 { res.status(400).json({ message: errors[0].msg, diff --git a/api/src/languages.js b/api/src/languages.js index 9fda547..23e352a 100644 --- a/api/src/languages.js +++ b/api/src/languages.js @@ -1,119 +1,5 @@ const { spawn } = require('child_process'); - -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 languages = require('../../shared/languages.json'); { const process = spawn(__dirname + '/../../lxc/versions'); diff --git a/cli/execute b/cli/execute new file mode 100755 index 0000000..a8dc72c --- /dev/null +++ b/cli/execute @@ -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); +})(); diff --git a/lxc/execute b/lxc/execute index 42d5b52..d7e5b22 100755 --- a/lxc/execute +++ b/lxc/execute @@ -14,13 +14,15 @@ epoch=$(date +%s%N) basepath="/var/lib/lxc/piston/rootfs" filepath="/tmp/$epoch/code.code" argpath="/tmp/$epoch/args.args" +stdinpath="/tmp/$epoch/stdin.stdin" arg=$(basename $argpath) # write arg file mkdir -p $basepath/tmp/$epoch chmod 777 $basepath/tmp/$epoch cat $2 > $basepath$filepath -echo "${@:3}" > $basepath$argpath +echo $3 > $basepath$stdinpath +echo "${@:4}" > $basepath$argpath # process incrementor exec 200>$dir/lockfile @@ -39,17 +41,21 @@ exec 200>&- # runner timeout -s KILL 20 \ 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 lxc-attach --clear-env -n piston -- \ - /bin/bash -l -c "\ - for i in {1..100}; do pkill -u runner$newinc --signal SIGKILL; done ;\ - find /tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\ - find /var/tmp -user runner$newinc -exec /bin/rm -rf {} \; ;\ - find /var/lock -user runner$newinc -exec /bin/rm -rf {} \; ;\ - find /dev/shm -user runner$newinc -exec /bin/rm -rf {} \; ;\ - find /run/lock -user runner$newinc -exec /bin/rm -rf {} \; ;\ - " > /dev/null 2>&1 & + /bin/bash -l -c " + for i in {1..100} + do + pkill -u runner$newinc --signal SIGKILL + done + + find /tmp -user runner$newinc -delete + 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 diff --git a/lxc/executors/awk b/lxc/executors/awk index 26723b3..dafa525 100755 --- a/lxc/executors/awk +++ b/lxc/executors/awk @@ -1,4 +1,6 @@ +#!/bin/bash + cd /tmp/$2 timeout -s KILL 2 sed "/___code___/Q" code.code > code.stdin 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 diff --git a/lxc/executors/bash b/lxc/executors/bash index c13ad07..6aeb899 100755 --- a/lxc/executors/bash +++ b/lxc/executors/bash @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/brainfuck b/lxc/executors/brainfuck index b4458a0..7ec1bfe 100755 --- a/lxc/executors/brainfuck +++ b/lxc/executors/brainfuck @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/c b/lxc/executors/c index 3130257..096b1d9 100755 --- a/lxc/executors/c +++ b/lxc/executors/c @@ -1,4 +1,5 @@ -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - gcc -std=c11 -o binary -x c code.code ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +#!/usr/bin/bash + +cd /tmp/$2 +gcc -std=c11 -o binary -x c code.code +timeout -s KILL 3 xargs -a args.args ./binary < stdin.stdin diff --git a/lxc/executors/cpp b/lxc/executors/cpp index 4b8c6cf..5f7008b 100755 --- a/lxc/executors/cpp +++ b/lxc/executors/cpp @@ -1,4 +1,5 @@ -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - g++ -std=c++17 -o binary -x c++ code.code ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +#!/bin/bash + +cd /tmp/$2 +g++ -std=c++17 -o binary -x c++ code.code +timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin diff --git a/lxc/executors/csharp b/lxc/executors/csharp index 2c742c7..f35c876 100755 --- a/lxc/executors/csharp +++ b/lxc/executors/csharp @@ -1,4 +1,5 @@ -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 mono binary" +#!/bin/bash + +cd /tmp/$2 +mcs $(echo code.code | sed 's/\///') -nowarn:0219 -out:binary +timeout -s KILL 3 xargs -a args.args -d '\n' mono binary < stdin.stdin diff --git a/lxc/executors/deno b/lxc/executors/deno index b466288..31132fa 100755 --- a/lxc/executors/deno +++ b/lxc/executors/deno @@ -1,7 +1,4 @@ -cd /tmp/$2 +#!/bin/bash -if [[ -z $(grep '[^[:space:]]' args.args) ]]; then - runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 deno run code.code" -else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 deno run code.code" -fi +cd /tmp/$2 +timeout -s KILL 3 xargs -a args.args -d '\n' deno run code.code < stdin.stdin diff --git a/lxc/executors/elixir b/lxc/executors/elixir index 49f9679..49bfb78 100755 --- a/lxc/executors/elixir +++ b/lxc/executors/elixir @@ -1,7 +1,4 @@ -cd /tmp/$2 +#!/bin/bash -if [[ -z $(grep '[^[:space:]]' args.args) ]]; then - runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 elixir code.code" -else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args ; xargs -d '\n' timeout -s KILL 3 elixir code.code" -fi +cd /tmp/$2 +timeout -s KILL 3 xargs -a args.args -d '\n' elixir code.code < stdin.stdin diff --git a/lxc/executors/emacs b/lxc/executors/emacs index 31f6186..ab41624 100755 --- a/lxc/executors/emacs +++ b/lxc/executors/emacs @@ -1,7 +1,4 @@ -cd /tmp/$2 +#!/bin/bash -if [[ -z $(grep '[^[:space:]]' args.args) ]]; then - runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 emacs -Q --script code.code" -else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 emacs -Q --script code.code" -fi +cd /tmp/$2 +timeout -s KILL 3 xargs -a args.args -d '\n' emacs -Q --script code.code < stdin.stdin diff --git a/lxc/executors/go b/lxc/executors/go index 063af21..09c1e68 100755 --- a/lxc/executors/go +++ b/lxc/executors/go @@ -1,6 +1,6 @@ +#!/bin/bash + cd /tmp/$2 cp code.code interim.go -file="interim.go" -go build $file -file=${file%%.*} -runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 ./$file" +go build interim.go +timeout -s KILL 3 xargs -a args.args -d '\n' ./interim < stdin.stdin diff --git a/lxc/executors/haskell b/lxc/executors/haskell index 7593b60..df517d9 100755 --- a/lxc/executors/haskell +++ b/lxc/executors/haskell @@ -1,7 +1,6 @@ -cd /tmp/$2 -mv code.code code.hs +#!/bin/bash -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - ghc -dynamic -o binary code.hs > /dev/null 2>&1 ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +cd /tmp/$2 +cp code.code code.hs +ghc -dynamic -o binary code.hs > /dev/null 2>&1 +timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin diff --git a/lxc/executors/java b/lxc/executors/java index 1ad5cad..5539c4a 100755 --- a/lxc/executors/java +++ b/lxc/executors/java @@ -1,9 +1,7 @@ -cd /tmp/$2 -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 +#!/bin/bash -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - javac $name.java ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 java $name" +cd /tmp/$2 +name=$(grep -Po "(?<=\n|\A)\s*(public\s+)?(class|interface)\s+\K([^\/\\\\\n\s{]+)" code.code) +cp code.code $name.java +javac $name.java +timeout -s KILL 3 xargs -a args.args -d '\n' java $name < stdin.stdin diff --git a/lxc/executors/jelly b/lxc/executors/jelly index 4107b6b..945444e 100755 --- a/lxc/executors/jelly +++ b/lxc/executors/jelly @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/julia b/lxc/executors/julia index 7dddb24..e8b4ea5 100755 --- a/lxc/executors/julia +++ b/lxc/executors/julia @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/kotlin b/lxc/executors/kotlin index 5d5070a..c304575 100755 --- a/lxc/executors/kotlin +++ b/lxc/executors/kotlin @@ -1,6 +1,6 @@ +#!/bin/bash + cd /tmp/$2 cp code.code code.kt -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - kotlinc code.kt -include-runtime -d code.jar ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 java -jar code.jar" +kotlinc code.kt -include-runtime -d code.jar +timeout -s KILL 3 xargs -a args.args -d '\n' java -jar code.jar < stdin.stdin diff --git a/lxc/executors/lua b/lxc/executors/lua index b412013..4caa4b6 100755 --- a/lxc/executors/lua +++ b/lxc/executors/lua @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/nasm b/lxc/executors/nasm index 22026af..ffe8fd0 100755 --- a/lxc/executors/nasm +++ b/lxc/executors/nasm @@ -1,5 +1,6 @@ -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - nasm -f elf32 -o binary.o code.code ; \ - ld -m elf_i386 binary.o -o binary ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +#!/bin/bash + +cd /tmp/$2 +nasm -f elf32 -o binary.o code.code +ld -m elf_i386 binary.o -o binary +timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin diff --git a/lxc/executors/nasm64 b/lxc/executors/nasm64 index a6dc197..98c2aab 100755 --- a/lxc/executors/nasm64 +++ b/lxc/executors/nasm64 @@ -1,5 +1,6 @@ -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - nasm -f elf64 -o binary.o code.code ; \ - ld -m elf_x86_64 binary.o -o binary ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +#!/bin/bash + +cd /tmp/$2 +nasm -f elf64 -o binary.o code.code +ld -m elf_x86_64 binary.o -o binary +timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin diff --git a/lxc/executors/node b/lxc/executors/node index 5fd7d26..3262b53 100755 --- a/lxc/executors/node +++ b/lxc/executors/node @@ -1,7 +1,4 @@ -cd /tmp/$2 +#!/bin/bash -if [[ -z $(grep '[^[:space:]]' args.args) ]]; then - runuser runner$1 -c "cd /tmp/$2 ; timeout -s KILL 3 node code.code" -else - runuser runner$1 -c "cd /tmp/$2 ; cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code" -fi +cd /tmp/$2 +timeout -s KILL 3 xargs -a args.args -d '\n' node code.code < stdin.stdin diff --git a/lxc/executors/paradoc b/lxc/executors/paradoc index 33fe880..be58dad 100755 --- a/lxc/executors/paradoc +++ b/lxc/executors/paradoc @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/perl b/lxc/executors/perl index afb8ad5..8b65855 100755 --- a/lxc/executors/perl +++ b/lxc/executors/perl @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/php b/lxc/executors/php index d88a8d2..9f21f7b 100755 --- a/lxc/executors/php +++ b/lxc/executors/php @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/python2 b/lxc/executors/python2 index d172f18..5fa1c3e 100755 --- a/lxc/executors/python2 +++ b/lxc/executors/python2 @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/python3 b/lxc/executors/python3 index 0749d88..6cb93f8 100755 --- a/lxc/executors/python3 +++ b/lxc/executors/python3 @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/ruby b/lxc/executors/ruby index 9de7477..ebb9b06 100755 --- a/lxc/executors/ruby +++ b/lxc/executors/ruby @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/rust b/lxc/executors/rust index c939259..137dffa 100755 --- a/lxc/executors/rust +++ b/lxc/executors/rust @@ -1,5 +1,5 @@ +#!/bin/bash + cd /tmp/$2 rustc -o binary code.code -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 ./binary" +timeout -s KILL 3 xargs -a args.args -d '\n' ./binary < stdin.stdin diff --git a/lxc/executors/swift b/lxc/executors/swift index 4b5d8a1..a8327f7 100755 --- a/lxc/executors/swift +++ b/lxc/executors/swift @@ -1,2 +1,4 @@ +#!/bin/bash + 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 diff --git a/lxc/executors/typescript b/lxc/executors/typescript index 863d8d0..8c1873d 100755 --- a/lxc/executors/typescript +++ b/lxc/executors/typescript @@ -1,7 +1,8 @@ -runuser runner$1 -c "\ - cd /tmp/$2 ; \ - mv code.code interim.ts ; \ - tsc interim.ts ; \ - rm -f interim.ts ; \ - mv interim.js code.code ; \ - cat args.args | xargs -d '\n' timeout -s KILL 3 node code.code" +#!/bin/bash + +cd /tmp/$2 +mv code.code interim.ts +tsc interim.ts +rm -f interim.ts +mv interim.js code.code +timeout -s KILL 3 xargs -a args.args -d '\n' node code.code < stdin.stdin diff --git a/lxc/start b/lxc/start index 6c13c98..fec7aef 100755 --- a/lxc/start +++ b/lxc/start @@ -3,7 +3,7 @@ mkdir -p /var/lib/lxc/piston/rootfs/exec rm -f /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 lxc-start -n piston -d diff --git a/lxc/test_all_lxc b/lxc/test_all_lxc index 873c840..493c4b5 100755 --- a/lxc/test_all_lxc +++ b/lxc/test_all_lxc @@ -2,54 +2,54 @@ cd tests echo 'testing awk' -../execute awk test.awk +../../cli/execute awk test.awk echo 'testing c' -../execute c test.c +../../cli/execute c test.c echo 'testing cpp' -../execute cpp test.cpp +../../cli/execute cpp test.cpp echo 'testing cs' -../execute cs test.cs +../../cli/execute cs test.cs echo 'testing deno' -../execute deno testdeno.ts +../../cli/execute deno testdeno.ts echo 'testing elisp' -../execute elisp test.el +../../cli/execute elisp test.el echo 'testing elixir' -../execute exs test.exs +../../cli/execute exs test.exs echo 'testing go' -../execute go test.go +../../cli/execute go test.go echo 'testing haskell' -../execute haskell test.hs +../../cli/execute haskell test.hs echo 'testing java' -../execute java test.java +../../cli/execute java test.java echo 'testing jelly' -../execute jelly test.jelly +../../cli/execute jelly test.jelly echo 'testing jl' -../execute jl test.jl +../../cli/execute jl test.jl echo 'testing js' -../execute js test.js +../../cli/execute js test.js echo 'testing kotlin' -../execute kotlin test.kt +../../cli/execute kotlin test.kt echo 'testing asm 32 bit' -../execute asm test.nasm +../../cli/execute asm test.nasm echo 'testing asm 64 bit' -../execute asm64 test64.nasm +../../cli/execute asm64 test64.nasm echo 'testing php' -../execute php test.php +../../cli/execute php test.php echo 'testing perl' -../execute perl test.pl +../../cli/execute perl test.pl echo 'testing ruby' -../execute ruby test.rb +../../cli/execute ruby test.rb echo 'testing rust' -../execute rust test.rs +../../cli/execute rust test.rs echo 'testing bash' -../execute bash test.sh +../../cli/execute bash test.sh echo 'testing swift' -../execute swift test.swift +../../cli/execute swift test.swift echo 'testing typescript' -../execute typescript test.ts +../../cli/execute typescript test.ts echo 'testing python2' -../execute python2 test2.py +../../cli/execute python2 test2.py echo 'testing python3' -../execute python3 test3.py +../../cli/execute python3 test3.py echo 'testing paradoc' -../execute python3 test_paradoc.py +../../cli/execute python3 test_paradoc.py diff --git a/readme.md b/readme.md index 0702805..0a7fa19 100644 --- a/readme.md +++ b/readme.md @@ -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 software. +#### CLI Usage +- `cli/execute [language] [file path] [args]`
# Usage @@ -185,14 +187,17 @@ Content-Type: application/json [ { "name": "awk", + "aliases": ["awk"], "version": "1.3.3" }, { "name": "bash", + "aliases": ["bash"], "version": "4.4.20" }, { "name": "c", + "aliases": ["c"], "version": "7.5.0" } ] @@ -236,8 +241,7 @@ HTTP/1.1 400 Bad Request Content-Type: application/json { - "code": "unsupported_language", - "message": "whatever is not supported by Piston" + "message": "Provided language is not supported by Piston" } ``` diff --git a/shared/execute.js b/shared/execute.js new file mode 100644 index 0000000..188a123 --- /dev/null +++ b/shared/execute.js @@ -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, +}; diff --git a/shared/languages.json b/shared/languages.json new file mode 100644 index 0000000..b158de2 --- /dev/null +++ b/shared/languages.json @@ -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" + ] + } +] diff --git a/var/install.txt b/var/install.txt index 9be572e..ae81b05 100644 --- a/var/install.txt +++ b/var/install.txt @@ -96,11 +96,13 @@ make make install # install rust -# final binary: /opt/.cargo/bin/rustc -# get version: /opt/.cargo/bin/rustc --version -curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -echo 'export PATH=$PATH:/opt/.cargo/bin' >> /opt/.profile -source /opt/.profile +# final binary: /usr/local/bin/rustc +# get version: /usr/local/bin/rustc --version +cd /opt && mkdir rust && cd rust +wget https://static.rust-lang.org/dist/rust-1.49.0-x86_64-unknown-linux-gnu.tar.gz +tar -xzf rust-1.49.0-x86_64-unknown-linux-gnu.tar.gz +cd rust-1.49.0-x86_64-unknown-linux-gnu +./install.sh # install swift # final binary: /opt/swift/swift-5.1.5-RELEASE-ubuntu18.04/usr/bin/swift