mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-09 08:40:25 +08:00
服务支持自定义访客IP地址获取方式
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -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;
|
||||
|
||||
35
pkg/serverconfigs/http_remote_addr_config.go
Normal file
35
pkg/serverconfigs/http_remote_addr_config.go
Normal 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
|
||||
}
|
||||
54
pkg/serverconfigs/http_remote_addr_config_test.go
Normal file
54
pkg/serverconfigs/http_remote_addr_config_test.go
Normal 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())
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user