piston improvements. adds version endpoint, new execution output, awk language support, and installs all dependencies directly when possible and avoids apt unless not possible
This commit is contained in:
parent
f6af99bbee
commit
8fcdec7275
163
api/main.go
163
api/main.go
|
@ -5,29 +5,29 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type inbound struct {
|
type Inbound struct {
|
||||||
Language string `json:"language"`
|
Language string `json:"language"`
|
||||||
Source string `json:"source"`
|
Source string `json:"source"`
|
||||||
Args []string `json:"args"`
|
Args []string `json:"args"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type problem struct {
|
type Problem struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type outbound struct {
|
type Outbound struct {
|
||||||
Ran bool `json:"ran"`
|
Ran bool `json:"ran"`
|
||||||
Output string `json:"output"`
|
Language string `json:"language"`
|
||||||
|
Version string `json:"version"`
|
||||||
|
Output string `json:"output"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Language struct {
|
type Language struct {
|
||||||
|
@ -36,61 +36,65 @@ type Language struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var instance int
|
var instance int
|
||||||
var versionRegex = regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)")
|
|
||||||
var javaRegex = regexp.MustCompile("([0-9]+)")
|
|
||||||
var languages []Language
|
var languages []Language
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
port := "2000"
|
port := "2000"
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
languages, err = updateVersions()
|
languages, err = UpdateVersions()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
fmt.Println("could not get version info and therefore couldn't start")
|
||||||
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("starting api on port", port)
|
fmt.Println("starting api on port", port)
|
||||||
http.HandleFunc("/execute", Execute)
|
http.HandleFunc("/execute", Execute)
|
||||||
http.HandleFunc("/versions", versions)
|
http.HandleFunc("/versions", Versions)
|
||||||
http.ListenAndServe(":"+port, nil)
|
http.ListenAndServe(":" + port, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute(res http.ResponseWriter, req *http.Request) {
|
func Execute(res http.ResponseWriter, req *http.Request) {
|
||||||
res.Header().Set("Content-Type", "application/json")
|
res.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// get json
|
// get json
|
||||||
inbound := inbound{}
|
inbound := Inbound{}
|
||||||
message := json.NewDecoder(req.Body)
|
message := json.NewDecoder(req.Body)
|
||||||
message.Decode(&inbound)
|
message.Decode(&inbound)
|
||||||
|
|
||||||
whitelist := []string{
|
whitelist := []string{
|
||||||
|
"awk",
|
||||||
|
"bash",
|
||||||
"c",
|
"c",
|
||||||
"cpp", "c++",
|
"cpp", "c++",
|
||||||
"c#", "csharp", "cs",
|
"csharp", "cs", "c#",
|
||||||
"go",
|
"go",
|
||||||
"java",
|
"java",
|
||||||
"nasm", "asm",
|
"nasm", "asm",
|
||||||
"javascript", "js", "node",
|
"node", "javascript", "js",
|
||||||
"typescript", "ts",
|
|
||||||
"php",
|
"php",
|
||||||
"python", "python2", "python3",
|
"python2",
|
||||||
|
"python3", "python",
|
||||||
"ruby",
|
"ruby",
|
||||||
"swift",
|
|
||||||
"rust",
|
"rust",
|
||||||
"bash",
|
"swift",
|
||||||
|
"typescript", "ts",
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the supplied language is supported
|
// check if the supplied language is supported
|
||||||
// now calls function and returns
|
// now calls function and returns
|
||||||
for _, lang := range whitelist {
|
for _, lang := range whitelist {
|
||||||
if lang == inbound.Language {
|
if lang == inbound.Language {
|
||||||
|
|
||||||
launch(inbound, res)
|
launch(inbound, res)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// now only called when the language is not supported
|
// now only called when the language is not supported
|
||||||
problem := problem{
|
problem := Problem{
|
||||||
Code: "unsupported_language",
|
Code: "unsupported_language",
|
||||||
Message: inbound.Language + " is not supported by Piston",
|
Message: inbound.Language + " is not supported by Piston",
|
||||||
}
|
}
|
||||||
|
@ -100,7 +104,15 @@ func Execute(res http.ResponseWriter, req *http.Request) {
|
||||||
res.Write(pres)
|
res.Write(pres)
|
||||||
}
|
}
|
||||||
|
|
||||||
func launch(request inbound, res http.ResponseWriter) {
|
func Versions(res http.ResponseWriter, req *http.Request) {
|
||||||
|
res.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
data, _ := json.Marshal(languages)
|
||||||
|
|
||||||
|
res.Write(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func launch(request Inbound, res http.ResponseWriter) {
|
||||||
stamp := time.Now().UnixNano()
|
stamp := time.Now().UnixNano()
|
||||||
|
|
||||||
// write the code to temp dir
|
// write the code to temp dir
|
||||||
|
@ -126,10 +138,47 @@ func launch(request inbound, res http.ResponseWriter) {
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
|
|
||||||
|
// get the executing version of the language
|
||||||
|
execlang := request.Language
|
||||||
|
|
||||||
|
switch execlang {
|
||||||
|
case "c++":
|
||||||
|
execlang = "cpp"
|
||||||
|
break
|
||||||
|
case "cs":
|
||||||
|
case "c#":
|
||||||
|
execlang = "csharp"
|
||||||
|
break
|
||||||
|
case "asm":
|
||||||
|
execlang = "nasm"
|
||||||
|
break
|
||||||
|
case "javascript":
|
||||||
|
case "js":
|
||||||
|
execlang = "node"
|
||||||
|
break
|
||||||
|
case "python":
|
||||||
|
execlang = "python3"
|
||||||
|
break
|
||||||
|
case "ts":
|
||||||
|
execlang = "typescript"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
var version string
|
||||||
|
|
||||||
|
for _, lang := range languages {
|
||||||
|
if lang.Name == execlang {
|
||||||
|
version = lang.Version
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// prepare response
|
// prepare response
|
||||||
outbound := outbound{
|
outbound := Outbound{
|
||||||
Ran: err == nil,
|
Ran: err == nil,
|
||||||
Output: strings.TrimSpace(stdout.String()),
|
Language: request.Language,
|
||||||
|
Version: version,
|
||||||
|
Output: strings.TrimSpace(stdout.String()),
|
||||||
}
|
}
|
||||||
|
|
||||||
response, _ := json.Marshal(outbound)
|
response, _ := json.Marshal(outbound)
|
||||||
|
@ -137,72 +186,64 @@ func launch(request inbound, res http.ResponseWriter) {
|
||||||
res.Write(response)
|
res.Write(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateVersions() ([]Language, error) {
|
func UpdateVersions() ([]Language, error) {
|
||||||
f, err := os.Create("versions.json")
|
langs, err := GetVersions()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
langs, err := getVersions()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
res, err := json.Marshal(langs)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
f.Write(res)
|
|
||||||
return langs, nil
|
return langs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// get all the language and their current version
|
// get all the language and their current version
|
||||||
func getVersions() ([]Language, error) {
|
func GetVersions() ([]Language, error) {
|
||||||
var languages []Language
|
var languages []Language
|
||||||
res, err := execVersionScript()
|
|
||||||
|
res, err := ExecVersionScript()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
languageInfo := strings.Split(res, "---")
|
|
||||||
for _, v := range languageInfo {
|
info := strings.Split(res, "---")
|
||||||
|
|
||||||
|
for _, v := range info {
|
||||||
if len(v) < 2 {
|
if len(v) < 2 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
name, version := getVersion(v)
|
name, version := GetVersion(v)
|
||||||
languages = append(languages, Language{
|
languages = append(languages, Language{
|
||||||
Name: name,
|
Name: name,
|
||||||
Version: version,
|
Version: version,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return languages, nil
|
return languages, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// run the script that retrieves all the language versions
|
// run the script that retrieves all the language versions
|
||||||
func execVersionScript() (string, error) {
|
func ExecVersionScript() (string, error) {
|
||||||
fmt.Println("running script")
|
cmd := exec.Command("../lxc/versions")
|
||||||
output := bytes.Buffer{}
|
|
||||||
cmd := exec.Command("../lcx/versions")
|
var stdout bytes.Buffer
|
||||||
cmd.Stdout = &output
|
cmd.Stdout = &stdout
|
||||||
|
cmd.Stderr = &stdout
|
||||||
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
return strings.ToLower(output.String()), err
|
|
||||||
|
return strings.ToLower(stdout.String()), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the language and its version
|
// return the language and its version
|
||||||
// most of the time it is easy to get the name and version
|
// most of the time it is easy to get the name and version
|
||||||
// but for some languages helper functions are used
|
// but for some languages helper functions are used
|
||||||
|
func GetVersion(s string) (string, string) {
|
||||||
func getVersion(s string) (string, string) {
|
|
||||||
lines := strings.Split(s, "\n")
|
lines := strings.Split(s, "\n")
|
||||||
if lines[1] == "java" {
|
|
||||||
return "java", javaRegex.FindString(lines[2])
|
|
||||||
}
|
|
||||||
return lines[1], versionRegex.FindString(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func versions(w http.ResponseWriter, r *http.Request) {
|
if lines[1] == "java" {
|
||||||
data, err := json.Marshal(languages)
|
return "java", regexp.MustCompile("([0-9]+)").FindString(lines[2])
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
w.Write(data)
|
|
||||||
|
return lines[1], regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)").FindString(s)
|
||||||
}
|
}
|
||||||
|
|
49
lxc/execute
49
lxc/execute
|
@ -42,17 +42,11 @@ exec 200>&-
|
||||||
|
|
||||||
bin=
|
bin=
|
||||||
case "$lang" in
|
case "$lang" in
|
||||||
"python2")
|
"awk")
|
||||||
bin=python2
|
bin=awk
|
||||||
;;
|
;;
|
||||||
"python" | "python3")
|
"bash")
|
||||||
bin=python3
|
bin=bash
|
||||||
;;
|
|
||||||
"ruby")
|
|
||||||
bin=ruby
|
|
||||||
;;
|
|
||||||
"javascript" | "js" | "node")
|
|
||||||
bin=node
|
|
||||||
;;
|
;;
|
||||||
"c")
|
"c")
|
||||||
bin=c
|
bin=c
|
||||||
|
@ -60,29 +54,38 @@ case "$lang" in
|
||||||
"cpp" | "c++")
|
"cpp" | "c++")
|
||||||
bin=cpp
|
bin=cpp
|
||||||
;;
|
;;
|
||||||
"go")
|
"csharp" | "cs" | "c#")
|
||||||
bin=go
|
|
||||||
;;
|
|
||||||
"c#" | "csharp" | "cs")
|
|
||||||
bin=csharp
|
bin=csharp
|
||||||
;;
|
;;
|
||||||
"php")
|
"go")
|
||||||
bin=php
|
bin=go
|
||||||
;;
|
|
||||||
"nasm" | "asm")
|
|
||||||
bin=nasm
|
|
||||||
;;
|
;;
|
||||||
"java")
|
"java")
|
||||||
bin=java
|
bin=java
|
||||||
;;
|
;;
|
||||||
"swift")
|
"nasm" | "asm")
|
||||||
bin=swift
|
bin=nasm
|
||||||
|
;;
|
||||||
|
"node" | "js" | "javascript")
|
||||||
|
bin=node
|
||||||
|
;;
|
||||||
|
"php")
|
||||||
|
bin=php
|
||||||
|
;;
|
||||||
|
"python2")
|
||||||
|
bin=python2
|
||||||
|
;;
|
||||||
|
"python3" | "python")
|
||||||
|
bin=python3
|
||||||
|
;;
|
||||||
|
"ruby")
|
||||||
|
bin=ruby
|
||||||
;;
|
;;
|
||||||
"rust")
|
"rust")
|
||||||
bin=rust
|
bin=rust
|
||||||
;;
|
;;
|
||||||
"bash")
|
"swift")
|
||||||
bin=bash
|
bin=swift
|
||||||
;;
|
;;
|
||||||
"typescript" | "ts")
|
"typescript" | "ts")
|
||||||
bin=typescript
|
bin=typescript
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
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"
|
|
@ -1,5 +1,8 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
echo '---'
|
echo '---'
|
||||||
|
echo 'awk'
|
||||||
|
lxc-attach --clear-env -n piston -- /bin/bash -l -c "awk -W version"
|
||||||
|
echo '---'
|
||||||
echo 'bash'
|
echo 'bash'
|
||||||
lxc-attach --clear-env -n piston -- /bin/bash -l -c "bash --version"
|
lxc-attach --clear-env -n piston -- /bin/bash -l -c "bash --version"
|
||||||
echo '---'
|
echo '---'
|
||||||
|
|
|
@ -185,6 +185,8 @@ A typical response when everything succeeds will be similar to the following:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"ran": true,
|
"ran": true,
|
||||||
|
"language": "js",
|
||||||
|
"version": "12.13.0",
|
||||||
"output": "[ '/usr/bin/node',\n '/tmp/code.code',\n '1',\n '2',\n '3' ]"
|
"output": "[ '/usr/bin/node',\n '/tmp/code.code',\n '1',\n '2',\n '3' ]"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -198,7 +200,7 @@ If an invalid language is supplied, a typical response will look like the follow
|
||||||
|
|
||||||
#### Supported Languages
|
#### Supported Languages
|
||||||
Currently python2, python3, c, c++, go, node, ruby, r, c#, nasm, php, java,
|
Currently python2, python3, c, c++, go, node, ruby, r, c#, nasm, php, java,
|
||||||
swift, brainfuck, rust, bash, and typescript is supported.
|
swift, brainfuck, rust, bash, awk, and typescript is supported.
|
||||||
|
|
||||||
#### Principle of Operation
|
#### Principle of Operation
|
||||||
Piston utilizes LXC as the primary mechanism for sandboxing. There is a small API written in Go which takes
|
Piston utilizes LXC as the primary mechanism for sandboxing. There is a small API written in Go which takes
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
good
|
||||||
|
___code___
|
||||||
|
{ print }
|
|
@ -1,5 +1,7 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
echo 'testing awk'
|
||||||
|
../lxc/execute awk test.awk
|
||||||
echo 'testing c'
|
echo 'testing c'
|
||||||
../lxc/execute c test.c
|
../lxc/execute c test.c
|
||||||
echo 'testing cpp'
|
echo 'testing cpp'
|
||||||
|
|
Loading…
Reference in New Issue