路径规则、重写规则、URL跳转规则均支持匹配条件

This commit is contained in:
刘祥超
2021-06-09 21:43:58 +08:00
parent df4b79c4fe
commit 64c693b9e1
7 changed files with 208 additions and 152 deletions

View File

@@ -1,6 +1,7 @@
package serverconfigs
import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"net/url"
"regexp"
)
@@ -13,9 +14,10 @@ type HTTPHostRedirectConfig struct {
BeforeURL string `yaml:"beforeURL" json:"beforeURL"` // 跳转前的地址
AfterURL string `yaml:"afterURL" json:"afterURL"` // 跳转后的地址
MatchPrefix bool `yaml:"matchPrefix" json:"matchPrefix"` // 只匹配前缀部分
MatchRegexp bool `yaml:"matchRegexp" json:"matchRegexp"` // 匹配正则表达式
KeepRequestURI bool `yaml:"keepRequestURI" json:"keepRequestURI"` // 保留请求URI
MatchPrefix bool `yaml:"matchPrefix" json:"matchPrefix"` // 只匹配前缀部分
MatchRegexp bool `yaml:"matchRegexp" json:"matchRegexp"` // 匹配正则表达式
KeepRequestURI bool `yaml:"keepRequestURI" json:"keepRequestURI"` // 保留请求URI
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
realBeforeURL string
beforeURLRegexp *regexp.Regexp
@@ -41,6 +43,13 @@ func (this *HTTPHostRedirectConfig) Init() error {
this.beforeURLRegexp = reg
}
if this.Conds != nil {
err := this.Conds.Init()
if err != nil {
return err
}
}
return nil
}
@@ -53,3 +62,11 @@ func (this *HTTPHostRedirectConfig) RealBeforeURL() string {
func (this *HTTPHostRedirectConfig) BeforeURLRegexp() *regexp.Regexp {
return this.beforeURLRegexp
}
// MatchRequest 判断请求是否符合条件
func (this *HTTPHostRedirectConfig) MatchRequest(formatter func(source string) string) bool {
if this.Conds == nil {
return true
}
return this.Conds.MatchRequest(formatter)
}

View File

@@ -19,7 +19,7 @@ type HTTPLocationConfig struct {
ReverseProxy *ReverseProxyConfig `yaml:"reverseProxy" json:"reverseProxy"` // 反向代理设置
IsBreak bool `yaml:"isBreak" json:"isBreak"` // 终止向下解析
Children []*HTTPLocationConfig `yaml:"children" json:"children"` // 子规则
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件 TODO
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
patternType HTTPLocationPatternType // 规则类型LocationPattern*
prefix string // 前缀

View File

@@ -19,12 +19,7 @@ const (
HTTPRewriteModeProxy HTTPRewriteMode = "proxy" // 代理
)
// 重写规则定义
//
// 参考
// - http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
// - https://httpd.apache.org/docs/current/mod/mod_rewrite.html
// - https://httpd.apache.org/docs/2.4/rewrite/flags.html
// HTTPRewriteRule 重写规则定义
// TODO 实现对其他代理服务的引用
type HTTPRewriteRule struct {
Id int64 `yaml:"id" json:"id"` // ID
@@ -35,7 +30,7 @@ type HTTPRewriteRule struct {
// - cond ${status} gte 200
// - cond ${arg.name} eq lily
// - cond ${requestPath} regexp .*\.png
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件 TODO
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
// 规则
// 语法为pattern regexp 比如:
@@ -61,7 +56,7 @@ type HTTPRewriteRule struct {
proxyHostHasVariables bool
}
// 校验
// Init 校验
func (this *HTTPRewriteRule) Init() error {
this.replaceHasVariables = configutils.HasVariables(this.Replace)
this.proxyHostHasVariables = configutils.HasVariables(this.ProxyHost)
@@ -83,7 +78,7 @@ func (this *HTTPRewriteRule) Init() error {
return nil
}
// 对某个请求执行规则
// MatchRequest 对某个请求执行规则
func (this *HTTPRewriteRule) MatchRequest(requestPath string, formatter func(source string) string) (replace string, varMapping map[string]string, matched bool) {
if this.reg == nil {
return "", nil, false
@@ -124,12 +119,12 @@ func (this *HTTPRewriteRule) MatchRequest(requestPath string, formatter func(sou
return replace, varMapping, true
}
// 判断是否是外部URL
// IsExternalURL 判断是否是外部URL
func (this *HTTPRewriteRule) IsExternalURL(url string) bool {
return shared.RegexpExternalURL.MatchString(url)
}
// 判断ProxyHost是否有变量
// ProxyHostHasVariables 判断ProxyHost是否有变量
func (this *HTTPRewriteRule) ProxyHostHasVariables() bool {
return this.proxyHostHasVariables
}