public override bool Apply(Request httpRequest) { string referer; string request; if (httpRequest.Referer == null) referer = ""; else referer = new Uri (httpRequest.Referer).Host; request = httpRequest.Uri.Host; if (request == referer) return false; using (listLock.Read) { foreach (RefererPair pair in watchlist) { if (pair.MatchFrom (referer) == false) continue; if (pair.MatchTo (httpRequest.Dns.NameList) == false) continue; httpRequest.Flags.Set (pair.Flags); if (pair.Flags ["block"]) { httpRequest.SetTriggerHtml (Html.Format (@" <h1 style=""text-align:center""><a href=""{0}"" style=""font-size: 3em;"">{1}</a></h1> <p>Blocked by: {2} <a href=""{3}?delete={4}&return={5}"">delete</a></p>", httpRequest.Uri, httpRequest.Uri.Host, pair, Filters.WebUI.FilterUrl (this), pair.GetHashCode (), Uri.EscapeUriString (httpRequest.Uri.ToString ()))); return true; } httpRequest.SetTriggerHtml (Html.Escape (pair.ToString ())); return true; } } //Already blocked, don't add to blocked list if (httpRequest.Flags ["block"]) return true; using (listLock.UpgradeableRead) { foreach (string h in httpRequest.Dns.NameList) { RefererPair rp = new RefererPair (referer, h); if (blocked.Contains (rp) == false) { using (listLock.Write) { blocked.Insert (0, rp); } } } } //Allow empty referers if (referer == "") return false; //Default action: none(pass) return false; //Default action: Remove //httpRequest.Flags.Set ("remove"); //Default action: Block /* httpRequest.Flags.Set ("block"); httpRequest.SetTriggerHtml (Html.Format (@" <h1 style=""text-align:center""><a href=""{0}"" style=""font-size: 3em;"">{1}</a></h1> <p style=""text-align:center""><a href=""{0}"">{2}</a></p>", httpRequest.Uri, httpRequest.Uri.Host, httpRequest.Uri.PathAndQuery)); httpRequest.SetTriggerHtml (Form (referer, httpRequest.Uri.Host, httpRequest.Uri.ToString ())); return true; */ }
public bool Match(RefererPair requestPair) { if (MatchStrings (FromHost, requestPair.FromHost) == false) return false; if (MatchStrings (ToHost, requestPair.ToHost) == false) return false; return true; }
private Html Form(RefererPair pair) { return Form (pair.FromHost, pair.ToHost, null); }
private Html Form(RefererPair pair, string returnUrl) { return Form (pair.FromHost, pair.ToHost, returnUrl); }
public override Response Status(NameValueCollection httpGet, Request request) { Html html = new Html (); if (httpGet ["delete"] != null) { int item = int.Parse (httpGet ["delete"]); using (listLock.Write) { foreach (RefererPair rp in watchlist.ToArray ()) { if (rp.GetHashCode () == item) watchlist.Remove (rp); } } SaveFilters (); } if (httpGet ["clear"] != null) { using (listLock.Write) { blocked.Clear (); } } if (httpGet ["action"] != null || httpGet ["flags"] != null) { RefererPair p = new RefererPair (httpGet ["from"], httpGet ["to"]); p.Flags.Set (httpGet ["flags"]); if (httpGet ["action"].Contains (" ") == false) p.Flags.Set (httpGet ["action"]); using (listLock.Write) { watchlist.Add (p); foreach (RefererPair bp in blocked.ToArray ()) { if (p.Match (bp)) blocked.Remove (bp); } } SaveFilters (); } if (httpGet ["return"] != null) { Response resp = new Response (HttpStatusCode.Redirect, new Html ()); resp.ReplaceHeader ("Location", httpGet ["return"]); return resp; } html += Html.Format (@"<h2>Blocked <a href=""?clear=yes"">clear</a></h2>"); html += Html.Format ("<table><tr><th>From Domain</th><th>To Domain</th><th>Flags</th></tr>"); html += Form ("", ""); using (listLock.Read) { foreach (RefererPair pair in blocked) { html += Form (pair); } html += Html.Format ("</table>"); html += Html.Format ("<h2>Watchlist</h2>"); html += Html.Format ("<table><tr><th>From Domain</th><th>To Domain</th><th>Flags</th><th>Delete</th></tr>"); foreach (RefererPair pair in watchlist) { html += Html.Format ("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td><a href=\"?delete={3}\">delete</a></td></tr>", pair.FromHost, pair.ToHost, pair.Flags, pair.GetHashCode ()); } html += Html.Format ("</table>"); } html += Html.Format (@" <div> <ul> <li><strong>Pass</strong> Allow request to pass through unmodified</li> <li><strong>Fake</strong> Change referer to the root of the target host</li> <li><strong>Clean</strong> Change referer to the root of the source host</li> <li><strong>Remove</strong> Remove the referer header</li> <li><strong>Slow</strong> Do not modify the request but slow down the transfer speed</li> <li><strong>Block</strong> Block the entire request</li> </ul> <p>From/To: Wildcard(*) allowed in start of domains, applies to subdomains only</p> <p>Example: *example.com matches xyz.example.com and example.com but not badexample.com</p> </div>"); return WebUI.ResponseTemplate (ToString (), html); }