diff --git a/base/aaa/totp.go b/base/aaa/totp.go index 62f6bdc8..33b18d69 100644 --- a/base/aaa/totp.go +++ b/base/aaa/totp.go @@ -1,10 +1,6 @@ package aaa import ( - ice "github.com/shylinux/icebergs" - "github.com/shylinux/icebergs/base/mdb" - kit "github.com/shylinux/toolkits" - "bytes" "crypto/hmac" "crypto/sha1" @@ -13,6 +9,10 @@ import ( "math" "strings" "time" + + ice "github.com/shylinux/icebergs" + "github.com/shylinux/icebergs/base/mdb" + kit "github.com/shylinux/toolkits" ) func _totp_gen(per int64) string { @@ -47,8 +47,8 @@ func TOTP_GET(key string, num int, per int64) string { return _totp_get(key, num const ( SECRET = "secret" - NUMBER = "number" PERIOD = "period" + NUMBER = "number" ) const TOTP = "totp" @@ -61,47 +61,35 @@ func init() { }, Commands: map[string]*ice.Command{ TOTP: {Name: "totp name auto create", Help: "动态令牌", Action: map[string]*ice.Action{ - mdb.CREATE: {Name: "create user secret period=30 number=6", Help: "添加", Hand: func(m *ice.Message, arg ...string) { + mdb.CREATE: {Name: "create name secret period=30 number=6", Help: "添加", Hand: func(m *ice.Message, arg ...string) { if m.Option(SECRET) == "" { // 创建密钥 m.Option(SECRET, _totp_gen(kit.Int64(m.Option(PERIOD)))) } - // 添加密钥 - m.Cmd(mdb.INSERT, TOTP, "", mdb.HASH, kit.MDB_NAME, m.Option("user"), + m.Cmd(mdb.INSERT, TOTP, "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME), SECRET, m.Option(SECRET), PERIOD, m.Option(PERIOD), NUMBER, m.Option(NUMBER), ) - m.Cmdy("web.wiki.image", "qrcode", kit.Format(m.Conf(TOTP, "meta.link"), m.Option("user"), m.Option(SECRET))) + m.Option(ice.MSG_PROCESS, ice.PROCESS_REFRESH) }}, mdb.REMOVE: {Name: "remove", Help: "删除", Hand: func(m *ice.Message, arg ...string) { m.Cmdy(mdb.DELETE, TOTP, "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME)) }}, }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - if m.Option("_count") == "" { - m.Option("_count", "20") - } - if kit.Int(m.Option("_count")) > 0 { - m.Option(ice.MSG_PROCESS, "_refresh") - } + m.Fields(len(arg) == 0, "time,name,secret,period,number") + m.Cmd(mdb.SELECT, TOTP, "", mdb.HASH, kit.MDB_NAME, arg).Table(func(index int, value map[string]string, head []string) { + m.Push(kit.MDB_TIME, m.Time()) + m.Push(kit.MDB_NAME, value[kit.MDB_NAME]) - m.Option(mdb.FIELDS, kit.Select("time,name,secret,period,number", mdb.DETAIL, len(arg) > 0)) - if len(arg) == 0 { - // 密码列表 - m.Cmd(mdb.SELECT, TOTP, "", mdb.HASH).Table(func(index int, value map[string]string, head []string) { - per := kit.Int64(value[PERIOD]) - m.Push("time", m.Time()) - m.Push("rest", per-time.Now().Unix()%per) - m.Push("name", value["name"]) - m.Push("code", _totp_get(value[SECRET], kit.Int(value[NUMBER]), per)) - }) - m.PushAction(mdb.REMOVE) - m.Sort(kit.MDB_NAME) - return - } + period := kit.Int64(value[PERIOD]) + m.Push("rest", period-time.Now().Unix()%period) + m.Push("code", _totp_get(value[SECRET], kit.Int(value[NUMBER]), period)) - // 获取密码 - m.Cmd(mdb.SELECT, TOTP, "", mdb.HASH, kit.MDB_NAME, arg[0]).Table(func(index int, value map[string]string, head []string) { - m.Echo(_totp_get(value[SECRET], kit.Int(value[NUMBER]), kit.Int64(value[PERIOD]))) + if len(arg) > 0 { + m.PushQRCode("show", kit.Format(m.Conf(TOTP, kit.Keym(kit.MDB_LINK)), value[kit.MDB_NAME], value[SECRET])) + m.Echo(_totp_get(value[SECRET], kit.Int(value[NUMBER]), kit.Int64(value[PERIOD]))) + } }) + m.PushAction(mdb.REMOVE) }}, }, }) diff --git a/base/ctx/command.go b/base/ctx/command.go index 0db04c4e..3a45342a 100644 --- a/base/ctx/command.go +++ b/base/ctx/command.go @@ -1,75 +1,64 @@ package ctx import ( + "strings" + ice "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/mdb" kit "github.com/shylinux/toolkits" - - "sort" - "strings" ) +func _command_list(m *ice.Message, name string) { + if name == "" { // 命令列表 + for k, v := range m.Source().Commands { + if k[0] == '/' || k[0] == '_' { + continue // 内部命令 + } + + m.Push(kit.MDB_KEY, k) + m.Push(kit.MDB_NAME, v.Name) + m.Push(kit.MDB_HELP, v.Help) + } + m.Sort(kit.MDB_KEY) + return + } + + // 命令详情 + m.Spawn(m.Source()).Search(name, func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) { + m.Push(kit.MDB_KEY, s.Cap(ice.CTX_FOLLOW)) + m.Push(kit.MDB_NAME, kit.Format(cmd.Name)) + m.Push(kit.MDB_HELP, kit.Simple(cmd.Help)[0]) + m.Push(kit.MDB_META, kit.Formats(cmd.Meta)) + m.Push(kit.MDB_LIST, kit.Formats(cmd.List)) + }) +} func _command_search(m *ice.Message, kind, name, text string) { ice.Pulse.Travel(func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) { - if strings.HasPrefix(key, "_") || strings.HasPrefix(key, "/") { - return + if key[0] == '/' || key[0] == '_' { + return // 内部命令 } if name != "" && name != key && name != s.Name { return } - m.PushSearch(kit.SSH_CMD, COMMAND, - "context", s.Cap(ice.CTX_FOLLOW), "command", key, + m.PushSearch(kit.SSH_CMD, COMMAND, CONTEXT, s.Cap(ice.CTX_FOLLOW), COMMAND, key, kit.MDB_TYPE, kind, kit.MDB_NAME, key, kit.MDB_TEXT, s.Cap(ice.CTX_FOLLOW), ) }) } -func _command_list(m *ice.Message, name string) { - p := m.Spawn(m.Source()) - if name != "" { - // 命令详情 - p.Search(name, func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) { - m.Push(kit.MDB_KEY, s.Cap(ice.CTX_FOLLOW)) - m.Push(kit.MDB_NAME, kit.Format(cmd.Name)) - m.Push(kit.MDB_HELP, kit.Simple(cmd.Help)[0]) - m.Push(kit.MDB_META, kit.Formats(cmd.Meta)) - m.Push(kit.MDB_LIST, kit.Formats(cmd.List)) - }) - return - } - - list := []string{} - for k := range p.Target().Commands { - if k[0] == '/' || k[0] == '_' { - continue // 内部命令 - } - list = append(list, k) - } - sort.Strings(list) - - // 命令列表 - for _, k := range list { - v := p.Target().Commands[k] - m.Push(kit.MDB_KEY, k) - m.Push(kit.MDB_NAME, kit.Format(v.Name)) - m.Push(kit.MDB_HELP, kit.Simple(v.Help)[0]) - } -} const COMMAND = "command" func init() { - Index.Merge(&ice.Context{ - Commands: map[string]*ice.Command{ - COMMAND: {Name: "command key auto", Help: "命令", Action: map[string]*ice.Action{ - mdb.SEARCH: {Name: "search type name text", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { - if arg[0] == COMMAND { - _command_search(m, arg[0], arg[1], arg[2]) - } - }}, - }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - _command_list(m, strings.Join(arg, ".")) + Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ + COMMAND: {Name: "command key auto", Help: "命令", Action: map[string]*ice.Action{ + mdb.SEARCH: {Name: "search type name text", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { + if arg[0] == COMMAND { + _command_search(m, arg[0], arg[1], arg[2]) + } }}, - }, - }) + }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + _command_list(m, strings.Join(arg, ".")) + }}, + }}) } diff --git a/base/ctx/config.go b/base/ctx/config.go index 4efc5b8a..edf49f98 100644 --- a/base/ctx/config.go +++ b/base/ctx/config.go @@ -1,44 +1,39 @@ package ctx import ( - "sort" - - ice "github.com/shylinux/icebergs" - kit "github.com/shylinux/toolkits" - "encoding/json" "os" "path" "strings" + + ice "github.com/shylinux/icebergs" + kit "github.com/shylinux/toolkits" ) func _config_list(m *ice.Message) { - p := m.Spawn(m.Source()) - - list := []string{} - for k := range p.Target().Configs { + for k, v := range m.Source().Configs { if k[0] == '/' || k[0] == '_' { continue // 内部配置 } - list = append(list, k) - } - sort.Strings(list) - for _, k := range list { - v := p.Target().Configs[k] m.Push(kit.MDB_KEY, k) m.Push(kit.MDB_NAME, v.Name) m.Push(kit.MDB_VALUE, kit.Format(v.Value)) } + m.Sort(kit.MDB_KEY) } func _config_save(m *ice.Message, name string, arg ...string) { - msg := m.Spawn(m.Source()) - name = path.Join(msg.Conf(CONFIG, kit.META_PATH), name) + name = path.Join(m.Conf(CONFIG, kit.META_PATH), name) if f, p, e := kit.Create(name); m.Assert(e) { + defer f.Close() + + msg := m.Spawn(m.Source()) data := map[string]interface{}{} for _, k := range arg { data[k] = msg.Confv(k) } + + // 保存配置 if s, e := json.MarshalIndent(data, "", " "); m.Assert(e) { if n, e := f.Write(s); m.Assert(e) { m.Log_EXPORT(CONFIG, name, kit.MDB_FILE, p, kit.MDB_SIZE, n) @@ -48,43 +43,44 @@ func _config_save(m *ice.Message, name string, arg ...string) { } } func _config_load(m *ice.Message, name string, arg ...string) { - msg := m.Spawn(m.Source()) - name = path.Join(msg.Conf(CONFIG, kit.META_PATH), name) + name = path.Join(m.Conf(CONFIG, kit.META_PATH), name) if f, e := os.Open(name); e == nil { + defer f.Close() + + msg := m.Spawn(m.Source()) data := map[string]interface{}{} json.NewDecoder(f).Decode(&data) + // 加载配置 for k, v := range data { msg.Search(k, func(p *ice.Context, s *ice.Context, key string) { - // m.Log_IMPORT(CONFIG, kit.Keys(s.Name, key), kit.MDB_FILE, name) + m.Log_IMPORT(CONFIG, kit.Keys(s.Name, key), kit.MDB_FILE, name) s.Configs[key].Value = v }) } } } -func _config_make(m *ice.Message, chain string, arg ...string) { +func _config_make(m *ice.Message, key string, arg ...string) { msg := m.Spawn(m.Source()) if len(arg) > 1 { if strings.HasPrefix(arg[1], "@") { - msg.Conf(chain, arg[0], msg.Cmdx("nfs.cat", arg[1][1:])) - } else { - msg.Conf(chain, arg[0], kit.Parse(nil, "", arg[1:]...)) + arg[1] = msg.Cmdx("nfs.cat", arg[1][1:]) } + // 修改配置 + msg.Confv(key, arg[0], kit.Parse(nil, "", arg[1:]...)) } if len(arg) > 0 { - // 读取配置 - m.Echo(kit.Formats(msg.Confv(chain, arg[0]))) + m.Echo(kit.Formats(msg.Confv(key, arg[0]))) } else { - // 读取配置 - m.Echo(kit.Formats(msg.Confv(chain))) + m.Echo(kit.Formats(msg.Confv(key))) } } -func _config_rich(m *ice.Message, name string, key string, arg ...string) { - m.Rich(name, key, kit.Data(arg)) +func _config_rich(m *ice.Message, key string, sub string, arg ...string) { + m.Rich(key, sub, kit.Data(arg)) } -func _config_grow(m *ice.Message, name string, key string, arg ...string) { - m.Grow(name, key, kit.Dict(arg)) +func _config_grow(m *ice.Message, key string, sub string, arg ...string) { + m.Grow(key, sub, kit.Dict(arg)) } const ( @@ -98,7 +94,7 @@ const CONFIG = "config" func init() { Index.Merge(&ice.Context{ Configs: map[string]*ice.Config{ - CONFIG: {Name: "config", Help: "配置", Value: kit.Data(kit.MDB_PATH, "var/conf")}, + CONFIG: {Name: CONFIG, Help: "配置", Value: kit.Data(kit.MDB_PATH, "var/conf")}, }, Commands: map[string]*ice.Command{ CONFIG: {Name: "config key auto", Help: "配置", Action: map[string]*ice.Action{ diff --git a/base/ctx/context.go b/base/ctx/context.go index e22a8a0a..9515f5b6 100644 --- a/base/ctx/context.go +++ b/base/ctx/context.go @@ -17,23 +17,21 @@ func _context_list(m *ice.Message, all bool) { const CONTEXT = "context" func init() { - Index.Merge(&ice.Context{ - Commands: map[string]*ice.Command{ - CONTEXT: {Name: "context name=web.chat action=context,command,config key auto", Help: "模块", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Search(kit.Select("ice", arg, 0)+".", func(p *ice.Context, s *ice.Context, key string) { - msg := m.Spawn(s) - defer m.Copy(msg) + Index.Merge(&ice.Context{Commands: map[string]*ice.Command{ + CONTEXT: {Name: "context name=web.chat action=context,command,config key auto", Help: "模块", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Search(kit.Select("ice", arg, 0)+".", func(p *ice.Context, s *ice.Context, key string) { + msg := m.Spawn(s) + defer m.Copy(msg) - switch kit.Select(CONTEXT, arg, 1) { - case CONTEXT: - _context_list(msg, true) - case COMMAND: - msg.Cmdy(COMMAND, arg[2:]) - case CONFIG: - msg.Cmdy(CONFIG, arg[2:]) - } - }) - }}, - }, - }) + switch kit.Select(CONTEXT, arg, 1) { + case CONTEXT: + _context_list(msg, true) + case COMMAND: + msg.Cmdy(COMMAND, arg[2:]) + case CONFIG: + msg.Cmdy(CONFIG, arg[2:]) + } + }) + }}, + }}) } diff --git a/base/ctx/ctx.go b/base/ctx/ctx.go index 7854fd5f..d7ddf789 100644 --- a/base/ctx/ctx.go +++ b/base/ctx/ctx.go @@ -5,13 +5,13 @@ import ( "github.com/shylinux/icebergs/base/mdb" ) -var Index = &ice.Context{Name: "ctx", Help: "配置模块", - Commands: map[string]*ice.Command{ - ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { - m.Cmd(mdb.SEARCH, mdb.CREATE, COMMAND, m.Prefix(COMMAND)) - }}, - ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}}, - }, -} +const CTX = "ctx" + +var Index = &ice.Context{Name: CTX, Help: "标准模块", Commands: map[string]*ice.Command{ + ice.CTX_INIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { + m.Cmd(mdb.SEARCH, mdb.CREATE, COMMAND, m.Prefix(COMMAND)) + }}, + ice.CTX_EXIT: {Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {}}, +}} func init() { ice.Index.Register(Index, nil, CONTEXT, COMMAND, CONFIG) } diff --git a/misc.go b/misc.go index 21a6ce70..edfbc203 100644 --- a/misc.go +++ b/misc.go @@ -114,11 +114,9 @@ func (m *Message) PushSearchWeb(cmd string, name string) { } func Render(m *Message, cmd string, args ...interface{}) string { - m.Debug("waht %v", cmd) if m.Option(MSG_USERUA) == "" || strings.Contains(m.Option(MSG_USERUA), "curl") { return "" } - m.Debug("waht %v", cmd) switch arg := kit.Simple(args...); cmd { case RENDER_DOWNLOAD: // [name] file