Wide Session

This commit is contained in:
Liang Ding 2014-09-17 10:54:57 +08:00
parent 470d0f9169
commit 5e01f1e394
7 changed files with 48 additions and 33 deletions

View File

@ -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()}

View File

@ -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))]

View File

@ -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{}{}

View File

@ -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');
};

View File

@ -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');
};

View File

@ -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');
};

View File

@ -16,7 +16,8 @@
var config = {
channel: {
shell: '{{.conf.ShellChannel}}'
}
},
wideSessionId: {{.session.Id}}
};
</script>
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script>