MCPcopy Index your code
hub / github.com/GoEdgeLab/EdgeNode / doCheckReferers

Method doCheckReferers

internal/nodes/http_request_referers.go:10–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8)
9
10func (this *HTTPRequest) doCheckReferers() (shouldStop bool) {
11 if this.web.Referers == nil {
12 return
13 }
14
15 // 检查URL
16 if !this.web.Referers.MatchURL(this.URL()) {
17 return
18 }
19
20 var origin = this.RawReq.Header.Get("Origin")
21
22 const cacheSeconds = "3600" // 时间不能过长,防止修改设置后长期无法生效
23
24 // 处理用到Origin的特殊功能
25 if this.web.Referers.CheckOrigin && len(origin) > 0 {
26 // 处理Websocket
27 if this.web.Websocket != nil && this.web.Websocket.IsOn && this.RawReq.Header.Get("Upgrade") == "websocket" {
28 originHost, _ := httpParseHost(origin)
29 if len(originHost) > 0 && this.web.Websocket.MatchOrigin(originHost) {
30 return
31 }
32 }
33 }
34
35 var refererURL = this.RawReq.Header.Get("Referer")
36 if len(refererURL) == 0 && this.web.Referers.CheckOrigin {
37 if len(origin) > 0 && origin != "null" {
38 if urlSchemeRegexp.MatchString(origin) {
39 refererURL = origin
40 } else {
41 refererURL = "https://" + origin
42 }
43 }
44 }
45
46 if len(refererURL) == 0 {
47 if this.web.Referers.MatchDomain(this.ReqHost, "") {
48 return
49 }
50
51 this.tags = append(this.tags, "refererCheck")
52 this.writer.Header().Set("Cache-Control", "max-age="+cacheSeconds)
53 this.writeCode(http.StatusForbidden, "The referer has been blocked.", "当前访问已被防盗链系统拦截。")
54
55 return true
56 }
57
58 u, err := url.Parse(refererURL)
59 if err != nil {
60 if this.web.Referers.MatchDomain(this.ReqHost, "") {
61 return
62 }
63
64 this.tags = append(this.tags, "refererCheck")
65 this.writer.Header().Set("Cache-Control", "max-age="+cacheSeconds)
66 this.writeCode(http.StatusForbidden, "The referer has been blocked.", "当前访问已被防盗链系统拦截。")
67

Callers 1

DoMethod · 0.95

Calls 8

URLMethod · 0.95
writeCodeMethod · 0.95
httpParseHostFunction · 0.85
MatchStringMethod · 0.80
ParseMethod · 0.80
GetMethod · 0.45
SetMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected