wide/shell/shells.go

172 lines
3.6 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"
2014-08-18 17:45:43 +04:00
"net/http"
"os"
"os/exec"
2014-09-05 10:33:43 +04:00
"runtime"
2014-08-18 17:45:43 +04:00
"strings"
2014-09-16 11:06:52 +04:00
"time"
2014-09-12 13:10:58 +04:00
"github.com/b3log/wide/conf"
"github.com/b3log/wide/i18n"
2014-09-17 10:35:48 +04:00
"github.com/b3log/wide/session"
2014-09-16 11:06:52 +04:00
"github.com/b3log/wide/util"
2014-09-12 13:10:58 +04:00
"github.com/golang/glog"
"github.com/gorilla/websocket"
2014-08-18 17:45:43 +04:00
)
2014-09-16 11:06:52 +04:00
// Shell 通道.
2014-09-16 13:32:55 +04:00
// <sid, *util.WSChannel>>
2014-09-25 05:51:00 +04:00
var ShellWS = map[string]*util.WSChannel{}
2014-08-18 17:45:43 +04:00
2014-09-16 11:06:52 +04:00
// Shell 首页.
2014-09-05 10:33:43 +04:00
func IndexHandler(w http.ResponseWriter, r *http.Request) {
i18n.Load()
2014-09-17 10:35:48 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-09-05 10:33:43 +04:00
2014-09-17 06:54:57 +04:00
if httpSession.IsNew {
2014-09-21 16:31:36 +04:00
http.Redirect(w, r, "/login", http.StatusForbidden)
2014-09-05 10:33:43 +04:00
2014-09-21 16:31:36 +04:00
return
2014-09-05 10:33:43 +04:00
}
2014-09-17 06:54:57 +04:00
httpSession.Save(r, w)
2014-09-17 10:35:48 +04:00
// 创建一个 Wide 会话
wideSession := session.WideSessions.New(httpSession)
2014-09-24 07:00:33 +04:00
username := httpSession.Values["username"].(string)
2014-09-17 10:35:48 +04:00
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
"session": wideSession}
2014-09-05 10:33:43 +04:00
2014-09-24 07:00:33 +04:00
wideSessions := session.WideSessions.GetByUsername(username)
2014-09-21 16:31:36 +04:00
glog.V(3).Infof("User [%s] has [%d] sessions", username, len(wideSessions))
2014-09-05 10:33:43 +04:00
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-09-16 11:06:52 +04:00
// 建立 Shell 通道.
2014-08-18 17:45:43 +04:00
func WSHandler(w http.ResponseWriter, r *http.Request) {
2014-09-17 10:35:48 +04:00
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
2014-09-17 06:54:57 +04:00
username := httpSession.Values["username"].(string)
sid := r.URL.Query()["sid"][0]
2014-08-18 17:45:43 +04:00
2014-09-16 11:06:52 +04:00
conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
2014-09-25 05:51:00 +04:00
ShellWS[sid] = &wsChan
2014-08-18 17:45:43 +04:00
ret := map[string]interface{}{"output": "Shell initialized", "cmd": "init-shell"}
2014-09-16 11:06:52 +04:00
wsChan.Conn.WriteJSON(&ret)
2014-08-18 17:45:43 +04:00
2014-09-25 05:51:00 +04:00
glog.V(4).Infof("Open a new [Shell] with session [%s], %d", sid, len(ShellWS))
2014-08-18 17:45:43 +04:00
input := map[string]interface{}{}
for {
2014-09-16 11:06:52 +04:00
if err := wsChan.Conn.ReadJSON(&input); err != nil {
2014-08-18 17:45:43 +04:00
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"}
2014-09-16 11:06:52 +04:00
if err := wsChan.Conn.WriteJSON(&ret); err != nil {
2014-08-18 17:45:43 +04:00
glog.Error("Shell WS ERROR: " + err.Error())
return
}
2014-09-16 11:11:14 +04:00
// 更新通道最近使用时间
wsChan.Time = time.Now()
2014-08-18 17:45:43 +04:00
}
}
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-09-21 16:53:59 +04:00
for _, command := range commands[:len(commands)-1] {
command.Wait()
}
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
}