wide/shell/shells.go

153 lines
3.2 KiB
Go
Raw Normal View History

2014-09-12 13:10:58 +04:00
// Shell.
2014-08-18 17:45:43 +04:00
package shell
import (
2014-09-05 10:33:43 +04:00
"html/template"
"math/rand"
2014-08-18 17:45:43 +04:00
"net/http"
"os"
"os/exec"
2014-09-05 10:33:43 +04:00
"runtime"
"strconv"
2014-08-18 17:45:43 +04:00
"strings"
2014-09-12 13:10:58 +04:00
"github.com/b3log/wide/conf"
"github.com/b3log/wide/i18n"
"github.com/b3log/wide/user"
"github.com/golang/glog"
"github.com/gorilla/websocket"
2014-08-18 17:45:43 +04:00
)
var shellWS = map[string]*websocket.Conn{}
2014-09-05 10:33:43 +04:00
func IndexHandler(w http.ResponseWriter, r *http.Request) {
i18n.Load()
model := map[string]interface{}{"Wide": conf.Wide, "i18n": i18n.GetLangs(r), "locale": i18n.GetLocale(r)}
session, _ := user.Session.Get(r, "wide-session")
if session.IsNew {
// TODO: 写死以 admin 作为用户登录
name := conf.Wide.Users[0].Name
session.Values["username"] = name
session.Values["id"] = strconv.Itoa(rand.Int())
// 一天过期
session.Options.MaxAge = 60 * 60 * 24
glog.Infof("Created a session [%s] for user [%s]", session.Values["id"].(string), name)
}
session.Save(r, w)
t, err := template.ParseFiles("view/shell.html")
if nil != err {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
t.Execute(w, model)
}
2014-08-18 17:45:43 +04:00
func WSHandler(w http.ResponseWriter, r *http.Request) {
2014-08-31 13:39:39 +04:00
session, _ := user.Session.Get(r, "wide-session")
2014-09-05 10:33:43 +04:00
username := session.Values["username"].(string)
2014-08-18 17:45:43 +04:00
sid := session.Values["id"].(string)
shellWS[sid], _ = websocket.Upgrade(w, r, nil, 1024, 1024)
ret := map[string]interface{}{"output": "Shell initialized", "cmd": "init-shell"}
shellWS[sid].WriteJSON(&ret)
glog.Infof("Open a new [Shell] with session [%s], %d", sid, len(shellWS))
input := map[string]interface{}{}
for {
if err := shellWS[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") {
2014-09-05 10:33:43 +04:00
output = pipeCommands(username, commands...)
2014-08-18 17:45:43 +04:00
}
ret = map[string]interface{}{"output": output, "cmd": "shell-output"}
if err := shellWS[sid].WriteJSON(&ret); err != nil {
glog.Error("Shell WS ERROR: " + err.Error())
return
}
}
}
2014-09-05 10:33:43 +04:00
func pipeCommands(username string, commands ...*exec.Cmd) string {
2014-08-18 17:45:43 +04:00
for i, command := range commands[:len(commands)-1] {
2014-09-05 10:33:43 +04:00
setCmdEnv(command, username)
2014-08-18 17:45:43 +04:00
2014-09-05 12:34:34 +04:00
stdout, err := command.StdoutPipe()
2014-08-18 17:45:43 +04:00
if nil != err {
return err.Error()
}
command.Start()
2014-09-05 12:34:34 +04:00
commands[i+1].Stdin = stdout
2014-08-18 17:45:43 +04:00
}
last := commands[len(commands)-1]
2014-09-05 10:33:43 +04:00
setCmdEnv(last, username)
2014-08-18 17:45:43 +04:00
2014-09-05 12:34:34 +04:00
out, err := last.CombinedOutput()
2014-09-05 10:33:43 +04:00
2014-08-18 17:45:43 +04:00
if err != nil {
return err.Error()
}
return string(out)
}
2014-09-05 10:33:43 +04:00
func setCmdEnv(cmd *exec.Cmd, username string) {
userWorkspace := conf.Wide.GetUserWorkspace(username)
cmd.Env = append(cmd.Env,
2014-09-05 12:34:34 +04:00
"TERM="+os.Getenv("TERM"),
2014-09-05 10:33:43 +04:00
"GOPATH="+userWorkspace,
"GOOS="+runtime.GOOS,
"GOARCH="+runtime.GOARCH,
"GOROOT="+runtime.GOROOT(),
"PATH="+os.Getenv("PATH"))
2014-08-18 17:45:43 +04:00
2014-09-05 10:33:43 +04:00
cmd.Dir = userWorkspace
2014-08-18 17:45:43 +04:00
}