ASP.NET防犯チェーンソース
13142 ワード
/*
*
* 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
*/
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>
/// IHttpHandler
/// http://www.softat.org/archiver/tid-52114.html
/// 2005-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:
//
}
public void ProcessRequest(HttpContext context)
{
eWebapp_NoLink_Message = ConfigurationSettings.AppSettings["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>
///
/// </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.AppSettings["eWebapp_Error_Width"]);
if (_myErrorWidth > 0)
{
myErrorWidth = _myErrorWidth;
}
}
catch
{
}
try
{
int _myErrorHeight = Convert.ToInt32(ConfigurationSettings.AppSettings["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>
///
/// </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();
//
// 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>
///
/// </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>
///
/// </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.AppSettings["eWebapp_NoLink"];
eWebapp_AllowLink = ConfigurationSettings.AppSettings["eWebapp_AllowLink"];
try
{
eWebapp_AllowOnlyFile = Convert.ToBoolean(ConfigurationSettings.AppSettings["eWebapp_AllowOnlyFile"]);
}
catch
{
eWebapp_AllowOnlyFile = true;
}
if (context.Request.UrlReferrer != null)
{
// referDomain IP
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
{
//
try
{
Regex myRegex;
//
if (!Empty(eWebapp_AllowLink))
{
myRegex = new Regex(eWebapp_AllowLink);
if (myRegex.IsMatch(myPath))
{
return false;
}
}
//
if (!Empty(eWebapp_NoLink))
{
myRegex = new Regex(eWebapp_NoLink);
if (myRegex.IsMatch(myPath))
{
return true;
}
else
{
return false;
}
}
return true;
}
catch
{
// ,
return true;
}
}
}
else
{
//
if (eWebapp_AllowOnlyFile)
{
return false;
}
else
{
return true;
}
}
}
}
}