forked from x/ContextOS
Merge branch '0.1.0' into 0.2.0
This commit is contained in:
commit
cd77c847c2
@ -51,17 +51,12 @@ snippet c
|
|||||||
|
|
||||||
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Close(m *ctx.Message, arg ...string) bool {
|
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Close(m *ctx.Message, arg ...string) bool {
|
||||||
switch `Filename()`.Context {
|
switch `Filename()`.Context {
|
||||||
case m.Target:
|
case m.Target:
|
||||||
if `Filename()`.Context == Index {
|
case m.Source:
|
||||||
return false
|
|
||||||
}
|
|
||||||
case m.Source:
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
return false
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var Pulse *ctx.Message
|
var Pulse *ctx.Message
|
||||||
var Index = &ctx.Context{Name: "`Filename()`", Help: "${1}",
|
var Index = &ctx.Context{Name: "`Filename()`", Help: "${1}",
|
||||||
Caches: map[string]*ctx.Cache{},
|
Caches: map[string]*ctx.Cache{},
|
||||||
|
18
etc/init.sh
18
etc/init.sh
@ -1,15 +1,24 @@
|
|||||||
~cli
|
~cli
|
||||||
@lex lex
|
@lex lex
|
||||||
|
~aaa login root root
|
||||||
|
~web serve
|
||||||
|
~ssh dial chat.shylinux.com:9090 true
|
||||||
|
sleep 1
|
||||||
|
~host1
|
||||||
|
ifneq load load end
|
||||||
|
remote context mpa register terminal shhylinux term term term 1
|
||||||
|
$sessid $result
|
||||||
|
remote cache sessid $sessid
|
||||||
|
~nfs save usr/sess.txt "terminal: " $sessid
|
||||||
|
~nfs genqr usr/sess.png "terminal: " $sessid
|
||||||
|
end
|
||||||
|
return
|
||||||
# ~cli
|
# ~cli
|
||||||
# remote slaver listen ":9393" tcp
|
# remote slaver listen ":9393" tcp
|
||||||
~aaa
|
|
||||||
login root root
|
|
||||||
# ~ssh
|
# ~ssh
|
||||||
# listen :9191
|
# listen :9191
|
||||||
~tcp
|
~tcp
|
||||||
listen :9393
|
listen :9393
|
||||||
~web
|
|
||||||
listen
|
|
||||||
|
|
||||||
# ~tcp dial ":9393"
|
# ~tcp dial ":9393"
|
||||||
# @debug on
|
# @debug on
|
||||||
@ -21,7 +30,6 @@
|
|||||||
# @debug on
|
# @debug on
|
||||||
# ~nfs
|
# ~nfs
|
||||||
# open hi.txt
|
# open hi.txt
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
@debug
|
@debug
|
||||||
|
@ -17,7 +17,7 @@ type AAA struct {
|
|||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aaa *AAA) session(meta string) string {
|
func (aaa *AAA) Session(meta string) string {
|
||||||
bs := md5.Sum([]byte(fmt.Sprintln("%d%d%s", time.Now().Unix(), rand.Int(), meta)))
|
bs := md5.Sum([]byte(fmt.Sprintln("%d%d%s", time.Now().Unix(), rand.Int(), meta)))
|
||||||
sessid := hex.EncodeToString(bs[:])
|
sessid := hex.EncodeToString(bs[:])
|
||||||
return sessid
|
return sessid
|
||||||
@ -45,7 +45,7 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
|||||||
return x.Value
|
return x.Value
|
||||||
}}
|
}}
|
||||||
|
|
||||||
aaa.Caches["sessid"] = &ctx.Cache{Name: "会话标识", Value: "", Help: "用户的会话标识"}
|
aaa.Caches["sessid"] = &ctx.Cache{Name: "会话令牌", Value: "", Help: "用户的会话标识"}
|
||||||
aaa.Caches["expire"] = &ctx.Cache{Name: "会话超时", Value: "", Help: "用户的会话标识"}
|
aaa.Caches["expire"] = &ctx.Cache{Name: "会话超时", Value: "", Help: "用户的会话标识"}
|
||||||
aaa.Caches["time"] = &ctx.Cache{Name: "登录时间", Value: fmt.Sprintf("%d", time.Now().Unix()), Help: "用户登录时间", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
aaa.Caches["time"] = &ctx.Cache{Name: "登录时间", Value: fmt.Sprintf("%d", time.Now().Unix()), Help: "用户登录时间", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
@ -60,28 +60,29 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
|||||||
if m.Target == Index {
|
if m.Target == Index {
|
||||||
Pulse = m
|
Pulse = m
|
||||||
}
|
}
|
||||||
|
|
||||||
aaa.Owner = aaa.Context
|
|
||||||
return aaa
|
return aaa
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool {
|
func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool {
|
||||||
if len(arg) > 1 {
|
if len(arg) > 1 && m.Cap("sessid") == "" {
|
||||||
if m.Cap("sessid") == "" {
|
m.Cap("group", arg[0])
|
||||||
m.Cap("sessid", aaa.session(arg[1]))
|
m.Cap("username", arg[1])
|
||||||
Pulse.Capi("nuser", 1)
|
|
||||||
}
|
|
||||||
m.Log("info", m.Source, "create %s %s", m.Cap("group", arg[0]), m.Cap("username", arg[1]))
|
|
||||||
m.Cap("stream", m.Cap("username"))
|
m.Cap("stream", m.Cap("username"))
|
||||||
|
m.Cap("sessid", aaa.Session(arg[1]))
|
||||||
|
Pulse.Capi("nuser", 1)
|
||||||
|
aaa.Owner = aaa.Context
|
||||||
|
aaa.Group = arg[0]
|
||||||
}
|
}
|
||||||
m.Log("info", m.Source, "login %s %s", m.Cap("group"), m.Cap("username"))
|
|
||||||
|
|
||||||
|
m.Log("info", m.Source, "%s login %s %s", Pulse.Cap("nuser"), m.Cap("group"), m.Cap("username"))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool {
|
func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool {
|
||||||
switch aaa.Context {
|
switch aaa.Context {
|
||||||
case m.Target:
|
case m.Target:
|
||||||
|
root := Pulse.Target.Server.(*AAA)
|
||||||
|
delete(root.sessions, m.Cap("sessid"))
|
||||||
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
||||||
case m.Source:
|
case m.Source:
|
||||||
}
|
}
|
||||||
@ -99,7 +100,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
"expire": &ctx.Config{Name: "会话超时(s)", Value: "120", Help: "会话超时"},
|
"expire": &ctx.Config{Name: "会话超时(s)", Value: "120", Help: "会话超时"},
|
||||||
},
|
},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Target, m.Master = c, c
|
m.Target, m.Master = c, c
|
||||||
aaa := c.Server.(*AAA)
|
aaa := c.Server.(*AAA)
|
||||||
|
|
||||||
@ -107,23 +108,20 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
case 0:
|
case 0:
|
||||||
m.Travel(c, func(m *ctx.Message) bool {
|
m.Travel(c, func(m *ctx.Message) bool {
|
||||||
m.Echo("%s(%s): %s\n", m.Target.Name, m.Cap("group"), m.Cap("time"))
|
m.Echo("%s(%s): %s\n", m.Target.Name, m.Cap("group"), m.Cap("time"))
|
||||||
|
if int64(m.Capi("expire")) < time.Now().Unix() {
|
||||||
|
m.Target.Close(m)
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
case 1:
|
case 1:
|
||||||
if s, ok := aaa.sessions[arg[0]]; ok {
|
s, ok := aaa.sessions[arg[0]]
|
||||||
if m.Target = s; int64(m.Capi("expire")) < time.Now().Unix() {
|
m.Assert(ok, "会话失败")
|
||||||
s.Close(m)
|
m.Target = s
|
||||||
return
|
m.Assert(int64(m.Capi("expire")) > time.Now().Unix(), "会话失败")
|
||||||
}
|
|
||||||
|
|
||||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||||
m.Log("info", m.Source, "logon %s", m.Cap("group"), m.Cap("username"))
|
m.Log("info", m.Source, "logon %s %s", m.Cap("username"), m.Cap("group"))
|
||||||
if m.Name != "" {
|
m.Echo(m.Cap("username"))
|
||||||
c.Requests = append(c.Requests, m)
|
|
||||||
m.Index = len(m.Target.Requests)
|
|
||||||
}
|
|
||||||
m.Echo(m.Cap("username"))
|
|
||||||
}
|
|
||||||
case 2, 3:
|
case 2, 3:
|
||||||
group, username, password := arg[0], arg[0], arg[1]
|
group, username, password := arg[0], arg[0], arg[1]
|
||||||
if len(arg) == 3 {
|
if len(arg) == 3 {
|
||||||
@ -132,15 +130,10 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
|
|
||||||
if username == Pulse.Conf("rootname") {
|
if username == Pulse.Conf("rootname") {
|
||||||
m.Set("detail", group, username).Target.Start(m)
|
m.Set("detail", group, username).Target.Start(m)
|
||||||
} else if msg := m.Find(username); msg == nil {
|
} else if msg := m.Find(username, false); msg == nil {
|
||||||
m.Start(username, "认证用户", group, username)
|
m.Start(username, "认证用户", group, username)
|
||||||
} else {
|
} else {
|
||||||
m.Target = msg.Target
|
m.Target = msg.Target
|
||||||
msg.Target.Start(msg)
|
|
||||||
if m.Name != "" {
|
|
||||||
m.Target.Requests = append(m.Target.Requests, m)
|
|
||||||
m.Index = len(m.Target.Requests)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Cap("password", password)
|
m.Cap("password", password)
|
||||||
|
@ -8,6 +8,7 @@ import ( // {{{
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
@ -128,10 +129,15 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
|||||||
|
|
||||||
if r := rune(ls[i][0]); r == '$' || r == '_' || (!unicode.IsNumber(r) && !unicode.IsLetter(r)) {
|
if r := rune(ls[i][0]); r == '$' || r == '_' || (!unicode.IsNumber(r) && !unicode.IsLetter(r)) {
|
||||||
if c, ok := cli.alias[string(r)]; ok {
|
if c, ok := cli.alias[string(r)]; ok {
|
||||||
if msg.Add("detail", c); len(ls[i]) > 1 {
|
if i == 0 {
|
||||||
|
if msg.Add("detail", c); len(ls[i]) == 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
ls[i] = ls[i][1:]
|
ls[i] = ls[i][1:]
|
||||||
} else {
|
} else if len(ls[i]) > 1 {
|
||||||
continue
|
mm := m.Spawn(cli.target)
|
||||||
|
m.Assert(mm.Exec(c, ls[i][1:]))
|
||||||
|
ls[i] = mm.Get("result")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -149,7 +155,10 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
|||||||
msg.Wait = make(chan bool)
|
msg.Wait = make(chan bool)
|
||||||
msg.Post(cli.Context)
|
msg.Post(cli.Context)
|
||||||
|
|
||||||
cli.echo(strings.Join(msg.Meta["result"], ""))
|
result := strings.TrimRight(strings.Join(msg.Meta["result"], ""), "\n") + "\n"
|
||||||
|
if len(result) > 1 {
|
||||||
|
cli.echo(m.Cap("result", result))
|
||||||
|
}
|
||||||
cli.target = msg.Target
|
cli.target = msg.Target
|
||||||
cli.back = line
|
cli.back = line
|
||||||
return true
|
return true
|
||||||
@ -168,6 +177,7 @@ func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||||
|
cli.Caches["result"] = &ctx.Cache{Name: "前一条指令执行结果", Value: "", Help: "前一条指令执行结果"}
|
||||||
cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽"}
|
cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽"}
|
||||||
cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符,target:显示当前模块,detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符,target:显示当前模块,detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||||
if len(arg) > 0 { // {{{
|
if len(arg) > 0 { // {{{
|
||||||
@ -326,22 +336,24 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
|
|
||||||
m.Deal(nil, func(msg *ctx.Message, arg ...string) bool {
|
m.Deal(nil, func(msg *ctx.Message, arg ...string) bool {
|
||||||
if msg.Get("result") == "error: " {
|
if msg.Get("result") == "error: " {
|
||||||
msg.Log("system", nil, "%v", msg.Meta["detail"])
|
if msg.Get("detail") != "login" {
|
||||||
|
msg.Log("system", nil, "%v", msg.Meta["detail"])
|
||||||
|
|
||||||
msg.Set("result")
|
msg.Set("result")
|
||||||
msg.Set("append")
|
msg.Set("append")
|
||||||
c := exec.Command(msg.Meta["detail"][0], msg.Meta["detail"][1:]...)
|
c := exec.Command(msg.Meta["detail"][0], msg.Meta["detail"][1:]...)
|
||||||
|
|
||||||
if len(cli.ins) == 1 && cli.Context == Index {
|
if len(cli.ins) == 1 && cli.Context == Index {
|
||||||
c.Stdin, c.Stdout, c.Stderr = cli.in, cli.out, cli.out
|
c.Stdin, c.Stdout, c.Stderr = cli.in, cli.out, cli.out
|
||||||
msg.Assert(c.Start())
|
msg.Assert(c.Start())
|
||||||
msg.Assert(c.Wait())
|
msg.Assert(c.Wait())
|
||||||
} else {
|
|
||||||
if out, e := c.CombinedOutput(); e == nil {
|
|
||||||
msg.Echo(string(out))
|
|
||||||
} else {
|
} else {
|
||||||
msg.Echo("error: ")
|
if out, e := c.CombinedOutput(); e == nil {
|
||||||
msg.Echo("%s\n", e)
|
msg.Echo(string(out))
|
||||||
|
} else {
|
||||||
|
msg.Echo("error: ")
|
||||||
|
msg.Echo("%s\n", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -397,6 +409,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
|
"sleep": &ctx.Command{Name: "sleep time", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
|
t, e := strconv.Atoi(arg[0])
|
||||||
|
m.Assert(e)
|
||||||
|
m.Log("info", nil, "sleep %ds", t)
|
||||||
|
time.Sleep(time.Second * time.Duration(t))
|
||||||
|
}},
|
||||||
"return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
cli := c.Server.(*CLI) // {{{
|
cli := c.Server.(*CLI) // {{{
|
||||||
cli.bio.Discard(cli.bio.Buffered())
|
cli.bio.Discard(cli.bio.Buffered())
|
||||||
|
@ -136,7 +136,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (c *Context) Start(m *Message) bool { // {{{
|
func (c *Context) Start(m *Message) bool { // {{{
|
||||||
if c.Requests = append(c.Requests, m); m.Cap("status") != "start" {
|
if c.Requests, m.Index = append(c.Requests, m), len(c.Requests)+1; m.Cap("status") != "start" {
|
||||||
running := make(chan bool)
|
running := make(chan bool)
|
||||||
go m.AssertOne(m, true, func(m *Message) {
|
go m.AssertOne(m, true, func(m *Message) {
|
||||||
m.Log(m.Cap("status", "start"), nil, "%d server %v", m.root.Capi("nserver", 1), m.Meta["detail"])
|
m.Log(m.Cap("status", "start"), nil, "%d server %v", m.root.Capi("nserver", 1), m.Meta["detail"])
|
||||||
@ -519,6 +519,8 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { // {{{
|
|||||||
if len(msg) > 2 {
|
if len(msg) > 2 {
|
||||||
msg = msg[2:]
|
msg = msg[2:]
|
||||||
}
|
}
|
||||||
|
case *Message:
|
||||||
|
panic(e)
|
||||||
default:
|
default:
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -538,6 +540,11 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { // {{{
|
|||||||
func (m *Message) AssertOne(msg *Message, safe bool, hand ...func(msg *Message)) *Message { // {{{
|
func (m *Message) AssertOne(msg *Message, safe bool, hand ...func(msg *Message)) *Message { // {{{
|
||||||
defer func() {
|
defer func() {
|
||||||
if e := recover(); e != nil {
|
if e := recover(); e != nil {
|
||||||
|
switch e.(type) {
|
||||||
|
case *Message:
|
||||||
|
panic(e)
|
||||||
|
}
|
||||||
|
|
||||||
msg.Log("error", nil, "error: %v", e)
|
msg.Log("error", nil, "error: %v", e)
|
||||||
if msg.root.Conf("debug") == "on" && e != io.EOF {
|
if msg.root.Conf("debug") == "on" && e != io.EOF {
|
||||||
fmt.Printf("\n\033[31m%s error: %v\033[0m\n", msg.Target.Name, e)
|
fmt.Printf("\n\033[31m%s error: %v\033[0m\n", msg.Target.Name, e)
|
||||||
@ -682,9 +689,9 @@ func (m *Message) Search(key string, root ...bool) []*Message { // {{{
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (m *Message) Find(name string, root ...bool) *Message { // {{{
|
func (m *Message) Find(name string, root ...bool) *Message { // {{{
|
||||||
target := m.Target
|
target := m.Target.root
|
||||||
if len(root) > 0 && root[0] {
|
if len(root) > 0 && !root[0] {
|
||||||
target = m.Target.root
|
target = m.Target
|
||||||
}
|
}
|
||||||
|
|
||||||
cs := target.contexts
|
cs := target.contexts
|
||||||
@ -746,9 +753,7 @@ func (m *Message) Set(meta string, arg ...string) *Message { // {{{
|
|||||||
m.Meta[meta] = arg
|
m.Meta[meta] = arg
|
||||||
case "option", "append":
|
case "option", "append":
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
if _, ok := m.Meta[arg[0]]; !ok {
|
m.Meta[meta] = []string{arg[0]}
|
||||||
m.Meta[meta] = append(m.Meta[meta], arg[0])
|
|
||||||
}
|
|
||||||
m.Meta[arg[0]] = arg[1:]
|
m.Meta[arg[0]] = arg[1:]
|
||||||
} else {
|
} else {
|
||||||
for _, k := range m.Meta[meta] {
|
for _, k := range m.Meta[meta] {
|
||||||
@ -808,6 +813,13 @@ func (m *Message) Get(key string) string { // {{{
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// }}}
|
||||||
|
func (m *Message) Geti(key string) int { // {{{
|
||||||
|
n, e := strconv.Atoi(m.Get(key))
|
||||||
|
m.Assert(e)
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
func (m *Message) Echo(str string, arg ...interface{}) *Message { // {{{
|
func (m *Message) Echo(str string, arg ...interface{}) *Message { // {{{
|
||||||
return m.Add("result", fmt.Sprintf(str, arg...))
|
return m.Add("result", fmt.Sprintf(str, arg...))
|
||||||
@ -830,7 +842,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
|
|||||||
m.Master = m.Source
|
m.Master = m.Source
|
||||||
if x, ok := s.Commands[key]; ok && x.Hand != nil && m.Check(c, "commands", key) {
|
if x, ok := s.Commands[key]; ok && x.Hand != nil && m.Check(c, "commands", key) {
|
||||||
m.AssertOne(m, true, func(m *Message) {
|
m.AssertOne(m, true, func(m *Message) {
|
||||||
m.Log("cmd", s, "%s %v", key, arg)
|
m.Log("cmd", s, "%s %v %v", key, arg, m.Meta["option"])
|
||||||
|
|
||||||
if x.Options != nil {
|
if x.Options != nil {
|
||||||
for _, v := range m.Meta["option"] {
|
for _, v := range m.Meta["option"] {
|
||||||
@ -1250,10 +1262,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"context": &Command{Name: "context [root] [[find|search] name] [list|show|spawn|start|switch|close][args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块,args:启动参数",
|
"context": &Command{Name: "context [home] [[find|search] name] [info|list|show|spawn|start|switch|close][args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块,args:启动参数",
|
||||||
Formats: map[string]int{"root": 0, "back": 0, "home": 0, "find": 1, "search": 1, "list": 0, "show": 0, "close": 0, "switch": 0, "start": 0, "spawn": 0},
|
Formats: map[string]int{"back": 0, "home": 0, "find": 1, "search": 1, "info": 1, "list": 0, "show": 0, "close": 0, "switch": 0, "start": 0, "spawn": 0},
|
||||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
root := true || m.Has("root") // {{{
|
root := !m.Has("home") // {{{
|
||||||
|
|
||||||
ms := []*Message{}
|
ms := []*Message{}
|
||||||
switch {
|
switch {
|
||||||
@ -1277,14 +1289,17 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range ms {
|
for _, v := range ms {
|
||||||
m.Target = v.Target
|
v.Meta = m.Meta
|
||||||
|
v.Data = m.Data
|
||||||
switch {
|
switch {
|
||||||
case m.Has("switch"):
|
case m.Has("switch"):
|
||||||
|
m.Target = v.Target
|
||||||
case m.Has("spawn"):
|
case m.Has("spawn"):
|
||||||
v.Set("detail", arg[2:]...).Target.Spawn(v, arg[0], arg[1]).Begin(v)
|
v.Set("detail", arg[2:]...).Target.Spawn(v, arg[0], arg[1]).Begin(v)
|
||||||
|
m.Target = v.Target
|
||||||
case m.Has("start"):
|
case m.Has("start"):
|
||||||
v.Set("detail", arg...).Target.Start(v)
|
v.Set("detail", arg...).Target.Start(v)
|
||||||
|
m.Target = v.Target
|
||||||
case m.Has("close"):
|
case m.Has("close"):
|
||||||
v.Target.Close(v)
|
v.Target.Close(v)
|
||||||
case m.Has("show"):
|
case m.Has("show"):
|
||||||
@ -1304,6 +1319,15 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
m.Echo(" %s: -> %s %v\n", k, v.Target.Name, v.Meta["detail"])
|
m.Echo(" %s: -> %s %v\n", k, v.Target.Name, v.Meta["detail"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case m.Has("info"):
|
||||||
|
switch m.Get("info") {
|
||||||
|
case "name":
|
||||||
|
m.Echo("%s", v.Target.Name)
|
||||||
|
case "owner":
|
||||||
|
m.Echo("%s", v.Target.Owner.Name)
|
||||||
|
default:
|
||||||
|
m.Echo("%s(%s): %s\n", v.Target.Name, v.Target.Owner.Name, v.Target.Help)
|
||||||
|
}
|
||||||
case m.Has("list") || len(m.Meta["detail"]) == 1:
|
case m.Has("list") || len(m.Meta["detail"]) == 1:
|
||||||
m.Travel(v.Target, func(msg *Message) bool {
|
m.Travel(v.Target, func(msg *Message) bool {
|
||||||
target := msg.Target
|
target := msg.Target
|
||||||
@ -1338,7 +1362,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
case len(arg) > 0 && v != m:
|
case len(arg) > 0 && v != m:
|
||||||
v.Set("detail", arg...).Cmd()
|
v.Cmd(arg...)
|
||||||
|
// m.Meta = v.Meta
|
||||||
|
// m.Target = target
|
||||||
|
default:
|
||||||
|
m.Target = v.Target
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// }}}
|
// }}}
|
||||||
@ -1456,18 +1484,21 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.BackTrace(func(m *Message) bool {
|
m.BackTrace(func(m *Message) bool {
|
||||||
if all {
|
// if all {
|
||||||
m.Echo("%s config:\n", m.Target.Name)
|
// m.Echo("%s config:\n", m.Target.Name)
|
||||||
}
|
// }
|
||||||
if x, ok := m.Target.Configs[arg[0]]; ok {
|
if x, ok := m.Target.Configs[arg[0]]; ok {
|
||||||
if m.Check(m.Target, "configs", arg[0]) {
|
if m.Check(m.Target, "configs", arg[0]) {
|
||||||
if all {
|
// if all {
|
||||||
m.Echo(" ")
|
// m.Echo(" ")
|
||||||
}
|
// }
|
||||||
m.Echo("%s: %s\n", x.Name, x.Help)
|
// m.Echo("%s: %s\n", x.Name, x.Help)
|
||||||
|
m.Echo("%s", x.Value)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return all
|
return true
|
||||||
|
// return all
|
||||||
})
|
})
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
@ -1512,18 +1543,21 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.BackTrace(func(m *Message) bool {
|
m.BackTrace(func(m *Message) bool {
|
||||||
if all {
|
// if all {
|
||||||
m.Echo("%s config:\n", m.Target.Name)
|
// m.Echo("%s config:\n", m.Target.Name)
|
||||||
}
|
// }
|
||||||
if x, ok := m.Target.Caches[arg[0]]; ok {
|
if x, ok := m.Target.Caches[arg[0]]; ok {
|
||||||
if m.Check(m.Target, "caches", arg[0]) {
|
if m.Check(m.Target, "caches", arg[0]) {
|
||||||
if all {
|
// if all {
|
||||||
m.Echo(" ")
|
// m.Echo(" ")
|
||||||
}
|
// }
|
||||||
m.Echo("%s: %s\n", x.Name, x.Help)
|
// m.Echo("%s: %s\n", x.Name, x.Help)
|
||||||
|
m.Echo("%s", x.Value)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return all
|
return true
|
||||||
|
// return all
|
||||||
})
|
})
|
||||||
case 2:
|
case 2:
|
||||||
m.Cap(arg[0], arg[1])
|
m.Cap(arg[0], arg[1])
|
||||||
|
@ -11,20 +11,25 @@ import (
|
|||||||
|
|
||||||
type SSH struct {
|
type SSH struct {
|
||||||
send map[string]*ctx.Message
|
send map[string]*ctx.Message
|
||||||
|
*bufio.Writer
|
||||||
*bufio.Reader
|
*bufio.Reader
|
||||||
net.Conn
|
net.Conn
|
||||||
|
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
c.Caches = map[string]*ctx.Cache{
|
c.Caches = map[string]*ctx.Cache{
|
||||||
"nsend": &ctx.Cache{Name: "nsend", Value: "0", Help: "消息发送数量"},
|
"nsend": &ctx.Cache{Name: "消息发送数量", Value: "0", Help: "消息发送数量"},
|
||||||
|
"nrecv": &ctx.Cache{Name: "消息接收数量", Value: "0", Help: "消息接收数量"},
|
||||||
|
"target": &ctx.Cache{Name: "消息接收模块", Value: "ssh", Help: "消息接收模块"},
|
||||||
|
"result": &ctx.Cache{Name: "前一条指令执行结果", Value: "", Help: "前一条指令执行结果"},
|
||||||
|
"sessid": &ctx.Cache{Name: "会话令牌", Value: "", Help: "会话令牌"},
|
||||||
}
|
}
|
||||||
c.Configs = map[string]*ctx.Config{}
|
c.Configs = map[string]*ctx.Config{}
|
||||||
|
|
||||||
s := new(SSH)
|
s := new(SSH)
|
||||||
s.Context = c
|
s.Context = c
|
||||||
s.send = make(map[string]*ctx.Message)
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,10 +41,13 @@ func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||||
|
ssh.Group = ""
|
||||||
ssh.Owner = nil
|
ssh.Owner = nil
|
||||||
ssh.Conn = m.Data["io"].(net.Conn)
|
ssh.Conn = m.Data["io"].(net.Conn)
|
||||||
ssh.Reader = bufio.NewReader(ssh.Conn)
|
ssh.Reader = bufio.NewReader(ssh.Conn)
|
||||||
m.Log("info", nil, "%d remote %v", 0, ssh.Conn.RemoteAddr())
|
ssh.Writer = bufio.NewWriter(ssh.Conn)
|
||||||
|
ssh.send = make(map[string]*ctx.Message)
|
||||||
|
m.Log("info", nil, "%s connect %v <-> %v", Pulse.Cap("nhost"), ssh.Conn.LocalAddr(), ssh.Conn.RemoteAddr())
|
||||||
|
|
||||||
target, msg := m.Target, m.Spawn(m.Target)
|
target, msg := m.Target, m.Spawn(m.Target)
|
||||||
|
|
||||||
@ -48,31 +56,30 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
|||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
|
|
||||||
if line = strings.TrimSpace(line); len(line) == 0 {
|
if line = strings.TrimSpace(line); len(line) == 0 {
|
||||||
if msg.Has("detail") {
|
if msg.Log("info", nil, "remote: %v", msg.Meta["option"]); msg.Has("detail") {
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["detail"])
|
msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"])
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["option"])
|
|
||||||
|
|
||||||
msg.Cmd(msg.Meta["detail"]...)
|
msg.Cmd(msg.Meta["detail"]...)
|
||||||
target = msg.Target
|
target = msg.Target
|
||||||
|
m.Cap("target", target.Name)
|
||||||
|
|
||||||
fmt.Fprintf(ssh.Conn, "result: ")
|
|
||||||
for _, v := range msg.Meta["result"] {
|
for _, v := range msg.Meta["result"] {
|
||||||
fmt.Fprintf(ssh.Conn, "%s", url.QueryEscape(v))
|
fmt.Fprintf(ssh.Writer, "result: %s\n", url.QueryEscape(v))
|
||||||
}
|
}
|
||||||
fmt.Fprintf(ssh.Conn, "\n")
|
|
||||||
|
|
||||||
msg.Meta["append"] = append(msg.Meta["append"], "nsend")
|
fmt.Fprintf(ssh.Writer, "nsend: %s\n", msg.Get("nrecv"))
|
||||||
msg.Add("append", "nsend", msg.Get("nsend"))
|
|
||||||
for _, k := range msg.Meta["append"] {
|
for _, k := range msg.Meta["append"] {
|
||||||
for _, v := range msg.Meta[k] {
|
for _, v := range msg.Meta[k] {
|
||||||
fmt.Fprintf(ssh.Conn, "%s: %s\n", k, v)
|
fmt.Fprintf(ssh.Writer, "%s: %s\n", k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(ssh.Conn, "\n")
|
fmt.Fprintf(ssh.Writer, "\n")
|
||||||
} else if msg.Has("result") {
|
ssh.Writer.Flush()
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["result"])
|
} else {
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["append"])
|
msg.Log("info", nil, "%s echo: %v", msg.Get("nsend"), msg.Meta["result"])
|
||||||
|
|
||||||
|
m.Cap("result", msg.Get("result"))
|
||||||
|
msg.Meta["append"] = msg.Meta["option"]
|
||||||
send := ssh.send[msg.Get("nsend")]
|
send := ssh.send[msg.Get("nsend")]
|
||||||
send.Meta = msg.Meta
|
send.Meta = msg.Meta
|
||||||
send.Recv <- true
|
send.Recv <- true
|
||||||
@ -101,36 +108,39 @@ func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool {
|
|||||||
var Pulse *ctx.Message
|
var Pulse *ctx.Message
|
||||||
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
"nhost": &ctx.Cache{Name: "nhost", Value: "0", Help: "主机数量"},
|
"nhost": &ctx.Cache{Name: "主机数量", Value: "0", Help: "主机数量"},
|
||||||
},
|
},
|
||||||
Configs: map[string]*ctx.Config{},
|
Configs: map[string]*ctx.Config{},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"listen": &ctx.Command{Name: "listen address", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"listen": &ctx.Command{Name: "listen address protocol", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
m.Find("tcp").Cmd(m.Meta["detail"]...)
|
||||||
}},
|
}},
|
||||||
"dial": &ctx.Command{Name: "dial address", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"dial": &ctx.Command{Name: "dial address protocol", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
m.Find("tcp").Cmd(m.Meta["detail"]...)
|
||||||
}},
|
}},
|
||||||
"open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Start(fmt.Sprintf("host%s", Pulse.Capi("nhost", 1)), "主机连接")
|
m.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "主机连接")
|
||||||
}},
|
}},
|
||||||
"remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
"remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
ssh, ok := m.Target.Server.(*SSH)
|
ssh, ok := m.Target.Server.(*SSH)
|
||||||
m.Assert(ok)
|
m.Assert(ok)
|
||||||
|
|
||||||
m.Capi("nsend", 1)
|
m.Capi("nsend", 1)
|
||||||
m.Recv = make(chan bool)
|
m.Add("option", "nrecv", m.Cap("nsend"))
|
||||||
m.Add("option", "nsend", m.Cap("nsend"))
|
|
||||||
ssh.send[m.Cap("nsend")] = m
|
ssh.send[m.Cap("nsend")] = m
|
||||||
|
|
||||||
for _, v := range arg {
|
for _, v := range arg {
|
||||||
fmt.Fprintf(ssh.Conn, "detail: %v\n", v)
|
fmt.Fprintf(ssh.Writer, "detail: %v\n", v)
|
||||||
}
|
}
|
||||||
for _, k := range m.Meta["option"] {
|
for _, k := range m.Meta["option"] {
|
||||||
for _, v := range m.Meta[k] {
|
for _, v := range m.Meta[k] {
|
||||||
fmt.Fprintf(ssh.Conn, "%s: %s\n", k, v)
|
fmt.Fprintf(ssh.Writer, "%s: %s\n", k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Fprintf(ssh.Conn, "\n")
|
fmt.Fprintf(ssh.Writer, "\n")
|
||||||
|
ssh.Writer.Flush()
|
||||||
|
|
||||||
|
m.Recv = make(chan bool)
|
||||||
<-m.Recv
|
<-m.Recv
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user