2018-01-01 07:35:13 +03:00
|
|
|
// Copyright (c) 2014-2018, b3log.org & hacpai.com
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +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
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2018-03-12 07:28:33 +03:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2014-11-20 06:30:18 +03:00
|
|
|
//
|
2014-11-12 18:13:14 +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.
|
|
|
|
|
2014-12-07 06:42:34 +03:00
|
|
|
// Package shell include shell related mainipulations.
|
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-12-13 13:47:41 +03:00
|
|
|
"github.com/b3log/wide/log"
|
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/gorilla/websocket"
|
2014-08-18 17:45:43 +04:00
|
|
|
)
|
|
|
|
|
2014-10-29 13:15:18 +03:00
|
|
|
// Shell channel.
|
2014-09-25 09:37:59 +04:00
|
|
|
//
|
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-12-13 13:47:41 +03:00
|
|
|
// Logger.
|
|
|
|
var logger = log.NewLogger(os.Stdout)
|
|
|
|
|
2014-10-29 13:15:18 +03:00
|
|
|
// IndexHandler handles request of Shell index.
|
2014-09-05 10:33:43 +04:00
|
|
|
func IndexHandler(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
|
|
|
if httpSession.IsNew {
|
2016-02-26 08:45:54 +03:00
|
|
|
http.Redirect(w, r, conf.Wide.Context+"/login", http.StatusFound)
|
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-10-13 08:22:50 +04:00
|
|
|
httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
|
2014-12-11 10:59:58 +03:00
|
|
|
if "" != conf.Wide.Context {
|
|
|
|
httpSession.Options.Path = conf.Wide.Context
|
|
|
|
}
|
2014-09-17 06:54:57 +04:00
|
|
|
httpSession.Save(r, w)
|
|
|
|
|
2014-09-24 07:00:33 +04:00
|
|
|
username := httpSession.Values["username"].(string)
|
2014-12-19 10:43:59 +03:00
|
|
|
locale := conf.GetUser(username).Locale
|
2014-09-24 07:00:33 +04:00
|
|
|
|
2014-10-23 17:43:35 +04:00
|
|
|
model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
|
2015-09-27 02:36:34 +03:00
|
|
|
"sid": session.WideSessions.GenId()}
|
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
|
|
|
|
2014-12-14 18:05:54 +03:00
|
|
|
logger.Tracef("User [%s] has [%d] sessions", username, len(wideSessions))
|
2014-09-21 16:31:36 +04:00
|
|
|
|
2014-10-24 07:41:23 +04:00
|
|
|
t, err := template.ParseFiles("views/shell.html")
|
2014-09-05 10:33:43 +04:00
|
|
|
|
|
|
|
if nil != err {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error(err)
|
2014-09-05 10:33:43 +04:00
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Execute(w, model)
|
|
|
|
}
|
|
|
|
|
2014-10-29 13:15:18 +03:00
|
|
|
// WSHandler handles request of creating Shell channel.
|
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-11-26 08:49:27 +03:00
|
|
|
if httpSession.IsNew {
|
|
|
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
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-08-18 17:45:43 +04:00
|
|
|
ret := map[string]interface{}{"output": "Shell initialized", "cmd": "init-shell"}
|
2014-11-20 08:59:08 +03:00
|
|
|
err := wsChan.WriteJSON(&ret)
|
2014-11-20 06:30:18 +03:00
|
|
|
if nil != err {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ShellWS[sid] = &wsChan
|
2014-08-18 17:45:43 +04:00
|
|
|
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Debugf("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-11-20 09:11:54 +03:00
|
|
|
if err := wsChan.ReadJSON(&input); err != nil {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error("Shell WS ERROR: " + err.Error())
|
2014-11-20 09:11:54 +03:00
|
|
|
|
2014-08-18 17:45:43 +04:00
|
|
|
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-11-20 08:59:08 +03:00
|
|
|
if err := wsChan.WriteJSON(&ret); err != nil {
|
2014-12-13 13:47:41 +03:00
|
|
|
logger.Error("Shell WS ERROR: " + err.Error())
|
2014-08-18 17:45:43 +04:00
|
|
|
return
|
|
|
|
}
|
2014-09-16 11:11:14 +04:00
|
|
|
|
2014-10-29 13:15:18 +03:00
|
|
|
wsChan.Refresh()
|
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-10-29 13:15:18 +03:00
|
|
|
// release resources
|
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) {
|
2014-12-19 10:43:59 +03:00
|
|
|
userWorkspace := conf.GetUserWorkspace(username)
|
2014-09-05 10:33:43 +04:00
|
|
|
|
|
|
|
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
|
|
|
}
|