added load balance support and verbose level

This commit is contained in:
yusing
2024-03-06 12:34:06 +08:00
parent a5c53a4f4f
commit 2f439233ed
25 changed files with 530 additions and 240 deletions

50
src/go-proxy/utils.go Normal file → Executable file
View File

@@ -1,11 +1,16 @@
package main
import (
"bytes"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"time"
xhtml "golang.org/x/net/html"
)
type Utils struct {
@@ -82,3 +87,48 @@ func (*Utils) healthCheckStream(scheme string, host string) error {
conn.Close()
return nil
}
func (*Utils) snakeToCamel(s string) string {
toHyphenCamel := http.CanonicalHeaderKey(strings.ReplaceAll(s, "_", "-"))
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 (*Utils) respRemovePath(r *http.Response, path string) error {
// remove all path prefix from relative path in script, img, a, ...
doc, err := xhtml.Parse(r.Body)
if err != nil {
return err
}
htmlNodesSubPath(doc, path)
var buf bytes.Buffer
err = xhtml.Render(&buf, doc)
if err != nil {
return err
}
r.Body = io.NopCloser(strings.NewReader(buf.String()))
return nil
}