mirror of
https://shylinux.com/x/ContextOS
synced 2025-04-26 17:24:05 +08:00
mac pro code
This commit is contained in:
parent
db10496c7f
commit
4084e0ef74
19
etc/init.sh
19
etc/init.sh
@ -1,19 +1,26 @@
|
|||||||
~cli
|
~cli
|
||||||
@lex lex
|
@lex lex
|
||||||
|
# ~cli
|
||||||
|
# remote slaver listen ":9393" tcp
|
||||||
~aaa
|
~aaa
|
||||||
login root root
|
login root root
|
||||||
|
# ~ssh
|
||||||
|
# listen :9191
|
||||||
~tcp
|
~tcp
|
||||||
listen :9393
|
listen :9393
|
||||||
|
~web
|
||||||
|
listen
|
||||||
|
|
||||||
# ~tcp dial ":9393"
|
# ~tcp dial ":9393"
|
||||||
# ~cli remote slaver listen ":9393" tcp
|
|
||||||
# @debug on
|
# @debug on
|
||||||
|
# ~aaa
|
||||||
# ~mdb open chat chat "chat:chat@/chat" mysql
|
# login shy shy
|
||||||
|
# ~mdb
|
||||||
|
# open chat:chat@/chat mysql
|
||||||
# ~web listen
|
# ~web listen
|
||||||
# ~ssh listen :9898
|
# @debug on
|
||||||
|
# ~nfs
|
||||||
|
# open hi.txt
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package aaa // {{{
|
package aaa
|
||||||
// }}}
|
|
||||||
import ( // {{{
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
@ -12,22 +12,18 @@ import ( // {{{
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
type AAA struct {
|
type AAA struct {
|
||||||
sessions map[string]*ctx.Context
|
sessions map[string]*ctx.Context
|
||||||
*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
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
|
|
||||||
func (aaa *AAA) 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{}
|
||||||
|
|
||||||
@ -36,32 +32,29 @@ func (aaa *AAA) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|
||||||
aaa.Caches["group"] = &ctx.Cache{Name: "用户组", Value: "", Help: "用户组"}
|
aaa.Caches["group"] = &ctx.Cache{Name: "用户组", Value: "", Help: "用户组"}
|
||||||
aaa.Caches["username"] = &ctx.Cache{Name: "用户名", Value: "", Help: "用户名"}
|
aaa.Caches["username"] = &ctx.Cache{Name: "用户名", Value: "", Help: "用户名"}
|
||||||
aaa.Caches["password"] = &ctx.Cache{Name: "用户密码", Value: "", Help: "用户密码,加密存储", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
aaa.Caches["password"] = &ctx.Cache{Name: "用户密码", Value: "", Help: "用户密码,加密存储", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
||||||
if len(arg) > 0 { // {{{
|
if len(arg) > 0 {
|
||||||
bs := md5.Sum([]byte(fmt.Sprintln("用户密码:%s", arg[0])))
|
bs := md5.Sum([]byte(fmt.Sprintln("用户密码:%s", arg[0])))
|
||||||
m.Assert(x.Value == "" || x.Value == hex.EncodeToString(bs[:]), "密码错误")
|
m.Assert(x.Value == "" || x.Value == hex.EncodeToString(bs[:]), "密码错误")
|
||||||
m.Cap("expire", fmt.Sprintf("%d", time.Now().Unix()+int64(Pulse.Confi("expire"))))
|
m.Cap("expire", fmt.Sprintf("%d", time.Now().Unix()+int64(Pulse.Confi("expire"))))
|
||||||
return hex.EncodeToString(bs[:])
|
return hex.EncodeToString(bs[:])
|
||||||
}
|
}
|
||||||
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 {
|
||||||
return arg[0]
|
return arg[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
n, e := strconv.Atoi(x.Value)
|
n, e := strconv.Atoi(x.Value)
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
return time.Unix(int64(n), 0).Format("15:03:04")
|
return time.Unix(int64(n), 0).Format("15:03:04")
|
||||||
// }}}
|
|
||||||
}}
|
}}
|
||||||
|
|
||||||
if m.Target == Index {
|
if m.Target == Index {
|
||||||
@ -72,8 +65,7 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|||||||
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 {
|
||||||
if m.Cap("sessid") == "" {
|
if m.Cap("sessid") == "" {
|
||||||
m.Cap("sessid", aaa.session(arg[1]))
|
m.Cap("sessid", aaa.session(arg[1]))
|
||||||
@ -87,25 +79,16 @@ func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
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:
|
||||||
if aaa.Context == Index {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(aaa.Context.Requests) == 0 {
|
|
||||||
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:
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
var Pulse *ctx.Message
|
var Pulse *ctx.Message
|
||||||
var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
@ -116,8 +99,8 @@ 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) 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)
|
||||||
|
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
@ -130,7 +113,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
if s, ok := aaa.sessions[arg[0]]; ok {
|
if s, ok := aaa.sessions[arg[0]]; ok {
|
||||||
if m.Target = s; int64(m.Capi("expire")) < time.Now().Unix() {
|
if m.Target = s; int64(m.Capi("expire")) < time.Now().Unix() {
|
||||||
s.Close(m)
|
s.Close(m)
|
||||||
return ""
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||||
@ -139,7 +122,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
c.Requests = append(c.Requests, m)
|
c.Requests = append(c.Requests, m)
|
||||||
m.Index = len(m.Target.Requests)
|
m.Index = len(m.Target.Requests)
|
||||||
}
|
}
|
||||||
return m.Cap("username")
|
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]
|
||||||
@ -163,10 +146,8 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
|||||||
m.Cap("password", password)
|
m.Cap("password", password)
|
||||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||||
aaa.sessions[m.Cap("sessid")] = m.Target
|
aaa.sessions[m.Cap("sessid")] = m.Target
|
||||||
return m.Cap("sessid")
|
m.Echo(m.Cap("sessid"))
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Index: map[string]*ctx.Context{
|
Index: map[string]*ctx.Context{
|
||||||
|
@ -386,7 +386,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
},
|
},
|
||||||
Configs: map[string]*ctx.Config{},
|
Configs: map[string]*ctx.Config{},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"source": &ctx.Command{Name: "source file", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"source": &ctx.Command{Name: "source file", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
cli := c.Server.(*CLI) // {{{
|
cli := c.Server.(*CLI) // {{{
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
case 1:
|
case 1:
|
||||||
@ -395,16 +395,14 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
cli.push(f)
|
cli.push(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) 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())
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"alias": &ctx.Command{Name: "alias [short [long]]", Help: "查看日志", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
cli := c.Server.(*CLI) // {{{
|
cli := c.Server.(*CLI) // {{{
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
case 0:
|
case 0:
|
||||||
@ -423,12 +421,11 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
cli.alias[arg[0]] = strings.Join(arg[1:], " ")
|
cli.alias[arg[0]] = strings.Join(arg[1:], " ")
|
||||||
m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]])
|
m.Echo("%s: %s\n", arg[0], cli.alias[arg[0]])
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"remote": &ctx.Command{Name: "remote [send args...]|[[master|slaver] listen|dial address protocol]", Help: "建立远程连接",
|
"remote": &ctx.Command{Name: "remote [send args...]|[[master|slaver] listen|dial address protocol]", Help: "建立远程连接",
|
||||||
Formats: map[string]int{"send": -1, "master": 0, "slaver": 0, "listen": 1, "dial": 1},
|
Formats: map[string]int{"send": -1, "master": 0, "slaver": 0, "listen": 1, "dial": 1},
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
if m.Has("send") { // {{{
|
if m.Has("send") { // {{{
|
||||||
cli := m.Target.Server.(*CLI)
|
cli := m.Target.Server.(*CLI)
|
||||||
|
|
||||||
@ -441,7 +438,7 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
cli.bufs = cli.bufs[0:0]
|
cli.bufs = cli.bufs[0:0]
|
||||||
m.Echo("\n~~~remote~~~\n")
|
m.Echo("\n~~~remote~~~\n")
|
||||||
|
|
||||||
return ""
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
action := "dial"
|
action := "dial"
|
||||||
@ -456,19 +453,17 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
}
|
}
|
||||||
msg.Cmd(action, m.Get(action))
|
msg.Cmd(action, m.Get(action))
|
||||||
|
|
||||||
return ""
|
|
||||||
}},
|
}},
|
||||||
// }}}
|
// }}}
|
||||||
"open": &ctx.Command{Name: "open [master|slaver] [script [log]]", Help: "建立远程连接",
|
"open": &ctx.Command{Name: "open [master|slaver] [script [log]]", Help: "建立远程连接",
|
||||||
Options: map[string]string{"master": "主控终端", "slaver": "被控终端", "args": "启动参数", "io": "读写流"},
|
Options: map[string]string{"master": "主控终端", "slaver": "被控终端", "args": "启动参数", "io": "读写流"},
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Start(fmt.Sprintf("PTS%d", m.Capi("nterm")), "管理终端", "void.sh") // {{{
|
m.Start(fmt.Sprintf("PTS%d", m.Capi("nterm")), "管理终端", "void.sh") // {{{
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"master": &ctx.Command{Name: "open [master|slaver] [script [log]]", Help: "建立远程连接",
|
"master": &ctx.Command{Name: "open [master|slaver] [script [log]]", Help: "建立远程连接",
|
||||||
Options: map[string]string{"master": "主控终端", "slaver": "被控终端", "args": "启动参数", "io": "读写流"},
|
Options: map[string]string{"master": "主控终端", "slaver": "被控终端", "args": "启动参数", "io": "读写流"},
|
||||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
cli, ok := c.Server.(*CLI) // {{{
|
cli, ok := c.Server.(*CLI) // {{{
|
||||||
m.Assert(ok, "模块类型错误")
|
m.Assert(ok, "模块类型错误")
|
||||||
m.Assert(m.Target != c, "模块是主控模块")
|
m.Assert(m.Target != c, "模块是主控模块")
|
||||||
@ -476,14 +471,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
|||||||
msg := m.Spawn(c)
|
msg := m.Spawn(c)
|
||||||
msg.Start(fmt.Sprintf("PTS%d", cli.Capi("nterm")), arg[0], arg[1:]...)
|
msg.Start(fmt.Sprintf("PTS%d", cli.Capi("nterm")), arg[0], arg[1:]...)
|
||||||
m.Target.Master = msg.Target
|
m.Target.Master = msg.Target
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Index: map[string]*ctx.Context{
|
Index: map[string]*ctx.Context{
|
||||||
"void": &ctx.Context{Name: "void",
|
"void": &ctx.Context{Name: "void",
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"context": &ctx.Command{},
|
|
||||||
"open": &ctx.Command{},
|
"open": &ctx.Command{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -37,7 +37,7 @@ type Command struct {
|
|||||||
Formats map[string]int
|
Formats map[string]int
|
||||||
Options map[string]string
|
Options map[string]string
|
||||||
Appends map[string]string
|
Appends map[string]string
|
||||||
Hand func(m *Message, c *Context, key string, arg ...string) string
|
Hand func(m *Message, c *Context, key string, arg ...string)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Server interface {
|
type Server interface {
|
||||||
@ -120,7 +120,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
|||||||
c.Owner = m.Master.Owner
|
c.Owner = m.Master.Owner
|
||||||
c.Group = m.Master.Group
|
c.Group = m.Master.Group
|
||||||
|
|
||||||
m.Log("begin", nil, "%d context %v", m.Capi("ncontext", 1), m.Meta["detail"])
|
m.Log("begin", nil, "%d context %v", m.root.Capi("ncontext", 1), m.Meta["detail"])
|
||||||
for k, x := range c.Configs {
|
for k, x := range c.Configs {
|
||||||
if x.Hand != nil {
|
if x.Hand != nil {
|
||||||
m.Conf(k, x.Value)
|
m.Conf(k, x.Value)
|
||||||
@ -139,7 +139,7 @@ func (c *Context) Start(m *Message) bool { // {{{
|
|||||||
if c.Requests = append(c.Requests, m); m.Cap("status") != "start" {
|
if c.Requests = append(c.Requests, m); 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.Capi("nserver", 1), m.Meta["detail"])
|
m.Log(m.Cap("status", "start"), nil, "%d server %v", m.root.Capi("nserver", 1), m.Meta["detail"])
|
||||||
|
|
||||||
if running <- true; c.Server != nil && c.Server.Start(m, m.Meta["detail"]...) {
|
if running <- true; c.Server != nil && c.Server.Start(m, m.Meta["detail"]...) {
|
||||||
c.Close(m, m.Meta["detail"]...)
|
c.Close(m, m.Meta["detail"]...)
|
||||||
@ -828,7 +828,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
|
|||||||
for s := c; s != nil; s = s.context {
|
for s := c; s != nil; s = s.context {
|
||||||
|
|
||||||
m.Master = m.Source
|
m.Master = m.Source
|
||||||
if x, ok := s.Commands[key]; ok && m.Check(s, "commands", key) && x.Hand != nil {
|
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", key, arg)
|
||||||
|
|
||||||
@ -863,11 +863,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
|
|||||||
arg = m.Meta["args"]
|
arg = m.Meta["args"]
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Set("result")
|
x.Hand(m.Set("result").Set("append"), s, key, arg...)
|
||||||
m.Set("append")
|
|
||||||
if ret := x.Hand(m, s, key, arg...); ret != "" {
|
|
||||||
m.Echo(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
if x.Appends != nil {
|
if x.Appends != nil {
|
||||||
for _, v := range m.Meta["append"] {
|
for _, v := range m.Meta["append"] {
|
||||||
@ -1007,7 +1003,7 @@ func (m *Message) Conf(key string, arg ...string) string { // {{{
|
|||||||
return m.Conf(key, arg[1])
|
return m.Conf(key, arg[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Assert(true, "配置项操作错误")
|
m.Assert(false, key+"配置项操作错误")
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1066,7 +1062,7 @@ func (m *Message) Cap(key string, arg ...string) string { // {{{
|
|||||||
return m.Cap(key, arg[1])
|
return m.Cap(key, arg[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Assert(true, "缓存项操作错误")
|
m.Assert(false, key+"缓存项操作错误")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1131,7 +1127,7 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
Commands: map[string]*Command{
|
Commands: map[string]*Command{
|
||||||
"userinfo": &Command{Name: "userinfo [add|del [context key name help]|[command|config|cache group name]]", Help: "查看模块的用户信息",
|
"userinfo": &Command{Name: "userinfo [add|del [context key name help]|[command|config|cache group name]]", Help: "查看模块的用户信息",
|
||||||
Formats: map[string]int{"add": -1, "del": -1},
|
Formats: map[string]int{"add": -1, "del": -1},
|
||||||
Hand: func(m *Message, c *Context, key string, arg ...string) string {
|
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
switch { // {{{
|
switch { // {{{
|
||||||
case m.Has("add"):
|
case m.Has("add"):
|
||||||
m.Target.Add(m.Source.Group, m.Meta["add"]...)
|
m.Target.Add(m.Source.Group, m.Meta["add"]...)
|
||||||
@ -1164,10 +1160,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"server": &Command{Name: "server [start|exit|switch][args]", Help: "服务启动停止切换", Hand: func(m *Message, c *Context, key string, arg ...string) string {
|
"server": &Command{Name: "server [start|exit|switch][args]", Help: "服务启动停止切换", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
switch len(arg) { // {{{
|
switch len(arg) { // {{{
|
||||||
case 0:
|
case 0:
|
||||||
m.Travel(m.Target.root, func(m *Message) bool {
|
m.Travel(m.Target.root, func(m *Message) bool {
|
||||||
@ -1187,10 +1182,9 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
case "switch":
|
case "switch":
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"message": &Command{Name: "message code", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) string {
|
"message": &Command{Name: "message code", Help: "查看消息", Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
switch len(arg) { // {{{
|
switch len(arg) { // {{{
|
||||||
case 0:
|
case 0:
|
||||||
m.Echo("\033[31mrequests:\033[0m\n")
|
m.Echo("\033[31mrequests:\033[0m\n")
|
||||||
@ -1254,12 +1248,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"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 [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},
|
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},
|
||||||
Hand: func(m *Message, c *Context, key string, arg ...string) string {
|
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
root := true || m.Has("root") // {{{
|
root := true || m.Has("root") // {{{
|
||||||
|
|
||||||
ms := []*Message{}
|
ms := []*Message{}
|
||||||
@ -1348,12 +1341,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
v.Set("detail", arg...).Cmd()
|
v.Set("detail", arg...).Cmd()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"command": &Command{Name: "command [all] [key [name help]]", Help: "查看或修改命令",
|
"command": &Command{Name: "command [all] [key [name help]]", Help: "查看或修改命令",
|
||||||
Formats: map[string]int{"all": 0, "delete": 0, "void": 0},
|
Formats: map[string]int{"all": 0, "delete": 0, "void": 0},
|
||||||
Hand: func(m *Message, c *Context, key string, arg ...string) string {
|
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
all := m.Has("all") // {{{
|
all := m.Has("all") // {{{
|
||||||
|
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
@ -1428,12 +1420,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"config": &Command{Name: "config [all] [[delete|void] key [value]|[name value help]]", Help: "删除、置空、查看、修改或添加配置",
|
"config": &Command{Name: "config [all] [[delete|void] key [value]|[name value help]]", Help: "删除、置空、查看、修改或添加配置",
|
||||||
Formats: map[string]int{"all": 0, "delete": 0, "void": 0},
|
Formats: map[string]int{"all": 0, "delete": 0, "void": 0},
|
||||||
Hand: func(m *Message, c *Context, key string, arg ...string) string {
|
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
all := m.Has("all") // {{{
|
all := m.Has("all") // {{{
|
||||||
|
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
@ -1484,12 +1475,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
case 4:
|
case 4:
|
||||||
m.Conf(arg[0], arg[1:]...)
|
m.Conf(arg[0], arg[1:]...)
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
"cache": &Command{Name: "cache [all] [[delete|void] key [value]|[name value help]]", Help: "删除、置空、查看、修改或添加缓存",
|
"cache": &Command{Name: "cache [all] [[delete|void] key [value]|[name value help]]", Help: "删除、置空、查看、修改或添加缓存",
|
||||||
Formats: map[string]int{"all": 0, "delete": 0, "void": 0},
|
Formats: map[string]int{"all": 0, "delete": 0, "void": 0},
|
||||||
Hand: func(m *Message, c *Context, key string, arg ...string) string {
|
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||||
all := m.Has("all") // {{{
|
all := m.Has("all") // {{{
|
||||||
|
|
||||||
switch len(arg) {
|
switch len(arg) {
|
||||||
@ -1540,7 +1530,6 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
|||||||
case 4:
|
case 4:
|
||||||
m.Cap(arg[0], arg[1:]...)
|
m.Cap(arg[0], arg[1:]...)
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
// }}}
|
// }}}
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package ssh // {{{
|
package ssh
|
||||||
// }}}
|
|
||||||
import ( // {{{
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -9,8 +9,6 @@ import ( // {{{
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
type SSH struct {
|
type SSH struct {
|
||||||
send map[string]*ctx.Message
|
send map[string]*ctx.Message
|
||||||
*bufio.Reader
|
*bufio.Reader
|
||||||
@ -18,37 +16,32 @@ type SSH struct {
|
|||||||
*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: "nsend", Value: "0", Help: "消息发送数量"},
|
||||||
}
|
}
|
||||||
c.Configs = map[string]*ctx.Config{}
|
c.Configs = map[string]*ctx.Config{}
|
||||||
c.Commands = map[string]*ctx.Command{}
|
|
||||||
|
|
||||||
s := new(SSH)
|
s := new(SSH)
|
||||||
s.Context = c
|
s.Context = c
|
||||||
|
|
||||||
s.send = make(map[string]*ctx.Message)
|
s.send = make(map[string]*ctx.Message)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|
||||||
if ssh.Context == Index {
|
if ssh.Context == Index {
|
||||||
Pulse = m
|
Pulse = m
|
||||||
}
|
}
|
||||||
return ssh
|
return ssh
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||||
func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
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())
|
m.Log("info", nil, "%d remote %v", 0, ssh.Conn.RemoteAddr())
|
||||||
|
|
||||||
target := m.Target
|
target, msg := m.Target, m.Spawn(m.Target)
|
||||||
msg := m.Spawn(target)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
line, e := ssh.Reader.ReadString('\n')
|
line, e := ssh.Reader.ReadString('\n')
|
||||||
@ -58,6 +51,7 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
if msg.Has("detail") {
|
if msg.Has("detail") {
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["detail"])
|
msg.Log("info", nil, "remote: %v", msg.Meta["detail"])
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["option"])
|
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
|
||||||
|
|
||||||
@ -78,6 +72,7 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
} else if msg.Has("result") {
|
} else if msg.Has("result") {
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["result"])
|
msg.Log("info", nil, "remote: %v", msg.Meta["result"])
|
||||||
msg.Log("info", nil, "remote: %v", msg.Meta["append"])
|
msg.Log("info", nil, "remote: %v", msg.Meta["append"])
|
||||||
|
|
||||||
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
|
||||||
@ -95,12 +90,13 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool {
|
||||||
func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool { // {{{
|
switch ssh.Context {
|
||||||
return false
|
case m.Target:
|
||||||
|
case m.Source:
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
var Pulse *ctx.Message
|
var Pulse *ctx.Message
|
||||||
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
||||||
@ -109,25 +105,17 @@ var Index = &ctx.Context{Name: "ssh", 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) string {
|
"listen": &ctx.Command{Name: "listen address", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
tcp := m.Find("tcp", true) // {{{
|
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
||||||
tcp.Cmd(m.Meta["detail"]...)
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"dial": &ctx.Command{Name: "dial address", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"dial": &ctx.Command{Name: "dial address", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
tcp := m.Find("tcp", true) // {{{
|
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
||||||
tcp.Cmd(m.Meta["detail"]...)
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Start("host"+Pulse.Cap("nhost"), "主机连接") // {{{
|
m.Start(fmt.Sprintf("host%s", Pulse.Capi("nhost", 1)), "主机连接")
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) 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.Recv = make(chan bool)
|
||||||
@ -144,8 +132,6 @@ var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
|||||||
}
|
}
|
||||||
fmt.Fprintf(ssh.Conn, "\n")
|
fmt.Fprintf(ssh.Conn, "\n")
|
||||||
<-m.Recv
|
<-m.Recv
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user