forked from x/ContextOS
mac mod code 修改了一些代码
This commit is contained in:
parent
4084e0ef74
commit
b6b0b55c7c
@ -52,15 +52,10 @@ snippet c
|
||||
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch `Filename()`.Context {
|
||||
case m.Target:
|
||||
if `Filename()`.Context == Index {
|
||||
return false
|
||||
}
|
||||
case m.Source:
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
var Pulse *ctx.Message
|
||||
var Index = &ctx.Context{Name: "`Filename()`", Help: "${1}",
|
||||
|
18
etc/init.sh
18
etc/init.sh
@ -1,15 +1,24 @@
|
||||
~cli
|
||||
@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
|
||||
# remote slaver listen ":9393" tcp
|
||||
~aaa
|
||||
login root root
|
||||
# ~ssh
|
||||
# listen :9191
|
||||
~tcp
|
||||
listen :9393
|
||||
~web
|
||||
listen
|
||||
|
||||
# ~tcp dial ":9393"
|
||||
# @debug on
|
||||
@ -21,7 +30,6 @@
|
||||
# @debug on
|
||||
# ~nfs
|
||||
# open hi.txt
|
||||
return
|
||||
|
||||
|
||||
@debug
|
||||
|
@ -17,7 +17,7 @@ type AAA struct {
|
||||
*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)))
|
||||
sessid := hex.EncodeToString(bs[:])
|
||||
return sessid
|
||||
@ -45,7 +45,7 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
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["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 {
|
||||
@ -60,28 +60,29 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
if m.Target == Index {
|
||||
Pulse = m
|
||||
}
|
||||
|
||||
aaa.Owner = aaa.Context
|
||||
return aaa
|
||||
}
|
||||
|
||||
func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool {
|
||||
if len(arg) > 1 {
|
||||
if m.Cap("sessid") == "" {
|
||||
m.Cap("sessid", aaa.session(arg[1]))
|
||||
Pulse.Capi("nuser", 1)
|
||||
}
|
||||
m.Log("info", m.Source, "create %s %s", m.Cap("group", arg[0]), m.Cap("username", arg[1]))
|
||||
if len(arg) > 1 && m.Cap("sessid") == "" {
|
||||
m.Cap("group", arg[0])
|
||||
m.Cap("username", arg[1])
|
||||
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
|
||||
}
|
||||
|
||||
func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch aaa.Context {
|
||||
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"))
|
||||
case m.Source:
|
||||
}
|
||||
@ -99,7 +100,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
"expire": &ctx.Config{Name: "会话超时(s)", Value: "120", Help: "会话超时"},
|
||||
},
|
||||
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
|
||||
aaa := c.Server.(*AAA)
|
||||
|
||||
@ -107,23 +108,20 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
case 0:
|
||||
m.Travel(c, func(m *ctx.Message) bool {
|
||||
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
|
||||
})
|
||||
case 1:
|
||||
if s, ok := aaa.sessions[arg[0]]; ok {
|
||||
if m.Target = s; int64(m.Capi("expire")) < time.Now().Unix() {
|
||||
s.Close(m)
|
||||
return
|
||||
}
|
||||
s, ok := aaa.sessions[arg[0]]
|
||||
m.Assert(ok, "会话失败")
|
||||
m.Target = s
|
||||
m.Assert(int64(m.Capi("expire")) > time.Now().Unix(), "会话失败")
|
||||
|
||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||
m.Log("info", m.Source, "logon %s", m.Cap("group"), m.Cap("username"))
|
||||
if m.Name != "" {
|
||||
c.Requests = append(c.Requests, m)
|
||||
m.Index = len(m.Target.Requests)
|
||||
}
|
||||
m.Log("info", m.Source, "logon %s %s", m.Cap("username"), m.Cap("group"))
|
||||
m.Echo(m.Cap("username"))
|
||||
}
|
||||
case 2, 3:
|
||||
group, username, password := arg[0], arg[0], arg[1]
|
||||
if len(arg) == 3 {
|
||||
@ -132,15 +130,10 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
|
||||
if username == Pulse.Conf("rootname") {
|
||||
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)
|
||||
} else {
|
||||
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)
|
||||
|
@ -8,6 +8,7 @@ import ( // {{{
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@ -128,11 +129,16 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
||||
|
||||
if r := rune(ls[i][0]); r == '$' || r == '_' || (!unicode.IsNumber(r) && !unicode.IsLetter(r)) {
|
||||
if c, ok := cli.alias[string(r)]; ok {
|
||||
if msg.Add("detail", c); len(ls[i]) > 1 {
|
||||
ls[i] = ls[i][1:]
|
||||
} else {
|
||||
if i == 0 {
|
||||
if msg.Add("detail", c); len(ls[i]) == 1 {
|
||||
continue
|
||||
}
|
||||
ls[i] = ls[i][1:]
|
||||
} else if len(ls[i]) > 1 {
|
||||
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.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.back = line
|
||||
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 { // {{{
|
||||
cli.Caches["result"] = &ctx.Cache{Name: "前一条指令执行结果", Value: "", Help: "前一条指令执行结果"}
|
||||
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 {
|
||||
if len(arg) > 0 { // {{{
|
||||
@ -326,6 +336,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
|
||||
m.Deal(nil, func(msg *ctx.Message, arg ...string) bool {
|
||||
if msg.Get("result") == "error: " {
|
||||
if msg.Get("detail") != "login" {
|
||||
msg.Log("system", nil, "%v", msg.Meta["detail"])
|
||||
|
||||
msg.Set("result")
|
||||
@ -345,6 +356,7 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cli.target = msg.Target
|
||||
return cli.exit == false
|
||||
@ -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) {
|
||||
cli := c.Server.(*CLI) // {{{
|
||||
cli.bio.Discard(cli.bio.Buffered())
|
||||
|
@ -136,7 +136,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
||||
|
||||
// }}}
|
||||
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)
|
||||
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"])
|
||||
@ -519,6 +519,8 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { // {{{
|
||||
if len(msg) > 2 {
|
||||
msg = msg[2:]
|
||||
}
|
||||
case *Message:
|
||||
panic(e)
|
||||
default:
|
||||
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 { // {{{
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
switch e.(type) {
|
||||
case *Message:
|
||||
panic(e)
|
||||
}
|
||||
|
||||
msg.Log("error", nil, "error: %v", e)
|
||||
if msg.root.Conf("debug") == "on" && e != io.EOF {
|
||||
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 { // {{{
|
||||
target := m.Target
|
||||
if len(root) > 0 && root[0] {
|
||||
target = m.Target.root
|
||||
target := m.Target.root
|
||||
if len(root) > 0 && !root[0] {
|
||||
target = m.Target
|
||||
}
|
||||
|
||||
cs := target.contexts
|
||||
@ -746,9 +753,7 @@ func (m *Message) Set(meta string, arg ...string) *Message { // {{{
|
||||
m.Meta[meta] = arg
|
||||
case "option", "append":
|
||||
if len(arg) > 0 {
|
||||
if _, ok := m.Meta[arg[0]]; !ok {
|
||||
m.Meta[meta] = append(m.Meta[meta], arg[0])
|
||||
}
|
||||
m.Meta[meta] = []string{arg[0]}
|
||||
m.Meta[arg[0]] = arg[1:]
|
||||
} else {
|
||||
for _, k := range m.Meta[meta] {
|
||||
@ -808,6 +813,13 @@ func (m *Message) Get(key string) string { // {{{
|
||||
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 { // {{{
|
||||
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
|
||||
if x, ok := s.Commands[key]; ok && x.Hand != nil && m.Check(c, "commands", key) {
|
||||
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 {
|
||||
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:启动参数",
|
||||
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},
|
||||
"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{"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) {
|
||||
root := true || m.Has("root") // {{{
|
||||
root := !m.Has("home") // {{{
|
||||
|
||||
ms := []*Message{}
|
||||
switch {
|
||||
@ -1277,14 +1289,17 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
|
||||
for _, v := range ms {
|
||||
m.Target = v.Target
|
||||
|
||||
v.Meta = m.Meta
|
||||
v.Data = m.Data
|
||||
switch {
|
||||
case m.Has("switch"):
|
||||
m.Target = v.Target
|
||||
case m.Has("spawn"):
|
||||
v.Set("detail", arg[2:]...).Target.Spawn(v, arg[0], arg[1]).Begin(v)
|
||||
m.Target = v.Target
|
||||
case m.Has("start"):
|
||||
v.Set("detail", arg...).Target.Start(v)
|
||||
m.Target = v.Target
|
||||
case m.Has("close"):
|
||||
v.Target.Close(v)
|
||||
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"])
|
||||
}
|
||||
}
|
||||
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:
|
||||
m.Travel(v.Target, func(msg *Message) bool {
|
||||
target := msg.Target
|
||||
@ -1338,7 +1362,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
return true
|
||||
})
|
||||
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 {
|
||||
if all {
|
||||
m.Echo("%s config:\n", m.Target.Name)
|
||||
}
|
||||
// if all {
|
||||
// m.Echo("%s config:\n", m.Target.Name)
|
||||
// }
|
||||
if x, ok := m.Target.Configs[arg[0]]; ok {
|
||||
if m.Check(m.Target, "configs", arg[0]) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
// if all {
|
||||
// m.Echo(" ")
|
||||
// }
|
||||
// m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
m.Echo("%s", x.Value)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return all
|
||||
return true
|
||||
// return all
|
||||
})
|
||||
|
||||
case 2:
|
||||
@ -1512,18 +1543,21 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s config:\n", m.Target.Name)
|
||||
}
|
||||
// if all {
|
||||
// m.Echo("%s config:\n", m.Target.Name)
|
||||
// }
|
||||
if x, ok := m.Target.Caches[arg[0]]; ok {
|
||||
if m.Check(m.Target, "caches", arg[0]) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
// if all {
|
||||
// m.Echo(" ")
|
||||
// }
|
||||
// m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
m.Echo("%s", x.Value)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return all
|
||||
return true
|
||||
// return all
|
||||
})
|
||||
case 2:
|
||||
m.Cap(arg[0], arg[1])
|
||||
|
@ -11,20 +11,25 @@ import (
|
||||
|
||||
type SSH struct {
|
||||
send map[string]*ctx.Message
|
||||
*bufio.Writer
|
||||
*bufio.Reader
|
||||
net.Conn
|
||||
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
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{}
|
||||
|
||||
s := new(SSH)
|
||||
s.Context = c
|
||||
s.send = make(map[string]*ctx.Message)
|
||||
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 {
|
||||
ssh.Group = ""
|
||||
ssh.Owner = nil
|
||||
ssh.Conn = m.Data["io"].(net.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)
|
||||
|
||||
@ -48,31 +56,30 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||
m.Assert(e)
|
||||
|
||||
if line = strings.TrimSpace(line); len(line) == 0 {
|
||||
if msg.Has("detail") {
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["detail"])
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["option"])
|
||||
if msg.Log("info", nil, "remote: %v", msg.Meta["option"]); msg.Has("detail") {
|
||||
msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"])
|
||||
|
||||
msg.Cmd(msg.Meta["detail"]...)
|
||||
target = msg.Target
|
||||
m.Cap("target", target.Name)
|
||||
|
||||
fmt.Fprintf(ssh.Conn, "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")
|
||||
msg.Add("append", "nsend", msg.Get("nsend"))
|
||||
fmt.Fprintf(ssh.Writer, "nsend: %s\n", msg.Get("nrecv"))
|
||||
for _, k := range msg.Meta["append"] {
|
||||
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")
|
||||
} else if msg.Has("result") {
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["result"])
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["append"])
|
||||
fmt.Fprintf(ssh.Writer, "\n")
|
||||
ssh.Writer.Flush()
|
||||
} else {
|
||||
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.Meta = msg.Meta
|
||||
send.Recv <- true
|
||||
@ -101,36 +108,39 @@ func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool {
|
||||
var Pulse *ctx.Message
|
||||
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
||||
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{},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"listen": &ctx.Command{Name: "listen address", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
||||
"listen": &ctx.Command{Name: "listen address protocol", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
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) {
|
||||
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
||||
"dial": &ctx.Command{Name: "dial address protocol", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
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) {
|
||||
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) {
|
||||
ssh, ok := m.Target.Server.(*SSH)
|
||||
m.Assert(ok)
|
||||
|
||||
m.Capi("nsend", 1)
|
||||
m.Recv = make(chan bool)
|
||||
m.Add("option", "nsend", m.Cap("nsend"))
|
||||
m.Add("option", "nrecv", m.Cap("nsend"))
|
||||
ssh.send[m.Cap("nsend")] = m
|
||||
|
||||
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 _, 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
|
||||
}},
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user