🔒 #296 限制执行时间

This commit is contained in:
Liang Ding 2019-05-16 12:56:25 +08:00
parent 6bc8f32e85
commit c7e4034407
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
2 changed files with 13 additions and 3 deletions

View File

@ -63,12 +63,11 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
var cmd *exec.Cmd var cmd *exec.Cmd
if conf.Docker { if conf.Docker {
fileName := filepath.Base(filePath) fileName := filepath.Base(filePath)
cmd = exec.Command("docker", "run", "--rm", "-v", filePath+":/"+fileName, "busybox", "timeout", "10", "/"+fileName) cmd = exec.Command("timeout", "5", "docker", "run", "--rm", "-v", filePath+":/"+fileName, "busybox", "/"+fileName)
} else { } else {
cmd = exec.Command(filePath) cmd = exec.Command(filePath)
curDir := filepath.Dir(filePath) curDir := filepath.Dir(filePath)
cmd.Dir = curDir cmd.Dir = curDir
logger.Warnf("Executing user's program [" + filePath + "] without docker sandbox")
} }
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()

View File

@ -20,8 +20,11 @@ import (
"math/rand" "math/rand"
"net/http" "net/http"
"os/exec" "os/exec"
"path/filepath"
"time" "time"
"github.com/b3log/wide/conf"
"github.com/b3log/wide/output" "github.com/b3log/wide/output"
"github.com/b3log/wide/session" "github.com/b3log/wide/session"
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
@ -58,7 +61,15 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
filePath := args["executable"].(string) filePath := args["executable"].(string)
cmd := exec.Command(filePath) var cmd *exec.Cmd
if conf.Docker {
fileName := filepath.Base(filePath)
cmd = exec.Command("timeout", "5", "docker", "run", "--rm", "-v", filePath+":/"+fileName, "busybox", "/"+fileName)
} else {
cmd = exec.Command(filePath)
curDir := filepath.Dir(filePath)
cmd.Dir = curDir
}
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if nil != err { if nil != err {