forked from x/ContextOS
Merge branch '0.2.0'
This commit is contained in:
commit
93f1949b13
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 {
|
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(aaa.Context.Requests) == 0 {
|
|
||||||
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,15 +471,13 @@ 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 mdb // {{{
|
package mdb
|
||||||
// }}}
|
|
||||||
import ( // {{{
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
@ -9,14 +9,12 @@ import ( // {{{
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
type MDB struct {
|
type MDB struct {
|
||||||
*sql.DB
|
*sql.DB
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
c.Caches = map[string]*ctx.Cache{
|
c.Caches = map[string]*ctx.Cache{
|
||||||
"source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"},
|
"source": &ctx.Cache{Name: "数据库参数", Value: "", Help: "数据库参数"},
|
||||||
"driver": &ctx.Cache{Name: "数据库驱动", Value: "", Help: "数据库驱动"},
|
"driver": &ctx.Cache{Name: "数据库驱动", Value: "", Help: "数据库驱动"},
|
||||||
@ -28,56 +26,48 @@ func (mdb *MDB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
func (mdb *MDB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
if mdb.Context == Index {
|
||||||
|
Pulse = m
|
||||||
|
}
|
||||||
return mdb
|
return mdb
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool {
|
||||||
func (mdb *MDB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
m.Cap("source", arg[0])
|
m.Cap("source", arg[0])
|
||||||
}
|
}
|
||||||
if len(arg) > 1 {
|
if len(arg) > 1 {
|
||||||
m.Cap("driver", arg[1])
|
m.Cap("driver", arg[1])
|
||||||
} else {
|
} else {
|
||||||
m.Cap("driver", m.Conf("driver"))
|
m.Cap("driver", Pulse.Conf("driver"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Cap("source") == "" || m.Cap("driver") == "" {
|
if m.Cap("source") == "" || m.Cap("driver") == "" {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Cap("stream", m.Cap("source"))
|
|
||||||
db, e := sql.Open(m.Cap("driver"), m.Cap("source"))
|
db, e := sql.Open(m.Cap("driver"), m.Cap("source"))
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
mdb.DB = db
|
mdb.DB = db
|
||||||
|
|
||||||
m.Log("info", nil, "%d open %s %s", m.Capi("nsource", 1), m.Cap("driver"), m.Cap("source"))
|
m.Log("info", nil, "%d open %s %s", Pulse.Capi("nsource"), m.Cap("driver"), m.Cap("stream", m.Cap("source")))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool {
|
||||||
func (mdb *MDB) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
switch mdb.Context {
|
switch mdb.Context {
|
||||||
case m.Target:
|
case m.Target:
|
||||||
if mdb.Context == Index {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if mdb.DB != nil {
|
if mdb.DB != nil {
|
||||||
m.Log("info", nil, "%d close %s %s", m.Capi("nsource", -1)+1, m.Cap("driver"), m.Cap("source"))
|
m.Log("info", nil, "%d close %s %s", Pulse.Capi("nsource", -1)+1, m.Cap("driver"), m.Cap("source"))
|
||||||
mdb.DB.Close()
|
mdb.DB.Close()
|
||||||
mdb.DB = nil
|
mdb.DB = nil
|
||||||
}
|
}
|
||||||
case m.Source:
|
case m.Source:
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
var Pulse *ctx.Message
|
||||||
|
|
||||||
var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
"nsource": &ctx.Cache{Name: "数据源数量", Value: "0", Help: "已打开数据库的数量"},
|
"nsource": &ctx.Cache{Name: "数据源数量", Value: "0", Help: "已打开数据库的数量"},
|
||||||
@ -86,18 +76,15 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
"driver": &ctx.Config{Name: "数据库驱动(mysql)", Value: "mysql", Help: "数据库驱动"},
|
"driver": &ctx.Config{Name: "数据库驱动(mysql)", Value: "mysql", Help: "数据库驱动"},
|
||||||
},
|
},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"open": &ctx.Command{Name: "open name help source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"open": &ctx.Command{Name: "open source [driver]", Help: "打开数据库", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Assert(len(arg) > 2, "缺少参数") // {{{
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
m.Master, m.Target = c, c
|
m.Start(fmt.Sprintf("db%d", Pulse.Capi("nsource", 1)), "数据存储", arg...)
|
||||||
m.Cap("stream", m.Cap("nsource"))
|
Pulse.Cap("stream", Pulse.Cap("nsource"))
|
||||||
m.Start(arg[0], "数据存储", arg[2:]...)
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库",
|
"exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库",
|
||||||
Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"},
|
Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"},
|
||||||
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) {
|
||||||
mdb, ok := m.Target.Server.(*MDB) // {{{
|
mdb, ok := m.Target.Server.(*MDB)
|
||||||
m.Assert(ok, "目标模块类型错误")
|
m.Assert(ok, "目标模块类型错误")
|
||||||
m.Assert(len(arg) > 0, "缺少参数")
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
m.Assert(mdb.DB != nil, "数据库未打开")
|
m.Assert(mdb.DB != nil, "数据库未打开")
|
||||||
@ -118,11 +105,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
m.Add("append", "LastInsertId", fmt.Sprintf("%d", id))
|
m.Add("append", "LastInsertId", fmt.Sprintf("%d", id))
|
||||||
m.Add("append", "RowsAffected", fmt.Sprintf("%d", n))
|
m.Add("append", "RowsAffected", fmt.Sprintf("%d", n))
|
||||||
m.Log("info", nil, "last(%d) rows(%d)", id, n)
|
m.Log("info", nil, "last(%d) rows(%d)", id, n)
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"query": &ctx.Command{Name: "query sql [arg]", Help: "执行查询语句", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
mdb, ok := m.Target.Server.(*MDB) // {{{
|
mdb, ok := m.Target.Server.(*MDB)
|
||||||
m.Assert(ok, "目标模块类型错误")
|
m.Assert(ok, "目标模块类型错误")
|
||||||
m.Assert(len(arg) > 0, "缺少参数")
|
m.Assert(len(arg) > 0, "缺少参数")
|
||||||
m.Assert(mdb.DB != nil, "数据库未打开")
|
m.Assert(mdb.DB != nil, "数据库未打开")
|
||||||
@ -161,10 +146,15 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.Log("info", nil, "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
m.Log("info", nil, "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
Index: map[string]*ctx.Context{
|
||||||
|
"void": &ctx.Context{Name: "void",
|
||||||
|
Commands: map[string]*ctx.Command{
|
||||||
|
"open": &ctx.Command{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package nfs // {{{
|
package nfs
|
||||||
// }}}
|
|
||||||
import ( // {{{
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -9,69 +9,59 @@ import ( // {{{
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
type NFS struct {
|
type NFS struct {
|
||||||
file *os.File
|
file *os.File
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
func (nfs *NFS) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
info, ok := m.Data["info"].(os.FileInfo)
|
file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm)
|
||||||
m.Assert(ok)
|
m.Assert(e)
|
||||||
|
info, e := os.Stat(arg[0])
|
||||||
|
m.Assert(e)
|
||||||
|
|
||||||
c.Caches = map[string]*ctx.Cache{
|
c.Caches = map[string]*ctx.Cache{
|
||||||
"name": &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"},
|
"name": &ctx.Cache{Name: "name", Value: info.Name(), Help: "文件名"},
|
||||||
"mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件名"},
|
"mode": &ctx.Cache{Name: "mode", Value: info.Mode().String(), Help: "文件权限"},
|
||||||
"time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "文件名"},
|
"time": &ctx.Cache{Name: "time", Value: info.ModTime().Format("15:03:04"), Help: "创建时间"},
|
||||||
"size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件名"},
|
"size": &ctx.Cache{Name: "size", Value: fmt.Sprintf("%d", info.Size()), Help: "文件大小"},
|
||||||
"pos": &ctx.Cache{Name: "pos", Value: "0", Help: "文件名"},
|
"pos": &ctx.Cache{Name: "pos", Value: "0", Help: "读写位置"},
|
||||||
}
|
}
|
||||||
c.Configs = map[string]*ctx.Config{}
|
c.Configs = map[string]*ctx.Config{}
|
||||||
|
|
||||||
s := new(NFS)
|
s := new(NFS)
|
||||||
s.Context = c
|
s.Context = c
|
||||||
|
s.file = file
|
||||||
return s
|
return s
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
func (nfs *NFS) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|
||||||
if nfs.Context == Index {
|
if nfs.Context == Index {
|
||||||
Pulse = m
|
Pulse = m
|
||||||
}
|
}
|
||||||
return nfs
|
return nfs
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool {
|
||||||
func (nfs *NFS) Start(m *ctx.Message, arg ...string) bool { // {{{
|
m.Log("info", nil, "%d open %s", Pulse.Capi("nfile"), m.Cap("name"))
|
||||||
m.Log("info", nil, "%d open %s", Pulse.Capi("nfile", 1), m.Cap("name"))
|
|
||||||
m.Cap("stream", m.Cap("name"))
|
m.Cap("stream", m.Cap("name"))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool {
|
||||||
func (nfs *NFS) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
switch nfs.Context {
|
switch nfs.Context {
|
||||||
case m.Target:
|
case m.Target:
|
||||||
if nfs.Context == Index {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if nfs.file != nil {
|
if nfs.file != nil {
|
||||||
m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name"))
|
m.Log("info", nil, "%d close %s", Pulse.Capi("nfile", -1)+1, m.Cap("name"))
|
||||||
nfs.file.Close()
|
nfs.file.Close()
|
||||||
return true
|
nfs.file = nil
|
||||||
}
|
}
|
||||||
case m.Source:
|
case m.Source:
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
var Pulse *ctx.Message
|
var Pulse *ctx.Message
|
||||||
var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
@ -81,22 +71,11 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
"size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"},
|
"size": &ctx.Config{Name: "size", Value: "1024", Help: "读取文件的默认大小值"},
|
||||||
},
|
},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"open": &ctx.Command{Name: "open file", Help: "打开文件, file: 文件名", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
file, e := os.OpenFile(arg[0], os.O_RDWR|os.O_CREATE, os.ModePerm) // {{{
|
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", arg...)
|
||||||
m.Assert(e)
|
|
||||||
info, e := os.Stat(arg[0])
|
|
||||||
m.Assert(e)
|
|
||||||
m.Put("option", "info", info).Start("file"+m.Cap("nfile"), "打开文件", arg...)
|
|
||||||
|
|
||||||
nfs, ok := m.Target.Server.(*NFS)
|
|
||||||
m.Assert(ok)
|
|
||||||
|
|
||||||
nfs.file = file
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"read": &ctx.Command{Name: "read [size [pos]]", Help: "读取文件, size: 读取大小, pos: 读取位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
nfs, ok := m.Target.Server.(*NFS) // {{{
|
nfs, ok := m.Target.Server.(*NFS)
|
||||||
m.Assert(ok)
|
m.Assert(ok)
|
||||||
|
|
||||||
var e error
|
var e error
|
||||||
@ -105,45 +84,47 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
|||||||
n, e = strconv.Atoi(arg[0])
|
n, e = strconv.Atoi(arg[0])
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := make([]byte, n)
|
|
||||||
if len(arg) > 1 {
|
if len(arg) > 1 {
|
||||||
m.Cap("pos", arg[1])
|
m.Cap("pos", arg[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, n)
|
||||||
if n, e = nfs.file.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF {
|
if n, e = nfs.file.ReadAt(buf, int64(m.Capi("pos"))); e != io.EOF {
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
}
|
}
|
||||||
|
m.Echo(string(buf))
|
||||||
|
|
||||||
if m.Capi("pos", n); m.Capi("pos") == m.Capi("size") {
|
if m.Capi("pos", n); m.Capi("pos") == m.Capi("size") {
|
||||||
m.Cap("pos", "0")
|
m.Cap("pos", "0")
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(buf)
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"write": &ctx.Command{Name: "write string [pos]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
nfs, ok := m.Target.Server.(*NFS) // {{{
|
nfs, ok := m.Target.Server.(*NFS)
|
||||||
if m.Assert(ok); len(arg) > 1 {
|
if m.Assert(ok); len(arg) > 1 {
|
||||||
m.Cap("pos", arg[1])
|
m.Cap("pos", arg[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(arg[0]) == 0 {
|
if len(arg[0]) == 0 {
|
||||||
e := nfs.file.Truncate(0)
|
m.Assert(nfs.file.Truncate(int64(m.Capi("pos"))))
|
||||||
m.Assert(e)
|
m.Cap("size", m.Cap("pos"))
|
||||||
m.Cap("size", "0")
|
|
||||||
m.Cap("pos", "0")
|
m.Cap("pos", "0")
|
||||||
return ""
|
} else {
|
||||||
|
n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos")))
|
||||||
|
if m.Assert(e) && m.Capi("pos", n) > m.Capi("size") {
|
||||||
|
m.Cap("size", m.Cap("pos"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n, e := nfs.file.WriteAt([]byte(arg[0]), int64(m.Capi("pos")))
|
m.Echo(m.Cap("pos"))
|
||||||
if m.Assert(e); m.Capi("pos", n) > m.Capi("size") {
|
|
||||||
m.Cap("size", m.Cap("pos"))
|
|
||||||
}
|
|
||||||
return m.Cap("pos")
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
|
Index: map[string]*ctx.Context{
|
||||||
|
"void": &ctx.Context{Name: "void",
|
||||||
|
Commands: map[string]*ctx.Command{
|
||||||
|
"open": &ctx.Command{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -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,13 +90,14 @@ 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: "集群中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
@ -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 ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package tcp // {{{
|
package tcp
|
||||||
// }}}
|
|
||||||
import ( // {{{
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -8,15 +8,13 @@ import ( // {{{
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
type TCP struct {
|
type TCP struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
net.Listener
|
net.Listener
|
||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
c.Caches = map[string]*ctx.Cache{
|
c.Caches = map[string]*ctx.Cache{
|
||||||
"protocol": &ctx.Cache{Name: "protocol(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "监听地址"},
|
"protocol": &ctx.Cache{Name: "protocol(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "监听地址"},
|
||||||
"security": &ctx.Cache{Name: "security(true/false)", Value: m.Conf("security"), Help: "加密通信"},
|
"security": &ctx.Cache{Name: "security(true/false)", Value: m.Conf("security"), Help: "加密通信"},
|
||||||
@ -27,20 +25,17 @@ func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
s := new(TCP)
|
s := new(TCP)
|
||||||
s.Context = c
|
s.Context = c
|
||||||
return s
|
return s
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
if tcp.Context == Index {
|
||||||
if m.Target == Index {
|
|
||||||
Pulse = m
|
Pulse = m
|
||||||
}
|
}
|
||||||
|
|
||||||
return tcp
|
return tcp
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
|
||||||
func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
if len(arg) > 1 {
|
if len(arg) > 1 {
|
||||||
m.Cap("address", arg[1])
|
m.Cap("address", arg[1])
|
||||||
}
|
}
|
||||||
@ -53,92 +48,64 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
c, e := net.Dial(m.Cap("protocol"), m.Cap("address"))
|
c, e := net.Dial(m.Cap("protocol"), m.Cap("address"))
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
tcp.Conn = c
|
tcp.Conn = c
|
||||||
m.Log("info", nil, "dial(%d) %v->%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr())
|
m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr())))
|
||||||
m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))
|
|
||||||
|
|
||||||
msg := m.Spawn(m.Source).Put("option", "io", c)
|
msg := m.Reply("open").Put("option", "io", c)
|
||||||
msg.Cmd("open")
|
msg.Cmd("open")
|
||||||
msg.Cap("stream", tcp.RemoteAddr().String())
|
msg.Cap("stream", tcp.LocalAddr().String())
|
||||||
|
|
||||||
if tcp.Sessions == nil {
|
|
||||||
tcp.Sessions = make(map[string]*ctx.Message)
|
|
||||||
}
|
|
||||||
tcp.Sessions["open"] = msg
|
|
||||||
msg.Name = "open"
|
|
||||||
|
|
||||||
// m.Reply(c.LocalAddr().String()).Put("option", "io", c).Cmd("open")
|
|
||||||
return false
|
return false
|
||||||
case "accept":
|
case "accept":
|
||||||
c, e := m.Data["io"].(net.Conn)
|
c, e := m.Data["io"].(net.Conn)
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
tcp.Conn = c
|
tcp.Conn = c
|
||||||
m.Log("info", nil, "accept(%d) %v<-%v", m.Capi("nclient", 1), tcp.LocalAddr(), tcp.RemoteAddr())
|
m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr())))
|
||||||
m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))
|
|
||||||
|
|
||||||
s, e := m.Data["source"].(*ctx.Context)
|
msg := m.Spawn(m.Data["source"].(*ctx.Context), "open").Put("option", "io", c)
|
||||||
m.Assert(e)
|
|
||||||
msg := m.Spawn(s).Put("option", "io", c)
|
|
||||||
msg.Cmd("open")
|
msg.Cmd("open")
|
||||||
msg.Cap("stream", tcp.RemoteAddr().String())
|
msg.Cap("stream", tcp.RemoteAddr().String())
|
||||||
|
|
||||||
if tcp.Sessions == nil {
|
|
||||||
tcp.Sessions = make(map[string]*ctx.Message)
|
|
||||||
}
|
|
||||||
tcp.Sessions["open"] = msg
|
|
||||||
msg.Name = "open"
|
|
||||||
|
|
||||||
// m.Reply(c.RemoteAddr().String())
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
l, e := net.Listen(m.Cap("protocol"), m.Cap("address"))
|
l, e := net.Listen(m.Cap("protocol"), m.Cap("address"))
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
tcp.Listener = l
|
tcp.Listener = l
|
||||||
m.Log("info", nil, "listen(%d) %v", m.Capi("nlisten", 1), l.Addr())
|
m.Log("info", nil, "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", l.Addr())))
|
||||||
m.Cap("stream", fmt.Sprintf("%s", l.Addr()))
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
c, e := l.Accept()
|
c, e := l.Accept()
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source).Start(fmt.Sprintf("com%d", m.Capi("nclient", 1)), "网络连接", "accept", c.RemoteAddr().String())
|
m.Spawn(Index).Put("option", "io", c).Put("option", "source", m.Source).Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", "accept", c.RemoteAddr().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
|
||||||
func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
switch tcp.Context {
|
switch tcp.Context {
|
||||||
case m.Target:
|
case m.Target:
|
||||||
if tcp.Context == Index {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if tcp.Listener != nil {
|
if tcp.Listener != nil {
|
||||||
m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, tcp.Listener.Addr())
|
m.Log("info", nil, "%d close %v", Pulse.Capi("nlisten", -1)+1, m.Cap("stream"))
|
||||||
tcp.Listener.Close()
|
tcp.Listener.Close()
|
||||||
tcp.Listener = nil
|
tcp.Listener = nil
|
||||||
}
|
}
|
||||||
|
if tcp.Conn != nil {
|
||||||
|
m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, m.Cap("stream"))
|
||||||
|
tcp.Conn.Close()
|
||||||
|
tcp.Conn = nil
|
||||||
|
}
|
||||||
case m.Source:
|
case m.Source:
|
||||||
if tcp.Conn != nil {
|
if tcp.Conn != nil {
|
||||||
msg := m.Spawn(tcp.Context)
|
msg := m.Spawn(tcp.Context)
|
||||||
msg.Master = tcp.Context
|
if msg.Master = tcp.Context; !tcp.Context.Close(msg, arg...) {
|
||||||
if !tcp.Context.Close(msg, arg...) {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if tcp.Conn != nil {
|
|
||||||
m.Log("info", nil, "%d close %v", Pulse.Capi("nclient", -1)+1, tcp.Conn.LocalAddr())
|
|
||||||
tcp.Conn.Close()
|
|
||||||
tcp.Conn = nil
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
var Pulse *ctx.Message
|
||||||
|
|
||||||
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{
|
||||||
"nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"},
|
"nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"},
|
||||||
@ -149,53 +116,29 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
|||||||
"security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"},
|
"security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"},
|
||||||
},
|
},
|
||||||
Commands: map[string]*ctx.Command{
|
Commands: map[string]*ctx.Command{
|
||||||
"listen": &ctx.Command{Name: "listen [address [security]]", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"listen": &ctx.Command{Name: "listen address [security]", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
switch len(arg) { // {{{
|
m.Start(fmt.Sprintf("pub%d", Pulse.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...)
|
||||||
case 0:
|
|
||||||
m.Travel(nil, func(m *ctx.Message) bool {
|
|
||||||
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Listener != nil {
|
|
||||||
m.Echo("%s %v\n", m.Target.Name, tcp.Addr())
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
default:
|
|
||||||
m.Start(fmt.Sprintf("pub%d", m.Capi("nlisten")+1), "网络监听", m.Meta["detail"]...)
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
switch len(arg) { // {{{
|
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
|
||||||
case 0:
|
|
||||||
m.Travel(nil, func(m *ctx.Message) bool {
|
|
||||||
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil {
|
|
||||||
m.Echo("%s %v<->%v\n", m.Target.Name, tcp.LocalAddr(), tcp.RemoteAddr())
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
default:
|
|
||||||
m.Start(fmt.Sprintf("com%d", m.Capi("nclient")+1), "网络连接", m.Meta["detail"]...)
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"send": &ctx.Command{Name: "send message", Help: "发送消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{
|
tcp, ok := m.Target.Server.(*TCP)
|
||||||
tcp.Conn.Write([]byte(arg[0]))
|
m.Assert(ok && tcp.Conn != nil)
|
||||||
}
|
|
||||||
return ""
|
tcp.Conn.Write([]byte(arg[0]))
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"recv": &ctx.Command{Name: "recv size", Help: "接收消息", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
|
tcp, ok := m.Target.Server.(*TCP)
|
||||||
|
m.Assert(ok && tcp.Conn != nil)
|
||||||
|
|
||||||
size, e := strconv.Atoi(arg[0])
|
size, e := strconv.Atoi(arg[0])
|
||||||
m.Assert(e)
|
m.Assert(e)
|
||||||
if tcp, ok := m.Target.Server.(*TCP); ok && tcp.Conn != nil { // {{{
|
|
||||||
buf := make([]byte, size)
|
buf := make([]byte, size)
|
||||||
tcp.Conn.Read(buf)
|
tcp.Conn.Read(buf)
|
||||||
return string(buf)
|
m.Echo(string(buf))
|
||||||
}
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
Index: map[string]*ctx.Context{
|
Index: map[string]*ctx.Context{
|
||||||
@ -208,8 +151,6 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var Pulse *ctx.Message
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
tcp := &TCP{}
|
tcp := &TCP{}
|
||||||
tcp.Context = Index
|
tcp.Context = Index
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package web // {{{
|
package web
|
||||||
// }}}
|
|
||||||
import ( // {{{
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"html/template"
|
"html/template"
|
||||||
@ -13,12 +13,10 @@ import ( // {{{
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
type MUX interface {
|
type MUX interface {
|
||||||
Handle(string, http.Handler)
|
Handle(string, http.Handler)
|
||||||
HandleFunc(string, func(http.ResponseWriter, *http.Request))
|
HandleFunc(string, func(http.ResponseWriter, *http.Request))
|
||||||
Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string) string)
|
Trans(*ctx.Message, string, func(*ctx.Message, *ctx.Context, string, ...string))
|
||||||
}
|
}
|
||||||
|
|
||||||
type WEB struct {
|
type WEB struct {
|
||||||
@ -29,10 +27,9 @@ type WEB struct {
|
|||||||
*ctx.Context
|
*ctx.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string) string) { // {{{
|
func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.Context, string, ...string)) {
|
||||||
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
|
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
|
||||||
msg := m.Spawn(m.Target)
|
msg := m.Spawn(m.Target).Set("detail", key)
|
||||||
msg.Set("detail", key)
|
|
||||||
for k, v := range r.Form {
|
for k, v := range r.Form {
|
||||||
msg.Add("option", k)
|
msg.Add("option", k)
|
||||||
msg.Meta[k] = v
|
msg.Meta[k] = v
|
||||||
@ -44,16 +41,11 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C
|
|||||||
msg.Log("cmd", nil, "%s %v", key, msg.Meta["option"])
|
msg.Log("cmd", nil, "%s %v", key, msg.Meta["option"])
|
||||||
msg.Put("option", "request", r)
|
msg.Put("option", "request", r)
|
||||||
msg.Put("option", "response", w)
|
msg.Put("option", "response", w)
|
||||||
|
hand(msg, msg.Target, key)
|
||||||
ret := hand(msg, msg.Target, key)
|
|
||||||
if ret != "" {
|
|
||||||
msg.Echo(ret)
|
|
||||||
}
|
|
||||||
|
|
||||||
header := w.Header()
|
header := w.Header()
|
||||||
for _, k := range msg.Meta["append"] {
|
for _, k := range msg.Meta["append"] {
|
||||||
ce := &http.Cookie{Name: k, Value: msg.Get(k)}
|
header.Add("Set-Cookie", (&http.Cookie{Name: k, Value: msg.Get(k)}).String())
|
||||||
header.Add("Set-Cookie", ce.String())
|
|
||||||
}
|
}
|
||||||
for _, v := range msg.Meta["result"] {
|
for _, v := range msg.Meta["result"] {
|
||||||
w.Write([]byte(v))
|
w.Write([]byte(v))
|
||||||
@ -61,12 +53,10 @@ func (web *WEB) Trans(m *ctx.Message, key string, hand func(*ctx.Message, *ctx.C
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
|
||||||
if web.Message != nil {
|
if web.Message != nil {
|
||||||
log.Println()
|
log.Println()
|
||||||
web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL)
|
web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL)
|
||||||
defer log.Println()
|
|
||||||
|
|
||||||
if web.Cap("logheaders") == "yes" {
|
if web.Cap("logheaders") == "yes" {
|
||||||
for k, v := range r.Header {
|
for k, v := range r.Header {
|
||||||
@ -75,8 +65,7 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
|||||||
log.Println()
|
log.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
r.ParseForm()
|
if r.ParseForm(); len(r.PostForm) > 0 {
|
||||||
if len(r.PostForm) > 0 {
|
|
||||||
for k, v := range r.PostForm {
|
for k, v := range r.PostForm {
|
||||||
log.Printf("%s: %s", k, v[0])
|
log.Printf("%s: %s", k, v[0])
|
||||||
}
|
}
|
||||||
@ -86,21 +75,16 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) { // {{{
|
|||||||
|
|
||||||
web.ServeMux.ServeHTTP(w, r)
|
web.ServeMux.ServeHTTP(w, r)
|
||||||
|
|
||||||
if web.Message != nil {
|
if web.Message != nil && web.Cap("logheaders") == "yes" {
|
||||||
if web.Cap("logheaders") == "yes" {
|
for k, v := range w.Header() {
|
||||||
for k, v := range w.Header() {
|
log.Println(k+":", v[0])
|
||||||
log.Println(k+":", v[0])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
log.Println()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||||
|
c.Caches = map[string]*ctx.Cache{}
|
||||||
func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server { // {{{
|
|
||||||
c.Caches = map[string]*ctx.Cache{
|
|
||||||
"directory": &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"},
|
|
||||||
}
|
|
||||||
c.Configs = map[string]*ctx.Config{}
|
c.Configs = map[string]*ctx.Config{}
|
||||||
|
|
||||||
s := new(WEB)
|
s := new(WEB)
|
||||||
@ -108,15 +92,14 @@ func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||||
func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
web.Caches["directory"] = &ctx.Cache{Name: "directory", Value: "usr", Help: "服务目录"}
|
||||||
if len(arg) > 0 {
|
|
||||||
m.Cap("directory", arg[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
web.Caches["route"] = &ctx.Cache{Name: "route", Value: "/" + web.Context.Name + "/", Help: "请求路径"}
|
web.Caches["route"] = &ctx.Cache{Name: "route", Value: "/" + web.Context.Name + "/", Help: "请求路径"}
|
||||||
web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已注册"}
|
web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已注册"}
|
||||||
web.Caches["master"] = &ctx.Cache{Name: "master(yes/no)", Value: "no", Help: "日志输出请求头"}
|
web.Caches["master"] = &ctx.Cache{Name: "master(yes/no)", Value: "no", Help: "日志输出请求头"}
|
||||||
|
if len(arg) > 0 {
|
||||||
|
m.Cap("directory", arg[0])
|
||||||
|
}
|
||||||
|
|
||||||
web.ServeMux = http.NewServeMux()
|
web.ServeMux = http.NewServeMux()
|
||||||
if mux, ok := m.Target.Server.(MUX); ok {
|
if mux, ok := m.Target.Server.(MUX); ok {
|
||||||
@ -130,8 +113,7 @@ func (web *WEB) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
|||||||
return web
|
return web
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (web *WEB) Start(m *ctx.Message, arg ...string) bool {
|
||||||
func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
m.Cap("directory", arg[0])
|
m.Cap("directory", arg[0])
|
||||||
}
|
}
|
||||||
@ -192,32 +174,23 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool { // {{{
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
func (web *WEB) Close(m *ctx.Message, arg ...string) bool {
|
||||||
func (web *WEB) Close(m *ctx.Message, arg ...string) bool { // {{{
|
|
||||||
switch web.Context {
|
switch web.Context {
|
||||||
case m.Target:
|
case m.Target:
|
||||||
case m.Source:
|
case m.Source:
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// }}}
|
|
||||||
|
|
||||||
var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||||
Caches: map[string]*ctx.Cache{
|
Caches: map[string]*ctx.Cache{},
|
||||||
"directory": &ctx.Cache{Name: "directory", Value: "usr", 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 [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"listen": &ctx.Command{Name: "listen [directory [address [protocol]]]", Help: "开启网页服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Meta["detail"] = arg // {{{
|
m.Set("detail", arg...).Target.Start(m)
|
||||||
m.Target.Start(m)
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
mux, ok := m.Target.Server.(MUX) // {{{
|
mux, ok := m.Target.Server.(MUX)
|
||||||
m.Assert(ok, "模块类型错误")
|
m.Assert(ok, "模块类型错误")
|
||||||
m.Assert(len(arg) == 3, "缺少参数")
|
m.Assert(len(arg) == 3, "缺少参数")
|
||||||
|
|
||||||
@ -225,7 +198,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
case "directory":
|
case "directory":
|
||||||
mux.Handle(arg[1], http.FileServer(http.Dir(arg[2])))
|
mux.Handle(arg[1], http.FileServer(http.Dir(arg[2])))
|
||||||
case "template":
|
case "template":
|
||||||
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string { // {{{
|
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) {
|
||||||
w := m.Data["response"].(http.ResponseWriter)
|
w := m.Data["response"].(http.ResponseWriter)
|
||||||
|
|
||||||
if _, e := os.Stat(arg[2]); e == nil {
|
if _, e := os.Stat(arg[2]); e == nil {
|
||||||
@ -234,16 +207,13 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
template.Must(template.New("temp").Parse(arg[2])).Execute(w, m)
|
template.Must(template.New("temp").Parse(arg[2])).Execute(w, m)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
|
||||||
})
|
})
|
||||||
// }}}
|
|
||||||
case "script":
|
case "script":
|
||||||
cli := m.Find("cli", true) // {{{
|
cli := m.Find("cli", true)
|
||||||
lex := m.Find("lex", true)
|
lex := m.Find("lex", true)
|
||||||
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) string {
|
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) {
|
||||||
f, e := os.Open(arg[2])
|
f, e := os.Open(arg[2])
|
||||||
line, bio := "", bufio.NewReader(f)
|
line, bio := "", bufio.NewReader(f)
|
||||||
|
|
||||||
if e != nil {
|
if e != nil {
|
||||||
line = arg[2]
|
line = arg[2]
|
||||||
}
|
}
|
||||||
@ -260,18 +230,12 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
|
||||||
})
|
})
|
||||||
// }}}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
|
||||||
// }}}
|
|
||||||
}},
|
}},
|
||||||
"/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) string {
|
"/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||||
m.Add("append", "hi", "hello")
|
m.Add("append", "hi", "hello")
|
||||||
return "hello"
|
m.Echo("hello\n")
|
||||||
}},
|
}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user