125 lines
2.6 KiB
Go
125 lines
2.6 KiB
Go
// 通知.
|
|
package notification
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/b3log/wide/conf"
|
|
"github.com/b3log/wide/user"
|
|
"github.com/golang/glog"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
// 通知结构.
|
|
type Notification struct {
|
|
Event int
|
|
Type string
|
|
Severity string // ERROR/WARN/INFO
|
|
Message string
|
|
}
|
|
|
|
// 通知通道.
|
|
var notificationWS = map[string]*websocket.Conn{}
|
|
|
|
func WSHandler(w http.ResponseWriter, r *http.Request) {
|
|
session, _ := user.Session.Get(r, "wide-session")
|
|
username := session.Values["username"].(string)
|
|
sid := session.Values["id"].(string)
|
|
|
|
notificationWS[sid], _ = websocket.Upgrade(w, r, nil, 1024, 1024)
|
|
|
|
ret := map[string]interface{}{"output": "Notification initialized", "cmd": "init-notification"}
|
|
notificationWS[sid].WriteJSON(&ret)
|
|
|
|
glog.Infof("Open a new [Notification] with session [%s], %d", sid, len(notificationWS))
|
|
|
|
input := map[string]interface{}{}
|
|
|
|
for {
|
|
if err := notificationWS[sid].ReadJSON(&input); err != nil {
|
|
if err.Error() == "EOF" {
|
|
return
|
|
}
|
|
|
|
if err.Error() == "unexpected EOF" {
|
|
return
|
|
}
|
|
|
|
glog.Error("Shell WS ERROR: " + err.Error())
|
|
return
|
|
}
|
|
|
|
inputCmd := input["cmd"].(string)
|
|
|
|
cmds := strings.Split(inputCmd, "|")
|
|
commands := []*exec.Cmd{}
|
|
for _, cmdWithArgs := range cmds {
|
|
cmdWithArgs = strings.TrimSpace(cmdWithArgs)
|
|
cmdWithArgs := strings.Split(cmdWithArgs, " ")
|
|
args := []string{}
|
|
if len(cmdWithArgs) > 1 {
|
|
args = cmdWithArgs[1:]
|
|
}
|
|
|
|
cmd := exec.Command(cmdWithArgs[0], args...)
|
|
commands = append(commands, cmd)
|
|
}
|
|
|
|
output := ""
|
|
if !strings.Contains(inputCmd, "clear") {
|
|
output = pipeCommands(username, commands...)
|
|
}
|
|
|
|
ret = map[string]interface{}{"output": output, "cmd": "shell-output"}
|
|
|
|
if err := notificationWS[sid].WriteJSON(&ret); err != nil {
|
|
glog.Error("Shell WS ERROR: " + err.Error())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func pipeCommands(username string, commands ...*exec.Cmd) string {
|
|
for i, command := range commands[:len(commands)-1] {
|
|
setCmdEnv(command, username)
|
|
|
|
stdout, err := command.StdoutPipe()
|
|
if nil != err {
|
|
return err.Error()
|
|
}
|
|
|
|
command.Start()
|
|
|
|
commands[i+1].Stdin = stdout
|
|
}
|
|
|
|
last := commands[len(commands)-1]
|
|
setCmdEnv(last, username)
|
|
|
|
out, err := last.CombinedOutput()
|
|
|
|
if err != nil {
|
|
return err.Error()
|
|
}
|
|
|
|
return string(out)
|
|
}
|
|
|
|
func setCmdEnv(cmd *exec.Cmd, username string) {
|
|
userWorkspace := conf.Wide.GetUserWorkspace(username)
|
|
|
|
cmd.Env = append(cmd.Env,
|
|
"TERM="+os.Getenv("TERM"),
|
|
"GOPATH="+userWorkspace,
|
|
"GOOS="+runtime.GOOS,
|
|
"GOARCH="+runtime.GOARCH,
|
|
"GOROOT="+runtime.GOROOT(),
|
|
"PATH="+os.Getenv("PATH"))
|
|
|
|
cmd.Dir = userWorkspace
|
|
}
|