From fdef735fb2737581d0580871238cdd8c09365eab Mon Sep 17 00:00:00 2001 From: shaoying Date: Wed, 6 May 2020 14:22:06 +0800 Subject: [PATCH] opt some --- base/aaa/aaa.go | 16 +++++--- base/ctx/ctx.go | 11 +++++- base/log/log.go | 65 +++++++++++++++++++++----------- base/tcp/tcp.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++ conf.go | 8 ++-- misc/tmux/tmux.go | 4 -- misc/zsh/zsh.go | 20 ++++++++-- type.go | 12 ++++-- 8 files changed, 186 insertions(+), 44 deletions(-) diff --git a/base/aaa/aaa.go b/base/aaa/aaa.go index 63b9570c..972e62d5 100644 --- a/base/aaa/aaa.go +++ b/base/aaa/aaa.go @@ -27,18 +27,24 @@ var Index = &ice.Context{Name: "aaa", Help: "认证模块", m.Save(ice.AAA_ROLE, ice.AAA_USER, ice.AAA_SESS) }}, - ice.AAA_ROLE: {Name: "role check|black|white|right", Help: "角色", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + ice.AAA_ROLE: {Name: []string{ + "role check username", + "role right userrole chain", + "role userrole username ok", + "role black|white userrole enable|disable chain", + }, Help: "角色", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + + // ice.AAA_ROLE: {Name: "role check username; role right userrole chain; role userrole username ok; role black|white userrole enable|disable chain", Help: "角色", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if len(arg) == 0 { kit.Fetch(m.Confv("role", "meta.root"), func(key string, value string) { - m.Push("userrole", "root") - m.Push("username", key) + m.Push("userrole", "root").Push("username", key) }) kit.Fetch(m.Confv("role", "meta.tech"), func(key string, value string) { - m.Push("userrole", "tech") - m.Push("username", key) + m.Push("userrole", "tech").Push("username", key) }) return } + switch arg[0] { case "check": // 用户角色 diff --git a/base/ctx/ctx.go b/base/ctx/ctx.go index c6eb5e4b..047501f1 100644 --- a/base/ctx/ctx.go +++ b/base/ctx/ctx.go @@ -84,7 +84,7 @@ var Index = &ice.Context{Name: "ctx", Help: "配置模块", v := s.Commands[k] m.Push("key", s.Cap(ice.CTX_FOLLOW)) m.Push("index", k) - m.Push("name", v.Name) + m.Push("name", kit.Format(v.Name)) m.Push("help", kit.Simple(v.Help)[0]) m.Push("list", kit.Format(v.List)) } @@ -106,7 +106,14 @@ var Index = &ice.Context{Name: "ctx", Help: "配置模块", m.Push("help", kit.Simple(cmd.Help)[0]) m.Push("meta", kit.Format(cmd.Meta)) if len(cmd.List) == 0 { - list := kit.Split(cmd.Name) + var list []string + switch name := cmd.Name.(type) { + case []string, []interface{}: + list = kit.Split(kit.Simple(name)[0]) + default: + list = kit.Split(strings.Split(kit.Format(name), ";")[0]) + } + button := false for i, v := range list { if i > 0 { diff --git a/base/log/log.go b/base/log/log.go index 206b7e22..285b1b17 100644 --- a/base/log/log.go +++ b/base/log/log.go @@ -4,7 +4,7 @@ import ( "github.com/shylinux/icebergs" "github.com/shylinux/toolkits" - "fmt" + "bufio" "os" "path" ) @@ -36,18 +36,25 @@ func (f *Frame) Start(m *ice.Message, arg ...string) bool { break } else { // 日志文件 - file := kit.Select("bench", m.Conf("show", l.l+".file")) - f := m.Confv("file", file+".file").(*os.File) - - // 日志内容 - ls := []string{l.m.Format("prefix"), " "} - ls = append(ls, m.Conf("show", l.l+".prefix"), - l.l, " ", l.s, m.Conf("show", l.l+".suffix"), "\n") + file := kit.Select("bench", m.Conf("show", kit.Keys(l.l, "file"))) + // 日志格式 + view := m.Confm("view", m.Conf("show", kit.Keys(l.l, "view"))) // 输出日志 - for _, v := range ls { - fmt.Fprintf(f, v) + bio := m.Confv("file", file+".file").(*bufio.Writer) + bio.WriteString(l.m.Format("prefix")) + bio.WriteString(" ") + if p, ok := view["prefix"].(string); ok { + bio.WriteString(p) } + bio.WriteString(l.l) + bio.WriteString(" ") + bio.WriteString(l.s) + if p, ok := view["suffix"].(string); ok { + bio.WriteString(p) + } + bio.WriteString("\n") + bio.Flush() } } return true @@ -65,27 +72,43 @@ var Index = &ice.Context{Name: "log", Help: "日志模块", "error", kit.Dict("path", "var/log/error.log"), "trace", kit.Dict("path", "var/log/trace.log"), )}, - "show": &ice.Config{Name: "show", Help: "日志格式", Value: kit.Dict( + "view": &ice.Config{Name: "view", Help: "日志格式", Value: kit.Dict( + "red", kit.Dict("prefix", "\033[31m", "suffix", "\033[0m"), + "green", kit.Dict("prefix", "\033[32m", "suffix", "\033[0m"), + "yellow", kit.Dict("prefix", "\033[33m", "suffix", "\033[0m"), + )}, + "show": &ice.Config{Name: "show", Help: "日志分流", Value: kit.Dict( + // 数据 ice.LOG_ENABLE, kit.Dict("file", "watch"), ice.LOG_IMPORT, kit.Dict("file", "watch"), - ice.LOG_CREATE, kit.Dict("file", "watch"), - ice.LOG_INSERT, kit.Dict("file", "watch"), ice.LOG_EXPORT, kit.Dict("file", "watch"), + ice.LOG_CREATE, kit.Dict("file", "watch"), + ice.LOG_REMOVE, kit.Dict("file", "watch"), + ice.LOG_INSERT, kit.Dict("file", "watch"), + ice.LOG_DELETE, kit.Dict("file", "watch"), + ice.LOG_MODIFY, kit.Dict("file", "watch"), + ice.LOG_SELECT, kit.Dict("file", "watch"), + // 事件 ice.LOG_LISTEN, kit.Dict("file", "bench"), + ice.LOG_ACCEPT, kit.Dict("file", "bench", "view", "green"), + ice.LOG_FINISH, kit.Dict("file", "bench", "view", "red"), ice.LOG_SIGNAL, kit.Dict("file", "bench"), - ice.LOG_TIMERS, kit.Dict("file", "bench"), ice.LOG_EVENTS, kit.Dict("file", "bench"), + ice.LOG_TIMERS, kit.Dict("file", "bench"), + // 状态 ice.LOG_BEGIN, kit.Dict("file", "bench"), - ice.LOG_START, kit.Dict("file", "bench", "prefix", "\033[32m", "suffix", "\033[0m"), - ice.LOG_SERVE, kit.Dict("file", "bench", "prefix", "\033[32m", "suffix", "\033[0m"), - ice.LOG_CLOSE, kit.Dict("file", "bench", "prefix", "\033[31m", "suffix", "\033[0m"), + ice.LOG_START, kit.Dict("file", "bench", "view", "green"), + ice.LOG_SERVE, kit.Dict("file", "bench", "view", "green"), + ice.LOG_CLOSE, kit.Dict("file", "bench", "view", "red"), - ice.LOG_CMDS, kit.Dict("file", "bench", "prefix", "\033[32m", "suffix", "\033[0m"), - ice.LOG_COST, kit.Dict("file", "bench", "prefix", "\033[33m", "suffix", "\033[0m"), + // 分类 + ice.LOG_AUTH, kit.Dict("file", "bench", "view", "yellow"), + ice.LOG_CMDS, kit.Dict("file", "bench", "view", "green"), + ice.LOG_COST, kit.Dict("file", "bench", "view", "yellow"), ice.LOG_INFO, kit.Dict("file", "bench"), - ice.LOG_WARN, kit.Dict("file", "bench", "prefix", "\033[31m", "suffix", "\033[0m"), + ice.LOG_WARN, kit.Dict("file", "bench", "view", "red"), ice.LOG_ERROR, kit.Dict("file", "error"), ice.LOG_TRACE, kit.Dict("file", "trace"), )}, @@ -98,7 +121,7 @@ var Index = &ice.Context{Name: "log", Help: "日志模块", if f, p, e := kit.Create(kit.Format(value["path"])); m.Assert(e) { m.Cap(ice.CTX_STREAM, path.Base(p)) m.Log("create", "%s: %s", key, p) - value["file"] = f + value["file"] = bufio.NewWriter(f) } }) } diff --git a/base/tcp/tcp.go b/base/tcp/tcp.go index 5b996bb2..6e0a24a0 100644 --- a/base/tcp/tcp.go +++ b/base/tcp/tcp.go @@ -4,10 +4,15 @@ import ( "github.com/shylinux/icebergs" "github.com/shylinux/toolkits" + "bufio" "net" + "net/url" "strings" ) +type Frame struct { +} + var Index = &ice.Context{Name: "tcp", Help: "通信模块", Caches: map[string]*ice.Cache{}, Configs: map[string]*ice.Config{ @@ -73,6 +78,95 @@ var Index = &ice.Context{Name: "tcp", Help: "通信模块", } } }}, + + "check": {Name: "check addr", Help: "server", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if strings.Contains(arg[0], ".") { + switch list := strings.Split(arg[0], ":"); list[0] { + case "127.0.0.1": + m.Echo("local") + } + } else { + switch list := strings.Split(arg[0], "]:"); strings.TrimPrefix(list[0], "[") { + case "::1": + m.Echo("local") + } + } + }}, + "server": {Name: "server [tcp4|tcp6|udp4|udp6] addr", Help: "server", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + proto := "tcp4" + switch arg[0] { + case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "ip", "ip4", "ip6": + proto, arg = arg[0], arg[1:] + } + + if l, e := net.Listen(proto, arg[0]); m.Assert(e) { + m.Gos(m, func(m *ice.Message) { + // 启动服务 + m.Logs(ice.LOG_LISTEN, "addr", l.Addr()) + for { + if c, e := l.Accept(); m.Assert(e) { + m.Gos(m.Spawns(), func(msg *ice.Message) { + // 建立连接 + msg.Logs(ice.LOG_ACCEPT, "addr", c.RemoteAddr()) + msg.Option(ice.MSG_USERADDR, c.RemoteAddr()) + msg.Option(ice.MSG_USERNAME, "") + msg.Option(ice.MSG_USERROLE, "") + + switch msg.Cmdx("check", c.RemoteAddr().String()) { + case "local": + // 本机用户 + msg.Option(ice.MSG_USERNAME, msg.Conf(ice.CLI_RUNTIME, "boot.username")) + msg.Option(ice.MSG_USERROLE, msg.Cmdx(ice.AAA_ROLE, "check", msg.Option(ice.MSG_USERNAME))) + msg.Logs(ice.LOG_AUTH, "name", msg.Option(ice.MSG_USERNAME), "role", msg.Option(ice.MSG_USERROLE)) + } + + cmds := []string{} + buf := bufio.NewWriter(c) + for bio := bufio.NewScanner(c); bio.Scan(); { + text := bio.Text() + msg.Logs("scan", "len", len(text), "text", text) + + if len(text) == 0 { + if len(cmds) > 0 { + msg.Cmd(ice.AAA_ROLE, "right") + // 执行命令 + res := msg.Cmd(cmds) + + // 返回结果 + for _, str := range res.Resultv() { + buf.WriteString("result:") + buf.WriteString(url.QueryEscape(str)) + buf.WriteString("\n") + } + buf.WriteString("\n") + buf.Flush() + + cmds = cmds[:0] + } + continue + } + + // 解析请求 + line := strings.SplitN(bio.Text(), ":", 2) + line[0], e = url.QueryUnescape(line[0]) + m.Assert(e) + line[1], e = url.QueryUnescape(line[1]) + m.Assert(e) + switch line[0] { + case "cmds", ice.MSG_DETAIL: + cmds = append(cmds, line[1]) + default: + msg.Option(line[0], line[1]) + } + } + msg.Logs(ice.LOG_FINISH, "addr", c.RemoteAddr()) + }) + } + } + m.Logs(ice.LOG_FINISH, "addr", l.Addr()) + }) + } + }}, }, } diff --git a/conf.go b/conf.go index 92639b83..2faf0956 100644 --- a/conf.go +++ b/conf.go @@ -105,11 +105,13 @@ const ( // LOG LOG_REMOVE = "remove" LOG_INSERT = "insert" LOG_DELETE = "delete" - LOG_SELECT = "select" LOG_MODIFY = "modify" + LOG_SELECT = "select" // 事件 LOG_LISTEN = "listen" + LOG_ACCEPT = "accept" + LOG_FINISH = "finish" LOG_SIGNAL = "signal" LOG_EVENTS = "events" LOG_TIMERS = "timers" @@ -120,10 +122,8 @@ const ( // LOG LOG_SERVE = "serve" LOG_CLOSE = "close" - // 权限 - LOG_AUTH = "auth" - // 分类 + LOG_AUTH = "auth" LOG_CMDS = "cmds" LOG_COST = "cost" LOG_INFO = "info" diff --git a/misc/tmux/tmux.go b/misc/tmux/tmux.go index 9245b147..18bb2dfb 100644 --- a/misc/tmux/tmux.go +++ b/misc/tmux/tmux.go @@ -158,10 +158,6 @@ var Index = &ice.Context{Name: "tmux", Help: "工作台", m.Cmd(prefix, "new-window", "-dt", session, "-n", arg[0]) } - if arg[1] == "init" { - } else { - } - for _, v := range arg[1:] { switch ls := kit.Split(v); ls[1] { case "v": diff --git a/misc/zsh/zsh.go b/misc/zsh/zsh.go index e1513ab2..e3d4c45f 100644 --- a/misc/zsh/zsh.go +++ b/misc/zsh/zsh.go @@ -7,6 +7,7 @@ import ( kit "github.com/shylinux/toolkits" "io/ioutil" + "net/url" "os" "path" "strings" @@ -17,11 +18,10 @@ var Index = &ice.Context{Name: "zsh", Help: "命令行", Caches: map[string]*ice.Cache{}, Configs: map[string]*ice.Config{ "zsh": {Name: "zsh", Help: "命令行", Value: kit.Data( - "history", "zsh.history", "script", []interface{}{ + "proxy", "tmux", "history", "zsh.history", "script", []interface{}{ + ".vim/syntax/sh.vim", "etc/conf/sh.vim", ".bashrc", "etc/conf/bashrc", ".zshrc", "etc/conf/zshrc", - ".ish/plug.sh", "usr/shell/plug.sh", - ".vim/syntax/sh.vim", "etc/conf/sh.vim", }, )}, }, @@ -39,6 +39,9 @@ var Index = &ice.Context{Name: "zsh", Help: "命令行", }}, ice.CODE_PREPARE: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { m.Cmd("web.code.git.repos", "shell", "usr/shell") + p := path.Join(os.Getenv("HOME"), ".ish") + m.Cmd(ice.CLI_SYSTEM, "rm", p) + m.Cmd(ice.CLI_SYSTEM, "ln", "-s", kit.Path("usr/shell"), p) list := kit.Simple(m.Confv("zsh", "meta.script")) for i := 0; i < len(list); i += 2 { @@ -54,9 +57,10 @@ var Index = &ice.Context{Name: "zsh", Help: "命令行", } } + m.Option("you", m.Conf("zsh", "meta.proxy")) m.Richs("login", nil, m.Option("sid"), func(key string, value map[string]interface{}) { // 查找空间 - m.Option("you", value["you"]) + m.Option("you", kit.Select(m.Conf("zsh", "meta.proxy"), value["you"])) }) m.Logs(ice.LOG_AUTH, "you", m.Option("you"), "url", m.Option(ice.MSG_USERURL), "cmd", m.Optionv("cmds"), "sub", m.Optionv("sub")) @@ -204,6 +208,14 @@ var Index = &ice.Context{Name: "zsh", Help: "命令行", } }) }}, + "/ish": {Name: "/ish", Help: "命令", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + if sub, e := url.QueryUnescape(m.Option("sub")); m.Assert(e) { + m.Cmdy(kit.Split(sub)) + if len(m.Resultv()) == 0 { + m.Table() + } + } + }}, "/download": {Name: "/download", Help: "下载", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if len(arg) == 0 || arg[0] == "" { diff --git a/type.go b/type.go index 4cbf00ec..fafeecfe 100644 --- a/type.go +++ b/type.go @@ -1,3 +1,6 @@ +// icebergs: 后端 冰山架 挨撕不可 +// CMS: a cluster manager system + package ice import ( @@ -34,15 +37,16 @@ type Config struct { Value interface{} } type Command struct { - Name string - Help interface{} + Name interface{} // string []string + Help interface{} // string []string List []interface{} Meta map[string]interface{} Hand func(m *Message, c *Context, key string, arg ...string) } type Context struct { Name string - Help string + Help interface{} // string []string + Test interface{} // string []string Caches map[string]*Cache Configs map[string]*Config @@ -95,7 +99,7 @@ func (c *Context) Register(s *Context, x Server) *Context { if c.contexts == nil { c.contexts = map[string]*Context{} } - c.contexts[s.Name] = s + c.contexts[kit.Format(s.Name)] = s s.root = c.root s.context = c s.server = x