![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
*
* 防盗链IHttpHandler
*
*
* 增加了对文件关键字的选择(即仅对文件名存在某些关键字或不存在某些关键字进行过滤)
* 设置web.config中<appSettings>节以下值
* string eWebapp_NoLink 如果文件名符合该正确表态式将进行过滤(不设置对所有进行过滤)
* string eWebapp_AllowLink 如果文件名符合该正确表态式将不进行过滤(优先权高于AllowLink,不设置则服从AllowLink)
* bool eWebapp_ AllowOnlyFile 如果为False,(默认true)则不允许用户直接对该文件进行访问建议为true
*
*
* :)以下设置均可省略,设置只是为了增加灵活性与体验
* eWebapp_NoLink_Message 错误信息提示:默认为Link From:域名
* eWebapp_Error_Width 错误信息提示图片宽
* eWebapp_Error_Height 错误信息提示图片高
*
*
*
* 2005-9-11 创建
* http://ewebapp.net
*/
![](/Images/OutliningIndicators/None.gif)
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Configuration;
using System.Text.RegularExpressions;
![](/Images/OutliningIndicators/None.gif)
namespace eWebapp
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 防盗链IHttpHandler
/// 参考http://www.softat.org/archiver/tid-52114.html
/// 垃圾猪 2005-9-12 修正
/// </summary>
public class NoLink : IHttpHandler
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private string eWebapp_NoLink = string.Empty;
private string eWebapp_AllowLink = string.Empty;
private bool eWebapp_AllowOnlyFile = true;
![](/Images/OutliningIndicators/InBlock.gif)
private string eWebapp_NoLink_Message = string.Empty;
private bool error = false;
![](/Images/OutliningIndicators/InBlock.gif)
public NoLink()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![](/Images/OutliningIndicators/InBlock.gif)
public void ProcessRequest(HttpContext context)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
eWebapp_NoLink_Message = ConfigurationSettings.AppSettings["eWebapp_NoLink_Message"];
![](/Images/OutliningIndicators/InBlock.gif)
string myDomain = string.Empty;
![](/Images/OutliningIndicators/InBlock.gif)
error = errorLink(context, out myDomain);
![](/Images/OutliningIndicators/InBlock.gif)
if (Empty(eWebapp_NoLink_Message))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
eWebapp_NoLink_Message = "Link from :" + myDomain;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (error)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Jpg(context.Response,eWebapp_NoLink_Message);
Jpg(context.Response, eWebapp_NoLink_Message);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Real(context.Response, context.Request);
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
public bool IsReusable
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 输出错误信息
/// </summary>
/// <param name="Response"></param>
/// <param name="_word"></param>
private void Jpg(HttpResponse Response, string _word)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
int myErrorWidth = _word.Length * 15;
int myErrorHeight = 16;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int _myErrorWidth = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Width"]);
if (_myErrorWidth > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myErrorWidth = _myErrorWidth;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int _myErrorHeight = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Height"]);
if (_myErrorHeight > 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myErrorHeight = _myErrorHeight;
}
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
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();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 输出真实文件
/// </summary>
/// <param name="response"></param>
/// <param name="context"></param>
private void Real(HttpResponse response, HttpRequest request)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileInfo file = new System.IO.FileInfo(request.PhysicalPath);
![](/Images/OutliningIndicators/InBlock.gif)
response.Clear();
![](/Images/OutliningIndicators/InBlock.gif)
response.AddHeader("Content-Disposition", "filename=" + file.Name);
![](/Images/OutliningIndicators/InBlock.gif)
response.AddHeader("Content-Length", file.Length.ToString());
![](/Images/OutliningIndicators/InBlock.gif)
string fileExtension = file.Extension.ToLower();
![](/Images/OutliningIndicators/InBlock.gif)
//这里选择输出的文件格式
//可以参考http://ewebapp.cnblogs.com/articles/234756.html增加对更多文件格式的支持.
![](/Images/OutliningIndicators/InBlock.gif)
switch (fileExtension)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
case "mp3":
response.ContentType = "audio/mpeg3";
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "mpeg":
![](/Images/OutliningIndicators/InBlock.gif)
response.ContentType = "video/mpeg";
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "jpg":
![](/Images/OutliningIndicators/InBlock.gif)
response.ContentType = "image/jpeg";
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "bmp":
![](/Images/OutliningIndicators/InBlock.gif)
response.ContentType = "image/bmp";
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "gif":
![](/Images/OutliningIndicators/InBlock.gif)
response.ContentType = "image/gif";
break;
![](/Images/OutliningIndicators/InBlock.gif)
case "doc":
![](/Images/OutliningIndicators/InBlock.gif)
response.ContentType = "application/msword";
![](/Images/OutliningIndicators/InBlock.gif)
break;
case "css":
![](/Images/OutliningIndicators/InBlock.gif)
response.ContentType = "text/css";
break;
![](/Images/OutliningIndicators/InBlock.gif)
default:
![](/Images/OutliningIndicators/InBlock.gif)
response.ContentType = "application/octet-stream";
break;
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
response.WriteFile(file.FullName);
![](/Images/OutliningIndicators/InBlock.gif)
response.End();
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 确认字符串是否为空
/// </summary>
/// <param name="_value"></param>
/// <returns></returns>
private bool Empty(string _value)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_value == null | _value == string.Empty | _value == "")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 检查是否是非法链接
/// </summary>
/// <param name="context"></param>
/// <param name="_myDomain"></param>
/// <returns></returns>
private bool errorLink(HttpContext context, out string _myDomain)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpResponse response = context.Response;
string myDomain = context.Request.ServerVariables["SERVER_NAME"];
_myDomain = myDomain;
string myDomainIp = context.Request.UserHostAddress;
![](/Images/OutliningIndicators/InBlock.gif)
eWebapp_NoLink = ConfigurationSettings.AppSettings["eWebapp_NoLink"];
eWebapp_AllowLink = ConfigurationSettings.AppSettings["eWebapp_AllowLink"];
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
eWebapp_AllowOnlyFile = Convert.ToBoolean(ConfigurationSettings.AppSettings["eWebapp_AllowOnlyFile"]);
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
eWebapp_AllowOnlyFile = true;
}
![](/Images/OutliningIndicators/InBlock.gif)
if (context.Request.UrlReferrer != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
//判定referDomain是否存在网站的IP或域名
string referDomain = context.Request.UrlReferrer.AbsoluteUri.Replace(context.Request.UrlReferrer.AbsolutePath, "");
string myPath = context.Request.RawUrl;
![](/Images/OutliningIndicators/InBlock.gif)
if (referDomain.IndexOf(myDomainIp) >= 0 | referDomain.IndexOf(myDomain) >= 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//这里使用正则表达对规则进行匹配
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Regex myRegex;
![](/Images/OutliningIndicators/InBlock.gif)
//检查允许匹配
if (!Empty(eWebapp_AllowLink))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
myRegex = new Regex(eWebapp_AllowLink);
![](/Images/OutliningIndicators/InBlock.gif)
if (myRegex.IsMatch(myPath))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
//检查禁止匹配
if (!Empty(eWebapp_NoLink))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
myRegex = new Regex(eWebapp_NoLink);
if (myRegex.IsMatch(myPath))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
return true;
![](/Images/OutliningIndicators/InBlock.gif)
}
catch
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//如果匹配出错,链接错误
return true;
}
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//是否允许直接访问文件
if (eWebapp_AllowOnlyFile)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
posted @
2007-10-23 23:03
zhangh
阅读(
172)
评论()
编辑
收藏
举报