()
| 8 | ) |
| 9 | |
| 10 | func (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 |
no test coverage detected