1
0
mirror of https://shylinux.com/x/icebergs synced 2025-04-26 17:44:05 +08:00
This commit is contained in:
harveyshao 2021-04-22 22:08:51 +08:00
parent 8c3f7d6124
commit f9cd249a7e
6 changed files with 111 additions and 142 deletions

View File

@ -1,10 +1,6 @@
package aaa package aaa
import ( import (
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/mdb"
kit "github.com/shylinux/toolkits"
"bytes" "bytes"
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
@ -13,6 +9,10 @@ import (
"math" "math"
"strings" "strings"
"time" "time"
ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/mdb"
kit "github.com/shylinux/toolkits"
) )
func _totp_gen(per int64) string { 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 ( const (
SECRET = "secret" SECRET = "secret"
NUMBER = "number"
PERIOD = "period" PERIOD = "period"
NUMBER = "number"
) )
const TOTP = "totp" const TOTP = "totp"
@ -61,47 +61,35 @@ func init() {
}, },
Commands: map[string]*ice.Command{ Commands: map[string]*ice.Command{
TOTP: {Name: "totp name auto create", Help: "动态令牌", Action: map[string]*ice.Action{ 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) == "" { // 创建密钥 if m.Option(SECRET) == "" { // 创建密钥
m.Option(SECRET, _totp_gen(kit.Int64(m.Option(PERIOD)))) m.Option(SECRET, _totp_gen(kit.Int64(m.Option(PERIOD))))
} }
// 添加密钥 m.Cmd(mdb.INSERT, TOTP, "", mdb.HASH, kit.MDB_NAME, m.Option(kit.MDB_NAME),
m.Cmd(mdb.INSERT, TOTP, "", mdb.HASH, kit.MDB_NAME, m.Option("user"),
SECRET, m.Option(SECRET), PERIOD, m.Option(PERIOD), NUMBER, m.Option(NUMBER), 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) { 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)) 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) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
if m.Option("_count") == "" { m.Fields(len(arg) == 0, "time,name,secret,period,number")
m.Option("_count", "20") 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())
if kit.Int(m.Option("_count")) > 0 { m.Push(kit.MDB_NAME, value[kit.MDB_NAME])
m.Option(ice.MSG_PROCESS, "_refresh")
}
m.Option(mdb.FIELDS, kit.Select("time,name,secret,period,number", mdb.DETAIL, len(arg) > 0)) period := kit.Int64(value[PERIOD])
if len(arg) == 0 { 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).Table(func(index int, value map[string]string, head []string) {
per := kit.Int64(value[PERIOD]) if len(arg) > 0 {
m.Push("time", m.Time()) m.PushQRCode("show", kit.Format(m.Conf(TOTP, kit.Keym(kit.MDB_LINK)), value[kit.MDB_NAME], value[SECRET]))
m.Push("rest", per-time.Now().Unix()%per) m.Echo(_totp_get(value[SECRET], kit.Int(value[NUMBER]), kit.Int64(value[PERIOD])))
m.Push("name", value["name"]) }
m.Push("code", _totp_get(value[SECRET], kit.Int(value[NUMBER]), per))
}) })
m.PushAction(mdb.REMOVE) m.PushAction(mdb.REMOVE)
m.Sort(kit.MDB_NAME)
return
}
// 获取密码
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])))
})
}}, }},
}, },
}) })

View File

@ -1,66 +1,56 @@
package ctx package ctx
import ( import (
"strings"
ice "github.com/shylinux/icebergs" ice "github.com/shylinux/icebergs"
"github.com/shylinux/icebergs/base/mdb" "github.com/shylinux/icebergs/base/mdb"
kit "github.com/shylinux/toolkits" kit "github.com/shylinux/toolkits"
"sort"
"strings"
) )
func _command_search(m *ice.Message, kind, name, text string) { func _command_list(m *ice.Message, name string) {
ice.Pulse.Travel(func(p *ice.Context, s *ice.Context, key string, cmd *ice.Command) { if name == "" { // 命令列表
if strings.HasPrefix(key, "_") || strings.HasPrefix(key, "/") { for k, v := range m.Source().Commands {
return if k[0] == '/' || k[0] == '_' {
continue // 内部命令
} }
if name != "" && name != key && name != s.Name {
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 return
} }
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.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_KEY, s.Cap(ice.CTX_FOLLOW))
m.Push(kit.MDB_NAME, kit.Format(cmd.Name)) m.Push(kit.MDB_NAME, kit.Format(cmd.Name))
m.Push(kit.MDB_HELP, kit.Simple(cmd.Help)[0]) m.Push(kit.MDB_HELP, kit.Simple(cmd.Help)[0])
m.Push(kit.MDB_META, kit.Formats(cmd.Meta)) m.Push(kit.MDB_META, kit.Formats(cmd.Meta))
m.Push(kit.MDB_LIST, kit.Formats(cmd.List)) 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 key[0] == '/' || key[0] == '_' {
return // 内部命令
}
if name != "" && name != key && name != s.Name {
return return
} }
list := []string{} m.PushSearch(kit.SSH_CMD, COMMAND, CONTEXT, s.Cap(ice.CTX_FOLLOW), COMMAND, key,
for k := range p.Target().Commands { kit.MDB_TYPE, kind, kit.MDB_NAME, key, kit.MDB_TEXT, s.Cap(ice.CTX_FOLLOW),
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" const COMMAND = "command"
func init() { func init() {
Index.Merge(&ice.Context{ Index.Merge(&ice.Context{Commands: map[string]*ice.Command{
Commands: map[string]*ice.Command{
COMMAND: {Name: "command key auto", Help: "命令", Action: map[string]*ice.Action{ 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) { mdb.SEARCH: {Name: "search type name text", Help: "搜索", Hand: func(m *ice.Message, arg ...string) {
if arg[0] == COMMAND { if arg[0] == COMMAND {
@ -70,6 +60,5 @@ func init() {
}, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { }, Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) {
_command_list(m, strings.Join(arg, ".")) _command_list(m, strings.Join(arg, "."))
}}, }},
}, }})
})
} }

View File

@ -1,44 +1,39 @@
package ctx package ctx
import ( import (
"sort"
ice "github.com/shylinux/icebergs"
kit "github.com/shylinux/toolkits"
"encoding/json" "encoding/json"
"os" "os"
"path" "path"
"strings" "strings"
ice "github.com/shylinux/icebergs"
kit "github.com/shylinux/toolkits"
) )
func _config_list(m *ice.Message) { func _config_list(m *ice.Message) {
p := m.Spawn(m.Source()) for k, v := range m.Source().Configs {
list := []string{}
for k := range p.Target().Configs {
if k[0] == '/' || k[0] == '_' { if k[0] == '/' || k[0] == '_' {
continue // 内部配置 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_KEY, k)
m.Push(kit.MDB_NAME, v.Name) m.Push(kit.MDB_NAME, v.Name)
m.Push(kit.MDB_VALUE, kit.Format(v.Value)) m.Push(kit.MDB_VALUE, kit.Format(v.Value))
} }
m.Sort(kit.MDB_KEY)
} }
func _config_save(m *ice.Message, name string, arg ...string) { func _config_save(m *ice.Message, name string, arg ...string) {
msg := m.Spawn(m.Source()) name = path.Join(m.Conf(CONFIG, kit.META_PATH), name)
name = path.Join(msg.Conf(CONFIG, kit.META_PATH), name)
if f, p, e := kit.Create(name); m.Assert(e) { if f, p, e := kit.Create(name); m.Assert(e) {
defer f.Close()
msg := m.Spawn(m.Source())
data := map[string]interface{}{} data := map[string]interface{}{}
for _, k := range arg { for _, k := range arg {
data[k] = msg.Confv(k) data[k] = msg.Confv(k)
} }
// 保存配置
if s, e := json.MarshalIndent(data, "", " "); m.Assert(e) { if s, e := json.MarshalIndent(data, "", " "); m.Assert(e) {
if n, e := f.Write(s); 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) 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) { func _config_load(m *ice.Message, name string, arg ...string) {
msg := m.Spawn(m.Source()) name = path.Join(m.Conf(CONFIG, kit.META_PATH), name)
name = path.Join(msg.Conf(CONFIG, kit.META_PATH), name)
if f, e := os.Open(name); e == nil { if f, e := os.Open(name); e == nil {
defer f.Close()
msg := m.Spawn(m.Source())
data := map[string]interface{}{} data := map[string]interface{}{}
json.NewDecoder(f).Decode(&data) json.NewDecoder(f).Decode(&data)
// 加载配置
for k, v := range data { for k, v := range data {
msg.Search(k, func(p *ice.Context, s *ice.Context, key string) { 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 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()) msg := m.Spawn(m.Source())
if len(arg) > 1 { if len(arg) > 1 {
if strings.HasPrefix(arg[1], "@") { if strings.HasPrefix(arg[1], "@") {
msg.Conf(chain, arg[0], msg.Cmdx("nfs.cat", arg[1][1:])) arg[1] = msg.Cmdx("nfs.cat", arg[1][1:])
} else {
msg.Conf(chain, arg[0], kit.Parse(nil, "", arg[1:]...))
} }
// 修改配置
msg.Confv(key, arg[0], kit.Parse(nil, "", arg[1:]...))
} }
if len(arg) > 0 { if len(arg) > 0 {
// 读取配置 m.Echo(kit.Formats(msg.Confv(key, arg[0])))
m.Echo(kit.Formats(msg.Confv(chain, arg[0])))
} else { } else {
// 读取配置 m.Echo(kit.Formats(msg.Confv(key)))
m.Echo(kit.Formats(msg.Confv(chain)))
} }
} }
func _config_rich(m *ice.Message, name string, key string, arg ...string) { func _config_rich(m *ice.Message, key string, sub string, arg ...string) {
m.Rich(name, key, kit.Data(arg)) m.Rich(key, sub, kit.Data(arg))
} }
func _config_grow(m *ice.Message, name string, key string, arg ...string) { func _config_grow(m *ice.Message, key string, sub string, arg ...string) {
m.Grow(name, key, kit.Dict(arg)) m.Grow(key, sub, kit.Dict(arg))
} }
const ( const (
@ -98,7 +94,7 @@ const CONFIG = "config"
func init() { func init() {
Index.Merge(&ice.Context{ Index.Merge(&ice.Context{
Configs: map[string]*ice.Config{ 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{ Commands: map[string]*ice.Command{
CONFIG: {Name: "config key auto", Help: "配置", Action: map[string]*ice.Action{ CONFIG: {Name: "config key auto", Help: "配置", Action: map[string]*ice.Action{

View File

@ -17,8 +17,7 @@ func _context_list(m *ice.Message, all bool) {
const CONTEXT = "context" const CONTEXT = "context"
func init() { func init() {
Index.Merge(&ice.Context{ Index.Merge(&ice.Context{Commands: map[string]*ice.Command{
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) { 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) { m.Search(kit.Select("ice", arg, 0)+".", func(p *ice.Context, s *ice.Context, key string) {
msg := m.Spawn(s) msg := m.Spawn(s)
@ -34,6 +33,5 @@ func init() {
} }
}) })
}}, }},
}, }})
})
} }

View File

@ -5,13 +5,13 @@ import (
"github.com/shylinux/icebergs/base/mdb" "github.com/shylinux/icebergs/base/mdb"
) )
var Index = &ice.Context{Name: "ctx", Help: "配置模块", const CTX = "ctx"
Commands: map[string]*ice.Command{
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) { 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)) 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) {}}, 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) } func init() { ice.Index.Register(Index, nil, CONTEXT, COMMAND, CONFIG) }

View File

@ -114,11 +114,9 @@ func (m *Message) PushSearchWeb(cmd string, name string) {
} }
func Render(m *Message, cmd string, args ...interface{}) 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") { if m.Option(MSG_USERUA) == "" || strings.Contains(m.Option(MSG_USERUA), "curl") {
return "" return ""
} }
m.Debug("waht %v", cmd)
switch arg := kit.Simple(args...); cmd { switch arg := kit.Simple(args...); cmd {
case RENDER_DOWNLOAD: // [name] file case RENDER_DOWNLOAD: // [name] file