From ce7d6985509f9038cb2d4b2404c90befe663ebda Mon Sep 17 00:00:00 2001 From: shylinux Date: Sun, 22 Sep 2019 22:33:04 +0800 Subject: [PATCH] opt cookie.expire --- src/contexts/cli/version.go | 2 +- src/contexts/web/web.go | 16 ++++++++++++---- src/examples/chat/chat.go | 9 ++++++++- usr/librarys/context.js | 2 +- usr/librarys/example.js | 20 +++++++++++++++----- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/contexts/cli/version.go b/src/contexts/cli/version.go index 2f1693a7..cc610ad7 100644 --- a/src/contexts/cli/version.go +++ b/src/contexts/cli/version.go @@ -4,5 +4,5 @@ var version = struct { host string self int }{ - "2019-09-22 20:28:29", "com", 635, + "2019-09-22 22:04:56", "com", 640, } diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index e9a343cb..cf00580a 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -3,6 +3,7 @@ package web import ( "github.com/gorilla/websocket" "github.com/skip2/go-qrcode" + "time" "contexts/ctx" "toolkit" @@ -39,6 +40,13 @@ type WEB struct { *ctx.Context } +func Cookie(msg *ctx.Message, w http.ResponseWriter, r *http.Request) { + expire := time.Now().Add(kit.Duration(msg.Conf("login", "expire"))) + msg.Log("info", "expire %v", expire) + http.SetCookie(w, &http.Cookie{Name: "sessid", + Value: msg.Cmdx("aaa.user", "session", "select"), Path: "/", Expires: expire}) + return +} func proxy(m *ctx.Message, url string) string { if strings.HasPrefix(url, "//") { return "proxy/https:" + url @@ -99,8 +107,7 @@ func (web *WEB) Login(msg *ctx.Message, w http.ResponseWriter, r *http.Request) if msg.Options("username") && msg.Options("password") { if msg.Cmds("aaa.auth", "username", msg.Option("username"), "password", msg.Option("password")) { msg.Log("info", "login: %s", msg.Option("username")) - http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Cmdx("aaa.user", "session", "select"), Path: "/"}) - if msg.Options("relay") { + if Cookie(msg, w, r); msg.Options("relay") { if role := msg.Cmdx("aaa.relay", "check", msg.Option("relay"), "userrole"); role != "" { msg.Cmd("aaa.role", role, "user", msg.Option("username")) msg.Log("info", "relay: %s", role) @@ -122,7 +129,7 @@ func (web *WEB) Login(msg *ctx.Message, w http.ResponseWriter, r *http.Request) if relay.Appends("username") { name := msg.Cmdx("ssh._route", msg.Conf("runtime", "work.route"), "_check", "work", "create", relay.Append("username"), msg.Conf("runtime", "node.route")) msg.Log("info", "login: %s", msg.Option("username", name)) - http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Option("sessid", msg.Cmdx("aaa.user", "session", "select")), Path: "/"}) + Cookie(msg, w, r) } if role := relay.Append("userrole"); role != "" { msg.Cmd("aaa.role", role, "user", msg.Option("username")) @@ -151,7 +158,7 @@ func (web *WEB) Login(msg *ctx.Message, w http.ResponseWriter, r *http.Request) if !msg.Options("username") && kit.IsLocalIP(msg.Option("remote_ip")) && msg.Confs("web.login", "local") { msg.Cmd("aaa.role", "root", "user", msg.Cmdx("ssh.work", "create")) msg.Log("info", "%s: %s", msg.Option("remote_ip"), msg.Option("username", msg.Conf("runtime", "work.name"))) - http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Option("sessid", msg.Cmdx("aaa.user", "session", "select")), Path: "/"}) + Cookie(msg, w, r) } return true @@ -408,6 +415,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", }, }, Help: "功能配置"}, "login": &ctx.Config{Name: "login", Value: map[string]interface{}{ + "expire": "240h", "local": true, "check": true, "sess_void": false, diff --git a/src/examples/chat/chat.go b/src/examples/chat/chat.go index 62cd53a5..451cc543 100644 --- a/src/examples/chat/chat.go +++ b/src/examples/chat/chat.go @@ -3,6 +3,7 @@ package chat import ( "contexts/ctx" "contexts/web" + "net/http" "toolkit" ) @@ -27,7 +28,7 @@ func check(m *ctx.Message, arg []string) ([]string, string, bool) { var Index = &ctx.Context{Name: "chat", Help: "会议中心", Caches: map[string]*ctx.Cache{}, Configs: map[string]*ctx.Config{ - "login": &ctx.Config{Name: "login", Value: map[string]interface{}{"check": false, "local": true}, Help: "默认组件"}, + "login": &ctx.Config{Name: "login", Value: map[string]interface{}{"check": false, "local": true, "expire": "720h"}, Help: "默认组件"}, "componet": &ctx.Config{Name: "componet", Value: map[string]interface{}{ "index": []interface{}{ map[string]interface{}{"name": "chat", @@ -95,6 +96,8 @@ var Index = &ctx.Context{Name: "chat", Help: "会议中心", if msg := m.Find("cli.weixin", true); msg != nil { msg.Cmd("js_token") m.Copy(msg, "append") + m.Append("remote_ip", m.Option("remote_ip")) + m.Append("nickname", kit.Select(m.Option("username"), m.Option("nickname"))) m.Table() } return @@ -113,6 +116,10 @@ var Index = &ctx.Context{Name: "chat", Help: "会议中心", m.Cmd("aaa.auth", "username", arg[0], "data", "chat.default", m.Cmdx(".ocean", "spawn", "", m.Option("username")+"@"+m.Conf("runtime", "work.name"), m.Option("username"))) } + r := m.Optionv("request").(*http.Request) + w := m.Optionv("response").(http.ResponseWriter) + + web.Cookie(m, w, r) m.Echo(m.Option("sessid")) } } diff --git a/usr/librarys/context.js b/usr/librarys/context.js index 8fbd2d9d..9b1f7f4c 100644 --- a/usr/librarys/context.js +++ b/usr/librarys/context.js @@ -138,7 +138,7 @@ ctx = context = { location.search = arg.join("&"); return value }, - Cookie: function(key, value) { + Cookie: function(key, value, expire) { if (key == undefined) { cs = {} cookies = document.cookie.split("; ") diff --git a/usr/librarys/example.js b/usr/librarys/example.js index fc2d3f7f..625e73e5 100644 --- a/usr/librarys/example.js +++ b/usr/librarys/example.js @@ -460,7 +460,8 @@ function Page(page) { field.Pane.Login(ui.username.value, ui.password.value, function(sessid) { if (!sessid) {kit.alert("用户或密码错误"); return} - ctx.Cookie("sessid", sessid), page.login.Pane.Dialog(1, 1), page.onload() + // ctx.Cookie("sessid", sessid), + page.login.Pane.Dialog(1, 1), page.onload() }) }]}, {type: "br"}, ]) @@ -810,10 +811,19 @@ function Plugin(page, pane, field, runs) { }), plugin.Check() }, - getLocation: function(event, target, option, field, cb) { - page.getLocation && page.getLocation(cb || (function(res) { - page.ontoast(JSON.stringify(res)) - })) + getLocation: function(event) { + var x = parseFloat(option.x.value) + var y = parseFloat(option.y.value) + page.getLocation && page.getLocation(function(res) { + plugin.msg = { + append: ["longitude", "latitude", "accuracy", "speed"], + longitude: [res.longitude+x+""], + latitude: [res.latitude+y+""], + accuracy: [res.accuracy+""], + speed: [res.speed+""], + } + plugin.display("table") + }) }, openLocation: function(event) { var x = parseFloat(option.x.value)