add custom http response header for reverse proxy

This commit is contained in:
ginuerzh
2024-11-07 11:04:42 +08:00
parent a12870e766
commit 3db20563d2
5 changed files with 19 additions and 5 deletions
+2
View File
@@ -398,6 +398,8 @@ type HTTPNodeConfig struct {
Host string `yaml:",omitempty" json:"host,omitempty"` Host string `yaml:",omitempty" json:"host,omitempty"`
// additional request header // additional request header
Header map[string]string `yaml:",omitempty" json:"header,omitempty"` Header map[string]string `yaml:",omitempty" json:"header,omitempty"`
// additional response header
ResponseHeader map[string]string `yaml:",omitempty" json:"responseHeader,omitempty"`
// rewrite URL // rewrite URL
Rewrite []HTTPURLRewriteConfig `yaml:",omitempty" json:"rewrite,omitempty"` Rewrite []HTTPURLRewriteConfig `yaml:",omitempty" json:"rewrite,omitempty"`
// rewrite response body // rewrite response body
+1
View File
@@ -179,6 +179,7 @@ func ParseNode(hop string, cfg *config.NodeConfig, log logger.Logger) (*chain.No
settings := &chain.HTTPNodeSettings{ settings := &chain.HTTPNodeSettings{
Host: cfg.HTTP.Host, Host: cfg.HTTP.Host,
Header: cfg.HTTP.Header, Header: cfg.HTTP.Header,
ResponseHeader: cfg.HTTP.ResponseHeader,
} }
if auth := cfg.HTTP.Auth; auth != nil && auth.Username != "" { if auth := cfg.HTTP.Auth; auth != nil && auth.Username != "" {
+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.8 github.com/go-gost/core v0.1.9
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
@@ -50,8 +50,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.8 h1:OtE30bodK9v4lCCTCM89Wa3rYJD31pRgQQut1dZyFmk= github.com/go-gost/core v0.1.9 h1:qrKzhE9X2+zuNc8d4hRv4I8ZklwHAfja5HngrLucM2c=
github.com/go-gost/core v0.1.8/go.mod h1:WGI43jOka7FAsSAwi/fSMaqxdR+E339ycb4NBGlFr6A= github.com/go-gost/core v0.1.9/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=
+11
View File
@@ -404,6 +404,7 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node
req.Header.Set("Connection", "close") req.Header.Set("Connection", "close")
} }
var responseHeader map[string]string
var bodyRewrites []chain.HTTPBodyRewriteSettings var bodyRewrites []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 {
@@ -437,6 +438,7 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node
} }
} }
responseHeader = httpSettings.ResponseHeader
bodyRewrites = httpSettings.RewriteBody bodyRewrites = httpSettings.RewriteBody
} }
@@ -477,6 +479,15 @@ func (h *Sniffer) httpRoundTrip(ctx context.Context, rw, cc io.ReadWriter, node
defer resp.Body.Close() defer resp.Body.Close()
xio.SetReadDeadline(cc, time.Time{}) xio.SetReadDeadline(cc, time.Time{})
if len(responseHeader) > 0 {
if resp.Header == nil {
resp.Header = http.Header{}
}
for k, v := range responseHeader {
resp.Header.Set(k, v)
}
}
ro.HTTP.StatusCode = resp.StatusCode ro.HTTP.StatusCode = resp.StatusCode
ro.HTTP.Response.Header = resp.Header ro.HTTP.Response.Header = resp.Header
ro.HTTP.Response.ContentLength = resp.ContentLength ro.HTTP.Response.ContentLength = resp.ContentLength