forked from x/ContextOS
add web.proxy
This commit is contained in:
parent
4b2d693011
commit
dbf93004d5
@ -89,6 +89,7 @@ func (cli *CLI) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server
|
||||
"path": []string{"nfs.path"},
|
||||
"dir": []string{"nfs.dir"},
|
||||
"git": []string{"nfs.git"},
|
||||
"brow": []string{"web.brow"},
|
||||
}
|
||||
|
||||
return s
|
||||
@ -623,7 +624,12 @@ var Index = &ctx.Context{Name: "cli", Help: "管理中心",
|
||||
if m.Options("current_ctx") {
|
||||
args := []string{"context", m.Option("current_ctx")}
|
||||
arg = append(args, arg...)
|
||||
m.Option("current_ctx", "")
|
||||
} else {
|
||||
args := []string{"context", m.Source().Name}
|
||||
arg = append(args, arg...)
|
||||
}
|
||||
|
||||
m.Sess("yac").Call(func(msg *ctx.Message) *ctx.Message {
|
||||
switch msg.Cmd().Detail(0) {
|
||||
case "cmd":
|
||||
|
@ -775,6 +775,9 @@ func (m *Message) Search(key string, root ...bool) []*Message {
|
||||
m.Assert(e)
|
||||
|
||||
target := m.target
|
||||
if target == nil {
|
||||
return []*Message{nil}
|
||||
}
|
||||
if len(root) > 0 && root[0] {
|
||||
target = m.target.root
|
||||
}
|
||||
|
@ -19,6 +19,9 @@ func (log *LOG) LOG(msg *ctx.Message, action string, str string) {
|
||||
if m.Confs("silent", action) {
|
||||
return
|
||||
}
|
||||
if msg.Target() == nil {
|
||||
return
|
||||
}
|
||||
if m.Confs("module", fmt.Sprintf("%s.%s", msg.Target().Name, action)) {
|
||||
return
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import (
|
||||
"contexts/ctx"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
// "github.com/PuerkitoBio/goquery"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@ -35,7 +35,16 @@ type WEB struct {
|
||||
*ctx.Context
|
||||
}
|
||||
|
||||
func (web *WEB) Merge(m *ctx.Message, uri string, arg ...string) string {
|
||||
func proxy(m *ctx.Message, url string) string {
|
||||
if strings.HasPrefix(url, "//") {
|
||||
return "proxy/https:" + url
|
||||
}
|
||||
|
||||
return "proxy/" + url
|
||||
}
|
||||
func Merge(m *ctx.Message, uri string, arg ...string) string {
|
||||
uri = strings.Replace(uri, ":/", "://", -1)
|
||||
uri = strings.Replace(uri, ":///", "://", -1)
|
||||
add, e := url.Parse(uri)
|
||||
m.Assert(e)
|
||||
adds := []string{m.Confx("protocol", add.Scheme, "%s://"), m.Confx("hostname", add.Host)}
|
||||
@ -77,6 +86,7 @@ func (web *WEB) Merge(m *ctx.Message, uri string, arg ...string) string {
|
||||
|
||||
return strings.Join(adds, "")
|
||||
}
|
||||
|
||||
func (web *WEB) HandleCmd(m *ctx.Message, key string, cmd *ctx.Command) {
|
||||
web.HandleFunc(key, func(w http.ResponseWriter, r *http.Request) {
|
||||
m.TryCatch(m.Spawn(), true, func(msg *ctx.Message) {
|
||||
@ -148,28 +158,26 @@ func (web *WEB) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
if index {
|
||||
m.Log("info", "").Log("info", "%v %s %s", r.RemoteAddr, r.Method, r.URL)
|
||||
wd, _ := os.Getwd()
|
||||
m.Log("info", "wd: %s", wd)
|
||||
}
|
||||
// if index && m.Confs("logheaders") {
|
||||
// for k, v := range r.Header {
|
||||
// m.Log("info", "%s: %v", k, v)
|
||||
// }
|
||||
// m.Log("info", "")
|
||||
// }
|
||||
//
|
||||
// if r.URL.Path == "/" && m.Confs("root_index") {
|
||||
// r.URL.Path = m.Conf("root_index")
|
||||
// }
|
||||
//
|
||||
if index && m.Confs("logheaders") {
|
||||
for k, v := range r.Header {
|
||||
m.Log("info", "%s: %v", k, v)
|
||||
}
|
||||
m.Log("info", "")
|
||||
}
|
||||
|
||||
if r.URL.Path == "/" && m.Confs("root_index") {
|
||||
r.URL.Path = m.Conf("root_index")
|
||||
}
|
||||
|
||||
web.ServeMux.ServeHTTP(w, r)
|
||||
|
||||
// if index && m.Confs("logheaders") {
|
||||
// for k, v := range w.Header() {
|
||||
// m.Log("info", "%s: %v", k, v)
|
||||
// }
|
||||
// m.Log("info", "")
|
||||
// }
|
||||
if index && m.Confs("logheaders") {
|
||||
for k, v := range w.Header() {
|
||||
m.Log("info", "%s: %v", k, v)
|
||||
}
|
||||
m.Log("info", "")
|
||||
}
|
||||
}
|
||||
|
||||
func (web *WEB) Spawn(m *ctx.Message, c *ctx.Context, arg ...string) ctx.Server {
|
||||
@ -200,6 +208,7 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool {
|
||||
m.Cap("directory", m.Confx("directory", arg, 0))
|
||||
|
||||
render := m.Target().Commands["/render"]
|
||||
proxy := m.Target().Commands["/proxy/"]
|
||||
|
||||
m.Travel(func(m *ctx.Message, i int) bool {
|
||||
if h, ok := m.Target().Server.(MUX); ok && m.Cap("register") == "no" {
|
||||
@ -214,6 +223,9 @@ func (web *WEB) Start(m *ctx.Message, arg ...string) bool {
|
||||
if m.Target().Commands["/render"] == nil {
|
||||
m.Target().Commands["/render"] = render
|
||||
}
|
||||
if m.Target().Commands["/proxy/"] == nil {
|
||||
m.Target().Commands["/proxy/"] = proxy
|
||||
}
|
||||
|
||||
msg := m.Target().Message()
|
||||
for k, x := range m.Target().Commands {
|
||||
@ -361,7 +373,7 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
}
|
||||
|
||||
method := m.Confx("method")
|
||||
uri := web.Merge(m, arg[0], arg[1:]...)
|
||||
uri := Merge(m, arg[0], arg[1:]...)
|
||||
body, _ := m.Optionv("body").(io.Reader)
|
||||
|
||||
if method == "POST" && body == nil {
|
||||
@ -402,20 +414,21 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
return
|
||||
}
|
||||
m.Assert(e)
|
||||
for k, v := range res.Header {
|
||||
m.Log("info", "%s: %v", k, v)
|
||||
}
|
||||
|
||||
for _, v := range res.Cookies() {
|
||||
m.Confv("cookie", v.Name, v)
|
||||
m.Log("info", "set-cookie %s: %v", v.Name, v.Value)
|
||||
}
|
||||
|
||||
buf, e := ioutil.ReadAll(res.Body)
|
||||
m.Assert(e)
|
||||
|
||||
var result interface{}
|
||||
ct := res.Header.Get("Content-Type")
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(ct, "application/json"):
|
||||
json.Unmarshal(buf, &result)
|
||||
json.NewDecoder(res.Body).Decode(&result)
|
||||
if m.Has("parse") {
|
||||
msg := m.Spawn()
|
||||
msg.Put("option", "response", result)
|
||||
@ -424,12 +437,56 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
m.Copy(msg, "result")
|
||||
return
|
||||
}
|
||||
case strings.HasPrefix(ct, "text/html"):
|
||||
html, e := goquery.NewDocumentFromReader(res.Body)
|
||||
m.Assert(e)
|
||||
query := html.Find("html")
|
||||
if m.Has("parse") {
|
||||
query = query.Find(m.Option("parse"))
|
||||
}
|
||||
|
||||
query.Each(func(n int, s *goquery.Selection) {
|
||||
s.Find("a").Each(func(n int, s *goquery.Selection) {
|
||||
if attr, ok := s.Attr("href"); ok {
|
||||
s.SetAttr("href", proxy(m, attr))
|
||||
}
|
||||
})
|
||||
s.Find("img").Each(func(n int, s *goquery.Selection) {
|
||||
if attr, ok := s.Attr("src"); ok {
|
||||
s.SetAttr("src", proxy(m, attr))
|
||||
}
|
||||
if attr, ok := s.Attr("r-lazyload"); ok {
|
||||
s.SetAttr("src", proxy(m, attr))
|
||||
}
|
||||
})
|
||||
s.Find("script").Each(func(n int, s *goquery.Selection) {
|
||||
if attr, ok := s.Attr("src"); ok {
|
||||
s.SetAttr("src", proxy(m, attr))
|
||||
}
|
||||
})
|
||||
if html, e := s.Html(); e == nil {
|
||||
m.Add("append", "html", html)
|
||||
}
|
||||
})
|
||||
default:
|
||||
result = string(buf)
|
||||
if w, ok := m.Optionv("response").(http.ResponseWriter); ok {
|
||||
header := w.Header()
|
||||
for k, v := range res.Header {
|
||||
header.Add(k, v[0])
|
||||
}
|
||||
io.Copy(w, res.Body)
|
||||
return
|
||||
} else {
|
||||
buf, e := ioutil.ReadAll(res.Body)
|
||||
m.Assert(e)
|
||||
|
||||
m.Append("Content-Type", ct)
|
||||
result = string(buf)
|
||||
}
|
||||
|
||||
}
|
||||
m.Target().Configs[m.Confx("body_response")] = &ctx.Config{Value: result}
|
||||
m.Echo(string(buf))
|
||||
m.Echo("%v", result)
|
||||
}
|
||||
}},
|
||||
"post": &ctx.Command{Name: "post [file fieldname filename]", Help: "post请求",
|
||||
@ -506,6 +563,39 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
m.Copy(msg, "result").Copy(msg, "append")
|
||||
}},
|
||||
"brow": &ctx.Command{Name: "brow url", Help: "浏览网页", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
if _, ok := m.Optionv("request").(*http.Request); ok {
|
||||
action := false
|
||||
m.Travel(func(m *ctx.Message, i int) bool {
|
||||
for key, v := range m.Target().Commands {
|
||||
method, url := "", ""
|
||||
if strings.HasPrefix(v.Name, "get ") {
|
||||
method, url = "get", strings.TrimPrefix(v.Name, "get ")
|
||||
} else if strings.HasPrefix(v.Name, "post ") {
|
||||
method, url = "post", strings.TrimPrefix(v.Name, "post ")
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(arg) == 0 {
|
||||
m.Add("append", "method", method)
|
||||
m.Add("append", "request", url)
|
||||
} else if strings.HasPrefix(url, arg[0]) {
|
||||
msg := m.Spawn().Cmd(key, arg[1:])
|
||||
m.Copy(msg, "append").Copy(msg, "result")
|
||||
action = true
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
if !action {
|
||||
msg := m.Spawn().Cmd("get", arg)
|
||||
m.Copy(msg, "append").Copy(msg, "result")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
url := m.Confx("brow_home", arg, 0)
|
||||
switch runtime.GOOS {
|
||||
case "windows":
|
||||
@ -843,6 +933,11 @@ var Index = &ctx.Context{Name: "web", Help: "应用中心",
|
||||
}
|
||||
}
|
||||
}},
|
||||
"/proxy/": &ctx.Command{Name: "/proxy/", Help: "服务代理", Hand: func(m *ctx.Message, c *ctx.Context, key string, arg ...string) {
|
||||
m.Log("fuck", "what %v", key)
|
||||
msg := m.Spawn().Cmd("get", strings.TrimPrefix(key, "/proxy/"), arg)
|
||||
m.Copy(msg, "append").Copy(msg, "result")
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -97,7 +97,11 @@ function send_command(form, cb) {
|
||||
var append = document.querySelector("table.append."+name)
|
||||
|
||||
if (result && msg) {
|
||||
result.innerHTML = (msg.result || []).join("")
|
||||
if (msg["Content-Type"] && msg["Content-Type"].join("") == "text/html") {
|
||||
append_child(result, "iframe").innerHTML = (msg.result || []).join("")
|
||||
} else {
|
||||
result.innerHTML = (msg.result || []).join("")
|
||||
}
|
||||
}
|
||||
if (append && msg) {
|
||||
append.innerHTML = ""
|
||||
|
Loading…
x
Reference in New Issue
Block a user