2022-09-22 16:33:53 +08:00
|
|
|
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (this *HTTPRequest) doCheckReferers() (shouldStop bool) {
|
|
|
|
|
if this.web.Referers == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 15:24:30 +08:00
|
|
|
const cacheSeconds = "3600" // 时间不能过长,防止修改设置后长期无法生效
|
|
|
|
|
|
2022-09-22 16:33:53 +08:00
|
|
|
var refererURL = this.RawReq.Header.Get("Referer")
|
|
|
|
|
if len(refererURL) == 0 {
|
|
|
|
|
if this.web.Referers.MatchDomain(this.ReqHost, "") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.tags = append(this.tags, "refererCheck")
|
2022-11-02 15:24:30 +08:00
|
|
|
this.writer.Header().Set("Cache-Control", "max-age="+cacheSeconds)
|
2022-09-22 17:52:57 +08:00
|
|
|
this.writeCode(http.StatusForbidden, "The referer has been blocked.", "当前访问已被防盗链系统拦截。")
|
2022-09-22 16:33:53 +08:00
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(refererURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if this.web.Referers.MatchDomain(this.ReqHost, "") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.tags = append(this.tags, "refererCheck")
|
2022-11-02 15:24:30 +08:00
|
|
|
this.writer.Header().Set("Cache-Control", "max-age="+cacheSeconds)
|
2022-09-22 17:52:57 +08:00
|
|
|
this.writeCode(http.StatusForbidden, "The referer has been blocked.", "当前访问已被防盗链系统拦截。")
|
2022-09-22 16:33:53 +08:00
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !this.web.Referers.MatchDomain(this.ReqHost, u.Host) {
|
|
|
|
|
this.tags = append(this.tags, "refererCheck")
|
2022-11-02 15:24:30 +08:00
|
|
|
this.writer.Header().Set("Cache-Control", "max-age="+cacheSeconds)
|
2022-09-22 17:52:57 +08:00
|
|
|
this.writeCode(http.StatusForbidden, "The referer has been blocked.", "当前访问已被防盗链系统拦截。")
|
2022-09-22 16:33:53 +08:00
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|