From 25dcf536c6f52fb6dc8df8486b8776720ea709df Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Wed, 31 Jan 2024 23:18:42 +0800 Subject: [PATCH] added url path rewrite for forwarder node --- config/config.go | 12 +++++++++--- config/parsing/node/parse.go | 9 +++++++++ go.mod | 2 +- go.sum | 4 ++-- handler/forward/local/handler.go | 9 +++++++++ handler/forward/remote/handler.go | 9 +++++++++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index bb05bda..389bc95 100644 --- a/config/config.go +++ b/config/config.go @@ -364,10 +364,16 @@ type ForwardNodeConfig struct { Metadata map[string]any `yaml:",omitempty" json:"metadata,omitempty"` } +type HTTPURLRewriteConfig struct { + Match string + Replacement string +} + type HTTPNodeConfig struct { - Host string `yaml:",omitempty" json:"host,omitempty"` - Header map[string]string `yaml:",omitempty" json:"header,omitempty"` - Auth *AuthConfig `yaml:",omitempty" json:"auth,omitempty"` + Host string `yaml:",omitempty" json:"host,omitempty"` + Header map[string]string `yaml:",omitempty" json:"header,omitempty"` + Auth *AuthConfig `yaml:",omitempty" json:"auth,omitempty"` + Rewrite []HTTPURLRewriteConfig `yaml:",omitempty" json:"rewrite,omitempty"` } type TLSNodeConfig struct { diff --git a/config/parsing/node/parse.go b/config/parsing/node/parse.go index 7f51e99..b1eb9c2 100644 --- a/config/parsing/node/parse.go +++ b/config/parsing/node/parse.go @@ -3,6 +3,7 @@ package node import ( "fmt" "net" + "regexp" "strings" "time" @@ -189,6 +190,14 @@ func ParseNode(hop string, cfg *config.NodeConfig, log logger.Logger) (*chain.No })), ) } + for _, v := range cfg.HTTP.Rewrite { + if pattern, _ := regexp.Compile(v.Match); pattern != nil { + settings.Rewrite = append(settings.Rewrite, chain.HTTPURLRewriteSetting{ + Pattern: pattern, + Replacement: v.Replacement, + }) + } + } opts = append(opts, chain.HTTPNodeOption(settings)) } if cfg.TLS != nil { diff --git a/go.mod b/go.mod index f889440..d4e06ab 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/gin-contrib/cors v1.5.0 github.com/gin-gonic/gin v1.9.1 - github.com/go-gost/core v0.0.0-20240127130604-04314fa08476 + github.com/go-gost/core v0.0.0-20240131151724-a06608ccafbf github.com/go-gost/gosocks4 v0.0.1 github.com/go-gost/gosocks5 v0.4.0 github.com/go-gost/plugin v0.0.0-20240103125338-9c84e29cb81a diff --git a/go.sum b/go.sum index 25aa306..d08bdff 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= -github.com/go-gost/core v0.0.0-20240127130604-04314fa08476 h1:4TA4ErfFw2CsVv5K5oqqYpUn68aZFrV+ONb4aGPJ1QQ= -github.com/go-gost/core v0.0.0-20240127130604-04314fa08476/go.mod h1:ndkgWVYRLwupVaFFWv8ML1Nr8tD3xhHK245PLpUDg4E= +github.com/go-gost/core v0.0.0-20240131151724-a06608ccafbf h1:akQ96Ibm+P7IftDluZPoMCzBzbLR/TjFu8Wpjy3H7hM= +github.com/go-gost/core v0.0.0-20240131151724-a06608ccafbf/go.mod h1:ndkgWVYRLwupVaFFWv8ML1Nr8tD3xhHK245PLpUDg4E= github.com/go-gost/gosocks4 v0.0.1 h1:+k1sec8HlELuQV7rWftIkmy8UijzUt2I6t+iMPlGB2s= github.com/go-gost/gosocks4 v0.0.1/go.mod h1:3B6L47HbU/qugDg4JnoFPHgJXE43Inz8Bah1QaN9qCc= github.com/go-gost/gosocks5 v0.4.0 h1:EIrOEkpJez4gwHrMa33frA+hHXJyevjp47thpMQsJzI= diff --git a/handler/forward/local/handler.go b/handler/forward/local/handler.go index 5f7c960..0d8f9cb 100644 --- a/handler/forward/local/handler.go +++ b/handler/forward/local/handler.go @@ -266,6 +266,15 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, remot for k, v := range httpSettings.Header { req.Header.Set(k, v) } + + for _, re := range httpSettings.Rewrite { + if re.Pattern.MatchString(req.URL.Path) { + if s := re.Pattern.ReplaceAllString(req.URL.Path, re.Replacement); s != "" { + req.URL.Path = s + break + } + } + } } cc, err = h.router.Dial(ctx, "tcp", target.Addr) diff --git a/handler/forward/remote/handler.go b/handler/forward/remote/handler.go index dbd945d..c00da9f 100644 --- a/handler/forward/remote/handler.go +++ b/handler/forward/remote/handler.go @@ -266,6 +266,15 @@ func (h *forwardHandler) handleHTTP(ctx context.Context, rw io.ReadWriter, remot for k, v := range httpSettings.Header { req.Header.Set(k, v) } + + for _, re := range httpSettings.Rewrite { + if re.Pattern.MatchString(req.URL.Path) { + if s := re.Pattern.ReplaceAllString(req.URL.Path, re.Replacement); s != "" { + req.URL.Path = s + break + } + } + } } cc, err = h.router.Dial(ctx, "tcp", target.Addr)