From 6d4f81f4c3b558999a35f9127ff54e02e4e07d9f Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 24 Dec 2017 22:26:54 +0800 Subject: [PATCH] =?UTF-8?q?mac=20mod=20code=20=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E4=BA=9B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/context/mdb/mdb.go | 15 +++++--- src/context/nfs/nfs.go | 43 ++++++++++++++++++++- src/context/tcp/tcp.go | 75 +++++++++++++++++++++++-------------- src/context/web/web.go | 85 +++++++++++++++++++++++++----------------- 4 files changed, 150 insertions(+), 68 deletions(-) diff --git a/src/context/mdb/mdb.go b/src/context/mdb/mdb.go index 3ff097d7..bf88811c 100644 --- a/src/context/mdb/mdb.go +++ b/src/context/mdb/mdb.go @@ -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{ diff --git a/src/context/nfs/nfs.go b/src/context/nfs/nfs.go index 9b2e2201..40341867 100644 --- a/src/context/nfs/nfs.go +++ b/src/context/nfs/nfs.go @@ -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{}, }, }, }, diff --git a/src/context/tcp/tcp.go b/src/context/tcp/tcp.go index c6b7d118..8acb8a9d 100644 --- a/src/context/tcp/tcp.go +++ b/src/context/tcp/tcp.go @@ -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) diff --git a/src/context/web/web.go b/src/context/web/web.go index d4fa7171..a66eaa82 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -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") }}, }, }