mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2025-11-07 23:30:25 +08:00
防盗链功能增加“例外URL“和“限制URL”设置
This commit is contained in:
@@ -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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
package serverconfigs
|
package serverconfigs
|
||||||
|
|
||||||
import "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
import (
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||||
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
)
|
||||||
|
|
||||||
// NewReferersConfig 获取新防盗链配置对象
|
// NewReferersConfig 获取新防盗链配置对象
|
||||||
func NewReferersConfig() *ReferersConfig {
|
func NewReferersConfig() *ReferersConfig {
|
||||||
@@ -20,9 +23,27 @@ type ReferersConfig struct {
|
|||||||
AllowDomains []string `yaml:"allowDomains" json:"allowDomains"` // 允许的来源域名列表
|
AllowDomains []string `yaml:"allowDomains" json:"allowDomains"` // 允许的来源域名列表
|
||||||
DenyDomains []string `yaml:"denyDomains" json:"denyDomains"` // 禁止的来源域名列表
|
DenyDomains []string `yaml:"denyDomains" json:"denyDomains"` // 禁止的来源域名列表
|
||||||
CheckOrigin bool `yaml:"checkOrigin" json:"checkOrigin"` // 是否检查Origin
|
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 {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,3 +75,26 @@ func (this *ReferersConfig) MatchDomain(requestDomain string, refererDomain stri
|
|||||||
|
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user