mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-17 23:50:24 +08:00
访客IP设置中支持多个请求报头
This commit is contained in:
@@ -25,18 +25,40 @@ type HTTPRemoteAddrConfig struct {
|
|||||||
|
|
||||||
RequestHeaderName string `yaml:"requestHeaderName" json:"requestHeaderName"` // 请求报头名称(type = requestHeader时生效)
|
RequestHeaderName string `yaml:"requestHeaderName" json:"requestHeaderName"` // 请求报头名称(type = requestHeader时生效)
|
||||||
|
|
||||||
isEmpty bool
|
isEmpty bool
|
||||||
|
values []string
|
||||||
|
hasValues bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init 初始化
|
// Init 初始化
|
||||||
func (this *HTTPRemoteAddrConfig) Init() error {
|
func (this *HTTPRemoteAddrConfig) Init() error {
|
||||||
this.Value = strings.TrimSpace(this.Value)
|
this.Value = strings.TrimSpace(this.Value)
|
||||||
|
this.isEmpty = false
|
||||||
if len(this.Value) == 0 {
|
if len(this.Value) == 0 {
|
||||||
this.isEmpty = true
|
this.isEmpty = true
|
||||||
} else if regexp.MustCompile(`\s+`).ReplaceAllString(this.Value, "") == "${remoteAddr}" {
|
} else if regexp.MustCompile(`\s+`).ReplaceAllString(this.Value, "") == "${remoteAddr}" {
|
||||||
this.isEmpty = true
|
this.isEmpty = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// values
|
||||||
|
this.values = []string{}
|
||||||
|
var headerVarReg = regexp.MustCompile(`(\$\{header\.)([\w-,]+)(})`)
|
||||||
|
if headerVarReg.MatchString(this.Value) {
|
||||||
|
var subMatches = headerVarReg.FindStringSubmatch(this.Value)
|
||||||
|
if len(subMatches) > 3 {
|
||||||
|
var prefix = subMatches[1]
|
||||||
|
var headerNamesString = subMatches[2]
|
||||||
|
var suffix = subMatches[3]
|
||||||
|
for _, headerName := range strings.Split(headerNamesString, ",") {
|
||||||
|
headerName = strings.TrimSpace(headerName)
|
||||||
|
if len(headerName) > 0 {
|
||||||
|
this.values = append(this.values, prefix+headerName+suffix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.hasValues = len(this.values) > 1 // MUST be 1, not 0
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,3 +66,13 @@ func (this *HTTPRemoteAddrConfig) Init() error {
|
|||||||
func (this *HTTPRemoteAddrConfig) IsEmpty() bool {
|
func (this *HTTPRemoteAddrConfig) IsEmpty() bool {
|
||||||
return this.isEmpty
|
return this.isEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Values 可能的值变量
|
||||||
|
func (this *HTTPRemoteAddrConfig) Values() []string {
|
||||||
|
return this.values
|
||||||
|
}
|
||||||
|
|
||||||
|
// HasValues 检查是否有一组值
|
||||||
|
func (this *HTTPRemoteAddrConfig) HasValues() bool {
|
||||||
|
return this.hasValues
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTPRemoteAddrConfig_IsEmpty(t *testing.T) {
|
func TestHTTPRemoteAddrConfig_IsEmpty(t *testing.T) {
|
||||||
a := assert.NewAssertion(t)
|
var a = assert.NewAssertion(t)
|
||||||
|
|
||||||
{
|
{
|
||||||
var config = &HTTPRemoteAddrConfig{}
|
var config = &HTTPRemoteAddrConfig{}
|
||||||
@@ -52,3 +52,14 @@ func TestHTTPRemoteAddrConfig_IsEmpty(t *testing.T) {
|
|||||||
a.IsFalse(config.IsEmpty())
|
a.IsFalse(config.IsEmpty())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHTTPRemoteAddrConfig_Values(t *testing.T) {
|
||||||
|
for _, value := range []string{"${remoteAddr}", "${header.x-real-ip}", "${header.x-client-ip,x-real-ip,x-forwarded-for}"} {
|
||||||
|
var config = &HTTPRemoteAddrConfig{Value: value}
|
||||||
|
err := config.Init()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Log(value, "=>", config.Values())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user