2019-01-02 16:43:11 +03:00
|
|
|
// Copyright (c) 2014-2019, b3log.org & hacpai.com
|
2015-01-13 12:28:30 +03:00
|
|
|
//
|
|
|
|
// 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
|
2015-01-13 12:28:30 +03:00
|
|
|
//
|
|
|
|
// 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"
|
2015-01-14 06:09:10 +03:00
|
|
|
"time"
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
"github.com/b3log/wide/session"
|
|
|
|
"github.com/b3log/wide/util"
|
|
|
|
)
|
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
const (
|
2016-06-12 12:58:53 +03:00
|
|
|
outputBufMax = 1024 // 1024 string(rune)
|
|
|
|
outputTimeout = 100 // 100ms
|
2016-06-12 13:17:36 +03:00
|
|
|
outputCountMax = 30 // 30 reads
|
2015-01-14 06:09:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type outputBuf struct {
|
|
|
|
content string
|
|
|
|
millisecond int64
|
|
|
|
}
|
|
|
|
|
2015-01-13 12:28:30 +03:00
|
|
|
// 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)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
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
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sid := args["sid"].(string)
|
|
|
|
wSession := session.WideSessions.Get(sid)
|
|
|
|
if nil == wSession {
|
2015-11-24 11:44:34 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
filePath := args["executable"].(string)
|
|
|
|
curDir := filepath.Dir(filePath)
|
|
|
|
|
|
|
|
cmd := exec.Command(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
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
stderr, err := cmd.StderrPipe()
|
|
|
|
if nil != err {
|
|
|
|
logger.Error(err)
|
2015-11-24 11:44:34 +03:00
|
|
|
result.Succ = false
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
|
|
|
|
channelRet := map[string]interface{}{}
|
|
|
|
|
2015-11-24 11:44:34 +03:00
|
|
|
if !result.Succ {
|
2015-01-13 12:28:30 +03:00
|
|
|
if nil != wsChannel {
|
|
|
|
channelRet["cmd"] = "run-done"
|
|
|
|
channelRet["output"] = ""
|
|
|
|
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-01-13 12:28:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
go func(runningId int) {
|
|
|
|
defer util.Recover()
|
|
|
|
defer cmd.Wait()
|
|
|
|
|
|
|
|
logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.Username, sid, runningId, filePath)
|
|
|
|
|
|
|
|
// push once for front-end to get the 'run' state and pid
|
|
|
|
if nil != wsChannel {
|
|
|
|
channelRet["cmd"] = "run"
|
|
|
|
channelRet["output"] = ""
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-01-13 12:28:30 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2015-03-16 06:24:55 +03:00
|
|
|
defer util.Recover()
|
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
buf := outputBuf{}
|
2016-06-12 12:58:53 +03:00
|
|
|
count := 0
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
for {
|
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
if nil == wsChannel {
|
2015-01-13 12:28:30 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
r, _, err := outReader.ReadRune()
|
2016-06-12 12:58:53 +03:00
|
|
|
count++
|
2015-01-14 06:09:10 +03:00
|
|
|
|
2015-01-13 12:28:30 +03:00
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
// remove the exited process from user's process set
|
2015-02-13 04:59:51 +03:00
|
|
|
Processes.Remove(wSession, cmd.Process)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2015-09-26 10:40:05 +03:00
|
|
|
logger.Debugf("User [%s, %s] 's running [id=%d, file=%s] has done [stdout %v], ",
|
|
|
|
wSession.Username, sid, runningId, filePath, err)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
channelRet["cmd"] = "run-done"
|
|
|
|
channelRet["output"] = buf.content
|
|
|
|
err := wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-01-14 06:09:10 +03:00
|
|
|
break
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
wsChannel.Refresh()
|
|
|
|
|
2015-01-13 12:28:30 +03:00
|
|
|
break
|
2015-01-14 06:09:10 +03:00
|
|
|
}
|
|
|
|
|
2015-03-20 11:37:03 +03:00
|
|
|
oneRuneStr := string(r)
|
|
|
|
oneRuneStr = strings.Replace(oneRuneStr, "<", "<", -1)
|
|
|
|
oneRuneStr = strings.Replace(oneRuneStr, ">", ">", -1)
|
|
|
|
|
|
|
|
buf.content += oneRuneStr
|
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
now := time.Now().UnixNano() / int64(time.Millisecond)
|
|
|
|
|
|
|
|
if 0 == buf.millisecond {
|
|
|
|
buf.millisecond = now
|
|
|
|
}
|
|
|
|
|
2016-06-12 12:58:53 +03:00
|
|
|
flood := count > outputCountMax
|
|
|
|
|
|
|
|
if "\n" == oneRuneStr && !flood {
|
2015-01-14 06:09:10 +03:00
|
|
|
channelRet["cmd"] = "run"
|
|
|
|
channelRet["output"] = buf.content
|
|
|
|
|
|
|
|
buf = outputBuf{} // a new buffer
|
2016-06-12 12:58:53 +03:00
|
|
|
count = 0 // clear count
|
2015-01-14 06:09:10 +03:00
|
|
|
|
|
|
|
err = wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-01-14 06:09:10 +03:00
|
|
|
break
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
2015-01-14 06:09:10 +03:00
|
|
|
|
|
|
|
wsChannel.Refresh()
|
2016-06-12 12:58:53 +03:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if now-outputTimeout >= buf.millisecond || len(buf.content) > outputBufMax {
|
|
|
|
channelRet["cmd"] = "run"
|
|
|
|
channelRet["output"] = buf.content
|
|
|
|
|
|
|
|
buf = outputBuf{} // a new buffer
|
|
|
|
count = 0 // clear count
|
|
|
|
|
|
|
|
err = wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
|
|
|
logger.Warn(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
|
|
|
|
continue
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-01-14 09:19:31 +03:00
|
|
|
buf := outputBuf{}
|
2015-01-13 12:28:30 +03:00
|
|
|
for {
|
|
|
|
r, _, err := errReader.ReadRune()
|
|
|
|
|
2015-01-14 06:09:10 +03:00
|
|
|
wsChannel := session.OutputWS[sid]
|
|
|
|
if nil != err || nil == wsChannel {
|
2015-01-13 12:28:30 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-01-14 09:19:31 +03:00
|
|
|
oneRuneStr := string(r)
|
|
|
|
oneRuneStr = strings.Replace(oneRuneStr, "<", "<", -1)
|
|
|
|
oneRuneStr = strings.Replace(oneRuneStr, ">", ">", -1)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
2015-01-14 09:19:31 +03:00
|
|
|
buf.content += oneRuneStr
|
|
|
|
|
|
|
|
now := time.Now().UnixNano() / int64(time.Millisecond)
|
|
|
|
|
|
|
|
if 0 == buf.millisecond {
|
|
|
|
buf.millisecond = now
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|
|
|
|
|
2015-01-14 09:19:31 +03:00
|
|
|
if now-outputTimeout >= buf.millisecond || len(buf.content) > outputBufMax || oneRuneStr == "\n" {
|
|
|
|
channelRet["cmd"] = "run"
|
|
|
|
channelRet["output"] = "<span class='stderr'>" + buf.content + "</span>"
|
|
|
|
|
|
|
|
buf = outputBuf{} // a new buffer
|
|
|
|
|
|
|
|
err = wsChannel.WriteJSON(&channelRet)
|
|
|
|
if nil != err {
|
2015-03-20 11:49:58 +03:00
|
|
|
logger.Warn(err)
|
2015-01-14 09:19:31 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
wsChannel.Refresh()
|
|
|
|
}
|
2015-01-13 12:28:30 +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)
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
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
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
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
|
2015-01-13 12:28:30 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-02-13 04:59:51 +03:00
|
|
|
Processes.Kill(wSession, pid)
|
2015-01-13 12:28:30 +03:00
|
|
|
}
|