可以配置是否在反向代理中添加X-Real-IP和X-Forwarded-*

This commit is contained in:
GoEdgeLab
2021-01-26 20:28:46 +08:00
parent cf8a1ce68a
commit 1b32a76294
3 changed files with 66 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/schedulingconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/lists"
"sync"
)
@@ -30,6 +31,8 @@ type ReverseProxyConfig struct {
RequestHost string `yaml:"requestHost" json:"requestHost"` // 请求Host支持变量
RequestURI string `yaml:"requestURI" json:"requestURI"` // 请求URI支持变量如果同时定义了StripPrefix则先执行StripPrefix
AddHeaders []string `yaml:"addHeaders" json:"addHeaders"` // 自动添加的Header
AutoFlush bool `yaml:"autoFlush" json:"autoFlush"` // 是否自动刷新缓冲区在比如SSEserver-sent events场景下很有用
requestHostHasVariables bool
@@ -40,6 +43,13 @@ type ReverseProxyConfig struct {
schedulingIsBackup bool
schedulingObject schedulingconfigs.SchedulingInterface
schedulingLocker sync.Mutex
addXRealIPHeader bool
addXForwardedForHeader bool
addForwardedHeader bool
addXForwardedByHeader bool
addXForwardedHostHeader bool
addXForwardedProtoHeader bool
}
// 初始化
@@ -67,6 +77,13 @@ func (this *ReverseProxyConfig) Init() error {
// scheduling
this.SetupScheduling(false)
// Header
this.addXRealIPHeader = lists.ContainsString(this.AddHeaders, "X-Real-IP")
this.addXForwardedForHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-For")
this.addXForwardedByHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-By")
this.addXForwardedHostHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-Host")
this.addXForwardedProtoHeader = lists.ContainsString(this.AddHeaders, "X-Forwarded-Proto")
return nil
}
@@ -170,3 +187,28 @@ func (this *ReverseProxyConfig) RequestHostHasVariables() bool {
func (this *ReverseProxyConfig) RequestURIHasVariables() bool {
return this.requestURIHasVariables
}
// 是否添加X-Real-IP
func (this *ReverseProxyConfig) ShouldAddXRealIPHeader() bool {
return this.addXRealIPHeader
}
// 是否添加X-Forwarded-For
func (this *ReverseProxyConfig) ShouldAddXForwardedForHeader() bool {
return this.addXForwardedForHeader
}
// 是否添加X-Forwarded-By
func (this *ReverseProxyConfig) ShouldAddXForwardedByHeader() bool {
return this.addXForwardedByHeader
}
// 是否添加X-Forwarded-Host
func (this *ReverseProxyConfig) ShouldAddXForwardedHostHeader() bool {
return this.addXForwardedHostHeader
}
// 是否添加X-Forwarded-Proto
func (this *ReverseProxyConfig) ShouldAddXForwardedProtoHeader() bool {
return this.addXForwardedProtoHeader
}