From 419c5487f58eb3e2531c20ee61b7df9f16d95678 Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 16 Aug 2018 09:24:57 +0800 Subject: [PATCH] mac opt web.share --- src/contexts/aaa/aaa.go | 6 ++- src/contexts/ctx.go | 44 ++++++++++++++-- src/contexts/log/log.go | 2 +- src/contexts/nfs/nfs.go | 1 + src/contexts/web/web.go | 96 ++++++++++++++++++---------------- usr/library/context.js | 6 ++- usr/template/common/login.html | 60 ++++++++++++--------- 7 files changed, 136 insertions(+), 79 deletions(-) diff --git a/src/contexts/aaa/aaa.go b/src/contexts/aaa/aaa.go index 1cc7310c..043f77c6 100644 --- a/src/contexts/aaa/aaa.go +++ b/src/contexts/aaa/aaa.go @@ -211,6 +211,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", if int64(msg.Capi("expire")) > time.Now().Unix() { m.Echo(msg.Cap("username")) m.Copy(msg, "target") + m.Appendv("aaa", msg) m.Sess("aaa", msg) } else { delete(aaa.sessions, arg[0]) @@ -243,6 +244,8 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", if m.Cap("password") == aaa.Password(arg[1]) { m.Sess("aaa", m.Target()) m.Echo(m.Cap("sessid")) + m.Appendv("aaa", m) + m.Sess("aaa", m) } else { m.Sess("aaa", c) } @@ -255,8 +258,9 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", if !find { m.Start(m.Confx("aaa_name"), m.Confx("aaa_help"), arg[0], aaa.Password(arg[1]), aaa.Session(arg[0])) m.Cap("stream", arg[0]) - m.Sess("aaa", m) m.Echo(m.Cap("sessid")) + m.Appendv("aaa", m) + m.Sess("aaa", m) } } } diff --git a/src/contexts/ctx.go b/src/contexts/ctx.go index 0a74879f..b67a5c99 100644 --- a/src/contexts/ctx.go +++ b/src/contexts/ctx.go @@ -64,6 +64,7 @@ func Trans(arg ...interface{}) []string { // {{{ for _, v := range val { ls = append(ls, fmt.Sprintf("%d", v)) } + case nil: default: ls = append(ls, fmt.Sprintf("%v", val)) } @@ -574,6 +575,9 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { // {{{ } m.Log("error", "%s", fmt.Sprintln(e)) + if m.Confs("debug") { + debug.PrintStack() + } panic(m.Set("result", "error: ", fmt.Sprintln(e), "\n")) } @@ -1344,7 +1348,10 @@ func (m *Message) Optionv(key string, arg ...interface{}) interface{} { // {{{ } for _, k := range msg.Meta["option"] { if k == key { - return msg.Data[key] + if v, ok := msg.Data[key]; ok { + return v + } + return msg.Meta[key] } } } @@ -1409,7 +1416,10 @@ func (m *Message) Appendv(key string, arg ...interface{}) interface{} { // {{{ } for _, k := range ms[i].Meta["append"] { if k == key { - return ms[i].Data[key] + if v, ok := ms[i].Data[key]; ok { + return v + } + return ms[i].Meta[key] } } } @@ -2097,7 +2107,7 @@ var CGI = template.FuncMap{ } case string: if len(arg) == 2 { - return m.Meta[value] + return m.Optionv(value) } switch val := arg[2].(type) { @@ -2215,7 +2225,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心", "chain": &Config{Name: "chain", Value: map[string]interface{}{}, Help: "调试模式,on:打印,off:不打印)"}, "compact_log": &Config{Name: "compact_log(true/false)", Value: "true", Help: "调试模式,on:打印,off:不打印)"}, "auto_make": &Config{Name: "auto_make(true/false)", Value: "true", Help: "调试模式,on:打印,off:不打印)"}, - "debug": &Config{Name: "debug(on/off)", Value: "off", Help: "调试模式,on:打印,off:不打印)"}, + "debug": &Config{Name: "debug(on/off)", Value: "on", Help: "调试模式,on:打印,off:不打印)"}, "search_method": &Config{Name: "search_method(find/search)", Value: "search", Help: "搜索方法, find: 模块名精确匹配, search: 模块名或帮助信息模糊匹配"}, "search_choice": &Config{Name: "search_choice(first/last/rand/magic)", Value: "magic", Help: "搜索匹配, first: 匹配第一个模块, last: 匹配最后一个模块, rand: 随机选择, magic: 加权选择"}, @@ -3358,10 +3368,36 @@ var Index = &Context{Name: "ctx", Help: "模块中心", delete(cs[i].Commands, arg[2]) } else if len(arg) == 4 { delete(cs[i].Commands[arg[2]].Shares, arg[3]) + } else if len(arg) == 5 { + delete(cs[i].Commands[arg[2]].Shares, arg[3]) } } } + case "show": + switch arg[1] { + case "cache": + case "config": + case "command": + case "context": + for friend, x := range share.Index { + for command, c := range x.Commands { + m.Add("append", "friend", friend) + m.Add("append", "key", command) + m.Add("append", "arg", "") + m.Add("append", "value", "") + for a, s := range c.Shares { + for _, v := range s { + m.Log("fuck", "fuck %v %v %v %v", friend, command, a, v) + m.Add("append", "friend", friend) + m.Add("append", "key", command) + m.Add("append", "arg", a) + m.Add("append", "value", v) + } + } + } + } + } } // }}} }}, }, diff --git a/src/contexts/log/log.go b/src/contexts/log/log.go index 075f9482..6ea332e5 100644 --- a/src/contexts/log/log.go +++ b/src/contexts/log/log.go @@ -59,7 +59,7 @@ var Index = &ctx.Context{Name: "log", Help: "日志中心", "nlog": &ctx.Cache{Name: "nlog", Value: "0", Help: "日志屏蔽类型"}, }, Configs: map[string]*ctx.Config{ - "silent": &ctx.Config{Name: "silent", Value: map[string]interface{}{"cb": true}, Help: "日志屏蔽类型"}, + "silent": &ctx.Config{Name: "silent", Value: map[string]interface{}{"cb": true, "find": true}, Help: "日志屏蔽类型"}, "module": &ctx.Config{Name: "module", Value: map[string]interface{}{ "log": map[string]interface{}{"cmd": true}, "lex": map[string]interface{}{"cmd": true, "debug": true}, diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index ace94a3e..bc2dfe93 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -1234,6 +1234,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", continue case "csv": cmd := exec.Command("git", "log", "--shortstat", "--pretty=commit: %ad", "--date=format:%Y-%m-%d") + m.Log("fuck", "wh%v", cmd) if out, e := cmd.CombinedOutput(); e != nil { m.Echo("error: ") m.Echo("%s\n", e) diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 8d2c2d8c..492ed8e9 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -123,8 +123,7 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C // }}} func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{ - m := web.Message() - m.Log("cmd", "%v %s %s", r.RemoteAddr, r.Method, r.URL) + m := web.Message().Log("info", "").Log("info", "%v %s %s", r.RemoteAddr, r.Method, r.URL) if m.Confs("logheaders") { for k, v := range r.Header { @@ -303,6 +302,14 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", }, Help: "执行条件"}, "index": &ctx.Config{Name: "index", Value: map[string]interface{}{ "shy": []interface{}{ + map[string]interface{}{ + "template": "userinfo", "title": "userinfo", + }, + map[string]interface{}{ + "module": "web", "command": "/share", + "argument": []interface{}{}, + "template": "share", "title": "share", + }, map[string]interface{}{ "module": "cli", "command": "system", "argument": []interface{}{"tmux", "list-clients"}, @@ -630,6 +637,13 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", r := m.Optionv("request").(*http.Request) // {{{ w := m.Optionv("response").(http.ResponseWriter) + //权限检查 + dir := path.Join(m.Cap("directory"), m.Option("dir", strings.TrimPrefix(m.Option("path"), "/index"))) + if check := m.Spawn(c).Cmd("/check", "command", "/index/", "dir", dir); !check.Results(0) { + m.Copy(check, "append") + return + } + //执行命令 if m.Has("details") { if check := m.Spawn().Cmd("/check", "target", m.Option("module"), "command", m.Option("details")); !check.Results(0) { @@ -642,25 +656,19 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", return } - //权限检查 - dir := path.Join(m.Cap("directory"), m.Option("dir", strings.TrimPrefix(m.Option("path"), "/index"))) - check := m.Spawn(c).Cmd("/check", "command", "/index/", "dir", dir) - if !check.Results(0) { - m.Copy(check, "append") - return - } - //下载文件 - if s, e := os.Stat(dir); e == nil && m.Option("dir") != "" && !s.IsDir() { + if s, e := os.Stat(dir); e == nil && !s.IsDir() { http.ServeFile(w, r, dir) return } + if !m.Options("module") { + m.Option("module", "web") + } //浏览目录 - aaa := check.Appendv("aaa").(*ctx.Message) - m.Append("template", aaa.Cap("username")) + m.Append("template", m.Append("username")) m.Option("title", "index") - m.Option("dir", dir) + m.Option("username", m.Append("username")) // }}} }}, "/travel": &ctx.Command{Name: "/travel", Help: "文件上传", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { @@ -932,56 +940,52 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Append("redirect", m.Option("referer")) // }}} }}, - "/share": &ctx.Command{Name: "/share", Help: "资源共享", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - check := m.Spawn().Cmd("/check", "command", arg[0], arg[1], arg[2]) // {{{ - if !check.Results(0) { + "/share": &ctx.Command{Name: "/share arg...", Help: "资源共享", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + if check := m.Spawn().Cmd("/check", "target", m.Option("module"), m.Optionv("share")); !check.Results(0) { m.Copy(check, "append") return } - msg := check.Appendv("aaa").(*ctx.Message).Spawn(m.Target()) - if m.Options("shareto") { - msg.Cmd("right", m.Option("shareto"), "add", "command", arg[0], arg[1], arg[2]) + if m.Options("friend") && m.Options("module") { + m.Copy(m.Appendv("aaa").(*ctx.Message).Find(m.Option("module")).Cmd("right", m.Option("friend"), m.Option("action"), m.Optionv("share")), "result") + if m.Confv("index", m.Option("friend")) == nil { + m.Confv("index", m.Option("friend"), m.Confv("index", m.Append("username"))) + } + return } - if m.Options("notshareto") { - msg.Cmd("right", m.Option("notshareto"), "del", "command", arg[0], arg[1], arg[2]) + + msg := m.Spawn().Cmd("right", "target", m.Option("module"), m.Append("username"), "show", "context") + m.Copy(msg, "append") + }}, + "/check": &ctx.Command{Name: "/check arg...", Help: "权限检查, cache|config|command: 接口类型, name: 接口名称, args: 其它参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + if login := m.Spawn().Cmd("/login"); login.Has("template") { // {{{ + m.Echo("no").Copy(login, "append") + return } + + if msg := m.Spawn().Cmd("right", m.Append("username"), "check", arg); !msg.Results(0) { + m.Echo("no").Append("message", "no right, please contact manager") + return + } + m.Echo("ok") // }}} }}, - "/check": &ctx.Command{Name: "/check check", Help: "权限检查, cache|config|command: 接口类型, name: 接口名称, args: 其它参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - // w := m.Optionv("response").(http.ResponseWriter) //{{{ - if login := m.Spawn().Cmd("/login"); login.Has("redirect") { - aaa := m.Appendv("aaa").(*ctx.Message) - if msg := m.Spawn().Cmd("right", aaa.Cap("username"), "check", arg); msg.Results(0) { - m.Copy(login, "append").Echo(msg.Result(0)) - return - } - // w.WriteHeader(http.StatusForbidden) - m.Append("message", "no right, please contact manager") - m.Echo("no") - return - } else { - m.Copy(login, "append").Echo("no") - } - // }}} - }}, "/login": &ctx.Command{Name: "/login", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { - w := m.Optionv("response").(http.ResponseWriter) // {{{ - - if m.Options("sessid") { - if aaa := m.Find("aaa").Cmd("login", m.Option("sessid")); aaa.Results(0) { + if m.Options("sessid") { // {{{ + if aaa := m.Sess("aaa").Cmd("login", m.Option("sessid")); aaa.Results(0) { m.Append("redirect", m.Option("referer")) - m.Appendv("aaa", aaa) + m.Append("username", aaa.Cap("username")) return } } + w := m.Optionv("response").(http.ResponseWriter) if m.Options("username") && m.Options("password") { - if aaa := m.Find("aaa").Cmd("login", m.Option("username"), m.Option("password")); aaa.Results(0) { + if aaa := m.Sess("aaa").Cmd("login", m.Option("username"), m.Option("password")); aaa.Results(0) { http.SetCookie(w, &http.Cookie{Name: "sessid", Value: aaa.Result(0)}) m.Append("redirect", m.Option("referer")) - m.Appendv("aaa", aaa) + m.Append("username", m.Option("username")) return } } diff --git a/usr/library/context.js b/usr/library/context.js index e5541940..085ef767 100644 --- a/usr/library/context.js +++ b/usr/library/context.js @@ -73,7 +73,11 @@ ctx = { var args = []; for (k in form) { - if (form[k] != undefined) { + if (form[k] instanceof Array) { + for (i in form[k]) { + args.push(k+"="+encodeURIComponent(form[k][i])); + } + } else if (form[k] != undefined) { args.push(k+"="+encodeURIComponent(form[k])); } } diff --git a/usr/template/common/login.html b/usr/template/common/login.html index bf8c1830..1834e3b9 100644 --- a/usr/template/common/login.html +++ b/usr/template/common/login.html @@ -10,7 +10,7 @@ {{define "userinfo"}}
userinfo - welcome {{append . "username"}} to context world + welcome {{option . "username"}} to context world