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"`
|
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-03-27 22:07:31 +01:00
|
|
|
"c",
|
|
|
|
"cpp", "c++",
|
2020-03-29 21:40:34 +02:00
|
|
|
"csharp", "cs", "c#",
|
2020-06-04 22:24:04 +02:00
|
|
|
"elixir", "exs",
|
2020-03-27 22:07:31 +01:00
|
|
|
"go",
|
|
|
|
"java",
|
2020-05-02 06:00:46 +02:00
|
|
|
"julia", "jl",
|
2020-03-27 22:07:31 +01:00
|
|
|
"nasm", "asm",
|
2020-03-29 21:40:34 +02:00
|
|
|
"node", "javascript", "js",
|
2020-03-27 22:07:31 +01:00
|
|
|
"php",
|
2020-03-29 21:40:34 +02:00
|
|
|
"python2",
|
|
|
|
"python3", "python",
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2018-09-20 07:49:02 +02:00
|
|
|
|
2020-03-27 22:07:31 +01:00
|
|
|
err := cmd.Run()
|
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-03-31 14:37:23 +02:00
|
|
|
case "c++":
|
|
|
|
execlang = "cpp"
|
|
|
|
case "cs", "c#":
|
|
|
|
execlang = "csharp"
|
|
|
|
case "asm":
|
|
|
|
execlang = "nasm"
|
|
|
|
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: "",
|
2020-03-29 21:40:34 +02:00
|
|
|
Output: strings.TrimSpace(stdout.String()),
|
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-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
|
|
|
}
|