From fadbff380a524d08a153b026205804d910d01a16 Mon Sep 17 00:00:00 2001 From: shaoying Date: Thu, 18 Jun 2020 19:38:34 +0800 Subject: [PATCH] opt handleCmd --- base/web/serve.go | 140 ++++++++++++++++++++++------------------------ base/web/space.go | 13 +++-- base/web/web.go | 6 +- 3 files changed, 80 insertions(+), 79 deletions(-) diff --git a/base/web/serve.go b/base/web/serve.go index 337010d4..bf69cf8a 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -56,84 +56,80 @@ func Login(msg *ice.Message, w http.ResponseWriter, r *http.Request) bool { return msg.Option(ice.MSG_USERURL) != "" } -func Trans(web *Frame, m *ice.Message, key string, cmd *ice.Command) { - web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) { - m.TryCatch(m.Spawns(), true, func(msg *ice.Message) { - defer func() { msg.Cost("%s %v %v", r.URL.Path, msg.Optionv("cmds"), msg.Format("append")) }() - if u, e := url.Parse(r.Header.Get("Referer")); e == nil { - for k, v := range u.Query() { - msg.Logs("refer", k, v) - msg.Option(k, v) - } +func HandleCmd(key string, cmd *ice.Command, msg *ice.Message, w http.ResponseWriter, r *http.Request) { + defer func() { msg.Cost("%s %v %v", r.URL.Path, msg.Optionv("cmds"), msg.Format("append")) }() + if u, e := url.Parse(r.Header.Get("Referer")); e == nil { + for k, v := range u.Query() { + msg.Logs("refer", k, v) + msg.Option(k, v) + } + } + + // 用户请求 + msg.Option(ice.MSG_USERWEB, msg.Conf(SHARE, "meta.domain")) + msg.Option(ice.MSG_USERIP, r.Header.Get(ice.MSG_USERIP)) + msg.Option(ice.MSG_USERUA, r.Header.Get("User-Agent")) + msg.Option(ice.MSG_USERURL, r.URL.Path) + if msg.R, msg.W = r, w; r.Header.Get("X-Real-Port") != "" { + msg.Option(ice.MSG_USERADDR, msg.Option(ice.MSG_USERIP)+":"+r.Header.Get("X-Real-Port")) + } else { + msg.Option(ice.MSG_USERADDR, r.RemoteAddr) + } + + // 请求变量 + msg.Option(ice.MSG_SESSID, "") + msg.Option(ice.MSG_OUTPUT, "") + for _, v := range r.Cookies() { + msg.Option(v.Name, v.Value) + } + + // 解析引擎 + switch r.Header.Get("Content-Type") { + case "application/json": + var data interface{} + if e := json.NewDecoder(r.Body).Decode(&data); !msg.Warn(e != nil, "%s", e) { + msg.Optionv(ice.MSG_USERDATA, data) + msg.Logs("json", "value", kit.Formats(data)) + } + + switch d := data.(type) { + case map[string]interface{}: + for k, v := range d { + msg.Optionv(k, v) } - - // 用户请求 - msg.Option(ice.MSG_USERWEB, m.Conf(SHARE, "meta.domain")) - msg.Option(ice.MSG_USERIP, r.Header.Get(ice.MSG_USERIP)) - msg.Option(ice.MSG_USERUA, r.Header.Get("User-Agent")) - msg.Option(ice.MSG_USERURL, r.URL.Path) - if msg.R, msg.W = r, w; r.Header.Get("X-Real-Port") != "" { - msg.Option(ice.MSG_USERADDR, msg.Option(ice.MSG_USERIP)+":"+r.Header.Get("X-Real-Port")) - } else { - msg.Option(ice.MSG_USERADDR, r.RemoteAddr) + } + default: + r.ParseMultipartForm(kit.Int64(kit.Select(r.Header.Get("Content-Length"), "4096"))) + if r.ParseForm(); len(r.PostForm) > 0 { + for k, v := range r.PostForm { + msg.Logs("form", k, v) } + } + } - // 请求变量 - msg.Option(ice.MSG_SESSID, "") - msg.Option(ice.MSG_OUTPUT, "") - for _, v := range r.Cookies() { - msg.Option(v.Name, v.Value) - } + // 请求参数 + for k, v := range r.Form { + if msg.Optionv(k, v); k == ice.MSG_SESSID { + msg.Render("cookie", v[0]) + } + } - // 解析引擎 - switch r.Header.Get("Content-Type") { - case "application/json": - var data interface{} - if e := json.NewDecoder(r.Body).Decode(&data); !msg.Warn(e != nil, "%s", e) { - msg.Optionv(ice.MSG_USERDATA, data) - msg.Logs("json", "value", kit.Formats(data)) - } + // 请求命令 + if msg.Option(ice.MSG_USERPOD, msg.Option("pod")); msg.Optionv("cmds") == nil { + if p := strings.TrimPrefix(msg.Option(ice.MSG_USERURL), key); p != "" { + msg.Optionv("cmds", strings.Split(p, "/")) + } + } - switch d := data.(type) { - case map[string]interface{}: - for k, v := range d { - msg.Optionv(k, v) - } - } - default: - r.ParseMultipartForm(kit.Int64(kit.Select(r.Header.Get("Content-Length"), "4096"))) - if r.ParseForm(); len(r.PostForm) > 0 { - for k, v := range r.PostForm { - msg.Logs("form", k, v) - } - } - } + // 执行命令 + if cmds := kit.Simple(msg.Optionv("cmds")); Login(msg, w, r) { + msg.Option("_option", msg.Optionv(ice.MSG_OPTION)) + msg.Target().Run(msg, cmd, msg.Option(ice.MSG_USERURL), cmds...) + } - // 请求参数 - for k, v := range r.Form { - if msg.Optionv(k, v); k == ice.MSG_SESSID { - msg.Render("cookie", v[0]) - } - } - - // 请求命令 - if msg.Option(ice.MSG_USERPOD, msg.Option("pod")); msg.Optionv("cmds") == nil { - if p := strings.TrimPrefix(msg.Option(ice.MSG_USERURL), key); p != "" { - msg.Optionv("cmds", strings.Split(p, "/")) - } - } - - // 执行命令 - if cmds := kit.Simple(msg.Optionv("cmds")); Login(msg, w, r) { - msg.Option("_option", msg.Optionv(ice.MSG_OPTION)) - msg.Target().Run(msg, cmd, msg.Option(ice.MSG_USERURL), cmds...) - } - - // 渲染引擎 - _args, _ := msg.Optionv(ice.MSG_ARGS).([]interface{}) - Render(msg, msg.Option(ice.MSG_OUTPUT), _args...) - }) - }) + // 渲染引擎 + _args, _ := msg.Optionv(ice.MSG_ARGS).([]interface{}) + Render(msg, msg.Option(ice.MSG_OUTPUT), _args...) } func (web *Frame) ServeHTTP(w http.ResponseWriter, r *http.Request) { m := web.m diff --git a/base/web/space.go b/base/web/space.go index 8b91037d..4701e01c 100644 --- a/base/web/space.go +++ b/base/web/space.go @@ -17,6 +17,9 @@ import ( ) func _link(m *ice.Message, pod interface{}) string { + if m.Option(ice.MSG_USERUA) == "" { + return kit.Format(pod) + } return fmt.Sprintf(`%s`, kit.Select(m.Conf(SHARE, "meta.domain"), m.Option(ice.MSG_USERWEB)), pod, pod) } @@ -44,7 +47,7 @@ func _space_dial(m *ice.Message, dev, name string, arg ...string) { host := kit.Format(client["hostname"]) proto := kit.Select("ws", "wss", client["protocol"] == "https") - uri := kit.MergeURL(proto+"://"+host+"/space/", "name", name) + uri := kit.MergeURL(proto+"://"+host+"/space/", "name", name, "type", cli.NodeType) if u, e := url.Parse(uri); m.Assert(e) { task.Put(dev, func(task *task.Task) error { @@ -212,13 +215,11 @@ func init() { "/space/": {Name: "/space/", Help: "空间站", Hand: func(m *ice.Message, c *ice.Context, cmd string, arg ...string) { if s, e := websocket.Upgrade(m.W, m.R, nil, m.Confi(ice.WEB_SPACE, "meta.buffer.r"), m.Confi(ice.WEB_SPACE, "meta.buffer.w")); m.Assert(e) { name := m.Option(kit.MDB_NAME, strings.Replace(kit.Select(m.Option(ice.MSG_USERADDR), m.Option(kit.MDB_NAME)), ".", "_", -1)) + kind := kit.Select(ice.WEB_WORKER, m.Option(kit.MDB_TYPE)) // 添加节点 - h := m.Rich(ice.WEB_SPACE, nil, kit.Dict( - kit.MDB_TYPE, ice.WEB_WORKER, - kit.MDB_NAME, m.Option(kit.MDB_NAME), - kit.MDB_TEXT, s.RemoteAddr().String(), - "socket", s, + h := m.Rich(ice.WEB_SPACE, nil, kit.Dict("socket", s, + kit.MDB_TYPE, kind, kit.MDB_NAME, name, kit.MDB_TEXT, s.RemoteAddr().String(), )) m.Log_CREATE(SPACE, name) diff --git a/base/web/web.go b/base/web/web.go index caacbc23..a5d830de 100644 --- a/base/web/web.go +++ b/base/web/web.go @@ -120,7 +120,11 @@ func (web *Frame) Start(m *ice.Message, arg ...string) bool { m.Travel(func(p *ice.Context, sub *ice.Context, k string, x *ice.Command) { if s == sub && k[0] == '/' { msg.Log("route", "%s <- %s", s.Name, k) - Trans(w, msg, k, x) + w.HandleFunc(k, func(w http.ResponseWriter, r *http.Request) { + m.TryCatch(msg.Spawns(), true, func(msg *ice.Message) { + HandleCmd(k, x, msg, w, r) + }) + }) } }) }