服务支持自定义访客IP地址获取方式

This commit is contained in:
GoEdgeLab
2021-10-06 11:42:52 +08:00
parent 9b556abafe
commit bc8c1d8fb8
5 changed files with 448 additions and 220 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -25,6 +25,9 @@ service HTTPWebService {
// 更改WebP配置
rpc updateHTTPWebWebP (UpdateHTTPWebWebPRequest) returns (RPCSuccess);
// 更改RemoteAddr配置
rpc updateHTTPWebRemoteAddr(UpdateHTTPWebRemoteAddrRequest) returns (RPCSuccess);
// 更改字符集配置
rpc updateHTTPWebCharset (UpdateHTTPWebCharsetRequest) returns (RPCSuccess);
@@ -122,6 +125,12 @@ message UpdateHTTPWebWebPRequest {
bytes webpJSON = 2;
}
// 更改RemoteAddr配置
message UpdateHTTPWebRemoteAddrRequest {
int64 webId = 1;
bytes remoteAddrJSON = 2;
}
// 更改字符集配置
message UpdateHTTPWebCharsetRequest {
int64 webId = 1;

View File

@@ -0,0 +1,35 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package serverconfigs
import (
"regexp"
"strings"
)
// HTTPRemoteAddrConfig HTTP获取客户端IP地址方式
type HTTPRemoteAddrConfig struct {
IsPrior bool `yaml:"isPrior" json:"isPrior"`
IsOn bool `yaml:"isOn" json:"isOn"`
Value string `yaml:"value" json:"value"` // 值变量
isEmpty bool
}
// Init 初始化
func (this *HTTPRemoteAddrConfig) Init() error {
if len(this.Value) == 0 {
this.isEmpty = true
} else if regexp.MustCompile(`\s+`).ReplaceAllString(this.Value, "") == "${remoteAddr}" {
this.isEmpty = true
}
this.Value = strings.ReplaceAll(this.Value, "${remoteAddr}", "${remoteAddrValue}")
return nil
}
// IsEmpty 是否为空
func (this *HTTPRemoteAddrConfig) IsEmpty() bool {
return this.isEmpty
}

View File

@@ -0,0 +1,54 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package serverconfigs
import (
"github.com/iwind/TeaGo/assert"
"testing"
)
func TestHTTPRemoteAddrConfig_IsEmpty(t *testing.T) {
a := assert.NewAssertion(t)
{
var config = &HTTPRemoteAddrConfig{}
err := config.Init()
if err != nil {
t.Fatal(err)
}
a.IsTrue(config.IsEmpty())
}
{
var config = &HTTPRemoteAddrConfig{
Value: "${remoteAddr}",
}
err := config.Init()
if err != nil {
t.Fatal(err)
}
a.IsTrue(config.IsEmpty())
}
{
var config = &HTTPRemoteAddrConfig{
Value: "${ remoteAddr }",
}
err := config.Init()
if err != nil {
t.Fatal(err)
}
a.IsTrue(config.IsEmpty())
}
{
var config = &HTTPRemoteAddrConfig{
Value: "[${remoteAddr}]",
}
err := config.Init()
if err != nil {
t.Fatal(err)
}
a.IsFalse(config.IsEmpty())
}
}

View File

@@ -40,6 +40,8 @@ type HTTPWebConfig struct {
HostRedirects []*HTTPHostRedirectConfig `yaml:"hostRedirects" json:"hostRedirects"` // 主机跳转
Auth *HTTPAuthConfig `yaml:"auth" json:"auth"` // 认证配置
RemoteAddr *HTTPRemoteAddrConfig `yaml:"remoteAddr" json:"remoteAddr"`
}
func (this *HTTPWebConfig) Init() error {
@@ -241,6 +243,14 @@ func (this *HTTPWebConfig) Init() error {
}
}
// remoteAddr
if this.RemoteAddr != nil {
err := this.RemoteAddr.Init()
if err != nil {
return err
}
}
return nil
}