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