mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-31 06:03:06 +02:00
refactor: move some io, http and string utils to separate repo
This commit is contained in:
@@ -12,12 +12,11 @@ import (
|
||||
|
||||
"github.com/yusing/godoxy/internal/entrypoint"
|
||||
. "github.com/yusing/godoxy/internal/net/gphttp/middleware"
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/reverseproxy"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
"github.com/yusing/godoxy/internal/route"
|
||||
routeTypes "github.com/yusing/godoxy/internal/route/types"
|
||||
"github.com/yusing/godoxy/internal/task"
|
||||
expect "github.com/yusing/godoxy/internal/utils/testing"
|
||||
"github.com/yusing/goutils/http/reverseproxy"
|
||||
)
|
||||
|
||||
func noOpHandler(w http.ResponseWriter, r *http.Request) {}
|
||||
@@ -102,8 +101,10 @@ func (f fakeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
}
|
||||
|
||||
func TestReverseProxyBypass(t *testing.T) {
|
||||
rp := reverseproxy.NewReverseProxy("test", nettypes.MustParseURL("http://example.com"), fakeRoundTripper{})
|
||||
err := PatchReverseProxy(rp, map[string]OptionsRaw{
|
||||
url, err := url.Parse("http://example.com")
|
||||
expect.NoError(t, err)
|
||||
rp := reverseproxy.NewReverseProxy("test", url, fakeRoundTripper{})
|
||||
err = PatchReverseProxy(rp, map[string]OptionsRaw{
|
||||
"response": {
|
||||
"bypass": "path /test/* | path /api",
|
||||
"set_headers": map[string]string{
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
"github.com/yusing/godoxy/internal/utils/atomic"
|
||||
"github.com/yusing/godoxy/internal/utils/strutils"
|
||||
strutils "github.com/yusing/goutils/strings"
|
||||
)
|
||||
|
||||
type cloudflareRealIP struct {
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
gphttp "github.com/yusing/godoxy/internal/net/gphttp"
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/httpheaders"
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/middleware/errorpage"
|
||||
"github.com/yusing/goutils/http/httpheaders"
|
||||
)
|
||||
|
||||
type customErrorPage struct{}
|
||||
|
||||
@@ -7,9 +7,9 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/httpheaders"
|
||||
"github.com/yusing/godoxy/internal/route/routes"
|
||||
"github.com/yusing/godoxy/internal/utils"
|
||||
httputils "github.com/yusing/goutils/http"
|
||||
"github.com/yusing/goutils/http/httpheaders"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -91,7 +91,7 @@ func (m *forwardAuthMiddleware) before(w http.ResponseWriter, r *http.Request) (
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
body, release, err := utils.ReadAllBody(resp)
|
||||
body, release, err := httputils.ReadAllBody(resp)
|
||||
defer release()
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -12,8 +12,8 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
gphttp "github.com/yusing/godoxy/internal/net/gphttp"
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/reverseproxy"
|
||||
"github.com/yusing/godoxy/internal/serialization"
|
||||
"github.com/yusing/goutils/http/reverseproxy"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
"github.com/yusing/godoxy/internal/utils"
|
||||
"github.com/yusing/godoxy/internal/utils/strutils"
|
||||
strutils "github.com/yusing/goutils/strings"
|
||||
)
|
||||
|
||||
// snakes and cases will be stripped on `Get`
|
||||
|
||||
@@ -9,8 +9,9 @@ import (
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/rs/zerolog/log"
|
||||
gphttp "github.com/yusing/godoxy/internal/net/gphttp"
|
||||
"github.com/yusing/godoxy/internal/utils"
|
||||
"github.com/yusing/godoxy/internal/utils/synk"
|
||||
httputils "github.com/yusing/goutils/http"
|
||||
ioutils "github.com/yusing/goutils/io"
|
||||
"github.com/yusing/goutils/synk"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
@@ -40,7 +41,7 @@ func (m *modifyHTML) modifyResponse(resp *http.Response) error {
|
||||
}
|
||||
|
||||
// NOTE: do not put it in the defer, it will be used as resp.Body
|
||||
content, release, err := utils.ReadAllBody(resp)
|
||||
content, release, err := httputils.ReadAllBody(resp)
|
||||
if err != nil {
|
||||
resp.Body.Close()
|
||||
return err
|
||||
@@ -71,19 +72,19 @@ func (m *modifyHTML) modifyResponse(resp *http.Response) error {
|
||||
}
|
||||
|
||||
buf := bytes.NewBuffer(content[:0])
|
||||
err = buildHTML(m, doc, buf)
|
||||
err = buildHTML(doc, buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.ContentLength = int64(buf.Len())
|
||||
resp.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
|
||||
resp.Header.Set("Content-Type", "text/html; charset=utf-8")
|
||||
resp.Body = utils.NewHookCloser(io.NopCloser(bytes.NewReader(buf.Bytes())), release)
|
||||
resp.Body = ioutils.NewHookReadCloser(io.NopCloser(bytes.NewReader(buf.Bytes())), release)
|
||||
return nil
|
||||
}
|
||||
|
||||
// copied and modified from (*goquery.Selection).Html()
|
||||
func buildHTML(m *modifyHTML, s *goquery.Document, buf *bytes.Buffer) error {
|
||||
func buildHTML(s *goquery.Document, buf *bytes.Buffer) error {
|
||||
// Merge all head nodes into one
|
||||
headNodes := s.Find("head")
|
||||
if headNodes.Length() > 1 {
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/httpheaders"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
"github.com/yusing/goutils/http/httpheaders"
|
||||
)
|
||||
|
||||
// https://nginx.org/en/docs/http/ngx_http_realip_module.html
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/httpheaders"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
. "github.com/yusing/godoxy/internal/utils/testing"
|
||||
"github.com/yusing/goutils/http/httpheaders"
|
||||
)
|
||||
|
||||
func TestSetRealIPOpts(t *testing.T) {
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
"github.com/yusing/godoxy/internal/gperr"
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/reverseproxy"
|
||||
nettypes "github.com/yusing/godoxy/internal/net/types"
|
||||
. "github.com/yusing/godoxy/internal/utils/testing"
|
||||
"github.com/yusing/goutils/http/reverseproxy"
|
||||
)
|
||||
|
||||
//go:embed test_data/sample_headers.json
|
||||
@@ -152,7 +152,7 @@ func newMiddlewaresTest(middlewares []*Middleware, args *testArgs) (*TestResult,
|
||||
rr.parent = http.DefaultTransport
|
||||
}
|
||||
|
||||
rp := reverseproxy.NewReverseProxy("test", args.upstreamURL, rr)
|
||||
rp := reverseproxy.NewReverseProxy("test", &args.upstreamURL.URL, rr)
|
||||
patchReverseProxy(rp, middlewares)
|
||||
rp.ServeHTTP(w, req)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/yusing/godoxy/internal/net/gphttp/httpheaders"
|
||||
"github.com/yusing/goutils/http/httpheaders"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
Reference in New Issue
Block a user