mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-04-08 00:15:19 +08:00
增加防盗链功能
This commit is contained in:
@@ -40,6 +40,7 @@ type HTTPWebConfig struct {
|
||||
|
||||
HostRedirects []*HTTPHostRedirectConfig `yaml:"hostRedirects" json:"hostRedirects"` // 主机跳转
|
||||
Auth *HTTPAuthConfig `yaml:"auth" json:"auth"` // 认证配置
|
||||
Referers *ReferersConfig `yaml:"referers" json:"referers"` // 防盗链设置
|
||||
|
||||
RemoteAddr *HTTPRemoteAddrConfig `yaml:"remoteAddr" json:"remoteAddr"` // 客户端IP获取方式
|
||||
MergeSlashes bool `yaml:"mergeSlashes" json:"mergeSlashes"` // 是否合并路径中的斜杠(/)
|
||||
|
||||
37
pkg/serverconfigs/referers_config.go
Normal file
37
pkg/serverconfigs/referers_config.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package serverconfigs
|
||||
|
||||
import "github.com/TeaOSLab/EdgeCommon/pkg/configutils"
|
||||
|
||||
// ReferersConfig 防盗链设置
|
||||
type ReferersConfig struct {
|
||||
IsPrior bool `yaml:"isPrior" json:"isPrior"`
|
||||
IsOn bool `yaml:"isOn" json:"isOn"`
|
||||
AllowEmpty bool `yaml:"allowEmpty" json:"allowEmpty"` // 来源域名允许为空
|
||||
AllowSameDomain bool `yaml:"allowSameDomain" json:"allowSameDomain"` // 允许来源域名和当前访问的域名一致,相当于在站内访问
|
||||
AllowDomains []string `yaml:"allowDomains" json:"allowDomains"` // 允许的来源域名列表
|
||||
}
|
||||
|
||||
func (this *ReferersConfig) Init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *ReferersConfig) MatchDomain(requestDomain string, refererDomain string) bool {
|
||||
if len(refererDomain) == 0 {
|
||||
if this.AllowEmpty {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if this.AllowSameDomain && requestDomain == refererDomain {
|
||||
return true
|
||||
}
|
||||
|
||||
if len(this.AllowDomains) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return configutils.MatchDomains(this.AllowDomains, refererDomain)
|
||||
}
|
||||
Reference in New Issue
Block a user