From 3d2a7b7d3be5cbf3cd5cdc52fe8705ec98a28706 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Tue, 30 Jan 2024 18:19:39 +0800 Subject: [PATCH 1/2] move x/internal/ctx to x/ctx --- auth/plugin/grpc.go | 2 +- auth/plugin/http.go | 2 +- bypass/plugin/grpc.go | 2 +- bypass/plugin/http.go | 2 +- connector/tunnel/connector.go | 2 +- {internal/ctx => ctx}/value.go | 0 go.mod | 2 +- go.sum | 4 ++-- handler/auto/handler.go | 2 +- handler/forward/local/handler.go | 2 +- handler/forward/remote/handler.go | 2 +- handler/http/handler.go | 2 +- handler/http2/handler.go | 2 +- handler/http3/handler.go | 2 +- handler/relay/connect.go | 2 +- handler/relay/forward.go | 2 +- handler/relay/handler.go | 6 +++--- handler/sni/handler.go | 2 +- handler/socks/v4/handler.go | 2 +- handler/socks/v5/connect.go | 2 +- handler/socks/v5/handler.go | 2 +- handler/socks/v5/selector.go | 2 +- handler/socks/v5/udp.go | 2 +- handler/socks/v5/udp_tun.go | 2 +- handler/ss/handler.go | 2 +- handler/tunnel/connect.go | 2 +- handler/tunnel/handler.go | 2 +- hop/plugin/grpc.go | 2 +- hop/plugin/http.go | 2 +- hosts/plugin/grpc.go | 2 +- hosts/plugin/http.go | 2 +- resolver/plugin/grpc.go | 2 +- resolver/plugin/http.go | 2 +- selector/strategy.go | 2 +- service/service.go | 2 +- 35 files changed, 37 insertions(+), 37 deletions(-) rename {internal/ctx => ctx}/value.go (100%) diff --git a/auth/plugin/grpc.go b/auth/plugin/grpc.go index 625327a..144f141 100644 --- a/auth/plugin/grpc.go +++ b/auth/plugin/grpc.go @@ -7,7 +7,7 @@ import ( "github.com/go-gost/core/auth" "github.com/go-gost/core/logger" "github.com/go-gost/plugin/auth/proto" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" "google.golang.org/grpc" ) diff --git a/auth/plugin/http.go b/auth/plugin/http.go index 4b5bc5d..e867ad9 100644 --- a/auth/plugin/http.go +++ b/auth/plugin/http.go @@ -8,7 +8,7 @@ import ( "github.com/go-gost/core/auth" "github.com/go-gost/core/logger" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" ) diff --git a/bypass/plugin/grpc.go b/bypass/plugin/grpc.go index 1d8e8b3..37ad21f 100644 --- a/bypass/plugin/grpc.go +++ b/bypass/plugin/grpc.go @@ -7,7 +7,7 @@ import ( "github.com/go-gost/core/bypass" "github.com/go-gost/core/logger" "github.com/go-gost/plugin/bypass/proto" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" "google.golang.org/grpc" ) diff --git a/bypass/plugin/http.go b/bypass/plugin/http.go index 9add03c..2035251 100644 --- a/bypass/plugin/http.go +++ b/bypass/plugin/http.go @@ -8,7 +8,7 @@ import ( "github.com/go-gost/core/bypass" "github.com/go-gost/core/logger" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" ) diff --git a/connector/tunnel/connector.go b/connector/tunnel/connector.go index 2b4eb95..ac0624e 100644 --- a/connector/tunnel/connector.go +++ b/connector/tunnel/connector.go @@ -9,7 +9,7 @@ import ( "github.com/go-gost/core/connector" md "github.com/go-gost/core/metadata" "github.com/go-gost/relay" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/registry" ) diff --git a/internal/ctx/value.go b/ctx/value.go similarity index 100% rename from internal/ctx/value.go rename to ctx/value.go diff --git a/go.mod b/go.mod index 01089cb..f889440 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( 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 - github.com/go-gost/relay v0.4.1-0.20240128081525-e36d5f4a8322 + github.com/go-gost/relay v0.5.0 github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451 github.com/go-redis/redis/v8 v8.11.5 github.com/gobwas/glob v0.2.3 diff --git a/go.sum b/go.sum index 940a516..25aa306 100644 --- a/go.sum +++ b/go.sum @@ -57,8 +57,8 @@ github.com/go-gost/gosocks5 v0.4.0 h1:EIrOEkpJez4gwHrMa33frA+hHXJyevjp47thpMQsJz github.com/go-gost/gosocks5 v0.4.0/go.mod h1:1G6I7HP7VFVxveGkoK8mnprnJqSqJjdcASKsdUn4Pp4= github.com/go-gost/plugin v0.0.0-20240103125338-9c84e29cb81a h1:ME7P1Brcg4C640DSPqlvQr7JuvvQfJ8QpmS3yCFlK3A= github.com/go-gost/plugin v0.0.0-20240103125338-9c84e29cb81a/go.mod h1:qXr2Zm9Ex2ATqnWuNUzVZqySPMnuIihvblYZt4MlZLw= -github.com/go-gost/relay v0.4.1-0.20240128081525-e36d5f4a8322 h1:R2a+Lx6XVvWdskGUUjteJ62WYBAskDHySgqNC6y8dI8= -github.com/go-gost/relay v0.4.1-0.20240128081525-e36d5f4a8322/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8= +github.com/go-gost/relay v0.5.0 h1:JG1tgy/KWiVXS0ukuVXvbM0kbYuJTWxYpJ5JwzsCf/c= +github.com/go-gost/relay v0.5.0/go.mod h1:lcX+23LCQ3khIeASBo+tJ/WbwXFO32/N5YN6ucuYTG8= github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451 h1:xj8gUZGYO3nb5+6Bjw9+tsFkA9sYynrOvDvvC4uDV2I= github.com/go-gost/tls-dissector v0.0.2-0.20220408131628-aac992c27451/go.mod h1:/9QfdewqmHdaE362Hv5nDaSWLx3pCmtD870d6GaquXs= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= diff --git a/handler/auto/handler.go b/handler/auto/handler.go index 3b251ce..857fc4f 100644 --- a/handler/auto/handler.go +++ b/handler/auto/handler.go @@ -11,7 +11,7 @@ import ( md "github.com/go-gost/core/metadata" "github.com/go-gost/gosocks4" "github.com/go-gost/gosocks5" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" netpkg "github.com/go-gost/x/internal/net" "github.com/go-gost/x/registry" ) diff --git a/handler/forward/local/handler.go b/handler/forward/local/handler.go index cc9e3f0..5f7c960 100644 --- a/handler/forward/local/handler.go +++ b/handler/forward/local/handler.go @@ -21,7 +21,7 @@ import ( "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" "github.com/go-gost/x/config" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xio "github.com/go-gost/x/internal/io" xnet "github.com/go-gost/x/internal/net" "github.com/go-gost/x/internal/util/forward" diff --git a/handler/forward/remote/handler.go b/handler/forward/remote/handler.go index 6766805..dbd945d 100644 --- a/handler/forward/remote/handler.go +++ b/handler/forward/remote/handler.go @@ -22,7 +22,7 @@ import ( mdata "github.com/go-gost/core/metadata" mdutil "github.com/go-gost/core/metadata/util" "github.com/go-gost/x/config" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xio "github.com/go-gost/x/internal/io" xnet "github.com/go-gost/x/internal/net" "github.com/go-gost/x/internal/net/proxyproto" diff --git a/handler/http/handler.go b/handler/http/handler.go index 89a190e..149e349 100644 --- a/handler/http/handler.go +++ b/handler/http/handler.go @@ -22,7 +22,7 @@ import ( traffic "github.com/go-gost/core/limiter/traffic" "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" netpkg "github.com/go-gost/x/internal/net" stats_util "github.com/go-gost/x/internal/util/stats" traffic_wrapper "github.com/go-gost/x/limiter/traffic/wrapper" diff --git a/handler/http2/handler.go b/handler/http2/handler.go index 0af0776..97ae18a 100644 --- a/handler/http2/handler.go +++ b/handler/http2/handler.go @@ -23,7 +23,7 @@ import ( "github.com/go-gost/core/limiter/traffic" "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xio "github.com/go-gost/x/internal/io" netpkg "github.com/go-gost/x/internal/net" stats_util "github.com/go-gost/x/internal/util/stats" diff --git a/handler/http3/handler.go b/handler/http3/handler.go index 767657c..abee3b9 100644 --- a/handler/http3/handler.go +++ b/handler/http3/handler.go @@ -14,7 +14,7 @@ import ( "github.com/go-gost/core/hop" "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/registry" ) diff --git a/handler/relay/connect.go b/handler/relay/connect.go index aa7cc4a..7b08bca 100644 --- a/handler/relay/connect.go +++ b/handler/relay/connect.go @@ -11,7 +11,7 @@ import ( "github.com/go-gost/core/limiter/traffic" "github.com/go-gost/core/logger" "github.com/go-gost/relay" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xnet "github.com/go-gost/x/internal/net" serial "github.com/go-gost/x/internal/util/serial" "github.com/go-gost/x/limiter/traffic/wrapper" diff --git a/handler/relay/forward.go b/handler/relay/forward.go index 8a86cf8..6d309c7 100644 --- a/handler/relay/forward.go +++ b/handler/relay/forward.go @@ -10,7 +10,7 @@ import ( "github.com/go-gost/core/limiter/traffic" "github.com/go-gost/core/logger" "github.com/go-gost/relay" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" netpkg "github.com/go-gost/x/internal/net" "github.com/go-gost/x/limiter/traffic/wrapper" "github.com/go-gost/x/stats" diff --git a/handler/relay/handler.go b/handler/relay/handler.go index 17bebdc..850cae8 100644 --- a/handler/relay/handler.go +++ b/handler/relay/handler.go @@ -12,9 +12,9 @@ import ( "github.com/go-gost/core/hop" md "github.com/go-gost/core/metadata" "github.com/go-gost/relay" - ctxvalue "github.com/go-gost/x/internal/ctx" - "github.com/go-gost/x/registry" + ctxvalue "github.com/go-gost/x/ctx" stats_util "github.com/go-gost/x/internal/util/stats" + "github.com/go-gost/x/registry" ) var ( @@ -215,4 +215,4 @@ func (h *relayHandler) observeStats(ctx context.Context) { return } } -} \ No newline at end of file +} diff --git a/handler/sni/handler.go b/handler/sni/handler.go index f99161f..4cbb712 100644 --- a/handler/sni/handler.go +++ b/handler/sni/handler.go @@ -21,7 +21,7 @@ import ( "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" dissector "github.com/go-gost/tls-dissector" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xio "github.com/go-gost/x/internal/io" netpkg "github.com/go-gost/x/internal/net" "github.com/go-gost/x/registry" diff --git a/handler/socks/v4/handler.go b/handler/socks/v4/handler.go index f56d516..c0f06ab 100644 --- a/handler/socks/v4/handler.go +++ b/handler/socks/v4/handler.go @@ -12,7 +12,7 @@ import ( "github.com/go-gost/core/logger" md "github.com/go-gost/core/metadata" "github.com/go-gost/gosocks4" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" netpkg "github.com/go-gost/x/internal/net" "github.com/go-gost/x/limiter/traffic/wrapper" "github.com/go-gost/x/registry" diff --git a/handler/socks/v5/connect.go b/handler/socks/v5/connect.go index faacbc0..4384b54 100644 --- a/handler/socks/v5/connect.go +++ b/handler/socks/v5/connect.go @@ -9,7 +9,7 @@ import ( "github.com/go-gost/core/limiter/traffic" "github.com/go-gost/core/logger" "github.com/go-gost/gosocks5" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" netpkg "github.com/go-gost/x/internal/net" "github.com/go-gost/x/limiter/traffic/wrapper" "github.com/go-gost/x/stats" diff --git a/handler/socks/v5/handler.go b/handler/socks/v5/handler.go index b5a5d12..034b29e 100644 --- a/handler/socks/v5/handler.go +++ b/handler/socks/v5/handler.go @@ -10,7 +10,7 @@ import ( "github.com/go-gost/core/handler" md "github.com/go-gost/core/metadata" "github.com/go-gost/gosocks5" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/util/socks" stats_util "github.com/go-gost/x/internal/util/stats" "github.com/go-gost/x/registry" diff --git a/handler/socks/v5/selector.go b/handler/socks/v5/selector.go index f49adaf..23f027a 100644 --- a/handler/socks/v5/selector.go +++ b/handler/socks/v5/selector.go @@ -8,7 +8,7 @@ import ( "github.com/go-gost/core/auth" "github.com/go-gost/core/logger" "github.com/go-gost/gosocks5" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/util/socks" ) diff --git a/handler/socks/v5/udp.go b/handler/socks/v5/udp.go index 36e8ac1..709a899 100644 --- a/handler/socks/v5/udp.go +++ b/handler/socks/v5/udp.go @@ -10,7 +10,7 @@ import ( "github.com/go-gost/core/logger" "github.com/go-gost/gosocks5" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/net/udp" "github.com/go-gost/x/internal/util/socks" "github.com/go-gost/x/stats" diff --git a/handler/socks/v5/udp_tun.go b/handler/socks/v5/udp_tun.go index de49b54..33242f4 100644 --- a/handler/socks/v5/udp_tun.go +++ b/handler/socks/v5/udp_tun.go @@ -7,7 +7,7 @@ import ( "github.com/go-gost/core/logger" "github.com/go-gost/gosocks5" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/net/udp" "github.com/go-gost/x/internal/util/socks" "github.com/go-gost/x/stats" diff --git a/handler/ss/handler.go b/handler/ss/handler.go index 56f52e5..3b58609 100644 --- a/handler/ss/handler.go +++ b/handler/ss/handler.go @@ -10,7 +10,7 @@ import ( "github.com/go-gost/core/handler" md "github.com/go-gost/core/metadata" "github.com/go-gost/gosocks5" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" netpkg "github.com/go-gost/x/internal/net" "github.com/go-gost/x/internal/util/ss" "github.com/go-gost/x/registry" diff --git a/handler/tunnel/connect.go b/handler/tunnel/connect.go index c8689a6..eb798c6 100644 --- a/handler/tunnel/connect.go +++ b/handler/tunnel/connect.go @@ -9,7 +9,7 @@ import ( "github.com/go-gost/core/limiter/traffic" "github.com/go-gost/core/logger" "github.com/go-gost/relay" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xnet "github.com/go-gost/x/internal/net" "github.com/go-gost/x/limiter/traffic/wrapper" ) diff --git a/handler/tunnel/handler.go b/handler/tunnel/handler.go index 0d40624..90d4b37 100644 --- a/handler/tunnel/handler.go +++ b/handler/tunnel/handler.go @@ -15,7 +15,7 @@ import ( "github.com/go-gost/core/recorder" "github.com/go-gost/core/service" "github.com/go-gost/relay" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xnet "github.com/go-gost/x/internal/net" xrecorder "github.com/go-gost/x/recorder" "github.com/go-gost/x/registry" diff --git a/hop/plugin/grpc.go b/hop/plugin/grpc.go index c924800..2b26ace 100644 --- a/hop/plugin/grpc.go +++ b/hop/plugin/grpc.go @@ -12,7 +12,7 @@ import ( "github.com/go-gost/plugin/hop/proto" "github.com/go-gost/x/config" node_parser "github.com/go-gost/x/config/parsing/node" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" "google.golang.org/grpc" ) diff --git a/hop/plugin/http.go b/hop/plugin/http.go index 9e77da8..b08280d 100644 --- a/hop/plugin/http.go +++ b/hop/plugin/http.go @@ -11,7 +11,7 @@ import ( "github.com/go-gost/core/logger" "github.com/go-gost/x/config" node_parser "github.com/go-gost/x/config/parsing/node" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" ) diff --git a/hosts/plugin/grpc.go b/hosts/plugin/grpc.go index 96c1c85..5585c07 100644 --- a/hosts/plugin/grpc.go +++ b/hosts/plugin/grpc.go @@ -8,7 +8,7 @@ import ( "github.com/go-gost/core/hosts" "github.com/go-gost/core/logger" "github.com/go-gost/plugin/hosts/proto" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" "google.golang.org/grpc" ) diff --git a/hosts/plugin/http.go b/hosts/plugin/http.go index cacbb4a..ae5672b 100644 --- a/hosts/plugin/http.go +++ b/hosts/plugin/http.go @@ -9,7 +9,7 @@ import ( "github.com/go-gost/core/hosts" "github.com/go-gost/core/logger" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" ) diff --git a/resolver/plugin/grpc.go b/resolver/plugin/grpc.go index a221143..1d18c00 100644 --- a/resolver/plugin/grpc.go +++ b/resolver/plugin/grpc.go @@ -8,7 +8,7 @@ import ( "github.com/go-gost/core/logger" "github.com/go-gost/core/resolver" "github.com/go-gost/plugin/resolver/proto" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" "google.golang.org/grpc" ) diff --git a/resolver/plugin/http.go b/resolver/plugin/http.go index 4a4cb2a..2da0637 100644 --- a/resolver/plugin/http.go +++ b/resolver/plugin/http.go @@ -11,7 +11,7 @@ import ( "github.com/go-gost/core/logger" "github.com/go-gost/core/resolver" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" "github.com/go-gost/x/internal/plugin" ) diff --git a/selector/strategy.go b/selector/strategy.go index fe4d886..b9ae90f 100644 --- a/selector/strategy.go +++ b/selector/strategy.go @@ -12,7 +12,7 @@ import ( "github.com/go-gost/core/metadata" mdutil "github.com/go-gost/core/metadata/util" "github.com/go-gost/core/selector" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" ) type roundRobinStrategy[T any] struct { diff --git a/service/service.go b/service/service.go index e25b9e9..1f037f3 100644 --- a/service/service.go +++ b/service/service.go @@ -17,7 +17,7 @@ import ( "github.com/go-gost/core/observer" "github.com/go-gost/core/recorder" "github.com/go-gost/core/service" - ctxvalue "github.com/go-gost/x/internal/ctx" + ctxvalue "github.com/go-gost/x/ctx" xmetrics "github.com/go-gost/x/metrics" "github.com/go-gost/x/stats" "github.com/rs/xid" From 25dcf536c6f52fb6dc8df8486b8776720ea709df Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Wed, 31 Jan 2024 23:18:42 +0800 Subject: [PATCH 2/2] 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)