forked from x/ContextOS
Merge branch '0.2.0' 修改了一些代码
This commit is contained in:
commit
0674e757ec
@ -51,17 +51,12 @@ snippet c
|
||||
|
||||
func (`Filename()` *`toupper(substitute(expand("%:t"), ".go", "", ""))`) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch `Filename()`.Context {
|
||||
case m.Target:
|
||||
if `Filename()`.Context == Index {
|
||||
return false
|
||||
}
|
||||
case m.Source:
|
||||
return true
|
||||
case m.Target:
|
||||
case m.Source:
|
||||
}
|
||||
return false
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
var Pulse *ctx.Message
|
||||
var Index = &ctx.Context{Name: "`Filename()`", Help: "${1}",
|
||||
Caches: map[string]*ctx.Cache{},
|
||||
|
18
etc/init.sh
18
etc/init.sh
@ -1,15 +1,24 @@
|
||||
~cli
|
||||
@lex lex
|
||||
~aaa login root root
|
||||
~web serve
|
||||
~ssh dial chat.shylinux.com:9090 true
|
||||
sleep 1
|
||||
~host1
|
||||
ifneq load load end
|
||||
remote context mpa register terminal shhylinux term term term 1
|
||||
$sessid $result
|
||||
remote cache sessid $sessid
|
||||
~nfs save usr/sess.txt "terminal: " $sessid
|
||||
~nfs genqr usr/sess.png "terminal: " $sessid
|
||||
end
|
||||
return
|
||||
# ~cli
|
||||
# remote slaver listen ":9393" tcp
|
||||
~aaa
|
||||
login root root
|
||||
# ~ssh
|
||||
# listen :9191
|
||||
~tcp
|
||||
listen :9393
|
||||
~web
|
||||
listen
|
||||
|
||||
# ~tcp dial ":9393"
|
||||
# @debug on
|
||||
@ -21,7 +30,6 @@
|
||||
# @debug on
|
||||
# ~nfs
|
||||
# open hi.txt
|
||||
return
|
||||
|
||||
|
||||
@debug
|
||||
|
@ -17,7 +17,7 @@ type AAA struct {
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (aaa *AAA) session(meta string) string {
|
||||
func (aaa *AAA) Session(meta string) string {
|
||||
bs := md5.Sum([]byte(fmt.Sprintln("%d%d%s", time.Now().Unix(), rand.Int(), meta)))
|
||||
sessid := hex.EncodeToString(bs[:])
|
||||
return sessid
|
||||
@ -45,7 +45,7 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
return x.Value
|
||||
}}
|
||||
|
||||
aaa.Caches["sessid"] = &ctx.Cache{Name: "会话标识", Value: "", Help: "用户的会话标识"}
|
||||
aaa.Caches["sessid"] = &ctx.Cache{Name: "会话令牌", Value: "", Help: "用户的会话标识"}
|
||||
aaa.Caches["expire"] = &ctx.Cache{Name: "会话超时", Value: "", Help: "用户的会话标识"}
|
||||
aaa.Caches["time"] = &ctx.Cache{Name: "登录时间", Value: fmt.Sprintf("%d", time.Now().Unix()), Help: "用户登录时间", Hand: func(m *ctx.Message, x *ctx.Cache, arg ...string) string {
|
||||
if len(arg) > 0 {
|
||||
@ -60,28 +60,29 @@ func (aaa *AAA) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
if m.Target == Index {
|
||||
Pulse = m
|
||||
}
|
||||
|
||||
aaa.Owner = aaa.Context
|
||||
return aaa
|
||||
}
|
||||
|
||||
func (aaa *AAA) Start(m *ctx.Message, arg ...string) bool {
|
||||
if len(arg) > 1 {
|
||||
if m.Cap("sessid") == "" {
|
||||
m.Cap("sessid", aaa.session(arg[1]))
|
||||
Pulse.Capi("nuser", 1)
|
||||
}
|
||||
m.Log("info", m.Source, "create %s %s", m.Cap("group", arg[0]), m.Cap("username", arg[1]))
|
||||
if len(arg) > 1 && m.Cap("sessid") == "" {
|
||||
m.Cap("group", arg[0])
|
||||
m.Cap("username", arg[1])
|
||||
m.Cap("stream", m.Cap("username"))
|
||||
m.Cap("sessid", aaa.Session(arg[1]))
|
||||
Pulse.Capi("nuser", 1)
|
||||
aaa.Owner = aaa.Context
|
||||
aaa.Group = arg[0]
|
||||
}
|
||||
m.Log("info", m.Source, "login %s %s", m.Cap("group"), m.Cap("username"))
|
||||
|
||||
m.Log("info", m.Source, "%s login %s %s", Pulse.Cap("nuser"), m.Cap("group"), m.Cap("username"))
|
||||
return false
|
||||
}
|
||||
|
||||
func (aaa *AAA) Close(m *ctx.Message, arg ...string) bool {
|
||||
switch aaa.Context {
|
||||
case m.Target:
|
||||
root := Pulse.Target.Server.(*AAA)
|
||||
delete(root.sessions, m.Cap("sessid"))
|
||||
m.Log("info", nil, "%d logout %s", Pulse.Capi("nuser", -1)+1, m.Cap("username"))
|
||||
case m.Source:
|
||||
}
|
||||
@ -99,7 +100,7 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
"expire": &ctx.Config{Name: "会话超时(s)", Value: "120", Help: "会话超时"},
|
||||
},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
"login": &ctx.Command{Name: "login [sessid]|[[group] username password]]", Help: "用户登录", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Target, m.Master = c, c
|
||||
aaa := c.Server.(*AAA)
|
||||
|
||||
@ -107,23 +108,20 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
case 0:
|
||||
m.Travel(c, func(m *ctx.Message) bool {
|
||||
m.Echo("%s(%s): %s\n", m.Target.Name, m.Cap("group"), m.Cap("time"))
|
||||
if int64(m.Capi("expire")) < time.Now().Unix() {
|
||||
m.Target.Close(m)
|
||||
}
|
||||
return true
|
||||
})
|
||||
case 1:
|
||||
if s, ok := aaa.sessions[arg[0]]; ok {
|
||||
if m.Target = s; int64(m.Capi("expire")) < time.Now().Unix() {
|
||||
s.Close(m)
|
||||
return
|
||||
}
|
||||
s, ok := aaa.sessions[arg[0]]
|
||||
m.Assert(ok, "会话失败")
|
||||
m.Target = s
|
||||
m.Assert(int64(m.Capi("expire")) > time.Now().Unix(), "会话失败")
|
||||
|
||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||
m.Log("info", m.Source, "logon %s", m.Cap("group"), m.Cap("username"))
|
||||
if m.Name != "" {
|
||||
c.Requests = append(c.Requests, m)
|
||||
m.Index = len(m.Target.Requests)
|
||||
}
|
||||
m.Echo(m.Cap("username"))
|
||||
}
|
||||
m.Source.Group, m.Source.Owner = m.Cap("group"), m.Target
|
||||
m.Log("info", m.Source, "logon %s %s", m.Cap("username"), m.Cap("group"))
|
||||
m.Echo(m.Cap("username"))
|
||||
case 2, 3:
|
||||
group, username, password := arg[0], arg[0], arg[1]
|
||||
if len(arg) == 3 {
|
||||
@ -132,15 +130,10 @@ var Index = &ctx.Context{Name: "aaa", Help: "认证中心",
|
||||
|
||||
if username == Pulse.Conf("rootname") {
|
||||
m.Set("detail", group, username).Target.Start(m)
|
||||
} else if msg := m.Find(username); msg == nil {
|
||||
} else if msg := m.Find(username, false); msg == nil {
|
||||
m.Start(username, "认证用户", group, username)
|
||||
} else {
|
||||
m.Target = msg.Target
|
||||
msg.Target.Start(msg)
|
||||
if m.Name != "" {
|
||||
m.Target.Requests = append(m.Target.Requests, m)
|
||||
m.Index = len(m.Target.Requests)
|
||||
}
|
||||
}
|
||||
|
||||
m.Cap("password", password)
|
||||
|
@ -8,6 +8,7 @@ import ( // {{{
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
@ -128,10 +129,15 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
||||
|
||||
if r := rune(ls[i][0]); r == '$' || r == '_' || (!unicode.IsNumber(r) && !unicode.IsLetter(r)) {
|
||||
if c, ok := cli.alias[string(r)]; ok {
|
||||
if msg.Add("detail", c); len(ls[i]) > 1 {
|
||||
if i == 0 {
|
||||
if msg.Add("detail", c); len(ls[i]) == 1 {
|
||||
continue
|
||||
}
|
||||
ls[i] = ls[i][1:]
|
||||
} else {
|
||||
continue
|
||||
} else if len(ls[i]) > 1 {
|
||||
mm := m.Spawn(cli.target)
|
||||
m.Assert(mm.Exec(c, ls[i][1:]))
|
||||
ls[i] = mm.Get("result")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -149,7 +155,10 @@ func (cli *CLI) parse(m *ctx.Message) bool { // {{{
|
||||
msg.Wait = make(chan bool)
|
||||
msg.Post(cli.Context)
|
||||
|
||||
cli.echo(strings.Join(msg.Meta["result"], ""))
|
||||
result := strings.TrimRight(strings.Join(msg.Meta["result"], ""), "\n") + "\n"
|
||||
if len(result) > 1 {
|
||||
cli.echo(m.Cap("result", result))
|
||||
}
|
||||
cli.target = msg.Target
|
||||
cli.back = line
|
||||
return true
|
||||
@ -168,6 +177,7 @@ func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
||||
|
||||
// }}}
|
||||
func (cli *CLI) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{
|
||||
cli.Caches["result"] = &ctx.Cache{Name: "前一条指令执行结果", Value: "", Help: "前一条指令执行结果"}
|
||||
cli.Configs["slient"] = &ctx.Config{Name: "屏蔽脚本输出(yes/no)", Value: "yes", Help: "屏蔽脚本输出的信息,yes:屏蔽,no:不屏蔽"}
|
||||
cli.Configs["PS1"] = &ctx.Config{Name: "命令行提示符(target/detail)", Value: "target", Help: "命令行提示符,target:显示当前模块,detail:显示详细信息", Hand: func(m *ctx.Message, x *ctx.Config, arg ...string) string {
|
||||
if len(arg) > 0 { // {{{
|
||||
@ -326,22 +336,24 @@ func (cli *CLI) Start(m *ctx.Message, arg ...string) bool { // {{{
|
||||
|
||||
m.Deal(nil, func(msg *ctx.Message, arg ...string) bool {
|
||||
if msg.Get("result") == "error: " {
|
||||
msg.Log("system", nil, "%v", msg.Meta["detail"])
|
||||
if msg.Get("detail") != "login" {
|
||||
msg.Log("system", nil, "%v", msg.Meta["detail"])
|
||||
|
||||
msg.Set("result")
|
||||
msg.Set("append")
|
||||
c := exec.Command(msg.Meta["detail"][0], msg.Meta["detail"][1:]...)
|
||||
msg.Set("result")
|
||||
msg.Set("append")
|
||||
c := exec.Command(msg.Meta["detail"][0], msg.Meta["detail"][1:]...)
|
||||
|
||||
if len(cli.ins) == 1 && cli.Context == Index {
|
||||
c.Stdin, c.Stdout, c.Stderr = cli.in, cli.out, cli.out
|
||||
msg.Assert(c.Start())
|
||||
msg.Assert(c.Wait())
|
||||
} else {
|
||||
if out, e := c.CombinedOutput(); e == nil {
|
||||
msg.Echo(string(out))
|
||||
if len(cli.ins) == 1 && cli.Context == Index {
|
||||
c.Stdin, c.Stdout, c.Stderr = cli.in, cli.out, cli.out
|
||||
msg.Assert(c.Start())
|
||||
msg.Assert(c.Wait())
|
||||
} else {
|
||||
msg.Echo("error: ")
|
||||
msg.Echo("%s\n", e)
|
||||
if out, e := c.CombinedOutput(); e == nil {
|
||||
msg.Echo(string(out))
|
||||
} else {
|
||||
msg.Echo("error: ")
|
||||
msg.Echo("%s\n", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -397,6 +409,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
|
||||
// }}}
|
||||
}},
|
||||
"sleep": &ctx.Command{Name: "sleep time", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
t, e := strconv.Atoi(arg[0])
|
||||
m.Assert(e)
|
||||
m.Log("info", nil, "sleep %ds", t)
|
||||
time.Sleep(time.Second * time.Duration(t))
|
||||
}},
|
||||
"return": &ctx.Command{Name: "return", Help: "运行脚本", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
cli := c.Server.(*CLI) // {{{
|
||||
cli.bio.Discard(cli.bio.Buffered())
|
||||
|
@ -136,7 +136,7 @@ func (c *Context) Begin(m *Message) *Context { // {{{
|
||||
|
||||
// }}}
|
||||
func (c *Context) Start(m *Message) bool { // {{{
|
||||
if c.Requests = append(c.Requests, m); m.Cap("status") != "start" {
|
||||
if c.Requests, m.Index = append(c.Requests, m), len(c.Requests)+1; m.Cap("status") != "start" {
|
||||
running := make(chan bool)
|
||||
go m.AssertOne(m, true, func(m *Message) {
|
||||
m.Log(m.Cap("status", "start"), nil, "%d server %v", m.root.Capi("nserver", 1), m.Meta["detail"])
|
||||
@ -519,6 +519,8 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { // {{{
|
||||
if len(msg) > 2 {
|
||||
msg = msg[2:]
|
||||
}
|
||||
case *Message:
|
||||
panic(e)
|
||||
default:
|
||||
return true
|
||||
}
|
||||
@ -538,6 +540,11 @@ func (m *Message) Assert(e interface{}, msg ...string) bool { // {{{
|
||||
func (m *Message) AssertOne(msg *Message, safe bool, hand ...func(msg *Message)) *Message { // {{{
|
||||
defer func() {
|
||||
if e := recover(); e != nil {
|
||||
switch e.(type) {
|
||||
case *Message:
|
||||
panic(e)
|
||||
}
|
||||
|
||||
msg.Log("error", nil, "error: %v", e)
|
||||
if msg.root.Conf("debug") == "on" && e != io.EOF {
|
||||
fmt.Printf("\n\033[31m%s error: %v\033[0m\n", msg.Target.Name, e)
|
||||
@ -682,9 +689,9 @@ func (m *Message) Search(key string, root ...bool) []*Message { // {{{
|
||||
|
||||
// }}}
|
||||
func (m *Message) Find(name string, root ...bool) *Message { // {{{
|
||||
target := m.Target
|
||||
if len(root) > 0 && root[0] {
|
||||
target = m.Target.root
|
||||
target := m.Target.root
|
||||
if len(root) > 0 && !root[0] {
|
||||
target = m.Target
|
||||
}
|
||||
|
||||
cs := target.contexts
|
||||
@ -746,9 +753,7 @@ func (m *Message) Set(meta string, arg ...string) *Message { // {{{
|
||||
m.Meta[meta] = arg
|
||||
case "option", "append":
|
||||
if len(arg) > 0 {
|
||||
if _, ok := m.Meta[arg[0]]; !ok {
|
||||
m.Meta[meta] = append(m.Meta[meta], arg[0])
|
||||
}
|
||||
m.Meta[meta] = []string{arg[0]}
|
||||
m.Meta[arg[0]] = arg[1:]
|
||||
} else {
|
||||
for _, k := range m.Meta[meta] {
|
||||
@ -808,6 +813,13 @@ func (m *Message) Get(key string) string { // {{{
|
||||
return ""
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Geti(key string) int { // {{{
|
||||
n, e := strconv.Atoi(m.Get(key))
|
||||
m.Assert(e)
|
||||
return n
|
||||
}
|
||||
|
||||
// }}}
|
||||
func (m *Message) Echo(str string, arg ...interface{}) *Message { // {{{
|
||||
return m.Add("result", fmt.Sprintf(str, arg...))
|
||||
@ -830,7 +842,7 @@ func (m *Message) Exec(key string, arg ...string) string { // {{{
|
||||
m.Master = m.Source
|
||||
if x, ok := s.Commands[key]; ok && x.Hand != nil && m.Check(c, "commands", key) {
|
||||
m.AssertOne(m, true, func(m *Message) {
|
||||
m.Log("cmd", s, "%s %v", key, arg)
|
||||
m.Log("cmd", s, "%s %v %v", key, arg, m.Meta["option"])
|
||||
|
||||
if x.Options != nil {
|
||||
for _, v := range m.Meta["option"] {
|
||||
@ -1250,10 +1262,10 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
|
||||
// }}}
|
||||
}},
|
||||
"context": &Command{Name: "context [root] [[find|search] name] [list|show|spawn|start|switch|close][args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块,args:启动参数",
|
||||
Formats: map[string]int{"root": 0, "back": 0, "home": 0, "find": 1, "search": 1, "list": 0, "show": 0, "close": 0, "switch": 0, "start": 0, "spawn": 0},
|
||||
"context": &Command{Name: "context [home] [[find|search] name] [info|list|show|spawn|start|switch|close][args]", Help: "查找并操作模块,\n查找起点root:根模块、back:父模块、home:本模块,\n查找方法find:路径匹配、search:模糊匹配,\n查找对象name:支持点分和正则,\n操作类型show:显示信息、switch:切换为当前、start:启动模块、spawn:分裂子模块,args:启动参数",
|
||||
Formats: map[string]int{"back": 0, "home": 0, "find": 1, "search": 1, "info": 1, "list": 0, "show": 0, "close": 0, "switch": 0, "start": 0, "spawn": 0},
|
||||
Hand: func(m *Message, c *Context, key string, arg ...string) {
|
||||
root := true || m.Has("root") // {{{
|
||||
root := !m.Has("home") // {{{
|
||||
|
||||
ms := []*Message{}
|
||||
switch {
|
||||
@ -1277,14 +1289,17 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
|
||||
for _, v := range ms {
|
||||
m.Target = v.Target
|
||||
|
||||
v.Meta = m.Meta
|
||||
v.Data = m.Data
|
||||
switch {
|
||||
case m.Has("switch"):
|
||||
m.Target = v.Target
|
||||
case m.Has("spawn"):
|
||||
v.Set("detail", arg[2:]...).Target.Spawn(v, arg[0], arg[1]).Begin(v)
|
||||
m.Target = v.Target
|
||||
case m.Has("start"):
|
||||
v.Set("detail", arg...).Target.Start(v)
|
||||
m.Target = v.Target
|
||||
case m.Has("close"):
|
||||
v.Target.Close(v)
|
||||
case m.Has("show"):
|
||||
@ -1304,6 +1319,15 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
m.Echo(" %s: -> %s %v\n", k, v.Target.Name, v.Meta["detail"])
|
||||
}
|
||||
}
|
||||
case m.Has("info"):
|
||||
switch m.Get("info") {
|
||||
case "name":
|
||||
m.Echo("%s", v.Target.Name)
|
||||
case "owner":
|
||||
m.Echo("%s", v.Target.Owner.Name)
|
||||
default:
|
||||
m.Echo("%s(%s): %s\n", v.Target.Name, v.Target.Owner.Name, v.Target.Help)
|
||||
}
|
||||
case m.Has("list") || len(m.Meta["detail"]) == 1:
|
||||
m.Travel(v.Target, func(msg *Message) bool {
|
||||
target := msg.Target
|
||||
@ -1338,7 +1362,11 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
return true
|
||||
})
|
||||
case len(arg) > 0 && v != m:
|
||||
v.Set("detail", arg...).Cmd()
|
||||
v.Cmd(arg...)
|
||||
// m.Meta = v.Meta
|
||||
// m.Target = target
|
||||
default:
|
||||
m.Target = v.Target
|
||||
}
|
||||
}
|
||||
// }}}
|
||||
@ -1456,18 +1484,21 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s config:\n", m.Target.Name)
|
||||
}
|
||||
// if all {
|
||||
// m.Echo("%s config:\n", m.Target.Name)
|
||||
// }
|
||||
if x, ok := m.Target.Configs[arg[0]]; ok {
|
||||
if m.Check(m.Target, "configs", arg[0]) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
// if all {
|
||||
// m.Echo(" ")
|
||||
// }
|
||||
// m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
m.Echo("%s", x.Value)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return all
|
||||
return true
|
||||
// return all
|
||||
})
|
||||
|
||||
case 2:
|
||||
@ -1512,18 +1543,21 @@ var Index = &Context{Name: "ctx", Help: "模块中心",
|
||||
}
|
||||
|
||||
m.BackTrace(func(m *Message) bool {
|
||||
if all {
|
||||
m.Echo("%s config:\n", m.Target.Name)
|
||||
}
|
||||
// if all {
|
||||
// m.Echo("%s config:\n", m.Target.Name)
|
||||
// }
|
||||
if x, ok := m.Target.Caches[arg[0]]; ok {
|
||||
if m.Check(m.Target, "caches", arg[0]) {
|
||||
if all {
|
||||
m.Echo(" ")
|
||||
}
|
||||
m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
// if all {
|
||||
// m.Echo(" ")
|
||||
// }
|
||||
// m.Echo("%s: %s\n", x.Name, x.Help)
|
||||
m.Echo("%s", x.Value)
|
||||
return false
|
||||
}
|
||||
}
|
||||
return all
|
||||
return true
|
||||
// return all
|
||||
})
|
||||
case 2:
|
||||
m.Cap(arg[0], arg[1])
|
||||
|
@ -80,9 +80,10 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
||||
m.Assert(len(arg) > 0, "缺少参数")
|
||||
m.Start(fmt.Sprintf("db%d", Pulse.Capi("nsource", 1)), "数据存储", arg...)
|
||||
Pulse.Cap("stream", Pulse.Cap("nsource"))
|
||||
m.Echo(m.Target.Name)
|
||||
}},
|
||||
"exec": &ctx.Command{Name: "exec sql [arg]", Help: "操作数据库",
|
||||
Appends: map[string]string{"LastInsertId": "最后插入元组的标识", "RowsAffected": "修改元组的数量"},
|
||||
Appends: map[string]string{"last": "最后插入元组的标识", "nrow": "修改元组的数量"},
|
||||
Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
mdb, ok := m.Target.Server.(*MDB)
|
||||
m.Assert(ok, "目标模块类型错误")
|
||||
@ -102,9 +103,9 @@ var Index = &ctx.Context{Name: "mdb", Help: "数据中心",
|
||||
m.Assert(e)
|
||||
|
||||
m.Echo("%d", id).Echo("%d", n)
|
||||
m.Add("append", "LastInsertId", fmt.Sprintf("%d", id))
|
||||
m.Add("append", "RowsAffected", fmt.Sprintf("%d", n))
|
||||
m.Log("info", nil, "last(%d) rows(%d)", id, n)
|
||||
m.Add("append", "last", fmt.Sprintf("%d", id))
|
||||
m.Add("append", "nrow", fmt.Sprintf("%d", n))
|
||||
m.Log("info", nil, "last(%d) nrow(%d)", id, n)
|
||||
}},
|
||||
"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)
|
||||
@ -145,7 +146,11 @@ 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"]))
|
||||
if len(m.Meta["append"]) > 0 {
|
||||
m.Log("info", nil, "rows(%d) cols(%d)", len(m.Meta[m.Meta["append"][0]]), len(m.Meta["append"]))
|
||||
} else {
|
||||
m.Log("info", nil, "rows(0) cols(0)")
|
||||
}
|
||||
}},
|
||||
},
|
||||
Index: map[string]*ctx.Context{
|
||||
|
@ -4,9 +4,11 @@ import (
|
||||
"context"
|
||||
|
||||
"fmt"
|
||||
"github.com/skip2/go-qrcode"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type NFS struct {
|
||||
@ -73,6 +75,7 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||
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) {
|
||||
m.Start(fmt.Sprintf("file%d", Pulse.Capi("nfile", 1)), "打开文件", arg...)
|
||||
m.Echo(m.Target.Name)
|
||||
}},
|
||||
"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)
|
||||
@ -117,11 +120,49 @@ var Index = &ctx.Context{Name: "nfs", Help: "存储中心",
|
||||
|
||||
m.Echo(m.Cap("pos"))
|
||||
}},
|
||||
"load": &ctx.Command{Name: "load file [size]", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
f, e := os.Open(arg[0])
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
size := 1024
|
||||
if len(arg) > 1 {
|
||||
if s, e := strconv.Atoi(arg[1]); e == nil {
|
||||
size = s
|
||||
}
|
||||
}
|
||||
|
||||
buf := make([]byte, size)
|
||||
f.Read(buf)
|
||||
m.Echo(string(buf))
|
||||
}},
|
||||
"save": &ctx.Command{Name: "save file string...", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
f, e := os.Create(arg[0])
|
||||
m.Assert(e)
|
||||
defer f.Close()
|
||||
fmt.Fprint(f, strings.Join(arg[1:], ""))
|
||||
}},
|
||||
"genqr": &ctx.Command{Name: "genqr [size] file string...", Help: "写入文件, string: 写入内容, pos: 写入位置", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
size := 256
|
||||
if len(arg) > 2 {
|
||||
if s, e := strconv.Atoi(arg[0]); e == nil {
|
||||
arg = arg[1:]
|
||||
size = s
|
||||
}
|
||||
}
|
||||
m.Log("fuck", nil, "%v %v", arg[0], arg[1:])
|
||||
qrcode.WriteFile(strings.Join(arg[1:], ""), qrcode.Medium, size, arg[0])
|
||||
}},
|
||||
},
|
||||
Index: map[string]*ctx.Context{
|
||||
"void": &ctx.Context{Name: "void",
|
||||
Commands: map[string]*ctx.Command{
|
||||
"open": &ctx.Command{},
|
||||
"open": &ctx.Command{},
|
||||
"save": &ctx.Command{},
|
||||
"load": &ctx.Command{},
|
||||
"genqr": &ctx.Command{},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
@ -11,20 +11,25 @@ import (
|
||||
|
||||
type SSH struct {
|
||||
send map[string]*ctx.Message
|
||||
*bufio.Writer
|
||||
*bufio.Reader
|
||||
net.Conn
|
||||
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (ssh *SSH) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
c.Caches = map[string]*ctx.Cache{
|
||||
"nsend": &ctx.Cache{Name: "nsend", Value: "0", Help: "消息发送数量"},
|
||||
"nsend": &ctx.Cache{Name: "消息发送数量", Value: "0", Help: "消息发送数量"},
|
||||
"nrecv": &ctx.Cache{Name: "消息接收数量", Value: "0", Help: "消息接收数量"},
|
||||
"target": &ctx.Cache{Name: "消息接收模块", Value: "ssh", Help: "消息接收模块"},
|
||||
"result": &ctx.Cache{Name: "前一条指令执行结果", Value: "", Help: "前一条指令执行结果"},
|
||||
"sessid": &ctx.Cache{Name: "会话令牌", Value: "", Help: "会话令牌"},
|
||||
}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
|
||||
s := new(SSH)
|
||||
s.Context = c
|
||||
s.send = make(map[string]*ctx.Message)
|
||||
return s
|
||||
}
|
||||
|
||||
@ -36,10 +41,13 @@ func (ssh *SSH) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
}
|
||||
|
||||
func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||
ssh.Group = ""
|
||||
ssh.Owner = nil
|
||||
ssh.Conn = m.Data["io"].(net.Conn)
|
||||
ssh.Reader = bufio.NewReader(ssh.Conn)
|
||||
m.Log("info", nil, "%d remote %v", 0, ssh.Conn.RemoteAddr())
|
||||
ssh.Writer = bufio.NewWriter(ssh.Conn)
|
||||
ssh.send = make(map[string]*ctx.Message)
|
||||
m.Log("info", nil, "%s connect %v <-> %v", Pulse.Cap("nhost"), ssh.Conn.LocalAddr(), ssh.Conn.RemoteAddr())
|
||||
|
||||
target, msg := m.Target, m.Spawn(m.Target)
|
||||
|
||||
@ -48,31 +56,30 @@ func (ssh *SSH) Start(m *ctx.Message, arg ...string) bool {
|
||||
m.Assert(e)
|
||||
|
||||
if line = strings.TrimSpace(line); len(line) == 0 {
|
||||
if msg.Has("detail") {
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["detail"])
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["option"])
|
||||
if msg.Log("info", nil, "remote: %v", msg.Meta["option"]); msg.Has("detail") {
|
||||
msg.Log("info", nil, "%d exec: %v", m.Capi("nrecv", 1), msg.Meta["detail"])
|
||||
|
||||
msg.Cmd(msg.Meta["detail"]...)
|
||||
target = msg.Target
|
||||
m.Cap("target", target.Name)
|
||||
|
||||
fmt.Fprintf(ssh.Conn, "result: ")
|
||||
for _, v := range msg.Meta["result"] {
|
||||
fmt.Fprintf(ssh.Conn, "%s", url.QueryEscape(v))
|
||||
fmt.Fprintf(ssh.Writer, "result: %s\n", url.QueryEscape(v))
|
||||
}
|
||||
fmt.Fprintf(ssh.Conn, "\n")
|
||||
|
||||
msg.Meta["append"] = append(msg.Meta["append"], "nsend")
|
||||
msg.Add("append", "nsend", msg.Get("nsend"))
|
||||
fmt.Fprintf(ssh.Writer, "nsend: %s\n", msg.Get("nrecv"))
|
||||
for _, k := range msg.Meta["append"] {
|
||||
for _, v := range msg.Meta[k] {
|
||||
fmt.Fprintf(ssh.Conn, "%s: %s\n", k, v)
|
||||
fmt.Fprintf(ssh.Writer, "%s: %s\n", k, v)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(ssh.Conn, "\n")
|
||||
} else if msg.Has("result") {
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["result"])
|
||||
msg.Log("info", nil, "remote: %v", msg.Meta["append"])
|
||||
fmt.Fprintf(ssh.Writer, "\n")
|
||||
ssh.Writer.Flush()
|
||||
} else {
|
||||
msg.Log("info", nil, "%s echo: %v", msg.Get("nsend"), msg.Meta["result"])
|
||||
|
||||
m.Cap("result", msg.Get("result"))
|
||||
msg.Meta["append"] = msg.Meta["option"]
|
||||
send := ssh.send[msg.Get("nsend")]
|
||||
send.Meta = msg.Meta
|
||||
send.Recv <- true
|
||||
@ -101,36 +108,39 @@ func (ssh *SSH) Close(m *ctx.Message, arg ...string) bool {
|
||||
var Pulse *ctx.Message
|
||||
var Index = &ctx.Context{Name: "ssh", Help: "集群中心",
|
||||
Caches: map[string]*ctx.Cache{
|
||||
"nhost": &ctx.Cache{Name: "nhost", Value: "0", Help: "主机数量"},
|
||||
"nhost": &ctx.Cache{Name: "主机数量", Value: "0", Help: "主机数量"},
|
||||
},
|
||||
Configs: map[string]*ctx.Config{},
|
||||
Commands: map[string]*ctx.Command{
|
||||
"listen": &ctx.Command{Name: "listen address", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
||||
"listen": &ctx.Command{Name: "listen address protocol", Help: "监听连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Find("tcp").Cmd(m.Meta["detail"]...)
|
||||
}},
|
||||
"dial": &ctx.Command{Name: "dial address", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Find("tcp", true).Cmd(m.Meta["detail"]...)
|
||||
"dial": &ctx.Command{Name: "dial address protocol", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Find("tcp").Cmd(m.Meta["detail"]...)
|
||||
}},
|
||||
"open": &ctx.Command{Name: "open", Help: "打开连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Start(fmt.Sprintf("host%s", Pulse.Capi("nhost", 1)), "主机连接")
|
||||
m.Start(fmt.Sprintf("host%d", Pulse.Capi("nhost", 1)), "主机连接")
|
||||
}},
|
||||
"remote": &ctx.Command{Name: "remote detail...", Help: "远程执行", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
ssh, ok := m.Target.Server.(*SSH)
|
||||
m.Assert(ok)
|
||||
|
||||
m.Capi("nsend", 1)
|
||||
m.Recv = make(chan bool)
|
||||
m.Add("option", "nsend", m.Cap("nsend"))
|
||||
m.Add("option", "nrecv", m.Cap("nsend"))
|
||||
ssh.send[m.Cap("nsend")] = m
|
||||
|
||||
for _, v := range arg {
|
||||
fmt.Fprintf(ssh.Conn, "detail: %v\n", v)
|
||||
fmt.Fprintf(ssh.Writer, "detail: %v\n", v)
|
||||
}
|
||||
for _, k := range m.Meta["option"] {
|
||||
for _, v := range m.Meta[k] {
|
||||
fmt.Fprintf(ssh.Conn, "%s: %s\n", k, v)
|
||||
fmt.Fprintf(ssh.Writer, "%s: %s\n", k, v)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(ssh.Conn, "\n")
|
||||
fmt.Fprintf(ssh.Writer, "\n")
|
||||
ssh.Writer.Flush()
|
||||
|
||||
m.Recv = make(chan bool)
|
||||
<-m.Recv
|
||||
}},
|
||||
},
|
||||
|
@ -3,6 +3,7 @@ package tcp
|
||||
import (
|
||||
"context"
|
||||
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
@ -16,9 +17,9 @@ type TCP struct {
|
||||
|
||||
func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
c.Caches = map[string]*ctx.Cache{
|
||||
"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: "加密通信"},
|
||||
"address": &ctx.Cache{Name: "address", Value: "", Help: "监听地址"},
|
||||
"protocol": &ctx.Cache{Name: "网络协议(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "网络协议"},
|
||||
"security": &ctx.Cache{Name: "加密通信(true/false)", Value: m.Conf("security"), Help: "加密通信"},
|
||||
"address": &ctx.Cache{Name: "网络地址", Value: "", Help: "网络地址"},
|
||||
}
|
||||
c.Configs = map[string]*ctx.Config{}
|
||||
|
||||
@ -31,7 +32,6 @@ func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server {
|
||||
if tcp.Context == Index {
|
||||
Pulse = m
|
||||
}
|
||||
|
||||
return tcp
|
||||
}
|
||||
|
||||
@ -42,37 +42,59 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool {
|
||||
if len(arg) > 2 {
|
||||
m.Cap("security", arg[2])
|
||||
}
|
||||
if len(arg) > 3 {
|
||||
m.Cap("protocol", arg[3])
|
||||
}
|
||||
|
||||
switch arg[0] {
|
||||
case "dial":
|
||||
c, e := net.Dial(m.Cap("protocol"), m.Cap("address"))
|
||||
m.Assert(e)
|
||||
tcp.Conn = c
|
||||
m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr())))
|
||||
if m.Cap("security") != "false" {
|
||||
cert, e := tls.LoadX509KeyPair(m.Conf("cert"), m.Conf("key"))
|
||||
m.Assert(e)
|
||||
conf := &tls.Config{Certificates: []tls.Certificate{cert}}
|
||||
|
||||
msg := m.Reply("open").Put("option", "io", c)
|
||||
c, e := tls.Dial(m.Cap("protocol"), m.Cap("address"), conf)
|
||||
m.Assert(e)
|
||||
tcp.Conn = c
|
||||
} else {
|
||||
c, e := net.Dial(m.Cap("protocol"), m.Cap("address"))
|
||||
m.Assert(e)
|
||||
tcp.Conn = c
|
||||
}
|
||||
|
||||
msg := m.Reply("open").Put("option", "io", tcp.Conn)
|
||||
msg.Cmd("open")
|
||||
msg.Cap("stream", tcp.LocalAddr().String())
|
||||
m.Log("info", nil, "%s dial %s", Pulse.Cap("nclient"), msg.Cap("stream", m.Cap("stream", fmt.Sprintf("%s->%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
|
||||
return false
|
||||
case "accept":
|
||||
c, e := m.Data["io"].(net.Conn)
|
||||
m.Assert(e)
|
||||
tcp.Conn = c
|
||||
m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"), m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr())))
|
||||
|
||||
msg := m.Spawn(m.Data["source"].(*ctx.Context), "open").Put("option", "io", c)
|
||||
msg := m.Spawn(m.Data["source"].(*ctx.Context), "open").Put("option", "io", tcp.Conn)
|
||||
msg.Cmd("open")
|
||||
msg.Cap("stream", tcp.RemoteAddr().String())
|
||||
m.Log("info", nil, "%s accept %s", Pulse.Cap("nclient"), msg.Cap("stream", m.Cap("stream", fmt.Sprintf("%s<-%s", tcp.LocalAddr(), tcp.RemoteAddr()))))
|
||||
return false
|
||||
default:
|
||||
if m.Cap("security") != "false" {
|
||||
cert, e := tls.LoadX509KeyPair(m.Conf("cert"), m.Conf("key"))
|
||||
m.Assert(e)
|
||||
conf := &tls.Config{Certificates: []tls.Certificate{cert}}
|
||||
|
||||
l, e := tls.Listen(m.Cap("protocol"), m.Cap("address"), conf)
|
||||
m.Assert(e)
|
||||
tcp.Listener = l
|
||||
} else {
|
||||
l, e := net.Listen(m.Cap("protocol"), m.Cap("address"))
|
||||
m.Assert(e)
|
||||
tcp.Listener = l
|
||||
}
|
||||
|
||||
m.Log("info", nil, "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", tcp.Addr())))
|
||||
}
|
||||
|
||||
l, e := net.Listen(m.Cap("protocol"), m.Cap("address"))
|
||||
m.Assert(e)
|
||||
tcp.Listener = l
|
||||
m.Log("info", nil, "%d listen %v", Pulse.Capi("nlisten"), m.Cap("stream", fmt.Sprintf("%s", l.Addr())))
|
||||
|
||||
for {
|
||||
c, e := l.Accept()
|
||||
c, e := tcp.Accept()
|
||||
m.Assert(e)
|
||||
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())
|
||||
}
|
||||
@ -108,31 +130,28 @@ func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool {
|
||||
var Pulse *ctx.Message
|
||||
var Index = &ctx.Context{Name: "tcp", Help: "网络中心",
|
||||
Caches: map[string]*ctx.Cache{
|
||||
"nlisten": &ctx.Cache{Name: "nlisten", Value: "0", Help: "监听数量"},
|
||||
"nclient": &ctx.Cache{Name: "nclient", Value: "0", Help: "连接数量"},
|
||||
"nlisten": &ctx.Cache{Name: "监听数量", Value: "0", Help: "监听数量"},
|
||||
"nclient": &ctx.Cache{Name: "连接数量", Value: "0", Help: "连接数量"},
|
||||
},
|
||||
Configs: map[string]*ctx.Config{
|
||||
"protocol": &ctx.Config{Name: "protocol(tcp/tcp4/tcp6)", Value: "tcp4", Help: "连接协议"},
|
||||
"security": &ctx.Config{Name: "security(true/false)", Value: "false", Help: "加密通信"},
|
||||
"security": &ctx.Config{Name: "加密通信(true/false)", Value: "false", Help: "加密通信"},
|
||||
"protocol": &ctx.Config{Name: "网络协议(tcp/tcp4/tcp6)", Value: "tcp4", Help: "网络协议"},
|
||||
},
|
||||
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) {
|
||||
"listen": &ctx.Command{Name: "listen address [security [protocol]]", Help: "网络监听", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Start(fmt.Sprintf("pub%d", Pulse.Capi("nlisten", 1)), "网络监听", m.Meta["detail"]...)
|
||||
}},
|
||||
"dial": &ctx.Command{Name: "dial [address [security]]", Help: "建立连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
"dial": &ctx.Command{Name: "dial address [security [protocol]]", Help: "网络连接", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Start(fmt.Sprintf("com%d", Pulse.Capi("nclient", 1)), "网络连接", m.Meta["detail"]...)
|
||||
}},
|
||||
"send": &ctx.Command{Name: "send message", 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)
|
||||
|
||||
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) {
|
||||
tcp, ok := m.Target.Server.(*TCP)
|
||||
m.Assert(ok && tcp.Conn != nil)
|
||||
|
||||
size, e := strconv.Atoi(arg[0])
|
||||
m.Assert(e)
|
||||
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
@ -27,27 +28,43 @@ type WEB struct {
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (web *WEB) AppendJson(msg *ctx.Message) string {
|
||||
result := []string{"{"}
|
||||
for i, k := range msg.Meta["append"] {
|
||||
result = append(result, fmt.Sprintf("\"%s\": [", k))
|
||||
for j, v := range msg.Meta[k] {
|
||||
result = append(result, fmt.Sprintf("\"%s\"", v))
|
||||
if j < len(msg.Meta[k])-1 {
|
||||
result = append(result, ",")
|
||||
}
|
||||
}
|
||||
result = append(result, "]")
|
||||
if i < len(msg.Meta["append"])-1 {
|
||||
result = append(result, ", ")
|
||||
}
|
||||
}
|
||||
result = append(result, "}")
|
||||
return strings.Join(result, "")
|
||||
}
|
||||
|
||||
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) {
|
||||
msg := m.Spawn(m.Target).Set("detail", key)
|
||||
for k, v := range r.Form {
|
||||
msg.Add("option", k)
|
||||
msg.Meta[k] = v
|
||||
msg.Add("option", k, v...)
|
||||
}
|
||||
for _, v := range r.Cookies() {
|
||||
msg.Add("option", v.Name, v.Value)
|
||||
}
|
||||
|
||||
msg.Log("cmd", nil, "%s %v", key, msg.Meta["option"])
|
||||
msg.Put("option", "request", r)
|
||||
msg.Put("option", "response", w)
|
||||
hand(msg, msg.Target, key)
|
||||
|
||||
header := w.Header()
|
||||
for _, k := range msg.Meta["append"] {
|
||||
header.Add("Set-Cookie", (&http.Cookie{Name: k, Value: msg.Get(k)}).String())
|
||||
msg.Log("cmd", nil, "%s [] %v", key, msg.Meta["option"])
|
||||
msg.Put("option", "request", r).Put("option", "response", w)
|
||||
if hand(msg, msg.Target, key); len(msg.Meta["append"]) > 0 {
|
||||
msg.Set("result", web.AppendJson(msg))
|
||||
}
|
||||
|
||||
for _, v := range msg.Meta["result"] {
|
||||
msg.Log("info", nil, "%s", v)
|
||||
w.Write([]byte(v))
|
||||
}
|
||||
})
|
||||
@ -58,16 +75,16 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println()
|
||||
web.Log("cmd", nil, "%v %s %s", r.RemoteAddr, r.Method, r.URL)
|
||||
|
||||
if web.Cap("logheaders") == "yes" {
|
||||
if web.Conf("logheaders") == "yes" {
|
||||
for k, v := range r.Header {
|
||||
log.Println(k+":", v[0])
|
||||
log.Printf("%s: %v", k, v)
|
||||
}
|
||||
log.Println()
|
||||
}
|
||||
|
||||
if r.ParseForm(); len(r.PostForm) > 0 {
|
||||
for k, v := range r.PostForm {
|
||||
log.Printf("%s: %s", k, v[0])
|
||||
log.Printf("%s: %v", k, v)
|
||||
}
|
||||
log.Println()
|
||||
}
|
||||
@ -75,9 +92,9 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
web.ServeMux.ServeHTTP(w, r)
|
||||
|
||||
if web.Message != nil && web.Cap("logheaders") == "yes" {
|
||||
if web.Message != nil && web.Conf("logheaders") == "yes" {
|
||||
for k, v := range w.Header() {
|
||||
log.Println(k+":", v[0])
|
||||
log.Printf("%s: %v", k, v)
|
||||
}
|
||||
log.Println()
|
||||
}
|
||||
@ -93,10 +110,10 @@ func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, 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: "服务目录"}
|
||||
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["master"] = &ctx.Cache{Name: "master(yes/no)", Value: "no", Help: "日志输出请求头"}
|
||||
web.Caches["route"] = &ctx.Cache{Name: "请求路径", Value: "/" + web.Context.Name + "/", Help: "请求路径"}
|
||||
web.Caches["register"] = &ctx.Cache{Name: "已初始化(yes/no)", Value: "no", Help: "模块是否已初始化"}
|
||||
web.Caches["master"] = &ctx.Cache{Name: "服务入口(yes/no)", Value: "no", Help: "服务入口"}
|
||||
web.Caches["directory"] = &ctx.Cache{Name: "服务目录", Value: "usr", Help: "服务目录"}
|
||||
if len(arg) > 0 {
|
||||
m.Cap("directory", arg[0])
|
||||
}
|
||||
@ -118,9 +135,6 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool {
|
||||
m.Cap("directory", arg[0])
|
||||
}
|
||||
|
||||
web.Message = m
|
||||
m.Cap("master", "yes")
|
||||
|
||||
m.Travel(m.Target, func(m *ctx.Message) bool {
|
||||
if h, ok := m.Target.Server.(http.Handler); ok && m.Cap("register") == "no" {
|
||||
m.Cap("register", "yes")
|
||||
@ -147,8 +161,8 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool {
|
||||
return true
|
||||
})
|
||||
|
||||
web.Caches["address"] = &ctx.Cache{Name: "address", Value: ":9191", Help: "监听地址"}
|
||||
web.Caches["protocol"] = &ctx.Cache{Name: "protocol", Value: "http", Help: "服务协议"}
|
||||
web.Caches["address"] = &ctx.Cache{Name: "服务地址", Value: ":9191", Help: "服务地址"}
|
||||
web.Caches["protocol"] = &ctx.Cache{Name: "服务协议", Value: "http", Help: "服务协议"}
|
||||
if len(arg) > 1 {
|
||||
m.Cap("address", arg[1])
|
||||
}
|
||||
@ -156,16 +170,20 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool {
|
||||
m.Cap("protocol", arg[2])
|
||||
}
|
||||
|
||||
m.Cap("master", "yes")
|
||||
m.Cap("stream", m.Cap("address"))
|
||||
m.Log("info", nil, "address [%s]", m.Cap("address"))
|
||||
m.Log("info", nil, "protocol [%s]", m.Cap("protocol"))
|
||||
web.Server = &http.Server{Addr: m.Cap("address"), Handler: web}
|
||||
|
||||
web.Caches["logheaders"] = &ctx.Cache{Name: "日志输出报文头(yes/no)", Value: "yes", Help: "日志输出请求头"}
|
||||
web.Configs["logheaders"] = &ctx.Config{Name: "日志输出报文头(yes/no)", Value: "yes", Help: "日志输出报文头"}
|
||||
|
||||
if m.Cap("protocol") == "https" {
|
||||
m.Log("info", nil, "key [%s]", m.Cap("key"))
|
||||
if web.Message = m; m.Cap("protocol") == "https" {
|
||||
web.Caches["cert"] = &ctx.Cache{Name: "服务证书", Value: m.Conf("cert"), Help: "服务证书"}
|
||||
web.Caches["key"] = &ctx.Cache{Name: "服务密钥", Value: m.Conf("key"), Help: "服务密钥"}
|
||||
m.Log("info", nil, "cert [%s]", m.Cap("cert"))
|
||||
m.Log("info", nil, "key [%s]", m.Cap("key"))
|
||||
|
||||
web.Server.ListenAndServeTLS(m.Cap("cert"), m.Cap("key"))
|
||||
} else {
|
||||
web.Server.ListenAndServe()
|
||||
@ -186,17 +204,17 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
Caches: map[string]*ctx.Cache{},
|
||||
Configs: map[string]*ctx.Config{},
|
||||
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) {
|
||||
"serve": &ctx.Command{Name: "serve [directory [address [protocol]]]", Help: "开启应用服务", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Set("detail", arg...).Target.Start(m)
|
||||
}},
|
||||
"route": &ctx.Command{Name: "route [directory|template|script] route string", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
"route": &ctx.Command{Name: "route directory|template|script route content", Help: "添加应用内容", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
mux, ok := m.Target.Server.(MUX)
|
||||
m.Assert(ok, "模块类型错误")
|
||||
m.Assert(len(arg) == 3, "缺少参数")
|
||||
|
||||
switch arg[0] {
|
||||
case "directory":
|
||||
mux.Handle(arg[1], http.FileServer(http.Dir(arg[2])))
|
||||
mux.Handle(arg[1]+"/", http.StripPrefix(arg[1], http.FileServer(http.Dir(arg[2]))))
|
||||
case "template":
|
||||
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) {
|
||||
w := m.Data["response"].(http.ResponseWriter)
|
||||
@ -209,8 +227,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
|
||||
})
|
||||
case "script":
|
||||
cli := m.Find("cli", true)
|
||||
lex := m.Find("lex", true)
|
||||
cli := m.Find("cli")
|
||||
lex := m.Find("lex")
|
||||
mux.Trans(m, arg[1], func(m *ctx.Message, c *ctx.Context, key string, a ...string) {
|
||||
f, e := os.Open(arg[2])
|
||||
line, bio := "", bufio.NewReader(f)
|
||||
@ -233,9 +251,8 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
})
|
||||
}
|
||||
}},
|
||||
"/hi": &ctx.Command{Name: "/hi", Help: "添加响应", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
"/demo": &ctx.Command{Name: "/demo", Help: "应用示例", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Add("append", "hi", "hello")
|
||||
m.Echo("hello\n")
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user