mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-21 16:49:03 +01:00
fixing sub path_mode
This commit is contained in:
@@ -6,10 +6,14 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
xhtml "golang.org/x/net/html"
|
||||
)
|
||||
|
||||
@@ -79,7 +83,7 @@ func (*Utils) healthCheckHttp(targetUrl string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (*Utils) healthCheckStream(scheme string, host string) error {
|
||||
func (*Utils) healthCheckStream(scheme, host string) error {
|
||||
conn, err := net.DialTimeout(scheme, host, 5*time.Second)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -93,25 +97,49 @@ func (*Utils) snakeToCamel(s string) string {
|
||||
return strings.ReplaceAll(toHyphenCamel, "-", "")
|
||||
}
|
||||
|
||||
func htmlNodesSubPath(node *xhtml.Node, path string) {
|
||||
if node.Type == xhtml.ElementNode {
|
||||
for _, attr := range node.Attr {
|
||||
switch attr.Key {
|
||||
case "src": // img, script, etc.
|
||||
case "href": // link
|
||||
case "action": // form
|
||||
if strings.HasPrefix(attr.Val, path) {
|
||||
attr.Val = strings.Replace(attr.Val, path, "", 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := node.FirstChild; c != nil; c = c.NextSibling {
|
||||
htmlNodesSubPath(c, path)
|
||||
func tryAppendPathPrefixImpl(pOrig, pAppend string) string {
|
||||
switch {
|
||||
case strings.Contains(pOrig, "://"):
|
||||
return pOrig
|
||||
case pOrig == "", pOrig == "#", pOrig == "/":
|
||||
return pAppend
|
||||
case filepath.IsLocal(pOrig) && !strings.HasPrefix(pOrig, pAppend):
|
||||
return path.Join(pAppend, pOrig)
|
||||
default:
|
||||
return pOrig
|
||||
}
|
||||
}
|
||||
|
||||
func (*Utils) respRemovePath(r *http.Response, path string) error {
|
||||
var tryAppendPathPrefix func(string, string) string
|
||||
var _ = func() int {
|
||||
if glog.V(4) {
|
||||
tryAppendPathPrefix = func(s1, s2 string) string {
|
||||
replaced := tryAppendPathPrefixImpl(s1, s2)
|
||||
glog.Infof("[Path sub] %s -> %s", s1, replaced)
|
||||
return replaced
|
||||
}
|
||||
} else {
|
||||
tryAppendPathPrefix = tryAppendPathPrefixImpl
|
||||
}
|
||||
return 1
|
||||
}()
|
||||
|
||||
func htmlNodesSubPath(n *xhtml.Node, p string) {
|
||||
if n.Type == xhtml.ElementNode {
|
||||
for i, attr := range n.Attr {
|
||||
switch attr.Key {
|
||||
case "src", "href", "action": // img, script, link, form etc.
|
||||
n.Attr[i].Val = tryAppendPathPrefix(attr.Val, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
htmlNodesSubPath(c, p)
|
||||
}
|
||||
}
|
||||
|
||||
func (*Utils) respHTMLSubPath(r *http.Response, p string) error {
|
||||
// remove all path prefix from relative path in script, img, a, ...
|
||||
doc, err := xhtml.Parse(r.Body)
|
||||
|
||||
@@ -119,11 +147,14 @@ func (*Utils) respRemovePath(r *http.Response, path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
htmlNodesSubPath(doc, path)
|
||||
if p[0] == '/' {
|
||||
p = p[1:]
|
||||
}
|
||||
htmlNodesSubPath(doc, p)
|
||||
|
||||
var buf bytes.Buffer
|
||||
err = xhtml.Render(&buf, doc)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -132,3 +163,29 @@ func (*Utils) respRemovePath(r *http.Response, path string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*Utils) respJSSubPath(r *http.Response, p string) error {
|
||||
var buf bytes.Buffer
|
||||
|
||||
_, err := buf.ReadFrom(r.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p[0] == '/' {
|
||||
p = p[1:]
|
||||
}
|
||||
|
||||
js := buf.String()
|
||||
|
||||
re := regexp.MustCompile(`fetch\(["'].+["']\)`)
|
||||
replace := func(match string) string {
|
||||
match = match[7 : len(match)-2]
|
||||
replaced := tryAppendPathPrefix(match, p)
|
||||
return fmt.Sprintf(`fetch(%q)`, replaced)
|
||||
}
|
||||
js = re.ReplaceAllStringFunc(js, replace)
|
||||
|
||||
r.Body = io.NopCloser(strings.NewReader(js))
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user