增加CC防护相关API定义、配置
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -101,6 +101,12 @@ service HTTPWebService {
|
||||
// 查找UAM设置
|
||||
rpc findHTTPWebUAM(FindHTTPWebUAMRequest) returns (FindHTTPWebUAMResponse);
|
||||
|
||||
// 修改CC设置
|
||||
rpc updateHTTPWebCC(UpdateHTTPWebCCRequest) returns (RPCSuccess);
|
||||
|
||||
// 查找UAM设置
|
||||
rpc findHTTPWebCC(FindHTTPWebCCRequest) returns (FindHTTPWebCCResponse);
|
||||
|
||||
// 修改防盗链设置
|
||||
rpc updateHTTPWebReferers(UpdateHTTPWebReferersRequest) returns (RPCSuccess);
|
||||
|
||||
@@ -321,6 +327,21 @@ message FindHTTPWebUAMResponse {
|
||||
bytes uamJSON = 1;
|
||||
}
|
||||
|
||||
// 修改服务CC设置
|
||||
message UpdateHTTPWebCCRequest {
|
||||
int64 httpWebId = 1;
|
||||
bytes ccJSON = 2;
|
||||
}
|
||||
|
||||
// 查找服务UAM设置
|
||||
message FindHTTPWebCCRequest {
|
||||
int64 httpWebId = 1;
|
||||
}
|
||||
|
||||
message FindHTTPWebCCResponse {
|
||||
bytes ccJSON = 1;
|
||||
}
|
||||
|
||||
// 修改防盗链设置
|
||||
message UpdateHTTPWebReferersRequest {
|
||||
int64 httpWebId = 1;
|
||||
|
||||
@@ -1,47 +1,22 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build !plus
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "strings"
|
||||
|
||||
// HTTPCCConfig HTTP CC防护配置
|
||||
type HTTPCCConfig struct {
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否覆盖父级
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
WithRequestPath bool `yaml:"withRequestPath" json:"withRequestPath"` // 根据URL路径区分请求
|
||||
PeriodSeconds int32 `yaml:"periodSeconds" json:"periodSeconds"` // 计算周期
|
||||
MaxRequests int32 `yaml:"maxRequests" json:"maxRequests"` // 请求数最大值
|
||||
MaxConnections int32 `yaml:"maxConnections" json:"maxConnections"` // 连接数最大值
|
||||
IgnoreCommonFiles bool `yaml:"ignoreCommonFiles" json:"ignoreCommonFiles"` // 忽略常用文件,如CSS、JS等
|
||||
IgnoreCommonAgents bool `yaml:"ignoreCommonAgents" json:"ignoreCommonAgents"` // 忽略常见搜索引擎等
|
||||
Action string `yaml:"action" json:"action"` // 动作,比如block、captcha等
|
||||
|
||||
fullKey string
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否覆盖父级
|
||||
IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用
|
||||
}
|
||||
|
||||
func NewHTTPCCConfig() *HTTPCCConfig {
|
||||
return &HTTPCCConfig{
|
||||
WithRequestPath: false,
|
||||
PeriodSeconds: 10,
|
||||
MaxRequests: 60,
|
||||
MaxConnections: 10,
|
||||
IgnoreCommonFiles: false,
|
||||
IgnoreCommonAgents: true,
|
||||
Action: "captcha",
|
||||
}
|
||||
return &HTTPCCConfig{}
|
||||
}
|
||||
|
||||
func (this *HTTPCCConfig) Init() error {
|
||||
// 组合Key
|
||||
var keys = []string{"${remoteAddr}"}
|
||||
if this.WithRequestPath {
|
||||
keys = append(keys, "${requestPath}")
|
||||
}
|
||||
this.fullKey = strings.Join(keys, "@")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *HTTPCCConfig) Key() string {
|
||||
return this.fullKey
|
||||
func (this *HTTPCCConfig) MatchURL(url string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -50,8 +50,8 @@ type HTTPWebConfig struct {
|
||||
RequestScripts *HTTPRequestScriptsConfig `yaml:"requestScripts" json:"requestScripts"` // HTTP请求相关脚本
|
||||
|
||||
// UAM, CC ...
|
||||
UAM *UAMConfig `yaml:"uam" json:"uam"`
|
||||
CC *UAMConfig `yaml:"cc" json:"cc"`
|
||||
UAM *UAMConfig `yaml:"uam" json:"uam"`
|
||||
CC *HTTPCCConfig `yaml:"cc" json:"cc"`
|
||||
}
|
||||
|
||||
func (this *HTTPWebConfig) Init() error {
|
||||
|
||||
@@ -34,7 +34,11 @@ func (this *URLPattern) Init() error {
|
||||
for index, piece := range pieces {
|
||||
pieces[index] = regexp.QuoteMeta(piece)
|
||||
}
|
||||
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + strings.Join(pieces, "(.*)") + "$")
|
||||
var pattern = strings.Join(pieces, "(.*)")
|
||||
if len(pattern) > 0 && pattern[0] == '/' {
|
||||
pattern = "(http|https)://[\\w.-]+" + pattern
|
||||
}
|
||||
reg, err := regexp.Compile("(?i)" /** 大小写不敏感 **/ + "^" + pattern + "$")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -4,13 +4,10 @@ package shared_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestURLPattern_Match(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
type unitTest struct {
|
||||
patternType string
|
||||
pattern string
|
||||
@@ -55,6 +52,24 @@ func TestURLPattern_Match(t *testing.T) {
|
||||
url: "https://example.com",
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "https://example.com",
|
||||
url: "https://example.com",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "/hello/world",
|
||||
url: "https://example-test.com/hello/world",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
patternType: "wildcard",
|
||||
pattern: "/hello/world",
|
||||
url: "https://example-test.com/123/hello/world",
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
patternType: "regexp",
|
||||
pattern: ".*",
|
||||
@@ -94,6 +109,9 @@ func TestURLPattern_Match(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.IsTrue(p.Match(ut.url) == ut.result)
|
||||
var b = p.Match(ut.url) == ut.result
|
||||
if !b {
|
||||
t.Fatal("not matched pattern:", ut.pattern, "url:", ut.url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user