wide/output/run.go

194 lines
4.5 KiB
Go
Raw Normal View History

2019-05-17 06:28:50 +03:00
// Copyright (c) 2014-present, b3log.org
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
2018-03-12 07:28:33 +03:00
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package output
import (
"bufio"
"encoding/json"
"math/rand"
"net/http"
"os/exec"
"path/filepath"
"strings"
2019-05-16 05:53:22 +03:00
"github.com/b3log/wide/conf"
"github.com/b3log/wide/session"
"github.com/b3log/wide/util"
)
// RunHandler handles request of executing a binary file.
func RunHandler(w http.ResponseWriter, r *http.Request) {
2015-11-24 11:44:34 +03:00
result := util.NewResult()
defer util.RetResult(w, r, result)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
2015-11-24 11:44:34 +03:00
result.Succ = false
}
sid := args["sid"].(string)
wSession := session.WideSessions.Get(sid)
if nil == wSession {
2015-11-24 11:44:34 +03:00
result.Succ = false
}
filePath := args["executable"].(string)
2019-05-16 05:53:22 +03:00
var cmd *exec.Cmd
if conf.Docker {
fileName := filepath.Base(filePath)
2019-05-16 13:42:47 +03:00
cmd = exec.Command("timeout", "5", "docker", "run", "--rm", "-v", filePath+":/"+fileName, conf.DockerImageGo, "/"+fileName)
2019-05-16 05:53:22 +03:00
} else {
cmd = exec.Command(filePath)
curDir := filepath.Dir(filePath)
cmd.Dir = curDir
}
stdout, err := cmd.StdoutPipe()
if nil != err {
logger.Error(err)
2015-11-24 11:44:34 +03:00
result.Succ = false
}
stderr, err := cmd.StderrPipe()
if nil != err {
logger.Error(err)
2015-11-24 11:44:34 +03:00
result.Succ = false
}
outReader := bufio.NewReader(stdout)
errReader := bufio.NewReader(stderr)
if err := cmd.Start(); nil != err {
logger.Error(err)
2015-11-24 11:44:34 +03:00
result.Succ = false
}
wsChannel := session.OutputWS[sid]
channelRet := map[string]interface{}{}
2015-11-24 11:44:34 +03:00
if !result.Succ {
2019-05-16 12:27:43 +03:00
channelRet["cmd"] = "run-done"
channelRet["output"] = ""
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
return
}
channelRet["pid"] = cmd.Process.Pid
// add the process to user's process set
2015-02-13 04:59:51 +03:00
Processes.Add(wSession, cmd.Process)
2019-05-16 12:27:43 +03:00
// push once for front-end to get the 'run' state and pid
if nil != wsChannel {
channelRet["cmd"] = "run"
channelRet["output"] = ""
2019-05-16 12:50:58 +03:00
if nil != wsChannel {
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}
2019-05-16 12:27:43 +03:00
}
go func(runningId int) {
defer util.Recover()
2019-05-16 18:17:25 +03:00
logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.UserId, sid, runningId, filePath)
go func() {
2015-03-16 06:24:55 +03:00
defer util.Recover()
2019-05-16 13:10:44 +03:00
for {
r, _, err := outReader.ReadRune()
if nil != err {
break
}
oneRuneStr := string(r)
2015-03-20 11:37:03 +03:00
oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1)
oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1)
2019-05-16 12:27:43 +03:00
channelRet["cmd"] = "run"
2019-05-16 13:10:44 +03:00
channelRet["output"] = oneRuneStr
2019-05-16 12:27:43 +03:00
wsChannel := session.OutputWS[sid]
2019-05-16 12:50:58 +03:00
if nil != wsChannel {
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}
}
}()
2019-05-16 13:10:44 +03:00
for {
r, _, err := errReader.ReadRune()
if nil != err {
break
}
oneRuneStr := string(r)
2015-01-14 09:19:31 +03:00
oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1)
oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1)
2019-05-16 12:27:43 +03:00
channelRet["cmd"] = "run"
channelRet["output"] = "<span class='stderr'>" + oneRuneStr + "</span>"
wsChannel := session.OutputWS[sid]
2019-05-16 12:50:58 +03:00
if nil != wsChannel {
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}
}
2019-05-16 08:07:28 +03:00
2019-05-16 08:18:09 +03:00
cmd.Wait()
2019-05-16 12:27:43 +03:00
// remove the exited process from user's process set
Processes.Remove(wSession, cmd.Process)
channelRet["cmd"] = "run-done"
2019-05-16 08:46:53 +03:00
if 124 == cmd.ProcessState.ExitCode() {
2019-05-16 08:51:18 +03:00
channelRet["output"] = "<span class='stderr'>run program timeout in 5s</span>\n"
2019-05-16 12:27:43 +03:00
} else {
channelRet["output"] = "\n<span class='stderr'>run program complete</span>\n"
2019-05-16 08:46:53 +03:00
}
2019-05-16 12:50:58 +03:00
if nil != wsChannel {
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}
2019-05-16 08:12:04 +03:00
}(rand.Int())
}
// StopHandler handles request of stoping a running process.
func StopHandler(w http.ResponseWriter, r *http.Request) {
2015-11-24 11:44:34 +03:00
result := util.NewResult()
defer util.RetResult(w, r, result)
var args map[string]interface{}
if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
logger.Error(err)
2015-11-24 11:44:34 +03:00
result.Succ = false
return
}
sid := args["sid"].(string)
pid := int(args["pid"].(float64))
wSession := session.WideSessions.Get(sid)
if nil == wSession {
2015-11-24 11:44:34 +03:00
result.Succ = false
return
}
2015-02-13 04:59:51 +03:00
Processes.Kill(wSession, pid)
}