From 5e01f1e3946873a162e36bf05adfa6dd2cbe195d Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Wed, 17 Sep 2014 10:54:57 +0800 Subject: [PATCH] Wide Session --- notification/notifications.go | 5 ++--- output/outputs.go | 34 ++++++++++++++++++++-------------- shell/shells.go | 33 +++++++++++++++++++++------------ static/js/notification.js | 2 +- static/js/shell.js | 2 +- static/js/wide.js | 2 +- view/shell.html | 3 ++- 7 files changed, 48 insertions(+), 33 deletions(-) diff --git a/notification/notifications.go b/notification/notifications.go index 75a0ad1..f3d2f9c 100644 --- a/notification/notifications.go +++ b/notification/notifications.go @@ -9,7 +9,6 @@ import ( "strconv" "github.com/b3log/wide/event" "github.com/b3log/wide/i18n" - "github.com/b3log/wide/user" "github.com/b3log/wide/util" "github.com/golang/glog" "github.com/gorilla/websocket" @@ -67,8 +66,8 @@ func event2Notification(e *event.Event) { // 建立通知通道. func WSHandler(w http.ResponseWriter, r *http.Request) { - session, _ := user.HTTPSession.Get(r, "wide-session") - sid := session.Values["id"].(string) + // TODO: 会话校验 + sid := r.URL.Query()["sid"][0] conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024) wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()} diff --git a/output/outputs.go b/output/outputs.go index ee62adb..9561d60 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -26,8 +26,8 @@ var outputWS = map[string]*util.WSChannel{} // 建立输出通道. func WSHandler(w http.ResponseWriter, r *http.Request) { - session, _ := user.HTTPSession.Get(r, "wide-session") - sid := session.Values["id"].(string) + // TODO: 会话校验 + sid := r.URL.Query()["sid"][0] conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024) wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()} @@ -45,9 +45,6 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} defer util.RetJSON(w, r, data) - session, _ := user.HTTPSession.Get(r, "wide-session") - sid := session.Values["id"].(string) - decoder := json.NewDecoder(r.Body) var args map[string]interface{} @@ -59,6 +56,9 @@ func RunHandler(w http.ResponseWriter, r *http.Request) { return } + // TODO: 会话校验 + sid := args["sid"].(string) + filePath := args["executable"].(string) curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] @@ -150,9 +150,8 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} defer util.RetJSON(w, r, data) - session, _ := user.HTTPSession.Get(r, "wide-session") - sid := session.Values["id"].(string) - username := session.Values["username"].(string) + httpSession, _ := user.HTTPSession.Get(r, "wide-session") + username := httpSession.Values["username"].(string) decoder := json.NewDecoder(r.Body) @@ -165,6 +164,9 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) { return } + // TODO: 会话校验 + sid := args["sid"].(string) + filePath := args["file"].(string) curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] @@ -314,9 +316,8 @@ func GoInstallHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} defer util.RetJSON(w, r, data) - session, _ := user.HTTPSession.Get(r, "wide-session") - sid := session.Values["id"].(string) - username := session.Values["username"].(string) + httpSession, _ := user.HTTPSession.Get(r, "wide-session") + username := httpSession.Values["username"].(string) decoder := json.NewDecoder(r.Body) @@ -329,6 +330,9 @@ func GoInstallHandler(w http.ResponseWriter, r *http.Request) { return } + // TODO: 会话校验 + sid := args["sid"].(string) + filePath := args["file"].(string) curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] @@ -439,9 +443,8 @@ func GoGetHandler(w http.ResponseWriter, r *http.Request) { data := map[string]interface{}{"succ": true} defer util.RetJSON(w, r, data) - session, _ := user.HTTPSession.Get(r, "wide-session") - sid := session.Values["id"].(string) - username := session.Values["username"].(string) + httpSession, _ := user.HTTPSession.Get(r, "wide-session") + username := httpSession.Values["username"].(string) decoder := json.NewDecoder(r.Body) @@ -454,6 +457,9 @@ func GoGetHandler(w http.ResponseWriter, r *http.Request) { return } + // TODO: 会话校验 + sid := args["sid"].(string) + filePath := args["file"].(string) curDir := filePath[:strings.LastIndex(filePath, string(os.PathSeparator))] diff --git a/shell/shells.go b/shell/shells.go index 4024775..8a3b6fb 100644 --- a/shell/shells.go +++ b/shell/shells.go @@ -26,25 +26,32 @@ var shellWS = map[string]*util.WSChannel{} // Shell 首页. func IndexHandler(w http.ResponseWriter, r *http.Request) { + // 创建一个 Wide 会话 + wideSession := user.WideSessions.New() + i18n.Load() - model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r)} + model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r), + "session": wideSession} - session, _ := user.HTTPSession.Get(r, "wide-session") + httpSession, _ := user.HTTPSession.Get(r, "wide-session") - if session.IsNew { + if httpSession.IsNew { // TODO: 写死以 admin 作为用户登录 name := conf.Wide.Users[0].Name - session.Values["username"] = name - session.Values["id"] = strconv.Itoa(rand.Int()) + httpSession.Values["username"] = name + httpSession.Values["id"] = strconv.Itoa(rand.Int()) // 一天过期 - session.Options.MaxAge = 60 * 60 * 24 + httpSession.Options.MaxAge = 60 * 60 * 24 - glog.Infof("Created a session [%s] for user [%s]", session.Values["id"].(string), name) + glog.Infof("Created a HTTP session [%s] for user [%s]", session.Values["id"].(string), name) } - session.Save(r, w) + httpSession.Save(r, w) + + // Wide 会话关联 HTTP 会话 + wideSession.HTTPSessionId = httpSession.Values["id"].(string) t, err := template.ParseFiles("view/shell.html") @@ -60,9 +67,11 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { // 建立 Shell 通道. func WSHandler(w http.ResponseWriter, r *http.Request) { - session, _ := user.HTTPSession.Get(r, "wide-session") - username := session.Values["username"].(string) - sid := session.Values["id"].(string) + httpSession, _ := user.HTTPSession.Get(r, "wide-session") + username := httpSession.Values["username"].(string) + + // TODO: 会话校验 + sid := r.URL.Query()["sid"][0] conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024) wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()} @@ -72,7 +81,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) { ret := map[string]interface{}{"output": "Shell initialized", "cmd": "init-shell"} wsChan.Conn.WriteJSON(&ret) - glog.Infof("Open a new [Shell] with session [%s], %d", sid, len(shellWS)) + glog.V(4).Infof("Open a new [Shell] with session [%s], %d", sid, len(shellWS)) input := map[string]interface{}{} diff --git a/static/js/notification.js b/static/js/notification.js index f59d21a..2bb6f80 100644 --- a/static/js/notification.js +++ b/static/js/notification.js @@ -1,4 +1,4 @@ -var notificationWS = new WebSocket(config.channel.shell + '/notification/ws'); +var notificationWS = new WebSocket(config.channel.shell + '/notification/ws?sid=' + config.wideSessionId); notificationWS.onopen = function() { console.log('[notification onopen] connected'); }; diff --git a/static/js/shell.js b/static/js/shell.js index 3f4bdda..173e362 100644 --- a/static/js/shell.js +++ b/static/js/shell.js @@ -1,4 +1,4 @@ -var shellWS = new WebSocket(config.channel.shell + '/shell/ws'); +var shellWS = new WebSocket(config.channel.shell + '/shell/ws?sid=' + config.wideSessionId); shellWS.onopen = function() { console.log('[shell onopen] connected'); }; diff --git a/static/js/wide.js b/static/js/wide.js index 7662874..e1643e6 100644 --- a/static/js/wide.js +++ b/static/js/wide.js @@ -1,4 +1,4 @@ -var outputWS = new WebSocket(config.channel.output + '/output/ws'); +var outputWS = new WebSocket(config.channel.output + '/output/ws?sid=' + config.wideSessionId); outputWS.onopen = function() { console.log('[output onopen] connected'); }; diff --git a/view/shell.html b/view/shell.html index be1a015..19d9a29 100644 --- a/view/shell.html +++ b/view/shell.html @@ -16,7 +16,8 @@ var config = { channel: { shell: '{{.conf.ShellChannel}}' - } + }, + wideSessionId: {{.session.Id}} };