Anti-leech ihttphandler

Source: Internet
Author: User
Tags website ip
/*
*
* 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.
*
*
*
* Create a pig-9-11
* Http://ewebapp.net
*/

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;

}< br> catch
{< br> // If a matching error occurs, the link is incorrect.
return true;
}< BR >}< br> else
{< br> // whether direct file access is allowed
If (ewebapp_allowonlyfile)
{< br> return false;
}< br> else
{< br> return true;
}< BR >}< br> posting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.