防盗链功能增加“例外URL“和“限制URL”设置

This commit is contained in:
GoEdgeLab
2024-04-17 15:45:17 +08:00
parent 5d5f0855bf
commit 81b0a302e8
2 changed files with 53 additions and 1 deletions

View File

@@ -320,6 +320,14 @@ func (this *HTTPWebConfig) Init(ctx context.Context) error {
}
}
// referers
if this.Referers != nil {
err := this.Referers.Init()
if err != nil {
return err
}
}
return nil
}

View File

@@ -2,7 +2,10 @@
package serverconfigs
import "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
import (
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
)
// NewReferersConfig 获取新防盗链配置对象
func NewReferersConfig() *ReferersConfig {
@@ -20,9 +23,27 @@ type ReferersConfig struct {
AllowDomains []string `yaml:"allowDomains" json:"allowDomains"` // 允许的来源域名列表
DenyDomains []string `yaml:"denyDomains" json:"denyDomains"` // 禁止的来源域名列表
CheckOrigin bool `yaml:"checkOrigin" json:"checkOrigin"` // 是否检查Origin
OnlyURLPatterns []*shared.URLPattern `yaml:"onlyURLPatterns" json:"onlyURLPatterns"` // 仅限的URL
ExceptURLPatterns []*shared.URLPattern `yaml:"exceptURLPatterns" json:"exceptURLPatterns"` // 排除的URL
}
func (this *ReferersConfig) Init() error {
// url patterns
for _, pattern := range this.ExceptURLPatterns {
err := pattern.Init()
if err != nil {
return err
}
}
for _, pattern := range this.OnlyURLPatterns {
err := pattern.Init()
if err != nil {
return err
}
}
return nil
}
@@ -54,3 +75,26 @@ func (this *ReferersConfig) MatchDomain(requestDomain string, refererDomain stri
return false
}
func (this *ReferersConfig) MatchURL(url string) bool {
// except
if len(this.ExceptURLPatterns) > 0 {
for _, pattern := range this.ExceptURLPatterns {
if pattern.Match(url) {
return false
}
}
}
// only
if len(this.OnlyURLPatterns) > 0 {
for _, pattern := range this.OnlyURLPatterns {
if pattern.Match(url) {
return true
}
}
return false
}
return true
}