From ec4540e8ee8b4504b10dbab4ab65d5294dee5e2b Mon Sep 17 00:00:00 2001 From: shaoying Date: Mon, 25 Mar 2019 01:04:18 +0800 Subject: [PATCH] opt aaa Change-Id: Ice106fec6aa5077cf5a527f026c673bce2b91b7b --- src/contexts/aaa/aaa.go | 893 ++++++++++++++++------------------- src/contexts/cli/cli.go | 5 - src/contexts/ctx/ctx_cgi.go | 11 +- src/contexts/ctx/ctx_init.go | 6 +- src/contexts/ctx/ctx_type.go | 19 +- src/contexts/nfs/nfs.go | 9 +- src/contexts/ssh/ssh.go | 26 +- src/contexts/web/web.go | 101 ++-- src/examples/code/code.go | 9 +- src/toolkit/kit.go | 27 ++ usr/librarys/code.js | 31 +- usr/librarys/context.js | 8 +- usr/template/code/code.tmpl | 5 +- 13 files changed, 533 insertions(+), 617 deletions(-) diff --git a/src/contexts/aaa/aaa.go b/src/contexts/aaa/aaa.go index 9f31ad40..75bc4ee3 100644 --- a/src/contexts/aaa/aaa.go +++ b/src/contexts/aaa/aaa.go @@ -124,14 +124,10 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", "auth": &ctx.Config{Name: "auth", Value: map[string]interface{}{}, Help: "散列"}, "auth_expire": &ctx.Config{Name: "auth_expire", Value: "10m", Help: "权限超时"}, "auth_type": &ctx.Config{Name: "auth_type", Value: map[string]interface{}{ - "session": map[string]interface{}{"unique": true}, - "bench": map[string]interface{}{"unique": true}, - "cert": map[string]interface{}{"public": true}, - "username": map[string]interface{}{"public": true}, - "userrole": map[string]interface{}{"public": true}, - "password": map[string]interface{}{"secrete": true, "single": true}, - "uuid": map[string]interface{}{"secrete": true, "single": true}, - "ppid": map[string]interface{}{"secrete": true, "single": true}, + "unique": map[string]interface{}{"session": true, "bench": true}, + "public": map[string]interface{}{"userrole": true, "username": true, "cert": true}, + "single": map[string]interface{}{"password": true, "token": true, "uuid": true, "ppid": true}, + "secrete": map[string]interface{}{"password": true, "token": true, "uuid": true, "ppid": true}, }, Help: "散列"}, "secrete_key": &ctx.Config{Name: "secrete_key", Value: map[string]interface{}{"password": 1, "uuid": 1}, Help: "私钥文件"}, @@ -160,472 +156,468 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", m.Echo(hs) return }}, - "auth": &ctx.Command{Name: "auth [id] [[ship] type [meta]] [[data] key [val]] [[node] key [val]]", Help: "权限区块链", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if len(arg) == 0 { // 节点列表 - m.Confm("auth", func(key string, node map[string]interface{}) { - up := false - if ship, ok := node["ship"].(map[string]interface{}); ok { - for k, v := range ship { - val := v.(map[string]interface{}) - switch val["ship"].(string) { - case "0": - if !up { - up = true - m.Add("append", "up_key", k) - m.Add("append", "up_type", val["type"]) + "auth": &ctx.Command{Name: "auth [id] [delete data|ship|node] [[ship] type [meta]] [[data] key [val]] [[node] key [val]]", + Help: "权限区块链", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) == 0 { // 节点列表 + m.Confm("auth", func(key string, node map[string]interface{}) { + up := false + if ship, ok := node["ship"].(map[string]interface{}); ok { + for k, v := range ship { + val := v.(map[string]interface{}) + switch val["ship"].(string) { + case "0": + if !up { + up = true + m.Add("append", "up_key", k) + m.Add("append", "up_type", val["type"]) + m.Add("append", "up_ship", val["ship"]) + } } } } - } - if !up { - m.Add("append", "up_key", "") - m.Add("append", "up_type", "") - } - m.Add("append", "key", key) - m.Add("append", "type", node["type"]) - m.Add("append", "meta", node["meta"]) - }) - m.Table() - return - } - - s, t, a := "", "", "" - if v := m.Confm("auth", arg[0]); v != nil { - s, t, a, arg = arg[0], kit.Format(v["type"]), kit.Format(v["meta"]), arg[1:] - } - - if len(arg) > 0 && arg[0] == "delete" { - switch arg[1] { - case "data": - if data := m.Confm("auth", []string{s, "data"}); data != nil { - for _, k := range arg[2:] { - m.Log("info", "delete data %s %s %v", s, k, kit.Formats(data[k])) - delete(data, k) - } - } - case "ship": - if ship := m.Confm("auth", []string{s, "ship"}); ship != nil { - for _, k := range arg[2:] { - if val, ok := ship[k].(map[string]interface{}); ok { - m.Add("append", "key", k) - m.Add("append", "ship", val["ship"]) - m.Add("append", "type", val["type"]) - m.Add("append", "meta", val["meta"]) - } - - m.Log("info", "delete ship %s %s %v", s, k, kit.Formats(ship[k])) - delete(ship, k) - if peer := m.Confm("auth", []string{k, "ship"}); peer != nil { - m.Log("info", "delete ship %s %s %v", k, s, kit.Formats(peer[s])) - delete(peer, s) - } - } - m.Table() - } - case "node": - if ship := m.Confm("auth", []string{s, "ship"}); ship != nil { - for k, _ := range ship { - if val, ok := ship[k].(map[string]interface{}); ok { - m.Add("append", "key", k) - m.Add("append", "ship", val["ship"]) - m.Add("append", "type", val["type"]) - m.Add("append", "meta", val["meta"]) - } - - m.Log("info", "delete ship %s %s %v", s, k, kit.Formats(ship[k])) - delete(ship, k) - if peer := m.Confm("auth", []string{k, "ship"}); peer != nil { - m.Log("info", "delete ship %s %s %v", k, s, kit.Formats(peer[s])) - delete(peer, s) - } - } - m.Log("info", "delete node %s %v", s, kit.Formats(m.Confm("auth", s))) - delete(m.Confm("auth"), s) - m.Table() - } - } - return - } - - if len(arg) == 0 { // 查看节点 - m.Echo(t) - return - } - - p, route, block, chain := s, "ship", []map[string]string{}, []map[string]string{} - for i := 0; i < len(arg); i += 2 { - if p == "" { - m.Confm("auth", func(k string, node map[string]interface{}) { - if strings.HasSuffix(k, arg[i]) || strings.HasPrefix(k, arg[i]) { - arg[i] = k - } - }) - } else { - m.Confm("auth", []string{p, "ship"}, func(k string, ship map[string]interface{}) { - if strings.HasSuffix(k, arg[i]) || strings.HasPrefix(k, arg[i]) { - arg[i] = k + if !up { + m.Add("append", "up_key", "") + m.Add("append", "up_type", "") + m.Add("append", "up_ship", "") } + m.Add("append", "key", key) + m.Add("append", "type", node["type"]) + m.Add("append", "meta", node["meta"]) }) + m.Table() + return } - if node := m.Confm("auth", arg[i]); node != nil { - if i++; p != "" { // 添加链接 - d, e := time.ParseDuration(m.Conf("auth_expire")) - m.Assert(e) - expire := time.Now().Add(d).Unix() - m.Confv("auth", []string{p, "ship", arg[i-1]}, map[string]interface{}{ - "create_time": m.Time(), "type": node["type"], "meta": node["meta"], "ship": "4", "expire_time": expire, - }) - - m.Confv("auth", []string{arg[i-1], "ship", p}, map[string]interface{}{ - "create_time": m.Time(), "type": t, "meta": a, "ship": "5", "expire_time": expire, - }) - - } - p, t, a = arg[i-1], node["type"].(string), node["meta"].(string) + s, t, a := "", "", "" + if v := m.Confm("auth", arg[0]); v != nil { + s, t, a, arg = arg[0], kit.Format(v["type"]), kit.Format(v["meta"]), arg[1:] + } + if len(arg) == 0 { // 查看节点 + m.Echo(t) + return } - if i < len(arg) { - switch arg[i] { // 切换类型 - case "data", "node", "ship": - route, i = arg[i], i+1 - } - } - - if p == "" && route != "ship" { - break - } - - switch route { - case "ship": // 链接操作 - if i > len(arg)-1 { - m.Confm("auth", []string{p, "ship"}, func(k string, ship map[string]interface{}) { - if node := m.Confm("auth", k); node != nil { - m.Add("append", "key", k) - m.Add("append", "ship", ship["ship"]) - m.Add("append", "type", node["type"]) - m.Add("append", "meta", node["meta"]) - m.Add("append", "create_time", node["create_time"]) - } - }) - m.Set("result").Table() - break - } else if i == len(arg)-1 { // 读取链接 - if p == "" { - m.Confm("auth", func(k string, node map[string]interface{}) { - if node["type"].(string) == arg[i] || strings.HasSuffix(k, arg[i]) || strings.HasPrefix(k, arg[i]) { - m.Add("append", "key", k) - m.Add("append", "type", node["type"]) - m.Add("append", "meta", node["meta"]) - m.Add("append", "create_time", node["create_time"]) - } + p, route, block, chain := s, "ship", []map[string]string{}, []map[string]string{} + for i := 0; i < len(arg); i += 2 { + if node := m.Confm("auth", arg[i]); node != nil { + if i++; p != "" { // 朋友链接 + expire := kit.Int(m.Time(m.Conf("auth_expire"), "stamp")) + m.Confv("auth", []string{arg[i-1], "ship", p}, map[string]interface{}{ + "create_time": m.Time(), "expire_time": expire, + "type": t, "meta": a, "ship": "5", }) - } else { - if node := m.Confm("auth", []string{arg[i]}); node != nil { - m.Confv("auth", []string{p, "ship", arg[i]}, node) - } - - m.Confm("auth", []string{p, "ship"}, func(k string, ship map[string]interface{}) { - if node := m.Confm("auth", k); ship["type"].(string) == arg[i] || strings.HasSuffix(k, arg[i]) || strings.HasPrefix(k, arg[i]) { - m.Add("append", "key", k) - m.Add("append", "ship", ship["ship"]) - m.Add("append", "type", node["type"]) - m.Add("append", "meta", node["meta"]) - m.Add("append", "create_time", node["create_time"]) - } + m.Confv("auth", []string{p, "ship", arg[i-1]}, map[string]interface{}{ + "create_time": m.Time(), "expire_time": expire, + "type": node["type"], "meta": node["meta"], "ship": "4", }) } - m.Set("result").Table() + p, t, a = arg[i-1], node["type"].(string), node["meta"].(string) + } + + if i < len(arg) { + switch arg[i] { // 切换类型 + case "data", "node", "ship": + route, i = arg[i], i+1 + } + } + if p == "" && route != "ship" { return - } else if i == len(arg)-2 { - if p != "" && arg[i] == "session" { - m.Confm("auth", []string{p, "ship"}, func(k string, ship map[string]interface{}) { - if node := m.Confm("auth", k); ship["type"].(string) == arg[i] && (ship["meta"] == arg[i+1] || strings.HasSuffix(k, arg[i+1]) || strings.HasPrefix(k, arg[i+1])) { - m.Add("append", "key", k) - m.Add("append", "ship", ship["ship"]) - m.Add("append", "type", node["type"]) - m.Add("append", "meta", node["meta"]) - m.Add("append", "create_time", node["create_time"]) + } + + switch route { + case "ship": // 链接操作 + if i >= len(arg)-1 { + if p == "" { // 节点列表 + m.Confm("auth", func(k string, node map[string]interface{}) { + if i > len(arg)-1 || node["type"].(string) == arg[i] || strings.HasSuffix(k, arg[i]) || strings.HasPrefix(k, arg[i]) { + m.Add("append", "create_time", node["create_time"]) + m.Add("append", "key", k) + m.Add("append", "type", node["type"]) + m.Add("append", "meta", node["meta"]) + } + }) + } else { // 链接列表 + if i == len(arg)-1 { + m.Confm("auth", []string{arg[i]}, func(node map[string]interface{}) { + m.Confv("auth", []string{p, "ship", arg[i]}, node) + }) } - }) - m.Set("result").Table() + + m.Confm("auth", []string{p, "ship"}, func(k string, ship map[string]interface{}) { + if node := m.Confm("auth", k); i > len(arg)-1 || ship["type"].(string) == arg[i] || strings.HasSuffix(k, arg[i]) || strings.HasPrefix(k, arg[i]) { + m.Add("append", "create_time", node["create_time"]) + m.Add("append", "key", k) + m.Add("append", "ship", ship["ship"]) + m.Add("append", "type", node["type"]) + m.Add("append", "meta", node["meta"]) + } + }) + } + m.Sort("create_time", "time_r").Set("result").Table() return } - } - if arg[i] == "check" { - has := "false" - m.Confm("auth", []string{p, "ship"}, func(k string, ship map[string]interface{}) { - if i == len(arg)-2 && (ship["meta"] != arg[i+1] || k != arg[i+1]) { - return - } - if i == len(arg)-3 && (ship["type"] != arg[i+1] || ship["meta"] != arg[i+2]) { - return - } - - if ship["expire_time"] == nil || int64(kit.Int(ship["expire_time"])) > time.Now().Unix() { - has = k - } - }) - m.Set("result").Echo(has) - return - } - - meta := []string{arg[i]} - if m.Confs("auth_type", []string{arg[i], "secrete"}) { - meta = append(meta, Password(arg[i+1])) // 加密节点 - } else { - meta = append(meta, arg[i+1]) - } - if t != "session" && !m.Confs("auth_type", []string{arg[i], "public"}) { - meta = append(meta, p) // 私有节点 - } - if m.Confs("auth_type", []string{arg[i], "unique"}) { - meta = append(meta, "time", "rand") // 惟一节点 - } - - h := m.Cmdx("aaa.hash", meta) - if !m.Confs("auth", h) { - if m.Confs("auth_type", []string{arg[i], "single"}) && m.Confs("auth", p) && m.Cmds("aaa.auth", p, arg[i]) { - m.Set("result") - return // 单点认证失败 - } - - // 创建节点 - block = append(block, map[string]string{"hash": h, "type": arg[i], "meta": meta[1]}) - } - - if s != "" { // 创建根链接 - chain = append(chain, map[string]string{"node": s, "ship": "3", "hash": h, "type": arg[i], "meta": meta[1]}) - chain = append(chain, map[string]string{"node": h, "ship": "2", "hash": s, "type": arg[i], "meta": meta[1]}) - } - if p != "" { // 创建父链接 - chain = append(chain, map[string]string{"node": p, "ship": "1", "hash": h, "type": arg[i], "meta": meta[1]}) - chain = append(chain, map[string]string{"node": h, "ship": "0", "hash": p, "type": t, "meta": a}) - } - - p, t, a = h, arg[i], meta[1] - m.Echo(h) - case "node": // 节点操作 - if i > len(arg)-1 { // 查看节点 - m.Cmdy("aaa.config", "auth", p) - return - } else if i == len(arg)-1 { // 查询节点 - ps := []string{p} - for j := 0; j < len(ps); j++ { - if value := m.Confv("auth", []string{ps[j], arg[i]}); value != nil { - m.Put("option", "data", value).Cmdy("ctx.trans", "data") - break - } - - m.Confm("auth", []string{ps[j], "ship"}, func(key string, ship map[string]interface{}) { - if ship["ship"] != "0" { - ps = append(ps, key) + // 删除链接 + if arg[i] == "delete" { + m.Confm("auth", []string{p, "ship"}, func(ship map[string]interface{}) { + for _, k := range arg[i+1:] { + m.Confm("auth", []string{k, "ship"}, func(peer map[string]interface{}) { + m.Log("info", "delete peer %s %s %s", k, s, kit.Formats(peer[s])) + delete(peer, s) + }) + m.Log("info", "delete ship %s %s %s", s, k, kit.Formats(ship[k])) + delete(ship, k) } }) + return } - return - } else { // 修改节点 - m.Confv("auth", []string{p, arg[i]}, arg[i+1]) - } - case "data": // 数据操作 - if i > len(arg)-1 { // 查看数据 - m.Cmdy("ctx.config", "auth", strings.Join([]string{p, "data"}, ".")) - return - } else if i == len(arg)-1 { // 相询数据 - ps := []string{p} - for j := 0; j < len(ps); j++ { - if value := m.Confv("auth", []string{ps[j], "data", arg[i]}); value != nil { - m.Set("append").Set("result").Put("option", "data", value).Cmdy("ctx.trans", "data") - break - } - m.Confm("auth", []string{ps[j], "ship"}, func(key string, ship map[string]interface{}) { - if ship["ship"] != "0" { - ps = append(ps, key) + // 检查链接 + if arg[i] == "check" { + has := "false" + m.Confm("auth", []string{p, "ship"}, func(k string, ship map[string]interface{}) { + if i == len(arg)-2 && (ship["meta"] != arg[i+1] && k != arg[i+1]) { + return + } + if i == len(arg)-3 && (ship["type"] != arg[i+1] || ship["meta"] != arg[i+2]) { + return + } + + if ship["expire_time"] == nil || int64(kit.Int(ship["expire_time"])) > time.Now().Unix() { + has = k } }) + m.Set("result").Echo(has) + return } - return - } else { // 修改数据 - if arg[i] == "option" { - m.Confv("auth", []string{p, "data", arg[i+1]}, m.Optionv(arg[i+1])) + + meta := []string{arg[i]} + + // 加密节点 + if m.Confs("auth_type", []string{"secrete", arg[i]}) { + meta = append(meta, Password(arg[i+1])) } else { - m.Confv("auth", []string{p, "data", arg[i]}, arg[i+1]) + meta = append(meta, arg[i+1]) } + // 私有节点 + if !m.Confs("auth_type", []string{"public", arg[i]}) { + if m.Confs("auth_type", []string{"unique", arg[i]}) { + meta = append(meta, "uniq") + } else { + meta = append(meta, p) + } + } + + // h := m.Cmdx("aaa.hash", meta) + h, _ := kit.Hash(meta) + if !m.Confs("auth", h) { + m.Set("result") + if m.Confs("auth_type", []string{"single", arg[i]}) && m.Confs("auth", p) && m.Cmds("aaa.auth", p, arg[i]) { + m.Log("fuck", "password %s", h) + return // 单点认证失败 + } + + // 创建节点 + block = append(block, map[string]string{"hash": h, "type": arg[i], "meta": meta[1]}) + m.Echo(h) + } + + if s != "" { // 祖孙链接 + chain = append(chain, map[string]string{"node": s, "ship": "3", "hash": h, "type": arg[i], "meta": meta[1]}) + chain = append(chain, map[string]string{"node": h, "ship": "2", "hash": s, "type": arg[i], "meta": meta[1]}) + } + if p != "" { // 父子链接 + chain = append(chain, map[string]string{"node": p, "ship": "1", "hash": h, "type": arg[i], "meta": meta[1]}) + chain = append(chain, map[string]string{"node": h, "ship": "0", "hash": p, "type": t, "meta": a}) + } + + p, t, a = h, arg[i], meta[1] + m.Echo(h) + case "node": // 节点操作 + if i > len(arg)-1 { // 查看节点 + m.Cmdy("aaa.config", "auth", p) + } else if arg[i] == "delete" { // 删除节点 + m.Confm("auth", []string{p, "ship"}, func(ship map[string]interface{}) { + for k, _ := range ship { + m.Confm("auth", []string{k, "ship"}, func(peer map[string]interface{}) { + m.Log("info", "delete peer %s %s %s", k, s, kit.Formats(peer[s])) + delete(peer, s) + }) + m.Log("info", "delete ship %s %s %s", s, k, kit.Formats(ship[k])) + delete(ship, k) + } + m.Log("info", "delete node %s %s", s, kit.Formats(m.Confm("auth", s))) + delete(m.Confm("auth"), s) + }) + } else if i < len(arg)-1 { // 修改属性 + m.Confv("auth", []string{p, arg[i]}, arg[i+1]) + } else { // 搜索属性 + ps := []string{p} + for j := 0; j < len(ps); j++ { + if value := m.Confv("auth", []string{ps[j], arg[i]}); value != nil { + m.Put("option", "data", value).Cmdy("ctx.trans", "data") + break + } + m.Confm("auth", []string{ps[j], "ship"}, func(key string, ship map[string]interface{}) { + if ship["ship"] != "0" { + ps = append(ps, key) + } + }) + } + } + return + case "data": // 数据操作 + if i > len(arg)-1 { // 查看数据 + m.Cmdy("ctx.config", "auth", strings.Join([]string{p, "data"}, ".")) + } else if arg[i] == "delete" { // 删除数据 + m.Confm("auth", []string{s, "data"}, func(data map[string]interface{}) { + for _, k := range arg[i+1:] { + m.Log("info", "delete data %s %s %s", s, k, kit.Formats(data[k])) + delete(data, k) + } + }) + } else if i < len(arg)-1 { // 修改数据 + if arg[i] == "option" { + m.Confv("auth", []string{p, "data", arg[i+1]}, m.Optionv(arg[i+1])) + } else { + m.Confv("auth", []string{p, "data", arg[i]}, arg[i+1]) + } + m.Echo(arg[i+1]) + } else { // 搜索数据 + ps := []string{p} + for j := 0; j < len(ps); j++ { + if value := m.Confv("auth", []string{ps[j], "data", arg[i]}); value != nil { + m.Set("append").Set("result").Put("option", "data", value).Cmdy("ctx.trans", "data") + break + } + m.Confm("auth", []string{ps[j], "ship"}, func(key string, ship map[string]interface{}) { + if ship["ship"] != "0" { + ps = append(ps, key) + } + }) + } + } + return } } - } - m.Log("debug", "block: %v chain: %v", len(block), len(chain)) - for _, b := range block { // 添加节点 - m.Confv("auth", b["hash"], map[string]interface{}{"create_time": m.Time(), "type": b["type"], "meta": b["meta"]}) - } - for _, c := range chain { // 添加链接 - m.Confv("auth", []interface{}{c["node"], "ship", c["hash"]}, map[string]interface{}{"ship": c["ship"], "type": c["type"], "meta": c["meta"]}) - } - return - }}, - "role": &ctx.Command{Name: "role [name [[componet] name [[command] name]]]", Help: "用户角色", - Auto: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) bool { - switch len(arg) { - case 0: // 查看角色 - Auto(m, "ship", "userrole") - case 1: // 查看组件 - Auto(m, "ship", "userrole", arg[0], "componet") - case 2: // 查看功能 - Auto(m, "ship", "userrole", arg[0], "componet", arg[1], "command") + m.Log("debug", "block: %v chain: %v", len(block), len(chain)) + for _, b := range block { // 添加节点 + m.Confv("auth", b["hash"], map[string]interface{}{"create_time": m.Time(), "type": b["type"], "meta": b["meta"]}) } - return true - }, - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - switch len(arg) { - case 0: // 查看角色 + for _, c := range chain { // 添加链接 + m.Confv("auth", []interface{}{c["node"], "ship", c["hash"]}, map[string]interface{}{"ship": c["ship"], "type": c["type"], "meta": c["meta"]}) + } + return + }}, + + "role": &ctx.Command{Name: "role [name [componet [name [command [name]]]]|[user [name [password|uuid code]]]]", + Help: "用户角色, componet: 组件管理, user: 用户管理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) == 0 { // 角色列表 m.Cmdy("aaa.auth", "ship", "userrole") - case 1: // 查看组件 - m.Cmdy("aaa.auth", "ship", "userrole", arg[0], "componet") - case 2: // 查看功能 - m.Cmdy("aaa.auth", "ship", "userrole", arg[0], "componet", arg[1], "command") - case 3: // 查看接口 - m.Cmdy("aaa.auth", "ship", "userrole", arg[0], "componet", arg[1], "command", arg[2], "componet") - default: // 添加接口 - if arg[1] == "componet" && arg[3] == "command" { - for _, v := range arg[4:] { - m.Cmdy("aaa.auth", "ship", "userrole", arg[0], "componet", arg[2], "command", v) + return + } + + role, arg := arg[0], arg[1:] + switch arg[0] { + case "componet", "command": + componets, commands := []string{}, []string{} + for i := 0; i < len(arg); i++ { // 解析参数 + if arg[i] == "command" { + for i := i + 1; i < len(arg); i++ { + if arg[i] == "componet" { + break + } + commands = append(commands, arg[i]) + } + } + if arg[i] == "componet" { + continue + } + componets = append(componets, arg[i]) + } + + if len(componets) == 0 { // 查看组件 + m.Cmdy("aaa.auth", "ship", "userrole", role, "componet") + return + } + for i := 0; i < len(componets); i++ { + if len(commands) == 0 { // 查看命令 + m.Cmdy("aaa.auth", "ship", "userrole", role, "componet", componets[i], "command") + continue + } + for j := 0; j < len(commands); j++ { // 添加命令 + m.Cmd("aaa.auth", "ship", "userrole", role, "componet", componets[i], "command", commands[j]) + } + } + + case "user": + if len(arg) == 1 { // 查看用户 + m.Cmdy("aaa.auth", "ship", "userrole", role, "username") + break + } + for i := 1; i < len(arg); i++ { // 添加用户 + if m.Cmd("aaa.auth", "ship", "username", arg[i], "userrole", role); i < len(arg)-2 { + switch arg[i+1] { + case "password", "uuid": + m.Cmd("aaa.auth", "ship", "username", arg[i], arg[i+1], arg[i+2]) + i += 2 + } } } } return }}, - "user": &ctx.Command{Name: "user [role username password] [username password|uuid code] [username [sesstype]]", Help: "用户认证, 添加、认证、查询", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if len(arg) > 0 && arg[0] == "session" { - if len(arg) == 1 { // 查看会话 - m.Cmdy("aaa.auth", "ship", "username", m.Option("username"), "session") + "user": &ctx.Command{Name: "user cookie [role]|[login [password|uuid [code]]]|[service [name [value]]]|[session [select|create]]", + Help: "用户认证, cookie: cookie管理, session: 会话管理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) == 0 { // 查看用户 + m.Cmdy("aaa.auth", "ship", "username") return } - switch arg[1] { - case "select": // 选择会话 - defer func() { m.Log("info", "sessid: %s", m.Append("key")) }() - if m.Options("sessid") && m.Cmds("aaa.auth", m.Option("sessid"), "ship", "check", "username", m.Option("username")) { - m.Add("append", "key", m.Option("sessid")).Table() + + switch arg[0] { + case "role": // 用户角色 + m.Cmdy("aaa.auth", "ship", "username", m.Option("username"), "userrole") + + case "login": // 用户登录 + m.Cmdy("aaa.auth", "username", m.Option("username"), arg[1], arg[2]) + + case "cookie": + if len(arg) > 3 { // 设置cookie + m.Cmdy("aaa.auth", "username", m.Option("username"), "data", strings.Join(arg[:3], "."), arg[3]) + arg = arg[:3] + } + + // 查看cookie + m.Cmdy("aaa.auth", "username", m.Option("username"), "data", strings.Join(arg, ".")) + case "session": + if len(arg) == 1 { // 查看会话 + m.Cmdy("aaa.auth", "ship", "username", m.Option("username"), "session") return } - if m.Cmdy("aaa.auth", "ship", "username", m.Option("username"), "session"); m.Appends("key") { - return + + switch arg[1] { + case "select": // 选择会话 + defer func() { m.Log("info", "sessid: %s", m.Result(0)) }() + + if m.Options("sessid") && m.Cmds("aaa.auth", "ship", "username", m.Option("username"), "check", m.Option("sessid")) { + m.Echo(m.Option("sessid")) + return + } + if m.Cmdy("aaa.auth", "ship", "username", m.Option("username"), "session"); m.Appends("key") { + m.Set("result").Echo(m.Append("key")) + return + } + fallthrough + case "create": // 创建会话 + m.Cmdy("aaa.auth", "ship", "username", m.Option("username"), "session", kit.Select("web", arg, 2)) + m.Cmd("aaa.auth", m.Result(0), "data", "current.ctx", "mdb") } - fallthrough - case "create": // 创建会话 - m.Cmdy("aaa.auth", "ship", "session", kit.Select("web", arg, 2), "username", m.Option("username")) - m.Cmd("aaa.auth", m.Result(0), "data", "current.ctx", "mdb") } return - } - - switch len(arg) { - case 0: // 查看用户 - m.Cmdy("aaa.auth", "ship", "username") - case 1: // 查看角色 - m.Cmdy("aaa.auth", "ship", "username", arg[0], "userrole") - case 2: // 查看会话 - m.Cmdy("aaa.auth", "ship", "username", arg[0], "session", arg[1]) - case 3: // 用户认证 - if (arg[1] == "password" || arg[1] == "uuid") && m.Cmds("aaa.auth", "ship", "username", arg[0]) { - m.Cmdy("aaa.auth", "username", arg[0], arg[1], arg[2]) - break - } - fallthrough - default: // 添加用户 - for i := 1; i < len(arg); i += 2 { - if m.Cmdy("aaa.auth", "ship", "username", arg[i], "userrole", arg[0]); i < len(arg)-1 { - m.Cmd("aaa.auth", "ship", "username", arg[i], "password", arg[i+1]) - } - } - } - return - }}, - "sess": &ctx.Command{Name: "sess [sessid [meta]|[username password password]]|[type ip ip]", Help: "会话管理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if len(arg) > 0 && arg[0] == "bench" { - if len(arg) == 1 { // 查看空间 - m.Cmdy("aaa.auth", m.Option("sessid"), "ship", "bench") + }}, + "sess": &ctx.Command{Name: "sess [sessid] [current [pod|ctx|dir|env [value]]]|[bench [select|create]]", + Help: "会话管理, current: 指针管理, bench: 空间管理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) == 0 { // 会话列表 + m.Cmdy("aaa.auth", "ship", "session") return } - switch arg[1] { - case "select": // 选择空间 - defer func() { m.Log("info", "bench: %s", m.Append("key")) }() - if m.Options("bench") && m.Cmds("aaa.auth", m.Option("bench"), "ship", "check", m.Option("sessid")) { - m.Add("append", "key", m.Option("bench")).Table() + + sid := m.Option("sessid") + if m.Conf("auth", []string{arg[0], "type"}) == "session" { + sid, arg = arg[0], arg[1:] + } + if len(arg) == 0 { + m.Echo(sid) + return + } + + switch arg[0] { + case "user": // 查看用户 + m.Cmdy("aaa.auth", sid, "ship", "username") + + case "current": + if len(arg) > 2 { // 设置指针 + m.Cmd("aaa.auth", sid, "data", strings.Join(arg[:2], "."), arg[2]) + arg = arg[:2] + } + + // 查看指针 + m.Cmdy("aaa.auth", sid, "data", strings.Join(arg, ".")) + + case "bench": + if len(arg) == 1 { // 查看空间 + m.Cmdy("aaa.auth", m.Option("sessid"), "ship", "bench") return } - if m.Cmdy("aaa.auth", m.Option("sessid"), "ship", "bench"); m.Appends("key") { - return - } - fallthrough - case "create": // 创建空间 - m.Cmdy("aaa.auth", m.Option("sessid"), "ship", "bench", kit.Select("web", arg, 2)) - m.Cmd("aaa.auth", m.Result(0), "data", "name", "web") - } - return - } - if len(arg) > 0 && arg[0] == "current" { - switch len(arg) { - case 1: - m.Cmdy("aaa.auth", m.Option("sessid"), "data", "current") - case 2: - m.Cmdy("aaa.auth", m.Option("sessid"), "data", "current."+arg[1]) - default: - m.Cmd("aaa.auth", m.Option("sessid"), "data", "current."+arg[1], arg[2]) - m.Cmdy("aaa.auth", m.Option("sessid"), "data", "current."+arg[1]) - } - return - } - - switch len(arg) { - case 0: // 查看会话 - m.Cmdy("aaa.auth", "ship", "session") - case 1: // 查看会话 - m.Cmdy("aaa.auth", arg[0]) - case 2: // 查询会话 - m.Cmdy("aaa.auth", arg[0], "ship", arg[1]) - case 3: // 创建会话 - m.Cmdy("aaa.auth", "ship", "session", arg[0], arg[1], arg[2]) - case 4: // 用户登录 - m.Cmdy("aaa.auth", arg[0], "ship", "username", arg[1], arg[2], arg[3]) - } - return - }}, - "work": &ctx.Command{Name: "work [sessid create|select]|[benchid] [right [username [componet name [command name [argument name]]]]]", Help: "工作任务", - Auto: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (goon bool) { - switch len(arg) { - case 0: - Auto(m, "ship", "bench") - Auto(m, "ship", "session") - default: - switch m.Conf("auth", []string{arg[0], "type"}) { - case "session": - if len(arg) == 1 { - m.Auto("create", "create", "创建空间") - m.Auto("select", "select", "查找空间") - } else { + switch arg[1] { + case "select": // 选择空间 + defer func() { m.Log("info", "bench: %s", m.Result(0)) }() + if m.Options("bench") && m.Cmds("aaa.auth", m.Option("sessid"), "ship", "check", m.Option("bench")) { + m.Echo(m.Option("bench")) + return } - case "bench": - if len(arg) == 1 { - m.Auto("delete", "delete", "删除空间") - m.Auto("rename", "rename", "命名空间") - m.Auto("right", "right [username [componet [command]]]", "权限检查") - } else { + if m.Cmdy("aaa.auth", m.Option("sessid"), "ship", "bench"); m.Appends("key") { + m.Set("result").Echo(m.Append("key")) + return } - default: - m.Auto("invalid id") + fallthrough + case "create": // 创建空间 + m.Cmdy("aaa.auth", m.Option("sessid"), "ship", "bench", kit.Select("web", arg, 2)) + m.Cmd("aaa.auth", m.Result(0), "data", "name", "web") } } - return true - }, - Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if len(arg) > 0 && arg[0] == "right" { - // 查看权限 - if len(arg) == 1 { + return + }}, + "work": &ctx.Command{Name: "work [benchid] [sesion]|[delete]|[rename name]|[share public|protect|private][data arg...]|[right [componet [command [argument]]]]", + Help: []string{"工作空间", + "session: 查看会话", + "delete: 删除空间", + "rename [name]: 命名空间", + "share [public|protect|private]: 共享空间", + "data arg...: 读写数据", + "right [componet [command [arguments]]]: 权限检查", + }, Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { + if len(arg) == 0 { // 空间列表 + m.Cmdy("aaa.auth", "ship", "bench") + return + } + + bid := m.Option("bench") + if m.Conf("auth", []string{arg[0], "type"}) == "bench" { + bid, arg = arg[0], arg[1:] + } + if len(arg) == 0 { + m.Echo(bid) + return + } + + switch arg[0] { + case "session": // 查看会话 + m.Cmdy("aaa.auth", bid, "ship", "session") + + case "delete": // 删除空间 + m.Cmdy("aaa.auth", bid, "delete", "node") + + case "rename": // 命名空间 + if len(arg) > 1 { + m.Cmd("aaa.auth", bid, "data", "name", arg[1]) + } + m.Cmdy("aaa.auth", bid, "data", "name") + + case "share": // 共享空间 + if len(arg) > 1 { + m.Cmdy("aaa.auth", bid, "data", "share", arg[1]) + } + m.Cmdy("aaa.auth", bid, "data", "share") + + case "data": // 读写数据 + m.Cmdy("aaa.auth", bid, arg) + + case "right": + if len(arg) == 1 { // 查看权限 m.Cmd("aaa.auth", m.Option("bench"), "ship", "componet").CopyTo(m, "append") m.Cmd("aaa.auth", m.Option("bench"), "ship", "command").CopyTo(m, "append") m.Table() @@ -635,12 +627,13 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", // 检查权限 m.Cmd("aaa.auth", "ship", "username", m.Option("username"), "userrole").Table(func(node map[string]string) { if node["meta"] == "root" { // 超级用户 - m.Log("info", "root %s", m.Option("username")) + m.Log("info", "role: root") m.Echo("true") } else if len(arg) > 2 { // 接口权限 if m.Cmds("aaa.auth", m.Option("bench"), "ship", "check", arg[2]) { m.Echo("true") } else if cid := m.Cmdx("aaa.auth", "ship", "userrole", node["meta"], "componet", arg[1], "check", arg[2]); kit.Right(cid) { + m.Log("info", "role: %s", node["meta"]) m.Cmd("aaa.auth", m.Option("bench"), cid) m.Echo("true") } @@ -648,6 +641,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", if m.Cmds("aaa.auth", m.Option("bench"), "ship", "check", arg[1]) { m.Echo("true") } else if cid := m.Cmdx("aaa.auth", "ship", "userrole", node["meta"], "check", arg[1]); kit.Right(cid) { + m.Log("info", "role: %s", node["meta"]) m.Cmd("aaa.auth", m.Option("bench"), cid) m.Echo("true") } @@ -656,61 +650,6 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心", m.Log("right", "bench: %s sessid: %s user: %s com: %v result: %v", m.Option("bench"), m.Option("sessid"), m.Option("username"), arg[2:], m.Result(0)) - return - } - - if len(arg) == 0 { // 查看空间 - m.Cmdy("aaa.auth", "ship", "bench") - return - } - - bid := "" - switch m.Conf("auth", []string{arg[0], "type"}) { - case "session": // 创建空间 - bid, arg = m.Cmdx("aaa.auth", arg[0], "ship", "bench", arg[1]), arg[2:] - m.Cmd("aaa.auth", bid, "data", "name", "web") - defer func() { m.Set("result").Echo(bid) }() - case "bench": // 查询空间 - bid, arg = arg[0], arg[1:] - default: - return - } - if len(arg) == 0 { - m.Echo(bid) - return - } - - switch arg[0] { - case "delete": // 删除空间 - m.Cmd("aaa.auth", bid, "delete", "node") - case "rename": // 命名空间 - m.Cmd("aaa.auth", bid, "data", "name", arg[1]) - case "right": // 权限检查 - m.Cmd("aaa.auth", "ship", "username", arg[1], "userrole").Table(func(node map[string]string) { - if node["meta"] == "root" { // 超级用户 - m.Log("info", "root %s", arg[1]) - m.Echo("true") - } else if len(arg) >= 4 { // 功能权限 - if m.Cmds("aaa.auth", bid, "ship", "check", arg[3]) { - m.Echo("true") - } else if cid := m.Cmdx("aaa.auth", "ship", "userrole", node["meta"], "componet", arg[2], "check", arg[3]); kit.Right(cid) { - m.Cmd("aaa.auth", bid, cid) - m.Echo("true") - } - } else if len(arg) >= 3 { // 组件权限 - if m.Cmds("aaa.auth", bid, "ship", "check", arg[2]) { - m.Echo("true") - } else if cid := m.Cmdx("aaa.auth", "ship", "userrole", node["meta"], "check", arg[2]); kit.Right(cid) { - m.Cmd("aaa.auth", bid, cid) - m.Echo("true") - } - } - }) - - m.Log("right", "bench: %s sessid: %s user: %s com: %v result: %v", - m.Option("bench"), m.Option("sessid"), m.Option("username"), arg[2:], m.Result(0)) - default: // 读写数据 - m.Cmdx("aaa.auth", bid, arg) } return }}, diff --git a/src/contexts/cli/cli.go b/src/contexts/cli/cli.go index c183cd35..2a8d4198 100644 --- a/src/contexts/cli/cli.go +++ b/src/contexts/cli/cli.go @@ -464,11 +464,6 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心", } detail = append(detail, arg...) - if m.Options("current_ctx") && detail[0] != "context" { - detail = append([]string{"context", m.Option("current_ctx")}, detail...) - m.Option("current_ctx", "") - } - // 目标切换 target := m.Optionv("ps_target") if detail[0] != "context" { diff --git a/src/contexts/ctx/ctx_cgi.go b/src/contexts/ctx/ctx_cgi.go index fda1bab7..7c280d75 100644 --- a/src/contexts/ctx/ctx_cgi.go +++ b/src/contexts/ctx/ctx_cgi.go @@ -471,12 +471,8 @@ var CGI = template.FuncMap{ switch len(arg) { case 0: list := map[string]map[string]interface{}{} - m.Confm("auth", []string{m.Option("sessid"), "ship"}, func(key string, ship map[string]interface{}) { - if ship["type"] == "bench" { - if work := m.Confm("auth", key); work != nil { - list[key] = work - } - } + m.Cmd("aaa.sess", "bench").Table(func(node map[string]string) { + list[node["key"]] = m.Confm("auth", node["key"]) }) return list } @@ -485,7 +481,8 @@ var CGI = template.FuncMap{ "parse": func(m *Message, arg ...interface{}) interface{} { switch len(arg) { case 1: - return m.Parse(kit.Format(arg[0])) + v := m.Parse(kit.Format(arg[0])) + return v } return nil }, diff --git a/src/contexts/ctx/ctx_init.go b/src/contexts/ctx/ctx_init.go index d3e8cc30..92b79260 100644 --- a/src/contexts/ctx/ctx_init.go +++ b/src/contexts/ctx/ctx_init.go @@ -360,9 +360,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, } if v, ok := msg.Data[k]; ok { - b, e := json.MarshalIndent(v, "", " ") - m.Log("fuck", " %v", b) - m.Log("fuck", " %v", e) + json.MarshalIndent(v, "", " ") m.Echo(kit.Formats(v)) return e } @@ -378,7 +376,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心", Server: &CTX{}, break } } - m.Sort("key", "string").Table() + m.Sort("key", "str").Table() return }}, "magic": &Command{Name: "magic", Help: "随机组员", Hand: func(m *Message, c *Context, key string, arg ...string) (e error) { diff --git a/src/contexts/ctx/ctx_type.go b/src/contexts/ctx/ctx_type.go index f7259c87..dbd183a4 100644 --- a/src/contexts/ctx/ctx_type.go +++ b/src/contexts/ctx/ctx_type.go @@ -832,12 +832,18 @@ func (m *Message) Optionx(key string, arg ...string) interface{} { return value } func (m *Message) Magic(begin string, chain interface{}, args ...interface{}) interface{} { - auth := []string{"bench", "session", "username", "role", "componet", "command"} + auth := []string{"bench", "session", "user", "role", "componet", "command"} key := []string{"bench", "sessid", "username", "role", "componet", "command"} aaa := m.Sess("aaa", false) for i, v := range auth { if v == begin { - data := aaa.Confv("auth", []string{m.Option(key[i]), "data"}) + h := m.Option(key[i]) + if v == "user" { + h, _ = kit.Hash("username", m.Option("username")) + } + + data := aaa.Confv("auth", []string{h, "data"}) + if kit.Format(chain) == "" { return data } @@ -1104,7 +1110,14 @@ func (m *Message) Parse(arg interface{}) string { return m.Cap(str[1:]) } if len(str) > 1 && str[0] == '@' { - return m.Confx(str[1:]) + if v := m.Option(str[1:]); v != "" { + return v + } + if v := kit.Format(m.Magic("bench", str[1:])); v != "" { + return v + } + v := m.Conf(str[1:]) + return v } return str } diff --git a/src/contexts/nfs/nfs.go b/src/contexts/nfs/nfs.go index 18663585..d8b08811 100644 --- a/src/contexts/nfs/nfs.go +++ b/src/contexts/nfs/nfs.go @@ -866,13 +866,13 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { if len(arg) > 0 && arg[0] == "scan" { // 终端用户 - m.Cmd("aaa.user", "root", m.Option("username", m.Conf("runtime", "boot.USER")), "what") + m.Cmd("aaa.role", "root", "user", m.Option("username", m.Conf("runtime", "boot.USER"))) // 创建会话 - m.Option("sessid", m.Cmd("aaa.user", "session", "select").Append("key")) + m.Option("sessid", m.Cmdx("aaa.user", "session", "select")) // 创建空间 - m.Option("bench", m.Cmd("aaa.sess", "bench", "select").Append("key")) + m.Option("bench", m.Cmdx("aaa.sess", "bench", "select")) // 默认配置 m.Cap("stream", arg[1]) @@ -1167,6 +1167,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", if len(arg) == 0 { arg = append(arg, "") } + m.Magic("session", "current.dir", arg[0]) wd, e := os.Getwd() m.Assert(e) @@ -1175,7 +1176,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心", rg, e := regexp.Compile(m.Option("dir_reg")) m.Confm("paths", func(index int, value string) bool { - p := path.Join(value, m.Option("dir_root"), kit.Select("", arg)) + p := path.Join(value, m.Option("dir_root"), arg[0]) if s, e := os.Stat(p); e == nil { if s.IsDir() { dir(m, p, 0, kit.Right(m.Has("dir_deep")), m.Confx("dir_type"), trip, rg, diff --git a/src/contexts/ssh/ssh.go b/src/contexts/ssh/ssh.go index 7c088999..06521f3b 100644 --- a/src/contexts/ssh/ssh.go +++ b/src/contexts/ssh/ssh.go @@ -128,7 +128,6 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", nfs.Free(func(nfs *ctx.Message) bool { m.Conf("timer", m.Cmdx("cli.timer", "repeat", m.Conf("timer_interval"), "context", "ssh", "remote", "redial", arg[1:])) m.Cmd("aaa.auth", m.Cmdx("aaa.auth", "nodes", node.Append("node.name")), "delete", "node") - m.Log("info", "delete node %s", node.Append("node.name")) delete(m.Confm("node"), node.Append("node.name")) m.Cap("stream", "") @@ -165,6 +164,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", // 清理节点 m.Sess("ms_source", false).Free(func(msg *ctx.Message) bool { + m.Cmd("aaa.auth", m.Cmdx("aaa.auth", "nodes", name), "delete", "node") m.Log("info", "delete node %s", name) delete(m.Confm("node"), name) return true @@ -192,20 +192,14 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", // 路由转发 if rest := kit.Select("", names, 1); names[0] != "" { // 数字签名 - if !m.Options("remote_code") { - // 用户路由 - m.Option("user.route", kit.Select(m.Conf("runtime", "node.route"), m.Conf("runtime", "user.route"))) - m.Cmd("aaa.auth", "username", m.Option("username"), "session", "login").Table(func(line map[string]string) { - m.Option("user.route", m.Cmd("aaa.auth", line["key"], "login").Append("meta")) - }) - + if !m.Options("remote_code") && arg[0] != "check" { // 数据哈希 hash, meta := kit.Hash("rand", m.Option("text.time", m.Time("stamp")), m.Option("text.cmd", strings.Join(arg, " ")), m.Option("text.route", route), m.Option("node.route", m.Conf("runtime", "node.route")), - m.Option("user.route"), + m.Option("user.route", kit.Select(m.Conf("runtime", "node.route"), m.Conf("runtime", "user.route"))), m.Option("user.name", m.Option("username")), ) m.Option("text.rand", meta[0]) @@ -279,14 +273,13 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", } // 设备证书 - m.Option("node.cert", m.Cmd("aaa.auth", "nodes", m.Option("node.route"), "cert").Append("meta")) - if !m.Options("node.cert") { + if m.Option("node.cert", m.Cmd("aaa.auth", "nodes", m.Option("node.route"), "cert").Append("meta")); !m.Options("node.cert") { m.Option("node.cert", m.Spawn().Cmdx("ssh.remote", m.Option("node.route"), "sync", "check", "node")) m.Cmd("aaa.auth", "nodes", m.Option("node.route"), "cert", m.Option("node.cert")) } - // 设备验签 if !m.Cmds("aaa.rsa", "verify", m.Option("node.cert"), m.Option("node.sign"), m.Option("text.hash", hash)) { + m.Cmd("aaa.auth", "nodes", m.Option("node.route"), "cert", m.Option("node.cert")) m.Log("warning", "node error") return } @@ -302,8 +295,8 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", m.Conf("runtime", "user.route", user.Append("user.route")) m.Conf("runtime", "user.name", user.Append("user.name")) m.Conf("runtime", "user.cert", user.Append("user.cert")) + m.Cmd("aaa.role", "root", "user", user.Append("user.name")) m.Cmd("aaa.auth", "username", user.Append("user.name"), "cert", user.Append("user.cert")) - m.Cmd("aaa.user", "root", user.Append("user.name"), "what") return } @@ -312,8 +305,8 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", if !m.Options("remote_code") || m.Cmds("aaa.rsa", "verify", m.Conf("runtime", "user.cert"), m.Option("user.sign"), m.Option("text.hash")) { for _, v := range arg[2:] { user := m.Spawn().Cmd("ssh.remote", v, "sync", "check", "user") + m.Cmd("aaa.user", arg[1], "user", user.Append("user.name")) m.Cmd("aaa.auth", "username", user.Append("user.name"), "cert", user.Append("user.cert")) - m.Cmd("aaa.user", arg[1], user.Append("user.name"), "what") } return } @@ -354,6 +347,7 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", } if m.Options("remote_code") { + m.Log("info", "username %s", m.Option("user.name")) if m.Option("username", m.Option("user.name")); !m.Confs("trust", m.Option("node.route")) { // 用户签名 hash, _ := kit.Hash("rand", m.Option("text.time", m.Time("stamp")), m.Option("node.route")) @@ -371,10 +365,10 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心", } // 创建会话 - m.Option("sessid", m.Cmd("aaa.user", "session", "select").Append("key")) + m.Option("sessid", m.Cmdx("aaa.user", "session", "select")) // 创建空间 - m.Option("bench", m.Cmd("aaa.sess", "bench", "select").Append("key")) + m.Option("bench", m.Cmdx("aaa.sess", "bench", "select")) // 权限检查 if !m.Cmds("aaa.work", "right", "remote", arg[0]) { diff --git a/src/contexts/web/web.go b/src/contexts/web/web.go index 848fab16..60d4ca3f 100644 --- a/src/contexts/web/web.go +++ b/src/contexts/web/web.go @@ -108,23 +108,24 @@ func (web *WEB) Login(msg *ctx.Message, w http.ResponseWriter, r *http.Request) msg.Log("info", "cas_login %v", msg.Option("ticket")) if msg.Options("ticket") { - msg.Option("uuid", msg.Option(msg.Conf("login", "cas_uuid"))) msg.Option("username", cas.Username(r)) - - http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Cmdx("web.session", "login", "uuid"), Path: "/"}) + msg.Log("info", "login: %s", msg.Option("username")) + http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Option("sessid", msg.Cmdx("aaa.user", "session", "select")), Path: "/"}) http.Redirect(w, r, merge(msg, r.Header.Get("index_url"), "ticket", ""), http.StatusTemporaryRedirect) return false } } else if msg.Options("username") && msg.Options("password") { - if sessid := msg.Cmd("web.session", "login", "password").Result(0); sessid != "" { - http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Option("sessid", sessid), Path: "/"}) + if msg.Cmds("aaa.auth", "username", msg.Option("username"), "password", msg.Option("password")) { + msg.Log("info", "login: %s", msg.Option("username")) + http.SetCookie(w, &http.Cookie{Name: "sessid", Value: msg.Cmdx("aaa.user", "session", "select"), Path: "/"}) } else { w.WriteHeader(http.StatusUnauthorized) } return false } - if !msg.Options("current_ctx") { - http.SetCookie(w, &http.Cookie{Name: "current_ctx", Value: msg.Option("current_ctx", "mdb"), Path: "/"}) + if msg.Options("sessid") { + msg.Log("info", "sessid: %s", msg.Option("sessid")) + msg.Log("info", "username: %s", msg.Option("username", msg.Cmd("aaa.sess", "user").Append("meta"))) } return true } @@ -565,12 +566,14 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", if m.Options("content_length") { req.Header.Set("Content-Length", m.Option("content_length")) } - m.Confm("spide", []string{which, "cookie"}, func(key string, value string) { - if key != "" { - req.AddCookie(&http.Cookie{Name: key, Value: value}) - m.Log("info", "set-cookie %s: %v", key, value) - } + + // 请求cookie + kit.Structm(m.Magic("user", []string{"cookie", which}), func(key string, value string) { + req.AddCookie(&http.Cookie{Name: key, Value: value}) + m.Log("info", "set-cookie %s: %v", key, value) + }) + if kit.Right(client["logheaders"]) { for k, v := range req.Header { m.Log("info", "%s: %s", k, v) @@ -599,8 +602,9 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } } + // 响应cookie for _, v := range res.Cookies() { - m.Conf("spide", []string{which, "cookie", v.Name}, v.Value) + m.Magic("user", []string{"cookie", which, v.Name}, v.Value) m.Log("info", "get-cookie %s: %v", v.Name, v.Value) } @@ -842,50 +846,6 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", } return }}, - "session": &ctx.Command{Name: "session [login secrete] bench [check [componet [command]]]", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { - if len(arg) == 0 { - return - } - - username, sessid := m.Option("username"), m.Option("sessid") - - switch arg[0] { - case "login": - if len(arg) == 1 { // 查询用户 - m.Echo(m.Cmd("aaa.sess", sessid, "username").Append("meta")) - break - } - - if username == "" || !m.Options(arg[1]) { - break - } - - if sessid == "" || !m.Cmds("aaa.sess", sessid) { // 创建会话 - if sessid = m.Cmd("aaa.auth", "username", m.Option("username"), "session", "web").Append("key"); sessid == "" { - sessid = m.Cmdx("aaa.sess", "web", "ip", m.Option("remote_ip")) - } - } - if m.Cmds("aaa.sess", sessid, m.Option("username"), arg[1], m.Option(arg[1])) { // 用户登录 - m.Echo(sessid) - } - - case "bench": - if len(arg) == 1 { // 创建空间 - bench := m.Option("bench") - if bench == "" || !m.Cmds("aaa.work", bench) { - bench = m.Cmdx("aaa.work", sessid, "web") - } - m.Echo(bench) - break - } - // 添加数据 - m.Cmd("aaa.work", arg[1:]) - - case "check": // 检查权限 - m.Echo(m.Cmdx("aaa.work", arg[1], "right", arg[2:])) - } - return - }}, "/proxy/": &ctx.Command{Name: "/proxy/which/method/url", Help: "服务代理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { fields := strings.Split(key, "/") @@ -900,8 +860,6 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", return } - m.Option("current_ctx", kit.Select(m.Option("current_ctx", kit.Format(m.Magic("session", "current.ctx"))))) - if web, ok := m.Target().Server.(*WEB); m.Assert(ok) { // 响应类型 accept_json := strings.HasPrefix(m.Option("accept"), "application/json") @@ -923,18 +881,11 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", // 权限检查 if m.Confs("login", "check") { - if m.Option("username", m.Cmdx("web.session", "login")) == "" { // 没有登录 + if m.Option("username") == "" { // 没有登录 m.Set("option", "componet_group", "login").Set("option", "componet_name", "").Set("option", "bench", "") } else { - sessid, bench := m.Option("sessid"), m.Option("bench") - - // 创建会话 - if m.Option("sessid", m.Cmd("aaa.user", "session", "select").Append("key")) != sessid { - http.SetCookie(w, &http.Cookie{Name: "sessid", Value: m.Option("sessid"), Path: "/"}) - } - // 创建空间 - if m.Option("bench", m.Cmd("aaa.sess", "bench", "select").Append("key")) != bench { + if bench := m.Option("bench"); m.Option("bench", m.Cmdx("aaa.sess", "bench", "select")) != bench { m.Append("redirect", merge(m, m.Option("index_url"), "bench", m.Option("bench"))) return } @@ -955,6 +906,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", continue } + arg = arg[:0] if kit.Right(val["componet_pod"]) { arg = append(arg, "sh", "node", kit.Format(m.Magic("session", "current.pod"))) } @@ -1003,7 +955,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", for _, v := range val["inputs"].([]interface{}) { value := v.(map[string]interface{}) if value["name"] != nil && msg.Option(value["name"].(string)) == "" { - msg.Add("option", value["name"].(string), value["value"]) + msg.Add("option", value["name"].(string), m.Parse(value["value"])) } } } @@ -1016,7 +968,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", // 命令历史 msg.Put("option", name_alias, map[string]interface{}{ "cmd": arg, "order": m.Option("componet_name_order"), "action_time": msg.Time(), - }).Cmd("web.session", "bench", m.Option("bench"), "data", "option", name_alias, "modify_time", msg.Time()) + }).Cmd("aaa.work", m.Option("bench"), "data", "option", name_alias, "modify_time", msg.Time()) } } } @@ -1047,21 +999,24 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心", m.Assert(e) defer f.Close() - p := path.Join(m.Cmdx("nfs.path", m.Option("current_dir")), h.Filename) + p := path.Join(m.Cmdx("nfs.path", m.Magic("session", "current.dir")), h.Filename) + m.Log("upload", "file: %s", p) + m.Echo("%s", p) + o, e := os.Create(p) m.Assert(e) defer o.Close() io.Copy(o, f) - m.Log("upload", "file: %s", p) - m.Append("redirect", m.Option("referer")) return }}, "/download/": &ctx.Command{Name: "/download/", Help: "下载文件", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) (e error) { r := m.Optionv("request").(*http.Request) w := m.Optionv("response").(http.ResponseWriter) + p := m.Cmdx("nfs.path", strings.TrimPrefix(key, "/download/")) m.Log("info", "download %s %s", p, m.Cmdx("aaa.hash", "file", p)) + http.ServeFile(w, r, p) return }}, diff --git a/src/examples/code/code.go b/src/examples/code/code.go index 43792bcc..d5fe80d8 100644 --- a/src/examples/code/code.go +++ b/src/examples/code/code.go @@ -84,11 +84,12 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", // }, // }, map[string]interface{}{"name": "dir", "help": "dir", "template": "componet", - "componet_ctx": "nfs", "componet_cmd": "dir", "arguments": []interface{}{"@dir", "dir_sort", "@sort_field", "@sort_order"}, + "componet_ctx": "nfs", "componet_cmd": "dir", "arguments": []interface{}{"@current.dir", "dir_sort", "@sort_field", "@sort_order"}, "pre_run": true, "display_result": "", "inputs": []interface{}{ map[string]interface{}{"type": "choice", "name": "dir_type", "label": "dir_type", "value": "both", "choice": []interface{}{ + map[string]interface{}{"name": "all", "value": "all"}, map[string]interface{}{"name": "both", "value": "both"}, map[string]interface{}{"name": "file", "value": "file"}, map[string]interface{}{"name": "dir", "value": "dir"}, @@ -113,7 +114,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", map[string]interface{}{"name": "time_r", "value": "time_r"}, }, }, - map[string]interface{}{"type": "text", "name": "dir", "label": "dir"}, + map[string]interface{}{"type": "text", "name": "current.dir", "value": "@current.dir", "label": "dir"}, }, }, map[string]interface{}{"name": "upload", "help": "upload", "template": "componet", @@ -145,7 +146,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", map[string]interface{}{"name": "pod", "help": "pod", "template": "componet", "componet_ctx": "ssh", "componet_cmd": "remote", "inputs": []interface{}{ - map[string]interface{}{"type": "text", "name": "pod", "value": ""}, + map[string]interface{}{"type": "text", "name": "pod", "value": "@current.pod"}, map[string]interface{}{"type": "button", "value": "refresh"}, }, "pre_run": true, "display_result": "", @@ -154,7 +155,7 @@ var Index = &ctx.Context{Name: "code", Help: "代码中心", "componet_pod": "true", "componet_ctx": "ssh", "componet_cmd": "context", "arguments": []interface{}{"@ctx", "list"}, "display_result": "", "inputs": []interface{}{ - map[string]interface{}{"type": "text", "name": "ctx", "value": ""}, + map[string]interface{}{"type": "text", "name": "ctx", "value": "@current.ctx"}, map[string]interface{}{"type": "button", "value": "refresh"}, }, }, diff --git a/src/toolkit/kit.go b/src/toolkit/kit.go index e5bdb125..e6164946 100644 --- a/src/toolkit/kit.go +++ b/src/toolkit/kit.go @@ -230,6 +230,33 @@ func Trans(arg ...interface{}) []string { } return ls } +func Struct(arg ...interface{}) map[string]interface{} { + value := map[string]interface{}{} + if len(arg) == 0 { + return value + } + switch val := arg[0].(type) { + case map[string]interface{}: + return val + } + + return value +} +func Structm(args ...interface{}) map[string]interface{} { + Log("error", "what %v", Format(args)) + value := Struct(args...) + for _, arg := range args { + Log("error", "what %v", Format(arg)) + switch val := arg.(type) { + case func(k string, v string): + Log("error", "what %v", Format(val)) + for k, v := range value { + val(k, Format(v)) + } + } + } + return value +} func Array(list []string, index int, arg ...interface{}) []string { if len(arg) == 0 { if -1 < index && index < len(list) { diff --git a/usr/librarys/code.js b/usr/librarys/code.js index f84e135c..7ec43d2a 100644 --- a/usr/librarys/code.js +++ b/usr/librarys/code.js @@ -650,19 +650,14 @@ function init_download(event) { return } - option["dir"].value = dir - if (dir == "" || dir.endsWith("/")) { - context.Cookie("current_dir", option["dir"].value) - } + option["current.dir"].value = dir send_command(option) - option["dir"].value = context.Cookie("current_dir") - } insert_button(append, "root", function(event) { change("") }) insert_button(append, "back", function(event) { - var path = option["dir"].value.split("/") + var path = option["current.dir"].value.split("/") while (path.pop() == "") {} change(path.join("/")+(path.length? "/": "")) }) @@ -695,14 +690,12 @@ function init_download(event) { }) } - (option["dir"].value = context.Search("current_dir")) && send_command(option) - add_sort(append, "filename", function(event) { var dir = event.target.innerText - if (option["dir"].value && !option["dir"].value.endsWith("/")) { - change(option["dir"].value+"/"+dir, event.altKey, event.shiftKey) + if (option["current.dir"].value && !option["current.dir"].value.endsWith("/")) { + change(option["current.dir"].value+"/"+dir, event.altKey, event.shiftKey) } else { - change(option["dir"].value+dir, event.altKey, event.shiftKey) + change(option["current.dir"].value+dir, event.altKey, event.shiftKey) } }) } @@ -714,7 +707,6 @@ function init_contain() { function change(pod) { option["pod"].value = pod - context.Cookie("current_pod", option["pod"].value) context.GET("", { "componet_group": "index", "componet_name": "cmd", @@ -726,8 +718,6 @@ function init_contain() { add_sort(append, "key", function(event) { change(event.target.innerText.trim()) }) - - option["pod"].value = context.Cookie("current_pod") } function init_context() { var option = document.querySelector("form.option.ctx") @@ -737,7 +727,6 @@ function init_context() { function change(ctx) { option["ctx"].value = ctx send_command(option) - context.Cookie("current_ctx", option["ctx"].value) context.GET("", { "componet_group": "index", "componet_name": "cmd", @@ -756,7 +745,6 @@ function init_context() { change(event.target.innerText.trim()) }) - option["ctx"].value = context.Cookie("current_ctx") send_command(option) } function init_command() { @@ -891,11 +879,14 @@ function init_toolkit() { location.reload() return case "create_fly": - location.search = "" + context.Command(["sess", "bench", "create"], function(msg) { + context.Search("bench", msg.result[0]) + }) return case "rename_fly": - context.Command(["work", context.Search("bench"), "rename", prompt("name")]) - location.reload() + context.Command(["work", context.Search("bench"), "rename", prompt("name")], function() { + location.reload() + }) return case "remove_fly": var b = "" diff --git a/usr/librarys/context.js b/usr/librarys/context.js index a48b0385..50f3b773 100644 --- a/usr/librarys/context.js +++ b/usr/librarys/context.js @@ -62,7 +62,13 @@ context = { return this.Cookie(key); }, Command: function(cmd, option, cb) { - option = option || {} + if (typeof option == "function") { + cb = option + option = {} + } else { + option = option || {} + } + option["componet_index"] = "index" if (typeof cmd == "string") { option["componet_name"] = "source" diff --git a/usr/template/code/code.tmpl b/usr/template/code/code.tmpl index 4ddd17ab..0d82d0d6 100644 --- a/usr/template/code/code.tmpl +++ b/usr/template/code/code.tmpl @@ -216,14 +216,13 @@ {{$form_type := option . "form_type"|meta}} {{$msg := .}} - {{if eq $form_type "upload"}} - {{end}}