From 548cfe512e283cb6cd07fa209013360844dfbe65 Mon Sep 17 00:00:00 2001 From: shaoying Date: Sun, 8 Apr 2018 00:21:30 +0800 Subject: [PATCH] vps mod AppendJson --- src/context/tcp/tcp.go | 33 ++++++++++++++++++++++++++------- src/context/web/web.go | 9 ++++++++- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/context/tcp/tcp.go b/src/context/tcp/tcp.go index 8ada84e9..e96a9c19 100644 --- a/src/context/tcp/tcp.go +++ b/src/context/tcp/tcp.go @@ -1,6 +1,6 @@ -package tcp - -import ( +package tcp // {{{ +// }}} +import ( // {{{ "context" "crypto/tls" @@ -9,13 +9,15 @@ import ( "strconv" ) +// }}} + type TCP struct { net.Conn net.Listener *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{ "protocol": &ctx.Cache{Name: "网络协议(tcp/tcp4/tcp6)", Value: m.Conf("protocol"), Help: "网络协议"}, "security": &ctx.Cache{Name: "加密通信(true/false)", Value: m.Conf("security"), Help: "加密通信"}, @@ -28,7 +30,8 @@ func (tcp *TCP) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server return s } -func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { +// }}} +func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { // {{{ tcp.Context.Master(nil) if tcp.Context == Index { Pulse = m @@ -36,7 +39,8 @@ func (tcp *TCP) Begin(m *ctx.Message, arg ...string) ctx.Server { 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 { m.Cap("address", arg[1]) } @@ -105,7 +109,8 @@ func (tcp *TCP) Start(m *ctx.Message, arg ...string) bool { 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 { case m.Target(): if tcp.Listener != nil { @@ -130,6 +135,8 @@ func (tcp *TCP) Close(m *ctx.Message, arg ...string) bool { return true } +// }}} + var Pulse *ctx.Message var Index = &ctx.Context{Name: "tcp", Help: "网络中心", Caches: map[string]*ctx.Cache{ @@ -166,6 +173,18 @@ var Index = &ctx.Context{Name: "tcp", Help: "网络中心", tcp.Conn.Read(buf) m.Echo(string(buf)) }}, + "ifconfig": &ctx.Command{Name: "ifconfig", Help: "接收消息", + Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) { + ifs, e := net.Interfaces() + m.Assert(e) + for _, v := range ifs { + ips, e := v.Addrs() + m.Assert(e) + for _, x := range ips { + m.Echo("%d %s %v %v\n", v.Index, v.Name, v.HardwareAddr, x.String()) + } + } + }}, }, Index: map[string]*ctx.Context{ "void": &ctx.Context{ diff --git a/src/context/web/web.go b/src/context/web/web.go index 43a72ec8..6b7a453e 100644 --- a/src/context/web/web.go +++ b/src/context/web/web.go @@ -3,8 +3,10 @@ package web // {{{ import ( // {{{ "context" + "encoding/json" "html/template" "net/http" + "net/url" "bufio" "fmt" @@ -31,11 +33,15 @@ type WEB struct { } func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{ + b, e := json.Marshal(msg.Meta) + msg.Assert(e) + return string(b) + 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)) + result = append(result, fmt.Sprintf("\"%s\"", url.QueryEscape(v))) if j < len(msg.Meta[k])-1 { result = append(result, ",") } @@ -46,6 +52,7 @@ func (web *WEB) AppendJson(msg *ctx.Message) string { // {{{ } } result = append(result, "}") + return strings.Join(result, "") }