Created
February 9, 2020 03:25
-
-
Save reinforchu/b248a84197be94c0c5d54aad6634af91 to your computer and use it in GitHub Desktop.
AZBlocker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Text.RegularExpressions; | |
namespace AZBlocker | |
{ | |
/// <summary> | |
/// *.azurewebsites.net access blocker | |
/// </summary> | |
public class Class1 : IHttpModule | |
{ | |
private HttpApplication App; | |
private bool isMatched = false; | |
public void Init(HttpApplication context) | |
{ | |
App = context; | |
context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute); | |
context.PreSendRequestHeaders += new EventHandler(OnPreSendRequestHeaders); | |
context.PreSendRequestContent += new EventHandler(OnPreSendRequestContent); | |
} | |
private void OnPreRequestHandlerExecute(object sender, EventArgs e) | |
{ | |
string hostName = HttpContext.Current.Request.Url.Host; | |
// HttpContext.Current.Response.Headers.Add("Name", hostName); | |
if (!String.IsNullOrEmpty(hostName)) | |
{ | |
try | |
{ | |
Regex regx = new Regex(@"^(.*).azurewebsites.net$"); | |
// Regex regx = new Regex(@"^localhost$"); | |
if (regx.IsMatch(hostName)) | |
{ | |
// App.Response.Redirect("/"); // ERR_TOO_MANY_REDIRECTS: | |
// App.Response.Redirect("javascript:"); // Running javascript scheme at Top-level is prohibited. | |
// App.Response.Redirect("script:"); // Firefox say: http Unknown protocol | |
// App.Response.Close(); // ERR_CONNECTION_RESET: Connection reset | |
// App.Response.End(); // throw ThreadAbortException | |
// App.Context.Session.Timeout = 0; // Exception | |
// App.CompleteRequest(); // Safety? | |
// For Azure Web Service | |
App.Response.StatusCode = 302; | |
HttpContext.Current.Response.Headers.Add("Location", "script:"); | |
isMatched = true; | |
} | |
} catch (Exception) | |
{ | |
throw; | |
} | |
} | |
} | |
private void OnPreSendRequestHeaders(object sender, EventArgs e) | |
{ | |
if (isMatched) | |
{ | |
HttpContext.Current.Response.Headers.Remove("Server"); | |
HttpContext.Current.Response.Headers.Remove("Content-Type"); | |
HttpContext.Current.Response.Headers.Remove("Last-Modified"); | |
HttpContext.Current.Response.Headers.Remove("Etag"); | |
HttpContext.Current.Response.Headers.Remove("Accept-Ranges"); | |
HttpContext.Current.Response.Headers.Remove("X-AspNet-Version"); | |
HttpContext.Current.Response.Headers.Remove("X-AspNetMvc-Version"); | |
} | |
} | |
private void OnPreSendRequestContent(object sender, EventArgs e) | |
{ | |
if (isMatched) | |
{ | |
App.Context.Response.ClearContent(); | |
} | |
} | |
public void Dispose() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment