/*************************************** **************
* Anti-leech IHttpHandler
*
*
* Added the selection of file keywords (I .e., filtering only some keywords or some keywords in the file name)
* Set the following values in the <etettings> section of web. config:
* String eWebapp_NoLink: if the file name complies with the correct statement, the file will be filtered (not set to filter all files)
* String eWebapp_AllowLink: if the file name complies with the correct statement, no filtering will be performed (the priority is higher than AllowLink, And the AllowLink is obeyed if the parameter is not set)
* If bool eWebapp _ AllowOnlyFile is False (true by default), it is recommended that you do not allow direct access to this file.
*
*
* The following settings can be omitted only to increase flexibility and experience.
* EWebapp_NoLink_Message error message: Link From: domain name by default
* The error message "eWebapp_Error_Width" indicates the image width.
* The error message "eWebapp_Error_Height" indicates the Image Height.
*
*
**************************************** *************/
Using System;
Using System. Web;
Using System. Drawing;
Using System. Drawing. Imaging;
Using System. IO;
Using System. Configuration;
Using System. Text. RegularExpressions;
Namespace eWebapp
{
/// <Summary>
/// Anti-leech IHttpHandler
// Reference http://www.softat.org/archiver/tid-52114.html
/// Fixed the error-9-12
/// </Summary>
Public class NoLink: IHttpHandler
{
Private string eWebapp_NoLink = string. Empty;
Private string eWebapp_AllowLink = string. Empty;
Private bool eWebapp_AllowOnlyFile = true;
Private string eWebapp_NoLink_Message = string. Empty;
Private bool error = false;
Public NoLink ()
{
//
// TODO: add the constructor logic here
//
}
Public void ProcessRequest (HttpContext context)
{
EWebapp_NoLink_Message = ConfigurationSettings. receivettings ["eWebapp_NoLink_Message"];
String myDomain = string. Empty;
Error = errorLink (context, out myDomain );
If (Empty (eWebapp_NoLink_Message ))
{
EWebapp_NoLink_Message = "Link from:" + myDomain;
}
If (error)
{
// Jpg (context. Response, eWebapp_NoLink_Message );
Jpg (context. Response, eWebapp_NoLink_Message );
}
Else
{
Real (context. Response, context. Request );
}
}
Public bool IsReusable
{
Get
{
Return true;
}
}
/// <Summary>
/// Output error message
/// </Summary>
/// <Param name = "Response"> </param>
/// <Param name = "_ word"> </param>
Private void Jpg (HttpResponse Response, string _ word)
{
Int myErrorWidth = _ word. Length * 15;
Int myErrorHeight = 16;
Try
{
Int _ myErrorWidth = Convert. ToInt32 (ConfigurationSettings. receivettings ["eWebapp_Error_Width"]);
If (_ myErrorWidth> 0)
{
MyErrorWidth = _ myErrorWidth;
}
}
Catch
{
}
Try
{
Int _ myErrorHeight = Convert. ToInt32 (ConfigurationSettings. receivettings ["eWebapp_Error_Height"]);
If (_ myErrorHeight> 0)
{
MyErrorHeight = _ myErrorHeight;
}
}
Catch
{
}
Bitmap IMG = NULL;
Graphics G = NULL;
Memorystream MS = NULL;
IMG = new Bitmap (myerrorwidth, myerrorheight );
G = graphics. fromimage (IMG );
G. Clear (color. White );
Font F = new font ("Arial", 9 );
SolidBrush s = new SolidBrush (Color. Red );
G. DrawString (_ word, f, s, 3, 3 );
MS = new MemoryStream ();
Img. Save (MS, ImageFormat. Jpeg );
Response. ClearContent ();
Response. ContentType = "image/Gif ";
Response. BinaryWrite (ms. ToArray ());
G. Dispose ();
Img. Dispose ();
Response. End ();
}
/// <Summary>
/// Output real files
/// </Summary>
/// <Param name = "response"> </param>
/// <Param name = "context"> </param>
Private void Real (HttpResponse response, HttpRequest request)
{
FileInfo file = new System. IO. FileInfo (request. PhysicalPath );
Response. Clear ();
Response. AddHeader ("Content-Disposition", "filename =" + file. Name );
Response. AddHeader ("Content-Length", file. Length. ToString ());
String fileExtension = file. Extension. ToLower ();
// Select the output file format
// For more file formats, see http://ewebapp.cnblogs.com/articles/234756.html.
Switch (fileextension)
{
Case "MP3 ":
Response. contenttype = "audio/mpeg3 ";
Break;
Case "MPEG ":
Response. contenttype = "Video/MPEG ";
Break;
Case "jpg ":
Response. contenttype = "image/JPEG ";
Break;
Case "BMP ":
Response. contenttype = "image/BMP ";
Break;
Case "GIF ":
Response. contenttype = "image/GIF ";
Break;
Case "Doc ":
Response. contenttype = "application/MSWord ";
Break;
Case "CSS ":
Response. contenttype = "text/CSS ";
Break;
Default:
Response. contenttype = "application/octet-stream ";
Break;
}
Response. WriteFile (file. FullName );
Response. End ();
}
/// <Summary>
/// Check whether the string is null
/// </Summary>
/// <Param name = "_ value"> </param>
/// <Returns> </returns>
Private bool empty (string _ value)
{
If (_ value = NULL | _ value = string. Empty | _ value = "")
{
Return true;
}
Else
{
Return false;
}
}
/// <Summary>
/// Check whether the link is illegal
/// </Summary>
/// <Param name = "context"> </param>
/// <Param name = "_ mydomain"> </param>
/// <Returns> </returns>
Private bool errorlink (httpcontext context, out string _ mydomain)
{
Httpresponse response = context. response;
String myDomain = context. Request. ServerVariables ["SERVER_NAME"];
_ MyDomain = myDomain;
String myDomainIp = context. Request. UserHostAddress;
EWebapp_NoLink = ConfigurationSettings. receivettings ["eWebapp_NoLink"];
EWebapp_AllowLink = ConfigurationSettings. receivettings ["eWebapp_AllowLink"];
Try
{
EWebapp_AllowOnlyFile = Convert. ToBoolean (ConfigurationSettings. deleettings ["eWebapp_AllowOnlyFile"]);
}
Catch
{
EWebapp_AllowOnlyFile = true;
}
If (context. Request. UrlReferrer! = Null)
{
// Determine whether the referDomain has a website IP address or domain name
String referDomain = context. Request. UrlReferrer. AbsoluteUri. Replace (context. Request. UrlReferrer. AbsolutePath ,"");
String myPath = context. Request. RawUrl;
If (referDomain. IndexOf (myDomainIp)> = 0 | referDomain. IndexOf (myDomain)> = 0)
{
Return false;
}
Else
{
// Use regular expression to match the rule
Try
{
Regex myRegex;
// Check whether matching is allowed
If (! Empty (eWebapp_AllowLink ))
{
MyRegex = new Regex (eWebapp_AllowLink );
If (myRegex. IsMatch (myPath ))
{
Return false;
}
}
// Check whether matching is prohibited
If (! Empty (eWebapp_NoLink ))
{
MyRegex = new Regex (eWebapp_NoLink );
If (myRegex. IsMatch (myPath ))
{
Return true;
}
Else
{
Return false;
}
}
Return true;
}
Catch
{
// If the match fails, the link is incorrect.
Return true;
}
}
}
Else
{
// Whether to allow direct access to files
If (eWebapp_AllowOnlyFile)
{
Return false;
}
Else
{
Return true;
}
}
}
}
}