This commit is contained in:
Liang Ding 2019-05-16 17:27:43 +08:00
parent 1c3c96e371
commit c13b585d3a
No known key found for this signature in database
GPG Key ID: 136F30F901A2231D
2 changed files with 81 additions and 280 deletions

View File

@ -22,24 +22,12 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
"github.com/b3log/wide/session" "github.com/b3log/wide/session"
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
) )
const (
outputBufMax = 1024 // 1024 string(rune)
outputTimeout = 100 // 100ms
outputCountMax = 30 // 30 reads
)
type outputBuf struct {
content string
millisecond int64
}
// RunHandler handles request of executing a binary file. // RunHandler handles request of executing a binary file.
func RunHandler(w http.ResponseWriter, r *http.Request) { func RunHandler(w http.ResponseWriter, r *http.Request) {
result := util.NewResult() result := util.NewResult()
@ -89,24 +77,13 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
logger.Error(err) logger.Error(err)
result.Succ = false result.Succ = false
} }
wsChannel := session.OutputWS[sid] wsChannel := session.OutputWS[sid]
channelRet := map[string]interface{}{} channelRet := map[string]interface{}{}
if !result.Succ { if !result.Succ {
if nil != wsChannel { channelRet["cmd"] = "run-done"
channelRet["cmd"] = "run-done" channelRet["output"] = ""
channelRet["output"] = "" wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
logger.Warn(err)
return
}
wsChannel.Refresh()
}
return return
} }
@ -116,146 +93,60 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
// add the process to user's process set // add the process to user's process set
Processes.Add(wSession, cmd.Process) Processes.Add(wSession, cmd.Process)
// push once for front-end to get the 'run' state and pid
if nil != wsChannel {
channelRet["cmd"] = "run"
channelRet["output"] = ""
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}
go func(runningId int) { go func(runningId int) {
defer util.Recover() defer util.Recover()
defer cmd.Wait()
logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.Username, sid, runningId, filePath) 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 {
logger.Warn(err)
return
}
wsChannel.Refresh()
}
go func() { go func() {
defer util.Recover() defer util.Recover()
buf := outputBuf{} outScanner := bufio.NewScanner(outReader)
count := 0 for outScanner.Scan() {
oneRuneStr := outScanner.Text()
for {
wsChannel := session.OutputWS[sid]
if nil == wsChannel {
break
}
r, _, err := outReader.ReadRune()
count++
if nil != err {
// remove the exited process from user's process set
Processes.Remove(wSession, cmd.Process)
logger.Debugf("User [%s, %s] 's running [id=%d, file=%s] has done [stdout %v], ",
wSession.Username, sid, runningId, filePath, err)
channelRet["cmd"] = "run-done"
channelRet["output"] = buf.content
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
logger.Warn(err)
break
}
wsChannel.Refresh()
break
}
oneRuneStr := string(r)
oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1) oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1)
oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1) oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1)
channelRet["cmd"] = "run"
buf.content += oneRuneStr channelRet["output"] = oneRuneStr + "\n"
now := time.Now().UnixNano() / int64(time.Millisecond) wsChannel := session.OutputWS[sid]
if 0 == buf.millisecond { wsChannel.WriteJSON(&channelRet)
buf.millisecond = now wsChannel.Refresh()
}
flood := count > outputCountMax
if "\n" == oneRuneStr && !flood {
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
}
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
}
} }
}() }()
buf := outputBuf{} errScanner := bufio.NewScanner(errReader)
for { for errScanner.Scan() {
r, _, err := errReader.ReadRune() oneRuneStr := errScanner.Text()
wsChannel := session.OutputWS[sid]
if nil != err || nil == wsChannel {
break
}
oneRuneStr := string(r)
oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1) oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1)
oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1) oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1)
channelRet["cmd"] = "run"
buf.content += oneRuneStr channelRet["output"] = "<span class='stderr'>" + oneRuneStr + "</span>"
now := time.Now().UnixNano() / int64(time.Millisecond) wsChannel := session.OutputWS[sid]
if 0 == buf.millisecond {
buf.millisecond = now
}
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 {
logger.Warn(err)
break
}
wsChannel.Refresh()
}
}
cmd.Wait()
if 124 == cmd.ProcessState.ExitCode() {
channelRet["cmd"] = "run-done"
channelRet["output"] = "<span class='stderr'>run program timeout in 5s</span>\n"
wsChannel.WriteJSON(&channelRet) wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh() wsChannel.Refresh()
} }
cmd.Wait()
// remove the exited process from user's process set
Processes.Remove(wSession, cmd.Process)
channelRet["cmd"] = "run-done"
if 124 == cmd.ProcessState.ExitCode() {
channelRet["output"] = "<span class='stderr'>run program timeout in 5s</span>\n"
} else {
channelRet["output"] = "\n<span class='stderr'>run program complete</span>\n"
}
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}(rand.Int()) }(rand.Int())
} }

View File

@ -21,7 +21,7 @@ import (
"net/http" "net/http"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"time" "strings"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
"github.com/b3log/wide/output" "github.com/b3log/wide/output"
@ -29,17 +29,6 @@ import (
"github.com/b3log/wide/util" "github.com/b3log/wide/util"
) )
const (
outputBufMax = 1024 // 1024 string(rune)
outputTimeout = 100 // 100ms
outputCountMax = 30 // 30 reads
)
type outputBuf struct {
content string
millisecond int64
}
// RunHandler handles request of executing a binary file. // RunHandler handles request of executing a binary file.
func RunHandler(w http.ResponseWriter, r *http.Request) { func RunHandler(w http.ResponseWriter, r *http.Request) {
result := util.NewResult() result := util.NewResult()
@ -116,139 +105,60 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
// add the process to user's process set // add the process to user's process set
output.Processes.Add(wSession, cmd.Process) output.Processes.Add(wSession, cmd.Process)
// push once for front-end to get the 'run' state and pid
if nil != wsChannel {
channelRet["cmd"] = "run"
channelRet["output"] = ""
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}
go func(runningId int) { go func(runningId int) {
defer util.Recover() defer util.Recover()
defer cmd.Wait()
logger.Debugf("User [%s, %s] is running [id=%d, file=%s]", wSession.Username, sid, runningId, filePath) 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 {
logger.Warn(err)
return
}
wsChannel.Refresh()
}
go func() { go func() {
defer util.Recover() defer util.Recover()
buf := outputBuf{} outScanner := bufio.NewScanner(outReader)
count := 0 for outScanner.Scan() {
oneRuneStr := outScanner.Text()
for { oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1)
wsChannel := session.PlaygroundWS[sid] oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1)
if nil == wsChannel { channelRet["cmd"] = "run"
break channelRet["output"] = oneRuneStr + "\n"
} wsChannel := session.OutputWS[sid]
wsChannel.WriteJSON(&channelRet)
r, _, err := outReader.ReadRune() wsChannel.Refresh()
count++
if nil != err {
// remove the exited process from user process set
output.Processes.Remove(wSession, cmd.Process)
logger.Debugf("User [%s, %s] 's running [id=%d, file=%s] has done [stdout %v], ", wSession.Username, sid, runningId, filePath, err)
channelRet["cmd"] = "run-done"
channelRet["output"] = buf.content
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
logger.Warn(err)
break
}
wsChannel.Refresh()
break
}
oneRuneStr := string(r)
buf.content += oneRuneStr
now := time.Now().UnixNano() / int64(time.Millisecond)
if 0 == buf.millisecond {
buf.millisecond = now
}
flood := count > outputCountMax
if "\n" == oneRuneStr && !flood {
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
}
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
}
} }
}() }()
buf := outputBuf{} errScanner := bufio.NewScanner(errReader)
for { for errScanner.Scan() {
r, _, err := errReader.ReadRune() oneRuneStr := errScanner.Text()
oneRuneStr = strings.Replace(oneRuneStr, "<", "&lt;", -1)
wsChannel := session.PlaygroundWS[sid] oneRuneStr = strings.Replace(oneRuneStr, ">", "&gt;", -1)
if nil != err || nil == wsChannel { channelRet["cmd"] = "run"
break channelRet["output"] = "<span class='stderr'>" + oneRuneStr + "</span>"
} wsChannel := session.OutputWS[sid]
oneRuneStr := string(r)
buf.content += oneRuneStr
now := time.Now().UnixNano() / int64(time.Millisecond)
if 0 == buf.millisecond {
buf.millisecond = now
}
if now-outputTimeout >= buf.millisecond || len(buf.content) > outputBufMax || oneRuneStr == "\n" {
channelRet["cmd"] = "run"
channelRet["output"] = buf.content
buf = outputBuf{} // a new buffer
err = wsChannel.WriteJSON(&channelRet)
if nil != err {
logger.Warn(err)
break
}
wsChannel.Refresh()
}
}
cmd.Wait()
if 124 == cmd.ProcessState.ExitCode() {
channelRet["cmd"] = "run-done"
channelRet["output"] = "<span class='stderr'>run program timeout in 5s</span>\n"
wsChannel.WriteJSON(&channelRet) wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh() wsChannel.Refresh()
} }
cmd.Wait()
// remove the exited process from user's process set
output.Processes.Remove(wSession, cmd.Process)
channelRet["cmd"] = "run-done"
if 124 == cmd.ProcessState.ExitCode() {
channelRet["output"] = "<span class='stderr'>run program timeout in 5s</span>\n"
} else {
channelRet["output"] = "\n<span class='stderr'>run program complete</span>\n"
}
wsChannel.WriteJSON(&channelRet)
wsChannel.Refresh()
}(rand.Int()) }(rand.Int())
} }