fix http keepalive for forwarding

This commit is contained in:
ginuerzh
2024-11-17 16:16:20 +08:00
parent c2aedbabd9
commit 2e8a5d6f51
5 changed files with 20 additions and 15 deletions
+3 -1
View File
@@ -403,8 +403,10 @@ type NodeMatcherConfig struct {
type HTTPNodeConfig struct { type HTTPNodeConfig struct {
// rewrite host header // rewrite host header
Host string `yaml:",omitempty" json:"host,omitempty"` Host string `yaml:",omitempty" json:"host,omitempty"`
// additional request header // Deprecated: use requestHeader instead
Header map[string]string `yaml:",omitempty" json:"header,omitempty"` Header map[string]string `yaml:",omitempty" json:"header,omitempty"`
// additional request header
RequestHeader map[string]string `yaml:",omitempty" json:"requestHeader,omitempty"`
// additional response header // additional response header
ResponseHeader map[string]string `yaml:",omitempty" json:"responseHeader,omitempty"` ResponseHeader map[string]string `yaml:",omitempty" json:"responseHeader,omitempty"`
// rewrite URL // rewrite URL
+5 -2
View File
@@ -198,9 +198,12 @@ func ParseNode(hop string, cfg *config.NodeConfig, log logger.Logger) (*chain.No
if cfg.HTTP != nil { if cfg.HTTP != nil {
settings := &chain.HTTPNodeSettings{ settings := &chain.HTTPNodeSettings{
Host: cfg.HTTP.Host, Host: cfg.HTTP.Host,
Header: cfg.HTTP.Header, RequestHeader: cfg.HTTP.RequestHeader,
ResponseHeader: cfg.HTTP.ResponseHeader, ResponseHeader: cfg.HTTP.ResponseHeader,
} }
if settings.RequestHeader == nil {
settings.RequestHeader = cfg.HTTP.Header
}
if auth := cfg.HTTP.Auth; auth != nil && auth.Username != "" { if auth := cfg.HTTP.Auth; auth != nil && auth.Username != "" {
settings.Auther = xauth.NewAuthenticator( 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 { for _, v := range cfg.HTTP.RewriteBody {
if pattern, _ := regexp.Compile(v.Match); pattern != nil { 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, Type: v.Type,
Pattern: pattern, Pattern: pattern,
Replacement: []byte(v.Replacement), Replacement: []byte(v.Replacement),
+1 -1
View File
@@ -9,7 +9,7 @@ require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d
github.com/gin-contrib/cors v1.7.2 github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0 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/gosocks4 v0.0.1
github.com/go-gost/gosocks5 v0.4.2 github.com/go-gost/gosocks5 v0.4.2
github.com/go-gost/plugin v0.1.1 github.com/go-gost/plugin v0.1.1
+2 -2
View File
@@ -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-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 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y= 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.11 h1:7zmRx0stWrClUJM/IT/ltRQJC9wUc8f6TAj1fbBiSPs=
github.com/go-gost/core v0.1.10/go.mod h1:WGI43jOka7FAsSAwi/fSMaqxdR+E339ycb4NBGlFr6A= 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 h1:+k1sec8HlELuQV7rWftIkmy8UijzUt2I6t+iMPlGB2s=
github.com/go-gost/gosocks4 v0.0.1/go.mod h1:3B6L47HbU/qugDg4JnoFPHgJXE43Inz8Bah1QaN9qCc= github.com/go-gost/gosocks4 v0.0.1/go.mod h1:3B6L47HbU/qugDg4JnoFPHgJXE43Inz8Bah1QaN9qCc=
github.com/go-gost/gosocks5 v0.4.2 h1:IianxHTkACPqCwiOAT3MHoMdSUl+SEPSRu1ikawC1Pc= github.com/go-gost/gosocks5 v0.4.2 h1:IianxHTkACPqCwiOAT3MHoMdSUl+SEPSRu1ikawC1Pc=
+9 -9
View File
@@ -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 responseHeader map[string]string
var bodyRewrites []chain.HTTPBodyRewriteSettings var respBodyRewrites []chain.HTTPBodyRewriteSettings
if httpSettings := node.Options().HTTP; httpSettings != nil { if httpSettings := node.Options().HTTP; httpSettings != nil {
if auther := httpSettings.Auther; auther != nil { if auther := httpSettings.Auther; auther != nil {
username, password, _ := req.BasicAuth() username, password, _ := req.BasicAuth()
@@ -429,7 +425,7 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node
if httpSettings.Host != "" { if httpSettings.Host != "" {
req.Host = httpSettings.Host req.Host = httpSettings.Host
} }
for k, v := range httpSettings.Header { for k, v := range httpSettings.RequestHeader {
req.Header.Set(k, v) req.Header.Set(k, v)
} }
@@ -443,7 +439,7 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node
} }
responseHeader = httpSettings.ResponseHeader responseHeader = httpSettings.ResponseHeader
bodyRewrites = httpSettings.RewriteBody respBodyRewrites = httpSettings.RewriteResponseBody
} }
var reqBody *xhttp.Body var reqBody *xhttp.Body
@@ -515,7 +511,11 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node
resp.ProtoMinor = req.ProtoMinor 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) log.Errorf("rewrite body: %v", err)
return return
} }
@@ -696,7 +696,7 @@ func (h *Sniffer) copyWebsocketFrame(w io.Writer, r io.Reader, buf *bytes.Buffer
return nil 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 { if resp == nil || len(rewrites) == 0 || resp.ContentLength <= 0 {
return nil return nil
} }