From 2e8a5d6f513ca6012f6d0a77e44779a0908715b7 Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Sun, 17 Nov 2024 16:16:20 +0800 Subject: [PATCH] fix http keepalive for forwarding --- config/config.go | 4 +++- config/parsing/node/parse.go | 7 +++++-- go.mod | 2 +- go.sum | 4 ++-- internal/util/forwarder/sniffer.go | 18 +++++++++--------- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/config/config.go b/config/config.go index 68dbb1fa..b16f2604 100644 --- a/config/config.go +++ b/config/config.go @@ -403,8 +403,10 @@ type NodeMatcherConfig struct { type HTTPNodeConfig struct { // rewrite host header Host string `yaml:",omitempty" json:"host,omitempty"` - // additional request header + // Deprecated: use requestHeader instead Header map[string]string `yaml:",omitempty" json:"header,omitempty"` + // additional request header + RequestHeader map[string]string `yaml:",omitempty" json:"requestHeader,omitempty"` // additional response header ResponseHeader map[string]string `yaml:",omitempty" json:"responseHeader,omitempty"` // rewrite URL diff --git a/config/parsing/node/parse.go b/config/parsing/node/parse.go index 20feab35..dc8d7ea2 100644 --- a/config/parsing/node/parse.go +++ b/config/parsing/node/parse.go @@ -198,9 +198,12 @@ func ParseNode(hop string, cfg *config.NodeConfig, log logger.Logger) (*chain.No if cfg.HTTP != nil { settings := &chain.HTTPNodeSettings{ Host: cfg.HTTP.Host, - Header: cfg.HTTP.Header, + RequestHeader: cfg.HTTP.RequestHeader, ResponseHeader: cfg.HTTP.ResponseHeader, } + if settings.RequestHeader == nil { + settings.RequestHeader = cfg.HTTP.Header + } if auth := cfg.HTTP.Auth; auth != nil && auth.Username != "" { settings.Auther = xauth.NewAuthenticator( @@ -222,7 +225,7 @@ func ParseNode(hop string, cfg *config.NodeConfig, log logger.Logger) (*chain.No } for _, v := range cfg.HTTP.RewriteBody { if pattern, _ := regexp.Compile(v.Match); pattern != nil { - settings.RewriteBody = append(settings.RewriteBody, chain.HTTPBodyRewriteSettings{ + settings.RewriteResponseBody = append(settings.RewriteResponseBody, chain.HTTPBodyRewriteSettings{ Type: v.Type, Pattern: pattern, Replacement: []byte(v.Replacement), diff --git a/go.mod b/go.mod index 865c7c25..5f8043f2 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/gin-contrib/cors v1.7.2 github.com/gin-gonic/gin v1.10.0 - github.com/go-gost/core v0.1.10 + github.com/go-gost/core v0.1.11 github.com/go-gost/gosocks4 v0.0.1 github.com/go-gost/gosocks5 v0.4.2 github.com/go-gost/plugin v0.1.1 diff --git a/go.sum b/go.sum index 1548cd5d..5adf2f98 100644 --- a/go.sum +++ b/go.sum @@ -51,8 +51,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.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= -github.com/go-gost/core v0.1.10 h1:sONgU+BRbDE5owFh7IeJ1KBdG7JH6KmCAW0FpLoId2I= -github.com/go-gost/core v0.1.10/go.mod h1:WGI43jOka7FAsSAwi/fSMaqxdR+E339ycb4NBGlFr6A= +github.com/go-gost/core v0.1.11 h1:7zmRx0stWrClUJM/IT/ltRQJC9wUc8f6TAj1fbBiSPs= +github.com/go-gost/core v0.1.11/go.mod h1:WGI43jOka7FAsSAwi/fSMaqxdR+E339ycb4NBGlFr6A= 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.2 h1:IianxHTkACPqCwiOAT3MHoMdSUl+SEPSRu1ikawC1Pc= diff --git a/internal/util/forwarder/sniffer.go b/internal/util/forwarder/sniffer.go index a9b8f582..68d23c45 100644 --- a/internal/util/forwarder/sniffer.go +++ b/internal/util/forwarder/sniffer.go @@ -404,12 +404,8 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node } } - if !ho.HTTPKeepalive { - req.Header.Set("Connection", "close") - } - var responseHeader map[string]string - var bodyRewrites []chain.HTTPBodyRewriteSettings + var respBodyRewrites []chain.HTTPBodyRewriteSettings if httpSettings := node.Options().HTTP; httpSettings != nil { if auther := httpSettings.Auther; auther != nil { username, password, _ := req.BasicAuth() @@ -429,7 +425,7 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node if httpSettings.Host != "" { req.Host = httpSettings.Host } - for k, v := range httpSettings.Header { + for k, v := range httpSettings.RequestHeader { req.Header.Set(k, v) } @@ -443,7 +439,7 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node } responseHeader = httpSettings.ResponseHeader - bodyRewrites = httpSettings.RewriteBody + respBodyRewrites = httpSettings.RewriteResponseBody } var reqBody *xhttp.Body @@ -515,7 +511,11 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node resp.ProtoMinor = req.ProtoMinor } - if err = h.rewriteBody(resp, bodyRewrites...); err != nil { + if !ho.HTTPKeepalive { + resp.Header.Set("Connection", "close") + } + + if err = h.rewriteRespBody(resp, respBodyRewrites...); err != nil { log.Errorf("rewrite body: %v", err) return } @@ -696,7 +696,7 @@ func (h *Sniffer) copyWebsocketFrame(w io.Writer, r io.Reader, buf *bytes.Buffer return nil } -func (h *Sniffer) rewriteBody(resp *http.Response, rewrites ...chain.HTTPBodyRewriteSettings) error { +func (h *Sniffer) rewriteRespBody(resp *http.Response, rewrites ...chain.HTTPBodyRewriteSettings) error { if resp == nil || len(rewrites) == 0 || resp.ContentLength <= 0 { return nil }