From a5cab33b8fdba47696a47be98e1b853576b4bbe1 Mon Sep 17 00:00:00 2001 From: shylinux Date: Mon, 24 Aug 2020 17:50:38 +0800 Subject: [PATCH] opt web.share.proxy --- base/aaa/sess.go | 2 +- base/nfs/nfs.go | 12 ++++++------ base/web/serve.go | 6 +++++- base/web/share.go | 32 +++++++++++++++++++++++--------- base/web/spide.go | 6 ++++-- conf.go | 1 + core/chat/action.go | 19 +++++++++++++++++-- misc.go | 2 +- 8 files changed, 58 insertions(+), 22 deletions(-) diff --git a/base/aaa/sess.go b/base/aaa/sess.go index ad3c223c..9b264904 100644 --- a/base/aaa/sess.go +++ b/base/aaa/sess.go @@ -35,9 +35,9 @@ func _sess_check(m *ice.Message, sessid string) { } } m.Log_AUTH( + USERROLE, m.Option(ice.MSG_USERROLE, kit.Select(UserRole(m, value[USERNAME]))), USERNICK, m.Option(ice.MSG_USERNICK, value[USERNICK]), USERNAME, m.Option(ice.MSG_USERNAME, value[USERNAME]), - USERROLE, m.Option(ice.MSG_USERROLE, kit.Select(UserRole(m, value[USERNAME]))), ) }) }) diff --git a/base/nfs/nfs.go b/base/nfs/nfs.go index 2f1a669d..5d9e06ed 100644 --- a/base/nfs/nfs.go +++ b/base/nfs/nfs.go @@ -332,13 +332,13 @@ var Index = &ice.Context{Name: "nfs", Help: "存储模块", }}, DIR: {Name: "dir path=auto field... 查看:button=auto 返回 上传", Help: "目录", Action: map[string]*ice.Action{ "upload": {Name: "upload", Help: "上传", Hand: func(m *ice.Message, arg ...string) { - if m.Cmdy("cache", "upload"); m.Option("pod") == "" { - m.Cmdy("cache", "watch", m.Option("data"), path.Join(m.Option("path"), m.Option("name"))) - return + if len(arg) > 0 { + m.Cmdy("spide", "dev", "cache", "GET", kit.MergeURL(arg[2], "path", arg[1], "name", arg[0])) + m.Option("name", arg[0]) + } else { + m.Cmdy("cache", "upload") } - m.Cmdy("space", m.Option("pod"), "spide", "dev", "save", path.Join(m.Option("path"), m.Option("name")), - kit.MergeURL2(m.Option(ice.MSG_USERWEB), path.Join("/share/local/", m.Option("data")))) - + m.Cmdy("cache", "watch", m.Option("data"), path.Join(m.Option("path"), m.Option("name"))) }}, mdb.SEARCH: {Name: "search type name text", Help: "搜索", Hand: func(m *ice.Message, arg ...string) { _file_search(m, arg[0], arg[1], arg[2], arg[3:]...) diff --git a/base/web/serve.go b/base/web/serve.go index ced2ddf2..0adf3d89 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -21,7 +21,7 @@ const LOGIN = "_login" func _serve_login(msg *ice.Message, cmds []string, w http.ResponseWriter, r *http.Request) ([]string, bool) { msg.Option(ice.MSG_USERNAME, "") - msg.Option(ice.MSG_USERROLE, "") + msg.Option(ice.MSG_USERROLE, "void") if msg.Options(ice.MSG_SESSID) { // 会话认证 @@ -82,6 +82,7 @@ func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.Respon } // 用户请求 + msg.Option(ice.MSG_METHOD, r.Method) msg.Option(ice.MSG_USERWEB, kit.Select(msg.Conf(SHARE, "meta.domain"), r.Header.Get("Referer"))) msg.Option(ice.MSG_USERIP, r.Header.Get(ice.MSG_USERIP)) msg.Option(ice.MSG_USERUA, r.Header.Get("User-Agent")) @@ -118,6 +119,9 @@ func _serve_handle(key string, cmd *ice.Command, msg *ice.Message, w http.Respon // 请求参数 for k, v := range r.Form { + for i, p := range v { + v[i], _ = url.QueryUnescape(p) + } if msg.Optionv(k, v); k == ice.MSG_SESSID { msg.Render(COOKIE, v[0]) } diff --git a/base/web/share.go b/base/web/share.go index dcd83558..24e079f3 100644 --- a/base/web/share.go +++ b/base/web/share.go @@ -9,6 +9,7 @@ import ( kit "github.com/shylinux/toolkits" "fmt" + "net/http" "os" "path" "strings" @@ -69,32 +70,45 @@ func _share_repos(m *ice.Message, repos string, arg ...string) { } func _share_local(m *ice.Message, arg ...string) { p := path.Join(arg...) + switch ls := strings.Split(p, "/"); ls[0] { + case "etc", "var": + // 私有文件 + m.Render(STATUS, http.StatusUnauthorized, "not auth") + return + default: + if m.Warn(!m.Right(ls), ice.ErrNotAuth, m.Option(ice.MSG_USERROLE), " of ", p) { + m.Render(STATUS, http.StatusUnauthorized, "not auth") + return + } + } + if m.Option("pod") != "" { // 远程文件 - pp := path.Join("var/proxy", m.Option("pod"), p) cache := time.Now().Add(-time.Hour * 240000) if s, e := os.Stat(pp); e == nil { cache = s.ModTime() } - m.Cmdy(SPACE, m.Option("pod"), SPIDE, "dev", kit.MergeURL2(m.Option(ice.MSG_USERWEB), "/share/proxy/"), + m.Cmdy(SPACE, m.Option("pod"), SPIDE, "dev", "raw", kit.MergeURL2(m.Option(ice.MSG_USERWEB), "/share/proxy/"), "part", "pod", m.Option("pod"), "path", p, "cache", cache.Format(ice.MOD_TIME), "upload", "@"+p) m.Render(ice.RENDER_DOWNLOAD, path.Join("var/proxy", m.Option("pod"), p)) return } - switch ls := strings.Split(p, "/"); ls[0] { - case "etc", "var": - // 私有文件 - return - } // 本地文件 m.Render(ice.RENDER_DOWNLOAD, p) } func _share_proxy(m *ice.Message, arg ...string) { - m.Cmdy(CACHE, UPLOAD) - m.Cmdy(CACHE, WATCH, m.Option("data"), path.Join("var/proxy", m.Option("pod"), m.Option("path"))) + switch m.Option(ice.MSG_METHOD) { + case http.MethodGet: + m.Render(ice.RENDER_DOWNLOAD, path.Join("var/proxy", path.Join(m.Option("pod"), m.Option("path"), m.Option("name")))) + case http.MethodPost: + m.Cmdy(CACHE, UPLOAD) + m.Cmdy(CACHE, WATCH, m.Option("data"), path.Join("var/proxy", m.Option("pod"), m.Option("path"))) + m.Render(ice.RENDER_RESULT, m.Option("path")) + } + } func _share_remote(m *ice.Message, pod string, arg ...string) { m.Cmdy(SPACE, pod, "web./publish/", arg) diff --git a/base/web/spide.go b/base/web/spide.go index ea77e512..25d94aa1 100644 --- a/base/web/spide.go +++ b/base/web/spide.go @@ -321,8 +321,10 @@ func init() { m.Echo(m.Append(DATA)) case SPIDE_SAVE: if f, p, e := kit.Create(save); m.Assert(e) { - io.Copy(f, res.Body) - m.Echo(p) + if n, e := io.Copy(f, res.Body); m.Assert(e) { + m.Log_EXPORT(kit.MDB_SIZE, n, kit.MDB_FILE, p) + m.Echo(p) + } } case SPIDE_RAW: if b, e := ioutil.ReadAll(res.Body); m.Assert(e) { diff --git a/conf.go b/conf.go index 2cd454fe..539dffd6 100644 --- a/conf.go +++ b/conf.go @@ -42,6 +42,7 @@ const ( // MSG MSG_RIVER = "sess.river" MSG_STORM = "sess.storm" MSG_ACTIVE = "sess.active" + MSG_METHOD = "sess.method" ) const ( // CTX CTX_STREAM = "stream" diff --git a/core/chat/action.go b/core/chat/action.go index 6e8889ce..9706bc06 100644 --- a/core/chat/action.go +++ b/core/chat/action.go @@ -1,15 +1,15 @@ package chat import ( - "strings" - ice "github.com/shylinux/icebergs" "github.com/shylinux/icebergs/base/aaa" "github.com/shylinux/icebergs/base/ctx" "github.com/shylinux/icebergs/base/web" kit "github.com/shylinux/toolkits" + "path" "strconv" + "strings" ) func _action_share_create(m *ice.Message, name, text string, arg ...string) { @@ -145,6 +145,21 @@ func _action_show(m *ice.Message, river, storm, index string, arg ...string) { m.Render("status", 403, "not auth") return } + + m.Debug("what %v", cmds) + if p := m.Option(POD); p != "" { + if len(cmds) > 1 && cmds[1] == "action" { + switch cmds[2] { + case "upload": + msg := m.Cmd(web.CACHE, web.UPLOAD) + file := path.Join("var/proxy", p, msg.Option("path"), msg.Option("name")) + m.Cmd("cache", "watch", msg.Option("data"), file) + m.Cmdy(_action_proxy(m), cmds[0], "action", "upload", msg.Append("name"), msg.Option("path"), + kit.MergeURL2(m.Option(ice.MSG_USERWEB), "/share/proxy/", "pod", p)) + return + } + } + } m.Cmdy(_action_proxy(m), cmds) } func _action_proxy(m *ice.Message) (proxy []string) { diff --git a/misc.go b/misc.go index f6995b5f..666a310d 100644 --- a/misc.go +++ b/misc.go @@ -45,7 +45,7 @@ func (m *Message) Event(key string, arg ...string) *Message { return m } func (m *Message) Right(arg ...interface{}) bool { - return m.Option(MSG_USERROLE) == "root" || !m.Warn(m.Cmdx("aaa.role", "right", m.Option(MSG_USERROLE), kit.Keys(arg...)) != "ok", ErrNotAuth, strings.Join(kit.Simple(arg), ".")) + return m.Option(MSG_USERROLE) == "root" || !m.Warn(m.Cmdx("aaa.role", "right", m.Option(MSG_USERROLE), kit.Keys(arg...)) != "ok", ErrNotAuth, m.Option(MSG_USERROLE), " of ", strings.Join(kit.Simple(arg), ".")) } func (m *Message) Space(arg interface{}) []string { if arg == nil || arg == "" || kit.Format(arg) == m.Conf("cli.runtime", "node.name") {