国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

圖片地址防盜鏈,通過IHttpHandler實(shí)現(xiàn)

2019-11-18 16:56:17
字體:
供稿:網(wǎng)友

/*
 * 
 * 防盜鏈IHttpHandler
 * 
 * 
 * 增加了對(duì)文件關(guān)鍵字的選擇(即僅對(duì)文件名存在某些關(guān)鍵字或不存在某些關(guān)鍵字進(jìn)行過濾)
 * 設(shè)置web.config中<appSettings>節(jié)以下值
 * string eWebapp_NoLink    如果文件名符合該正確表態(tài)式將進(jìn)行過濾(不設(shè)置對(duì)所有進(jìn)行過濾)
 * string eWebapp_AllowLink            如果文件名符合該正確表態(tài)式將不進(jìn)行過濾(優(yōu)先權(quán)高于AllowLink,不設(shè)置則服從AllowLink)
 * bool eWebapp_ AllowOnlyFile        如果為False,(默認(rèn)true)則不允許用戶直接對(duì)該文件進(jìn)行訪問建議為true
 * 
 * 
 * :)以下設(shè)置均可省略,設(shè)置只是為了增加靈活性與體驗(yàn)
 * eWebapp_NoLink_Message    錯(cuò)誤信息提示:默認(rèn)為Link From:域名
 * eWebapp_Error_Width        錯(cuò)誤信息提示圖片寬
 * eWebapp_Error_Height        錯(cuò)誤信息提示圖片高
 * 
 * 
 * 
 *
 * 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
    ///
    /// </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: 在此處添加構(gòu)造函數(shù)邏輯
            //
        }

        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>
        /// 輸出錯(cuò)誤信息
        /// </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>
        /// 輸出真實(shí)文件
        /// </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.VEVb.com/articles/234756.html增加對(duì)更多文件格式的支持.

            
            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>
        /// 確認(rèn)字符串是否為空
        /// </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是否存在網(wǎng)站的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
                {
                    //這里使用正則表達(dá)對(duì)規(guī)則進(jìn)行匹配
                    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
                    {
                        //如果匹配出錯(cuò),鏈接錯(cuò)誤
                        return true;
                    }
                }
            }
            else
            {
                //是否允許直接訪問文件
                if(eWebapp_AllowOnlyFile)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }

        }

    }

}
http://m.survivalescaperooms.com/zhouxujian/archive/2006/10/12/527673.html


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹巴县| 四川省| 闵行区| 东方市| 郯城县| 天峨县| 皮山县| 庄河市| 德清县| 顺平县| 囊谦县| 平泉县| 大竹县| 奉化市| 遵化市| 嘉祥县| 平罗县| 乌什县| 丹凤县| 赤峰市| 博爱县| 岳池县| 济阳县| 长垣县| 梅河口市| 盱眙县| 开阳县| 临潭县| 慈利县| 和平县| 铜鼓县| 邢台市| 海南省| 文山县| 建瓯市| 泊头市| 称多县| 始兴县| 安多县| 屏山县| 礼泉县|