From c293a1bd95615c763ee319cc2bf481c68305d140 Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 12 Nov 2017 20:33:48 +0800 Subject: [PATCH] =?UTF-8?q?mac=20add=20userinfo=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- etc/init.sh | 6 ++++ src/context/cli/cli.go | 69 +++++++++++++++++++++++++++++++++++++++--- src/context/ctx.go | 49 ++++++++++++++++++++++-------- 3 files changed, 107 insertions(+), 17 deletions(-) diff --git a/etc/init.sh b/etc/init.sh index e69de29b..73f620ce 100644 --- a/etc/init.sh +++ b/etc/init.sh @@ -0,0 +1,6 @@ +~aaa +login root 94ca7394d007fa189cc4be0a2625d716 root +login root root + +~cli +remote slave listen :9393 tcp diff --git a/src/context/cli/cli.go b/src/context/cli/cli.go index 5396d2af..a5e37532 100644 --- a/src/context/cli/cli.go +++ b/src/context/cli/cli.go @@ -5,6 +5,7 @@ import ( // {{{ "context" "fmt" "io" + // "log" "os" "strconv" "strings" @@ -25,6 +26,7 @@ type CLI struct { alias map[string]string next string exit bool + login *ctx.Context target *ctx.Context *ctx.Context @@ -44,6 +46,33 @@ func (cli *CLI) push(f io.ReadCloser) { // {{{ // }}} func (cli *CLI) parse(m *ctx.Message) bool { // {{{ + if len(cli.ins) == 1 && cli.Owner == nil { + + username := "" + fmt.Fprintf(cli.out, "username>") + fmt.Fscanln(cli.in, &username) + + password := "" + fmt.Fprintf(cli.out, "password>") + fmt.Fscanln(cli.in, &password) + + if aaa := cli.Root.Find("aaa"); aaa != nil { + cli.Owner = Index.Owner + msg := m.Spawn(aaa, "user") + + if msg.Cmd("login", username, password) == "" { + fmt.Fprintln(cli.out, "登录失败") + m.Post(cli.Context, "exit") + cli.out.Close() + cli.in.Close() + return false + } + + cli.Owner = msg.Target + cli.Cap("user", msg.Target.Cap("username")) + } + } + if len(cli.ins) == 1 || cli.Conf("slient") != "yes" { cli.echo(cli.Conf("PS1")) } @@ -175,6 +204,7 @@ func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ // }}} func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{ + cli.Owner = nil cli.Capi("nterm", 1) defer cli.Capi("nterm", -1) @@ -253,9 +283,11 @@ func (cli *CLI) Exit(m *ctx.Message, arg ...string) bool { // {{{ var Index = &ctx.Context{Name: "cli", Help: "管理终端", Caches: map[string]*ctx.Cache{ "nterm": &ctx.Cache{Name: "终端数量", Value: "0", Help: "已经运行的终端数量"}, + "user": &ctx.Cache{Name: "登录用户", Value: "", Help: "登录用户名"}, }, Configs: map[string]*ctx.Config{ - "slient": &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽"}, + "slient": &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽"}, + "default": &ctx.Config{Name: "默认的搜索起点(root/back/home)", Value: "root", Help: "模块搜索的默认起点,root:从根模块,back:从父模块,home:从当前模块"}, // "hello": &ctx.Config{Name: "开场白", Value: "\n~~~ Hello Context & Message World ~~~\n", Help: "模块启动时输出的信息"}, // "byebye": &ctx.Config{Name: "结束语", Value: "\n~~~ Byebye Context & Message World ~~~\n", Help: "模块停止时输出的信息"}, @@ -278,15 +310,18 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", }, Commands: map[string]*ctx.Command{ "context": &ctx.Command{Name: "context [root|back|home] [[find|search] name] [show|spawn|start|switch][args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块,args:启动参数", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { - cli, ok := c.Server.(*CLI) // {{{ + cli, ok := m.Context.Server.(*CLI) // {{{ if !ok { - return "" + cli, ok = c.Server.(*CLI) + if !ok { + return "" + } } switch len(arg) { case 0: m.Target.Root.Travel(func(c *ctx.Context) bool { - if c.Context != nil { + if c.Context != nil && m.Context.Check(c) { m.Echo("%s: %s(%s)\n", c.Context.Name, c.Name, c.Help) } return true @@ -295,6 +330,16 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", } target := m.Target + switch c.Conf("default") { + case "root": + target = target.Root + case "back": + if target.Context != nil { + target = target.Context + } + case "home": + } + method := "search" action := "switch" which := "" @@ -343,6 +388,10 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", } for _, v := range cs { + if !m.Context.Check(v) { + continue + } + switch action { case "switch": cli.target = v @@ -565,6 +614,14 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", return "" // }}} }}, + "userinfo": &ctx.Command{Name: "userinfo", Help: "查看模块的用户信息", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { + o := m.Target.Owner // {{{ + if o != nil { + m.Echo("%s\n", o.Name) + } + return "" + // }}} + }}, "exit": &ctx.Command{Name: "exit", Help: "退出", Hand: func(c *ctx.Context, m *ctx.Message, key string, arg ...string) string { cli, ok := m.Target.Server.(*CLI) // {{{ if !ok { @@ -572,9 +629,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理终端", } if ok { if !cli.exit { - m.Echo(c.Conf("结束语")) + // m.Echo(c.Conf("结束语")) cli.Context.Exit(m) } + cli.in.Close() + cli.out.Close() cli.exit = true } diff --git a/src/context/ctx.go b/src/context/ctx.go index bde786dc..fed8eccb 100644 --- a/src/context/ctx.go +++ b/src/context/ctx.go @@ -204,7 +204,7 @@ func (m *Message) End(s bool) { // {{{ // }}} -func (m *Message) Cmd(arg ...string) string { // {{{ +func (m *Message) Start(key string, arg ...string) bool { // {{{ if len(arg) > 0 { if m.Meta == nil { m.Meta = make(map[string][]string) @@ -212,7 +212,9 @@ func (m *Message) Cmd(arg ...string) string { // {{{ m.Meta["detail"] = arg } - return m.Target.Cmd(m, m.Meta["detail"][0], m.Meta["detail"][1:]...) + m.Target.Spawn(m, key).Start(m) + + return true } // }}} @@ -225,7 +227,7 @@ func (m *Message) Post(c *Context, arg ...string) bool { // {{{ } if c.Messages == nil { - panic(m.Target.Name + " 没有开启消息处理") + panic(c.Name + " 没有开启消息处理") } c.Messages <- m @@ -237,7 +239,7 @@ func (m *Message) Post(c *Context, arg ...string) bool { // {{{ } // }}} -func (m *Message) Start(key string, arg ...string) bool { // {{{ +func (m *Message) Cmd(arg ...string) string { // {{{ if len(arg) > 0 { if m.Meta == nil { m.Meta = make(map[string][]string) @@ -245,9 +247,7 @@ func (m *Message) Start(key string, arg ...string) bool { // {{{ m.Meta["detail"] = arg } - m.Target.Spawn(m, key).Start(m) - - return true + return m.Target.Cmd(m, m.Meta["detail"][0], m.Meta["detail"][1:]...) } // }}} @@ -276,6 +276,7 @@ type Context struct { Requests []*Message Sessions map[string]*Message + Owner *Context Index map[string]*Context Groups map[string]*Context @@ -299,6 +300,7 @@ func (c *Context) Assert(e error) bool { // {{{ func (c *Context) AssertOne(m *Message, safe bool, hand ...func(c *Context, m *Message)) *Context { // {{{ defer func() { if e := recover(); e != nil { + log.Println(c.Name, e) if c.Conf("debug") == "on" && e != io.EOF { fmt.Println(c.Name, "error:", e) debug.PrintStack() @@ -381,10 +383,12 @@ func (c *Context) Start(m *Message) bool { // {{{ defer Index.Capi("nserver", -1) defer log.Printf("%d stop(%s): %s %s", m.Code, Index.Cap("nserver"), c.Name, c.Help) + c.Owner = m.Owner c.Requests = []*Message{m} c.Server.Start(m, m.Meta["detail"]...) }) } + Pulse.Wait <- true return true } @@ -657,6 +661,22 @@ func (c *Context) Del(arg ...string) { // {{{ // }}} +func (c *Context) Check(s *Context) bool { // {{{ + // if c.Owner != nil { + // log.Println("source:", c.Owner.Name) + // } + // if s.Owner != nil { + // log.Println("target:", s.Owner.Name) + // } + if c.Owner == s.Owner || c.Owner == Index.Owner { + // log.Println("match:") + return true + } + // log.Println("not match:") + return false +} + +// }}} func (c *Context) Travel(hand func(s *Context) bool) { // {{{ cs := []*Context{c} for i := 0; i < len(cs); i++ { @@ -687,8 +707,6 @@ func (c *Context) Search(name string) []*Context { // {{{ if strings.Contains(s.Name, name) || strings.Contains(s.Help, name) { cs = append(cs, s) log.Println(c.Name, "search:", s.Name, "[match]", name) - } else { - log.Println(c.Name, "search:", s.Name) } return true }) @@ -723,6 +741,11 @@ func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{ if x, ok := s.Commands[key]; ok { log.Printf("%s cmd(%s->%s): %v", c.Name, m.Context.Name, m.Target.Name, m.Meta["detail"]) + if !m.Context.Check(m.Target) { + log.Printf("没有权限:") + return "" + } + for _, v := range m.Meta["option"] { if _, ok := x.Options[v]; !ok { panic(errors.New(fmt.Sprintf("未知参数:" + v))) @@ -731,8 +754,10 @@ func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{ m.Meta["result"] = nil c.AssertOne(m, true, func(c *Context, m *Message) { - x.Hand(c, m, key, arg...) - + ret := x.Hand(c, m, key, arg...) + if ret != "" { + m.Echo(ret) + } }) if x.Appends != nil { @@ -753,6 +778,7 @@ func (c *Context) Cmd(m *Message, key string, arg ...string) string { // {{{ // }}} func (c *Context) Conf(key string, arg ...string) string { // {{{ for s := c; s != nil; s = s.Context { + if x, ok := s.Configs[key]; ok { switch len(arg) { case 0: @@ -935,7 +961,6 @@ func init() { Pulse.Target = Index Pulse.Context = Index Pulse.Wait = make(chan bool, 10) - } func Start(args ...string) {