1
0
forked from x/ContextOS

vps add ssh.send 添加主机路由

This commit is contained in:
shylinux 2018-04-22 11:03:18 +08:00
parent e463fe87cd
commit 1a66c4c170
7 changed files with 307 additions and 208 deletions

View File

@ -261,8 +261,8 @@ func (cli *CLI) Close(m *ctx.Message, arg ...string) bool { // {{{
switch cli.Context { switch cli.Context {
case m.Target(): case m.Target():
m.Echo(cli.nfs.Cap("return")) m.Echo(cli.nfs.Cap("return"))
if p, ok := m.Source().Server.(*CLI); ok { if _, ok := m.Source().Server.(*CLI); ok {
p.target = cli.target // p.target = cli.target
} }
case m.Source(): case m.Source():
if m.Name == "aaa" { if m.Name == "aaa" {

View File

@ -295,7 +295,7 @@ type Message struct {
Index int Index int
ncallback int ncallback int
callback func(ok bool, sub *Message) (done bool, up *Message) callback func(msg *Message) (sub *Message)
Template *Message Template *Message
} }
@ -1042,8 +1042,6 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
m.target.Historys = make([]*Message, 0, 10) m.target.Historys = make([]*Message, 0, 10)
} }
m.target.Historys = append(m.target.Historys, m) m.target.Historys = append(m.target.Historys, m)
m.Back(false, nil)
}) })
return m.Get("result") return m.Get("result")
@ -1126,34 +1124,43 @@ func (m *Message) Cmd(arg ...interface{}) *Message { // {{{
} }
// }}} // }}}
func (m *Message) Call(cb func(ok bool, msg *Message) (done bool, sub *Message), cmd bool) *Message { // {{{ func (m *Message) Call(cb func(msg *Message) (sub *Message), arg ...interface{}) *Message { // {{{
m.callback = cb m.callback = cb
m.message.ncallback++
m.Wait = nil m.Wait = nil
if cmd { m.Cmd(arg...)
m.Cmd() return m
}
// }}}
func (m *Message) Back(msg *Message) *Message { // {{{
if msg == nil || m.callback == nil {
return m
} }
m.Log("callback", nil, "%v %v", msg.Meta["result"], msg.Meta["append"])
if sub := m.callback(msg); sub != nil && m.message != nil && m.message != m {
m.Log("fuck", nil, "31")
m.message.Back(sub)
}
m.Log("fuck", nil, "41")
return m return m
} }
// }}} // }}}
func (m *Message) Back(ok bool, msg *Message) *Message { // {{{ func (m *Message) CallBack(cb func(msg *Message) (sub *Message), arg ...interface{}) *Message { // {{{
if m.callback == nil { wait := make(chan bool)
return m m.Log("fuck", nil, "callback 1")
} go m.Call(func(sub *Message) *Message {
m.Log("fuck", nil, "callback 4")
m.Log("info", nil, "back %v %v", m.Meta["result"], m.Meta["append"]) wait <- true
done, sub := m.callback(ok, msg) m.Log("fuck", nil, "callback 5")
if done { return cb(sub)
m.callback = nil }, arg...)
m.message.ncallback-- m.Log("fuck", nil, "callback 2")
} <-wait
m.Log("fuck", nil, "callback 3")
if sub != nil || m.message.ncallback == 0 {
m.message.Back(ok, sub)
}
return m return m
} }
@ -1388,7 +1395,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
for i, v := range m.target.Requests { for i, v := range m.target.Requests {
m.Echo("%d %s\n", i, v.Format()) m.Echo("%d %s\n", i, v.Format())
for i, v := range v.messages { for i, v := range v.messages {
m.Echo(" %d %s\n", i, v.Format()) if v.Detail(0) != "log" {
m.Echo(" %d %s\n", i, v.Format())
}
} }
} }
@ -1401,7 +1410,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
for i, v := range m.target.Historys { for i, v := range m.target.Historys {
m.Echo("%d %s\n", i, v.Format()) m.Echo("%d %s\n", i, v.Format())
for i, v := range v.messages { for i, v := range v.messages {
m.Echo(" %d %s\n", i, v.Format()) if v.Detail(0) != "log" {
m.Echo(" %d %s\n", i, v.Format())
}
} }
} }
case 1: case 1:

View File

@ -187,17 +187,19 @@ func init() {
ctx.Index.Register(Index, log) ctx.Index.Register(Index, log)
log.color = map[string]int{ log.color = map[string]int{
"error": 31, "error": 31,
"check": 31, "check": 31,
"cmd": 32, "cmd": 32,
"conf": 33, "conf": 33,
"search": 35, "search": 35,
"find": 35, "find": 35,
"spawn": 35, "callback": 35,
"begin": 36, "lock": 35,
"start": 36, "spawn": 35,
"close": 36, "begin": 36,
"debug": 0, "start": 36,
"close": 36,
"debug": 0,
} }
log.slient = map[string]bool{} log.slient = map[string]bool{}
log.module = map[string]map[string]bool{ log.module = map[string]map[string]bool{

View File

@ -4,6 +4,7 @@ import ( // {{{
"contexts" "contexts"
"bufio" "bufio"
"encoding/json"
"fmt" "fmt"
"github.com/skip2/go-qrcode" "github.com/skip2/go-qrcode"
"io" "io"
@ -19,7 +20,8 @@ type NFS struct {
io io.ReadWriteCloser io io.ReadWriteCloser
*bufio.Reader *bufio.Reader
*bufio.Writer *bufio.Writer
send map[int]*ctx.Message send map[int]*ctx.Message
target *ctx.Context
in *os.File in *os.File
out *os.File out *os.File
@ -87,15 +89,17 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
nfs.Reader = bufio.NewReader(nfs.io) nfs.Reader = bufio.NewReader(nfs.io)
nfs.Writer = bufio.NewWriter(nfs.io) nfs.Writer = bufio.NewWriter(nfs.io)
nfs.send = make(map[int]*ctx.Message) nfs.send = make(map[int]*ctx.Message)
nfs.target = m.Target()
if target, ok := m.Data["target"]; ok {
nfs.target = target.(*ctx.Context)
}
target, msg := m.Target(), m.Spawn(m.Target()) msg := m.Spawn(nfs.target)
nfs.Caches["target"] = &ctx.Cache{Name: "target", Value: target.Name, Help: "文件名"} nfs.Caches["target"] = &ctx.Cache{Name: "target", Value: nfs.target.Name, Help: "文件名"}
nsend := 0
for { for {
line, e := nfs.Reader.ReadString('\n') line, e := nfs.Reader.ReadString('\n')
m.Assert(e) m.Assert(e)
// m.Log("debug", nil, "recv(%d): %s", len(line), line)
if line = strings.TrimSpace(line); len(line) > 0 { if line = strings.TrimSpace(line); len(line) > 0 {
ls := strings.SplitN(line, ":", 2) ls := strings.SplitN(line, ":", 2)
@ -104,61 +108,72 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Assert(e) m.Assert(e)
switch ls[0] { switch ls[0] {
case "nsend": case "detail":
n, e := strconv.Atoi(ls[1]) msg.Add("detail", ls[1])
m.Assert(e) case "result":
nsend = n msg.Add("result", ls[1])
default: default:
msg.Add("option", ls[0], ls[1]) msg.Add("option", ls[0], ls[1])
} }
continue continue
} }
if msg.Log("info", nil, "remote: %v", msg.Meta["option"]); msg.Has("detail") { if msg.Has("detail") {
msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"]) msg.Log("info", nil, "%d recv", m.Capi("nrecv", 1))
msg.Log("info", nil, "detail: %v", msg.Meta["detail"])
msg.Log("info", nil, "option: %v", msg.Meta["option"])
msg.Options("stdio", false)
func() { func() {
fuck := msg cmd := msg
fuck.Call(func(ok bool, cmd *ctx.Message) (bool, *ctx.Message) { cmd.Call(func(sub *ctx.Message) *ctx.Message {
if ok { for _, v := range sub.Meta["result"] {
target = fuck.Target() n, e := fmt.Fprintf(nfs.Writer, "result: %s\n", url.QueryEscape(v))
m.Cap("target", target.Name) sub.Assert(e)
sub.Log("fuck", nil, "write %d", n)
for _, v := range fuck.Meta["result"] {
fmt.Fprintf(nfs.Writer, "result: %s\n", url.QueryEscape(v))
}
fmt.Fprintf(nfs.Writer, "nsend: %s\n", fuck.Get("nrecv"))
for _, k := range fuck.Meta["append"] {
for _, v := range fuck.Meta[k] {
fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v)
}
}
fmt.Fprintf(nfs.Writer, "\n")
nfs.Writer.Flush()
if fuck.Has("io") {
if f, ok := fuck.Data["io"].(io.ReadCloser); ok {
io.Copy(nfs.Writer, f)
nfs.Writer.Flush()
f.Close()
}
}
return true, fuck
} }
return false, nil
}, false).Cmd(fuck.Meta["detail"]) sub.Append("nsend", sub.Option("nsend"))
for _, k := range sub.Meta["append"] {
for _, v := range sub.Meta[k] {
n, e := fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v)
sub.Assert(e)
sub.Log("fuck", nil, "write %d", n)
}
}
sub.Log("info", nil, "%d send", sub.Optioni("nsend"))
sub.Log("info", nil, "result: %v", sub.Meta["result"])
sub.Log("info", nil, "append: %v", sub.Meta["append"])
n, e := fmt.Fprintf(nfs.Writer, "\n")
sub.Assert(e)
sub.Log("sub", nil, "write %d", n)
e = nfs.Writer.Flush()
sub.Assert(e)
if sub.Has("io") {
if f, ok := sub.Data["io"].(io.ReadCloser); ok {
io.Copy(nfs.Writer, f)
nfs.Writer.Flush()
f.Close()
}
}
return sub
})
}() }()
} else { } else {
msg.Log("info", nil, "%d echo: %v", nsend, msg.Meta["result"])
m.Cap("result", msg.Get("result"))
msg.Meta["append"] = msg.Meta["option"] msg.Meta["append"] = msg.Meta["option"]
delete(msg.Meta, "option") delete(msg.Meta, "option")
send := nfs.send[nsend]
send.Meta = msg.Meta msg.Log("info", nil, "%s send", msg.Meta["nsend"])
msg.Log("info", nil, "result: %v", msg.Meta["result"])
msg.Log("info", nil, "append: %v", msg.Meta["append"])
send := nfs.send[msg.Appendi("nsend")]
send.Copy(msg, "result")
send.Copy(msg, "append")
if send.Has("io") { if send.Has("io") {
if f, ok := send.Data["io"].(io.WriteCloser); ok { if f, ok := send.Data["io"].(io.WriteCloser); ok {
@ -167,11 +182,11 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
} }
} }
send.Recv <- true send.Back(send)
} }
msg = m.Spawn(target) msg = m.Spawn(nfs.target)
m.Cap("target", target.Name) m.Cap("target", nfs.target.Name)
} }
return true return true
} }
@ -225,6 +240,7 @@ func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
for text = nfs.buf[pos] + "\n"; text != ""; { for text = nfs.buf[pos] + "\n"; text != ""; {
line := m.Spawn(yac.Target()) line := m.Spawn(yac.Target())
line.Optioni("pos", pos) line.Optioni("pos", pos)
line.Options("stdio", true)
line.Put("option", "cli", cli.Target()) line.Put("option", "cli", cli.Target())
text = line.Cmd("parse", "line", "void", text).Get("result") text = line.Cmd("parse", "line", "void", text).Get("result")
cli.Target(line.Data["cli"].(*ctx.Context)) cli.Target(line.Data["cli"].(*ctx.Context))
@ -318,34 +334,31 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
}}, }},
"listen": &ctx.Command{Name: "listen args...", Help: "启动文件服务, args: 参考tcp模块, listen命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "listen": &ctx.Command{Name: "listen args...", Help: "启动文件服务, args: 参考tcp模块, listen命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("pub", "tcp") // {{{ msg := m.Find("tcp") // {{{
msg.Call(func(ok bool, com *ctx.Message) (bool, *ctx.Message) { msg.Call(func(com *ctx.Message) *ctx.Message {
if ok { sub := com.Spawn(m.Target())
sub := msg.Spawn(m.Target()) sub.Put("option", "target", m.Source())
sub.Put("option", "io", msg.Data["io"]) sub.Put("option", "io", com.Data["io"])
sub.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件") sub.Cmd(com.Meta["detail"])
sub.Cap("stream", com.Target().Name)
sub.Cap("stream", com.Target().Name) return sub
return false, sub }, m.Meta["detail"])
}
return false, nil
}, false).Cmd(m.Meta["detail"])
// }}} // }}}
}}, }},
"accept": &ctx.Command{Name: "accept args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件")
}},
"dial": &ctx.Command{Name: "dial args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "dial": &ctx.Command{Name: "dial args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("com", "tcp") // {{{ msg := m.Sess("com", "tcp") // {{{
msg.Call(func(ok bool, com *ctx.Message) (bool, *ctx.Message) { msg.CallBack(func(com *ctx.Message) *ctx.Message {
if ok { sub := com.Spawn(m.Target())
sub := msg.Spawn(m.Target()) sub.Put("option", "target", m.Source())
sub.Put("option", "io", msg.Data["io"]) sub.Put("option", "io", com.Data["io"])
sub.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件") sub.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件")
sub.Cap("stream", com.Target().Name)
sub.Cap("stream", msg.Target().Name) m.Target(sub.Target())
m.Target(sub.Target()) return sub
return true, sub }, m.Meta["detail"])
}
return false, nil
}, false).Cmd(m.Meta["detail"])
// }}} // }}}
}}, }},
"send": &ctx.Command{Name: "send [file] args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "send": &ctx.Command{Name: "send [file] args...", Help: "连接文件服务, args: 参考tcp模块, dial命令的参数", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -365,7 +378,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
} }
} else { } else {
nfs.send[m.Optioni("nrecv", m.Capi("nsend", 1))] = m nfs.send[m.Optioni("nsend", m.Capi("nsend", 1))] = m
if len(arg) > 1 && arg[0] == "file" { if len(arg) > 1 && arg[0] == "file" {
info, e := os.Stat(arg[1]) info, e := os.Stat(arg[1])
@ -389,21 +402,19 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v) fmt.Fprintf(nfs.Writer, "%s: %s\n", k, v)
} }
} }
m.Log("info", nil, "%d send", m.Optioni("nsend"))
m.Log("info", nil, "detail: %v", m.Meta["detail"])
m.Log("info", nil, "option: %v", m.Meta["option"])
fmt.Fprintf(nfs.Writer, "\n") fmt.Fprintf(nfs.Writer, "\n")
nfs.Writer.Flush() nfs.Writer.Flush()
if true { if len(arg) > 1 && arg[0] == "file" {
if len(arg) > 1 && arg[0] == "file" { f, e := os.Open(arg[1])
f, e := os.Open(arg[1]) m.Assert(e)
m.Assert(e) defer f.Close()
defer f.Close() _, e = io.Copy(nfs.Writer, f)
_, e = io.Copy(nfs.Writer, f)
}
} }
m.Recv = make(chan bool)
<-m.Recv
} }
} // }}} } // }}}
}}, }},
@ -445,9 +456,6 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
fmt.Fprintf(nfs.Writer, "\n") fmt.Fprintf(nfs.Writer, "\n")
nfs.Writer.Flush() nfs.Writer.Flush()
m.Recv = make(chan bool)
<-m.Recv
} // }}} } // }}}
}}, }},
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
@ -542,6 +550,18 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
} }
qrcode.WriteFile(strings.Join(arg[1:], ""), qrcode.Medium, size, arg[0]) // }}} qrcode.WriteFile(strings.Join(arg[1:], ""), qrcode.Medium, size, arg[0]) // }}}
}}, }},
"json": &ctx.Command{Name: "json file", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
buf, e := json.Marshal(m.Data["data"]) // {{{
m.Assert(e)
f, e := os.Create(arg[0])
m.Assert(e)
f.Write(buf)
f.Close()
m.Echo(string(buf))
// }}}
}},
"pwd": &ctx.Command{Name: "pwd", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "pwd": &ctx.Command{Name: "pwd", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
wd, e := os.Getwd() // {{{ wd, e := os.Getwd() // {{{

View File

@ -53,76 +53,110 @@ func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool { // {{{
// }}} // }}}
func Done(m *ctx.Message, lock chan bool) {
m.Log("lock", nil, "done before %v", m.Meta["detail"])
if m.Options("stdio") {
lock <- true
}
m.Log("lock", nil, "done after %v", m.Meta["detail"])
}
func Wait(m *ctx.Message, lock chan bool) {
m.Log("lock", nil, "wait before %v", m.Meta["detail"])
if m.Options("stdio") {
<-lock
}
m.Log("lock", nil, "wait after %v", m.Meta["detail"])
}
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: "主机数量", Value: "0", Help: "主机数量"}, "nhost": &ctx.Cache{Name: "主机数量", Value: "0", Help: "主机数量"},
"route": &ctx.Cache{Name: "route", Value: "ssh", Help: "主机数量"}, "route": &ctx.Cache{Name: "route", Value: "com", Help: "主机数量"},
"count": &ctx.Cache{Name: "count", Value: "3", Help: "主机数量"},
"share": &ctx.Cache{Name: "share", Value: "root", Help: "主机数量"},
"level": &ctx.Cache{Name: "level", Value: "root", Help: "主机数量"},
}, },
Configs: map[string]*ctx.Config{ Configs: map[string]*ctx.Config{
"route": &ctx.Config{Name: "route", Value: "com", Help: "主机数量"}, "route": &ctx.Config{Name: "route", Value: "com", Help: "主机数量"},
"route.json": &ctx.Config{Name: "route.json", Value: "var/route.json", Help: "主机数量"},
"route.png": &ctx.Config{Name: "route.png", Value: "var/route.png", Help: "主机数量"},
"type": &ctx.Config{Name: "type", Value: "terminal", Help: "主机数量"},
"kind": &ctx.Config{Name: "kind", Value: "terminal", Help: "主机数量"},
"name": &ctx.Config{Name: "name", Value: "vps", Help: "主机数量"},
"mark": &ctx.Config{Name: "mark", Value: "com", Help: "主机数量"},
}, },
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"listen": &ctx.Command{Name: "listen address protocol", 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) {
msg := m.Find("nfs") msg := m.Find("nfs") // {{{
msg.Call(func(ok bool, file *ctx.Message) (bool, *ctx.Message) { msg.Call(func(file *ctx.Message) *ctx.Message {
if ok { sub := file.Spawn(m.Target())
sub := msg.Spawn(m.Target()) sub.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "远程主机")
sub.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "远程主机") sub.Cap("stream", file.Target().Name)
sub.Cap("stream", file.Target().Name)
sub.Sess("file", "nfs."+file.Target().Name)
sub.Sess("file").Cmd("send", "context", "ssh")
sub.Sess("file").Cmd("send", "route", sub.Target().Name, msg.Cap("route"))
return false, sub
}
return false, nil
}, false).Cmd(m.Meta["detail"])
sub.Sess("file", "nfs."+file.Target().Name)
sub.Sess("file").Cmd("send", "route", sub.Target().Name, msg.Cap("route"))
return sub
}, m.Meta["detail"])
// }}}
}}, }},
"dial": &ctx.Command{Name: "dial address protocol", 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) {
msg := m.Sess("file", "nfs") msg := m.Sess("file", "nfs") // {{{
msg.Call(func(ok bool, file *ctx.Message) (bool, *ctx.Message) { msg.Call(func(file *ctx.Message) *ctx.Message {
if ok { m.Cap("stream", file.Target().Name)
m.Cap("stream", msg.Target().Name) return m
m.Sess("file").Cmd("send", "context", "ssh") }, m.Meta["detail"])
return true, m // }}}
}
return false, nil
}, false).Cmd(m.Meta["detail"])
}},
"send": &ctx.Command{Name: "send arg...", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
msg := m.Sess("file")
msg.Copy(m, "detail").Call(func(ok bool, file *ctx.Message) (bool, *ctx.Message) {
return ok, file
}, false).Cmd()
m.Copy(msg, "result")
}},
"pwd": &ctx.Command{Name: "pwd", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Echo(m.Cap("route"))
}}, }},
"route": &ctx.Command{Name: "route", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "route": &ctx.Command{Name: "route", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Conf("route", arg[0]) m.Conf("route", arg[0]) // {{{
m.Cap("route", arg[1]+"."+arg[0]) m.Cap("route", arg[1]+"."+arg[0])
info := map[string]string{"route": m.Cap("route")}
msg := m.Sess("file")
msg.Put("option", "data", info)
msg.Cmd("json", m.Conf("route.json"))
png := m.Sess("file")
png.Cmd("genqr", m.Conf("route.png"), msg.Result(0))
m.Back(m)
// }}}
}}, }},
"search": &ctx.Command{Name: "search route cmd arg...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "pwd": &ctx.Command{Name: "pwd", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if _, ok := m.Target().Server.(*SSH); m.Assert(ok) { m.Echo(m.Cap("route")) // {{{
m.Back(m)
// }}}
}},
"send": &ctx.Command{Name: "send route cmd arg...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if _, ok := m.Target().Server.(*SSH); m.Assert(ok) { // {{{
lock := make(chan bool)
if len(arg[0]) == 0 { if len(arg[0]) == 0 {
msg := m.Spawn(m.Target()).Cmd(arg[1:]) msg := m.Spawn(m.Target()).Cmd(arg[1:])
m.Copy(msg, "result") m.Copy(msg, "result")
m.Copy(msg, "append")
m.Back(m)
return return
} }
miss := true miss := true
self := true
target := strings.Split(arg[0], ".") target := strings.Split(arg[0], ".")
m.Travel(m.Target(), func(m *ctx.Message) bool { m.Travel(m.Target(), func(m *ctx.Message) bool {
if self {
self = false
return true
}
if m.Target().Name == target[0] { if m.Target().Name == target[0] {
msg := m.Spawn(m.Target()) msg := m.Sess("file")
msg.Call(func(ok bool, host *ctx.Message) (bool, *ctx.Message) { msg.Call(func(host *ctx.Message) *ctx.Message {
m.Copy(msg, "result") m.Copy(host, "result")
return ok, host m.Copy(host, "append")
}, false).Cmd("send", "search", strings.Join(target[1:], "."), arg[1:]) Done(m, lock)
return m
}, "send", "send", strings.Join(target[1:], "."), arg[1:])
miss = false miss = false
} }
@ -130,21 +164,42 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
}) })
if miss { if miss {
msg := m.Spawn(c) if target[0] == m.Conf("route") {
msg.Call(func(ok bool, host *ctx.Message) (bool, *ctx.Message) { m.Spawn(m.Target()).Call(func(host *ctx.Message) *ctx.Message {
m.Copy(msg, "result") m.Copy(host, "result")
return ok, host m.Copy(host, "append")
}, false).Cmd("send", "search", arg) Done(m, lock)
return m
}, "send", strings.Join(target[1:], "."), arg[1:])
} else if m.Cap("route") != "ssh" {
msg := m.Sess("file")
msg.Call(func(host *ctx.Message) *ctx.Message {
m.Copy(host, "result")
m.Copy(host, "append")
m.Back(m)
Done(m, lock)
return nil
}, "send", "send", arg)
} else {
m.Back(m)
return
}
} }
Wait(m, lock)
} }
// }}}
}}, }},
"dispatch": &ctx.Command{Name: "dispatch route cmd arg...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "good": &ctx.Command{Name: "good", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Travel(m.Target(), func(m *ctx.Message) bool { m.Append("share", m.Cap("share")) // {{{
m.Append("level", m.Cap("level"))
msg := m.Spawn(m.Target()) m.Append("type", m.Conf("type"))
msg.Cmd("send", arg) m.Append("value", m.Cap("route"))
return true m.Append("kind", m.Conf("kind"))
}) m.Append("name", m.Conf("name"))
m.Append("mark", m.Conf("mark"))
m.Append("count", m.Cap("count"))
m.Back(m)
// }}}
}}, }},
"register": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "register": &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)

View File

@ -69,7 +69,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"), m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"),
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr())))) m.Append("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
m.Put("append", "io", tcp.Conn).Back(true, m) m.Put("append", "io", tcp.Conn).Back(m)
return false return false
case "accept": case "accept":
c, e := m.Data["io"].(net.Conn) c, e := m.Data["io"].(net.Conn)
@ -78,8 +78,7 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"), m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"),
m.Append("stream", m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr())))) m.Append("stream", m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
m.Put("append", "io", tcp.Conn).Back(true, m) m.Put("append", "io", tcp.Conn).Back(m)
m.Log("fuck", nil, "accept")
return false return false
default: default:
if m.Cap("security") != "false" { if m.Cap("security") != "false" {
@ -103,16 +102,9 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
c, e := tcp.Accept() c, e := tcp.Accept()
m.Assert(e) m.Assert(e)
msg := m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source()) msg := m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source())
msg.Call(func(ok bool, com *ctx.Message) (bool, *ctx.Message) { msg.Call(func(com *ctx.Message) *ctx.Message {
if ok { return com
m.Append("stream", msg.Append("stream")) }, "accept", c.RemoteAddr().String(), m.Cap("security"), m.Cap("protocol"))
m.Put("append", "io", msg.Data["io"])
com.Log("fuck", nil, "listen")
}
return ok, com
}, false).Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接",
"accept", c.RemoteAddr().String(), m.Cap("security"), m.Cap("protocol"))
} }
return true return true
@ -161,6 +153,10 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("pub%d", Pulse.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...) m.Start(fmt.Sprintf("pub%d", Pulse.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...)
}}, }},
"accept": &ctx.Command{Name: "accept address [security [protocol]]", Help: "网络连接",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
}},
"dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接", "dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接",
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...) m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)

View File

@ -1,12 +1,14 @@
package yac package yac // {{{
// }}}
import ( import ( // {{{
"contexts" "contexts"
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
) )
// }}}
type Seed struct { type Seed struct {
page int page int
hash int hash int
@ -37,14 +39,15 @@ type YAC struct {
*ctx.Context *ctx.Context
} }
func (yac *YAC) name(page int) string { func (yac *YAC) name(page int) string { // {{{
if name, ok := yac.word[page]; ok { if name, ok := yac.word[page]; ok {
return name return name
} }
return fmt.Sprintf("yac%d", page) return fmt.Sprintf("yac%d", page)
} }
func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Point, []*Point) { // }}}
func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Point, []*Point) { // {{{
ss := []int{page} ss := []int{page}
sn := make([]bool, yac.Capi("nline")) sn := make([]bool, yac.Capi("nline"))
@ -183,7 +186,8 @@ func (yac *YAC) train(m *ctx.Message, page, hash int, word []string) (int, []*Po
return len(word), points, ends return len(word), points, ends
} }
func (yac *YAC) parse(m *ctx.Message, cli *ctx.Context, page, void int, line string) (*ctx.Context, string, []string) { // }}}
func (yac *YAC) parse(m *ctx.Message, cli *ctx.Context, page, void int, line string) (*ctx.Context, string, []string) { // {{{
level := m.Capi("level", 1) level := m.Capi("level", 1)
yac.Log("debug", nil, fmt.Sprintf("%s\\%d %s(%d):", m.Cap("label")[0:level], level, yac.word[page], page)) yac.Log("debug", nil, fmt.Sprintf("%s\\%d %s(%d):", m.Cap("label")[0:level], level, yac.word[page], page))
@ -252,7 +256,9 @@ func (yac *YAC) parse(m *ctx.Message, cli *ctx.Context, page, void int, line str
return cli, line, word return cli, line, word
} }
func (yac *YAC) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // }}}
func (yac *YAC) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
c.Caches = map[string]*ctx.Cache{} c.Caches = map[string]*ctx.Cache{}
c.Configs = map[string]*ctx.Config{} c.Configs = map[string]*ctx.Config{}
@ -261,7 +267,8 @@ func (yac *YAC) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
return s return s
} }
func (yac *YAC) Begin(m *ctx.Message, arg ...string) ctx.Server { // }}}
func (yac *YAC) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
if yac.Message = m; yac.Context == Index { if yac.Message = m; yac.Context == Index {
Pulse = m Pulse = m
} }
@ -299,12 +306,14 @@ func (yac *YAC) Begin(m *ctx.Message, arg ...string) ctx.Server {
return yac return yac
} }
func (yac *YAC) Start(m *ctx.Message, arg ...string) bool { // }}}
func (yac *YAC) Start(m *ctx.Message, arg ...string) bool { // {{{
yac.Message = m yac.Message = m
return false return false
} }
func (yac *YAC) Close(m *ctx.Message, arg ...string) bool { // }}}
func (yac *YAC) Close(m *ctx.Message, arg ...string) bool { // {{{
switch yac.Context { switch yac.Context {
case m.Target(): case m.Target():
case m.Source(): case m.Source():
@ -312,13 +321,15 @@ func (yac *YAC) Close(m *ctx.Message, arg ...string) bool {
return true return true
} }
// }}}
var Pulse *ctx.Message var Pulse *ctx.Message
var Index = &ctx.Context{Name: "yac", Help: "语法中心", var Index = &ctx.Context{Name: "yac", Help: "语法中心",
Caches: map[string]*ctx.Cache{}, Caches: map[string]*ctx.Cache{},
Configs: map[string]*ctx.Config{}, Configs: map[string]*ctx.Config{},
Commands: map[string]*ctx.Command{ Commands: map[string]*ctx.Command{
"train": &ctx.Command{Name: "train page hash word...", Help: "添加语法规则, page: 语法集合, hash: 语句类型, word: 语法模板", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "train": &ctx.Command{Name: "train page hash word...", Help: "添加语法规则, page: 语法集合, hash: 语句类型, word: 语法模板", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
page, ok := yac.page[arg[0]] page, ok := yac.page[arg[0]]
if !ok { if !ok {
page = m.Capi("npage", 1) page = m.Capi("npage", 1)
@ -352,9 +363,10 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
yac.seed = append(yac.seed, &Seed{page, hash, arg[2:]}) yac.seed = append(yac.seed, &Seed{page, hash, arg[2:]})
yac.Cap("stream", fmt.Sprintf("%d,%s,%s", yac.Capi("nseed", 1), yac.Cap("npage"), yac.Cap("nhash"))) yac.Cap("stream", fmt.Sprintf("%d,%s,%s", yac.Capi("nseed", 1), yac.Cap("npage"), yac.Cap("nhash")))
} }
// }}}
}}, }},
"parse": &ctx.Command{Name: "parse page void word...", Help: "解析语句, page: 语法集合, void: 空白语法集合, word: 语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "parse": &ctx.Command{Name: "parse page void word...", Help: "解析语句, page: 语法集合, void: 空白语法集合, word: 语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
page, ok := yac.page[arg[0]] page, ok := yac.page[arg[0]]
m.Assert(ok) m.Assert(ok)
void, ok := yac.page[arg[1]] void, ok := yac.page[arg[1]]
@ -366,9 +378,10 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
m.Result(0, rest, word) m.Result(0, rest, word)
} }
} }
// }}}
}}, }},
"info": &ctx.Command{Name: "info", Help: "显示缓存", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "info": &ctx.Command{Name: "info", Help: "显示缓存", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
for i, v := range yac.seed { for i, v := range yac.seed {
m.Echo("seed: %d %v\n", i, v) m.Echo("seed: %d %v\n", i, v)
} }
@ -389,9 +402,10 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
} }
} }
} }
// }}}
}}, }},
"check": &ctx.Command{Name: "check page void word...", Help: "解析语句, page: 语法集合, void: 空白语法集合, word: 语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { "check": &ctx.Command{Name: "check page void word...", Help: "解析语句, page: 语法集合, void: 空白语法集合, word: 语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { if yac, ok := m.Target().Server.(*YAC); m.Assert(ok) { // {{{
set := map[*State]bool{} set := map[*State]bool{}
nreal := 0 nreal := 0
for _, v := range yac.state { for _, v := range yac.state {
@ -414,6 +428,7 @@ var Index = &ctx.Context{Name: "yac", Help: "语法中心",
} }
m.Log("fuck", nil, "node: %d real: %d", nnode, nreal) m.Log("fuck", nil, "node: %d real: %d", nnode, nreal)
} }
// }}}
}}, }},
}, },
} }