2018-09-19 19:55:42 +02:00
|
|
|
package main
|
|
|
|
|
2018-09-20 07:49:02 +02:00
|
|
|
import (
|
2021-01-14 20:14:26 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-09-20 07:49:02 +02:00
|
|
|
)
|
|
|
|
|
2020-03-29 21:40:34 +02:00
|
|
|
type Inbound struct {
|
2021-01-14 20:14:26 +01:00
|
|
|
Language string `json:"language"`
|
|
|
|
Source string `json:"source"`
|
|
|
|
Args []string `json:"args"`
|
2018-09-20 07:49:02 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 21:40:34 +02:00
|
|
|
type Problem struct {
|
2021-01-14 20:14:26 +01:00
|
|
|
Code string `json:"code"`
|
|
|
|
Message string `json:"message"`
|
2018-09-20 17:21:46 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 21:40:34 +02:00
|
|
|
type Outbound struct {
|
2021-01-14 20:14:26 +01:00
|
|
|
Ran bool `json:"ran"`
|
|
|
|
Language string `json:"language"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Output string `json:"output"`
|
|
|
|
Stdout string `json:"stdout"`
|
|
|
|
Stderr string `json:"stderr"`
|
2020-03-27 21:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Language struct {
|
2021-01-14 20:14:26 +01:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Version string `json:"version,omitempty"`
|
|
|
|
Aliases []string `json:"aliases,omitempty"`
|
2018-09-20 07:49:02 +02:00
|
|
|
}
|
2018-09-19 19:55:42 +02:00
|
|
|
|
2018-10-08 00:54:29 +02:00
|
|
|
var instance int
|
2020-03-27 22:21:29 +01:00
|
|
|
var languages []Language
|
2018-10-08 00:54:29 +02:00
|
|
|
|
2018-09-19 19:55:42 +02:00
|
|
|
func main() {
|
2021-01-14 20:14:26 +01:00
|
|
|
port := "2000"
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
var err error
|
|
|
|
languages, err = UpdateVersions()
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("could not get version info and therefore couldn't start")
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2018-10-08 00:54:29 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
fmt.Println("starting api on port", port)
|
|
|
|
http.HandleFunc("/execute", Execute)
|
|
|
|
http.HandleFunc("/versions", Versions)
|
|
|
|
http.ListenAndServe(":"+port, nil)
|
2018-09-20 07:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Execute(res http.ResponseWriter, req *http.Request) {
|
2021-01-14 20:14:26 +01:00
|
|
|
res.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
// get json
|
|
|
|
inbound := Inbound{}
|
|
|
|
message := json.NewDecoder(req.Body)
|
|
|
|
message.Decode(&inbound)
|
|
|
|
|
|
|
|
whitelist := []string{
|
|
|
|
"awk",
|
|
|
|
"bash",
|
|
|
|
"brainfuck", "bf",
|
|
|
|
"c",
|
|
|
|
"cpp", "c++",
|
|
|
|
"csharp", "cs", "c#",
|
|
|
|
"deno", "denojs", "denots",
|
|
|
|
"elixir", "exs",
|
|
|
|
"emacs", "elisp", "el",
|
|
|
|
"go",
|
|
|
|
"haskell", "hs",
|
|
|
|
"java",
|
|
|
|
"jelly",
|
|
|
|
"julia", "jl",
|
|
|
|
"kotlin",
|
|
|
|
"lua",
|
|
|
|
"nasm", "asm",
|
|
|
|
"nasm64", "asm64",
|
|
|
|
"node", "javascript", "js",
|
|
|
|
"perl", "pl",
|
|
|
|
"php",
|
|
|
|
"python2",
|
|
|
|
"python3", "python",
|
|
|
|
"paradoc",
|
|
|
|
"ruby",
|
|
|
|
"rust",
|
|
|
|
"swift",
|
|
|
|
"typescript", "ts",
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the supplied language is supported
|
|
|
|
// now calls function and returns
|
|
|
|
for _, lang := range whitelist {
|
|
|
|
if lang == inbound.Language {
|
|
|
|
launch(inbound, res)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now only called when the language is not supported
|
|
|
|
problem := Problem{
|
|
|
|
Code: "unsupported_language",
|
|
|
|
Message: inbound.Language + " is not supported by Piston",
|
|
|
|
}
|
|
|
|
|
|
|
|
pres, _ := json.Marshal(problem)
|
|
|
|
|
|
|
|
res.WriteHeader(http.StatusBadRequest)
|
|
|
|
res.Write(pres)
|
2018-09-20 19:04:34 +02:00
|
|
|
}
|
2018-09-20 07:49:02 +02:00
|
|
|
|
2020-03-29 21:40:34 +02:00
|
|
|
func Versions(res http.ResponseWriter, req *http.Request) {
|
2021-01-14 20:14:26 +01:00
|
|
|
res.Header().Set("Content-Type", "application/json")
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
data, _ := json.Marshal(languages)
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
res.Write(data)
|
2020-03-29 21:40:34 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 06:45:48 +01:00
|
|
|
type StdWriter struct {
|
2021-01-14 20:14:26 +01:00
|
|
|
combined *string
|
|
|
|
separate *string
|
2021-01-14 06:45:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (writer *StdWriter) Write(data []byte) (int, error) {
|
2021-01-14 20:14:26 +01:00
|
|
|
*writer.combined += string(data)
|
|
|
|
*writer.separate += string(data)
|
2021-01-14 06:45:48 +01:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
return len(data), nil
|
2021-01-14 06:45:48 +01:00
|
|
|
}
|
|
|
|
|
2020-03-29 21:40:34 +02:00
|
|
|
func launch(request Inbound, res http.ResponseWriter) {
|
2021-01-14 20:14:26 +01:00
|
|
|
stamp := time.Now().UnixNano()
|
|
|
|
|
|
|
|
// write the code to temp dir
|
|
|
|
srcfile := fmt.Sprintf("/tmp/%d.code", stamp)
|
|
|
|
|
|
|
|
ioutil.WriteFile(srcfile, []byte(request.Source), 0644)
|
|
|
|
|
|
|
|
// set up the arguments to send to the execute command
|
|
|
|
cmd := exec.Command("../lxc/execute", request.Language, srcfile, strings.Join(request.Args, "\n"))
|
|
|
|
|
|
|
|
// capture out/err
|
|
|
|
var stdout, stderr, combined string
|
|
|
|
|
|
|
|
cmd.Stdout = &StdWriter{
|
|
|
|
combined: &combined,
|
|
|
|
separate: &stdout,
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Stderr = &StdWriter{
|
|
|
|
combined: &combined,
|
|
|
|
separate: &stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout = strings.TrimSpace(stdout)
|
|
|
|
stderr = strings.TrimSpace(stderr)
|
|
|
|
combined = strings.TrimSpace(combined)
|
|
|
|
|
|
|
|
if len(stdout) > 65536 {
|
|
|
|
stdout = stdout[:65536]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(stderr) > 65536 {
|
|
|
|
stderr = stdout[:65536]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(combined) > 65536 {
|
|
|
|
combined = combined[:65536]
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
|
|
|
|
// get the executing version of the language
|
|
|
|
execlang := request.Language
|
|
|
|
|
|
|
|
switch execlang {
|
|
|
|
case "bf":
|
|
|
|
execlang = "brainfuck"
|
|
|
|
case "c++":
|
|
|
|
execlang = "cpp"
|
|
|
|
case "cs", "c#":
|
|
|
|
execlang = "csharp"
|
|
|
|
case "denojs", "denots":
|
|
|
|
execlang = "deno"
|
|
|
|
case "el", "elisp":
|
|
|
|
execlang = "emacs"
|
|
|
|
case "exs":
|
|
|
|
execlang = "elixir"
|
|
|
|
case "hs":
|
|
|
|
execlang = "haskell"
|
|
|
|
case "asm":
|
|
|
|
execlang = "nasm"
|
|
|
|
case "asm64":
|
|
|
|
execlang = "nasm64"
|
|
|
|
case "js", "javascript":
|
|
|
|
execlang = "node"
|
|
|
|
case "jl":
|
|
|
|
execlang = "julia"
|
|
|
|
case "python":
|
|
|
|
execlang = "python3"
|
|
|
|
case "ts":
|
|
|
|
execlang = "typescript"
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare response
|
|
|
|
outbound := Outbound{
|
|
|
|
Ran: err == nil,
|
|
|
|
Language: request.Language,
|
|
|
|
Version: "",
|
|
|
|
Output: combined,
|
|
|
|
Stdout: stdout,
|
|
|
|
Stderr: stderr,
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve the language version
|
|
|
|
for _, lang := range languages {
|
|
|
|
if lang.Name == execlang {
|
|
|
|
outbound.Version = lang.Version
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response, _ := json.Marshal(outbound)
|
|
|
|
|
|
|
|
res.Write(response)
|
2020-03-27 21:30:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-29 21:40:34 +02:00
|
|
|
func UpdateVersions() ([]Language, error) {
|
2021-01-14 20:14:26 +01:00
|
|
|
langs, err := GetVersions()
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
return langs, nil
|
2020-03-27 21:30:10 +01:00
|
|
|
}
|
2018-09-20 07:49:02 +02:00
|
|
|
|
2020-03-27 21:30:10 +01:00
|
|
|
// get all the language and their current version
|
2020-03-29 21:40:34 +02:00
|
|
|
func GetVersions() ([]Language, error) {
|
2021-01-14 20:14:26 +01:00
|
|
|
var languages []Language
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
res, err := ExecVersionScript()
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
info := strings.Split(res, "---")
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
for _, v := range info {
|
|
|
|
if len(v) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name, version := GetVersion(v)
|
|
|
|
languages = append(languages, Language{
|
|
|
|
Name: name,
|
|
|
|
Version: version,
|
|
|
|
})
|
|
|
|
}
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
return languages, nil
|
2020-03-27 21:30:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// run the script that retrieves all the language versions
|
2020-03-29 21:40:34 +02:00
|
|
|
func ExecVersionScript() (string, error) {
|
2021-01-14 20:14:26 +01:00
|
|
|
cmd := exec.Command("../lxc/versions")
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stdout
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
err := cmd.Run()
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
return strings.ToLower(stdout.String()), err
|
2020-03-27 21:30:10 +01:00
|
|
|
}
|
2018-09-20 07:49:02 +02:00
|
|
|
|
2020-03-27 21:30:10 +01:00
|
|
|
// return the language and its version
|
|
|
|
// most of the time it is easy to get the name and version
|
|
|
|
// but for some languages helper functions are used
|
2020-03-29 21:40:34 +02:00
|
|
|
func GetVersion(s string) (string, string) {
|
2021-01-14 20:14:26 +01:00
|
|
|
lines := strings.Split(s, "\n")
|
2020-03-29 21:40:34 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
if lines[1] == "java" {
|
|
|
|
return "java", regexp.MustCompile("([0-9]+)").FindString(lines[2])
|
|
|
|
}
|
2020-03-27 22:21:29 +01:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
if lines[1] == "emacs" {
|
|
|
|
return "emacs", regexp.MustCompile("([0-9]+\\.[0-9]+)").FindString(lines[2])
|
|
|
|
}
|
2020-06-09 05:19:06 +02:00
|
|
|
|
2021-01-14 20:14:26 +01:00
|
|
|
return lines[1], regexp.MustCompile("([0-9]+\\.[0-9]+\\.[0-9]+)").FindString(s)
|
2020-03-27 22:21:29 +01:00
|
|
|
}
|