[垃圾猪]防盗链IHttpHandler源码

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
  2InBlock.gif * 
  3InBlock.gif * 防盗链IHttpHandler
  4InBlock.gif * 
  5InBlock.gif * 
  6InBlock.gif * 增加了对文件关键字的选择(即仅对文件名存在某些关键字或不存在某些关键字进行过滤)
  7InBlock.gif * 设置web.config中<appSettings>节以下值
  8InBlock.gif * string eWebapp_NoLink    如果文件名符合该正确表态式将进行过滤(不设置对所有进行过滤)
  9InBlock.gif * string eWebapp_AllowLink            如果文件名符合该正确表态式将不进行过滤(优先权高于AllowLink,不设置则服从AllowLink)
 10InBlock.gif * booleWebapp_ AllowOnlyFile        如果为False,(默认true)则不允许用户直接对该文件进行访问建议为true
 11InBlock.gif * 
 12InBlock.gif * 
 13InBlock.gif * :)以下设置均可省略,设置只是为了增加灵活性与体验
 14InBlock.gif * eWebapp_NoLink_Message    错误信息提示:默认为Link From:域名
 15InBlock.gif * eWebapp_Error_Width        错误信息提示图片宽
 16InBlock.gif * eWebapp_Error_Height        错误信息提示图片高
 17InBlock.gif * 
 18InBlock.gif * 
 19InBlock.gif * 
 20InBlock.gif * 垃圾猪 2005-9-11 创建
 21InBlock.gif * eWebapp@163.com
 22InBlock.gif * eWebapp.cnblogs.com
 23InBlock.gif * 
 24ExpandedBlockEnd.gif */

 25 None.gif
 26 None.gif
 27 None.gif using  System;
 28 None.gif using  System.Web;
 29 None.gif using  System.Drawing;
 30 None.gif using  System.Drawing.Imaging;
 31 None.gif using  System.IO;
 32 None.gif using  System.Configuration;
 33 None.gif using  System.Text.RegularExpressions;
 34 None.gif
 35 None.gif namespace  eWebapp.NoLink
 36 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 37ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 38InBlock.gif    /// 防盗链IHttpHandler
 39InBlock.gif    ///
 40InBlock.gif    /// 垃圾猪  2005-9-12 修正
 41ExpandedSubBlockEnd.gif    /// </summary>

 42InBlock.gif    public class IHandler : IHttpHandler
 43ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 44InBlock.gif        public IHandler()
 45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 46InBlock.gif            //
 47InBlock.gif            // TODO: 在此处添加构造函数逻辑
 48InBlock.gif            //
 49ExpandedSubBlockEnd.gif        }

 50InBlock.gif
 51InBlock.gif        private string eWebapp_NoLink = string.Empty;
 52InBlock.gif        private string eWebapp_AllowLink = string.Empty;
 53InBlock.gif        private bool eWebapp_AllowOnlyFile = true;
 54InBlock.gif
 55InBlock.gif        private string eWebapp_NoLink_Message = string.Empty;
 56InBlock.gif        private bool error = false;
 57InBlock.gif
 58InBlock.gif        public void ProcessRequest(HttpContext context)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            eWebapp_NoLink_Message = ConfigurationSettings.AppSettings["eWebapp_NoLink_Message"];
 61InBlock.gif            
 62InBlock.gif            
 63InBlock.gif            string myDomain = string.Empty;
 64InBlock.gif
 65InBlock.gif            error = errorLink(context,out myDomain);    
 66InBlock.gif
 67InBlock.gif            if(Empty(eWebapp_NoLink_Message)) 
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 69InBlock.gif                eWebapp_NoLink_Message = "Link from :" + myDomain;
 70ExpandedSubBlockEnd.gif            }

 71InBlock.gif
 72InBlock.gif
 73InBlock.gif
 74InBlock.gif            if(error)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 76InBlock.gif                //Jpg(context.Response,eWebapp_NoLink_Message);
 77InBlock.gif                Jpg(context.Response,eWebapp_NoLink_Message);
 78ExpandedSubBlockEnd.gif            }

 79InBlock.gif            else
 80ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 81InBlock.gif                Real(context.Response,context.Request);
 82ExpandedSubBlockEnd.gif            }

 83InBlock.gif
 84ExpandedSubBlockEnd.gif        }

 85InBlock.gif
 86InBlock.gif        public bool IsReusable
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 88InBlock.gif            get
 89InBlock.gif
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 91InBlock.gif                return true;
 92ExpandedSubBlockEnd.gif            }

 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif
 95InBlock.gif
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 97InBlock.gif        /// 输出错误信息
 98InBlock.gif        /// </summary>
 99InBlock.gif        /// <param name="Response"></param>
100ExpandedSubBlockEnd.gif        /// <param name="_word"></param>

101InBlock.gif        private void Jpg(HttpResponse Response,string _word) 
102ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
103InBlock.gif
104InBlock.gif
105InBlock.gif            int myErrorWidth = _word.Length*15;
106InBlock.gif            int myErrorHeight = 16;
107InBlock.gif            try
108ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
109InBlock.gif                int _myErrorWidth = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Width"]);
110InBlock.gif                if(_myErrorWidth > 0 )
111ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
112InBlock.gif                    myErrorWidth = _myErrorWidth;
113ExpandedSubBlockEnd.gif                }

114InBlock.gif
115ExpandedSubBlockEnd.gif            }

116InBlock.gif            catch
117ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
118InBlock.gif
119ExpandedSubBlockEnd.gif            }

120InBlock.gif            try
121ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
122InBlock.gif                int _myErrorHeight = Convert.ToInt32(ConfigurationSettings.AppSettings["eWebapp_Error_Height"]);
123InBlock.gif                if(_myErrorHeight  > 0 )
124ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
125InBlock.gif                    myErrorHeight = _myErrorHeight;
126ExpandedSubBlockEnd.gif                }

127ExpandedSubBlockEnd.gif            }

128InBlock.gif            catch
129ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
130InBlock.gif
131ExpandedSubBlockEnd.gif            }

132InBlock.gif            Bitmap Img=null;
133InBlock.gif            Graphics g=null;
134InBlock.gif            MemoryStream ms=null;
135InBlock.gif            Img=new Bitmap(myErrorWidth,myErrorHeight);
136InBlock.gif            g=Graphics.FromImage(Img);
137InBlock.gif            g.Clear(Color.White);
138InBlock.gif            Font f=new Font("Arial",9);
139InBlock.gif            SolidBrush s=new SolidBrush(Color.Red);
140InBlock.gif            g.DrawString(_word,f,s,3,3);
141InBlock.gif            ms=new MemoryStream();
142InBlock.gif            Img.Save(ms,ImageFormat.Jpeg);
143InBlock.gif            Response.ClearContent(); 
144InBlock.gif            Response.ContentType="image/Gif";
145InBlock.gif            Response.BinaryWrite(ms.ToArray());
146InBlock.gif            g.Dispose();
147InBlock.gif            Img.Dispose();
148InBlock.gif            Response.End();
149ExpandedSubBlockEnd.gif        }

150InBlock.gif
151ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
152InBlock.gif        /// 输出真实文件
153InBlock.gif        /// </summary>
154InBlock.gif        /// <param name="response"></param>
155ExpandedSubBlockEnd.gif        /// <param name="context"></param>

156InBlock.gif        private void Real(HttpResponse response,HttpRequest request)
157ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
158InBlock.gif            FileInfo file = new System.IO.FileInfo(request.PhysicalPath);
159InBlock.gif
160InBlock.gif            response.Clear();
161InBlock.gif
162InBlock.gif            response.AddHeader("Content-Disposition""filename=" + file.Name);
163InBlock.gif
164InBlock.gif            response.AddHeader("Content-Length", file.Length.ToString());
165InBlock.gif
166InBlock.gif            string fileExtension = file.Extension.ToLower();
167InBlock.gif
168InBlock.gif
169InBlock.gif            //这里选择输出的文件格式
170InBlock.gif            //可以参考http://ewebapp.cnblogs.com/articles/234756.html增加对更多文件格式的支持.
171InBlock.gif
172InBlock.gif            
173InBlock.gif            switch (fileExtension)
174ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
175InBlock.gif
176InBlock.gif                case "mp3":
177InBlock.gif                    response.ContentType = "audio/mpeg3";
178InBlock.gif                    break;
179InBlock.gif
180InBlock.gif                case "mpeg":
181InBlock.gif
182InBlock.gif                    response.ContentType = "video/mpeg";
183InBlock.gif                    break;
184InBlock.gif
185InBlock.gif                case "jpg":
186InBlock.gif
187InBlock.gif                    response.ContentType = "image/jpeg";
188InBlock.gif                    break;
189InBlock.gif
190InBlock.gif                case "bmp":
191InBlock.gif
192InBlock.gif                    response.ContentType = "image/bmp";
193InBlock.gif                    break;
194InBlock.gif
195InBlock.gif                case "gif":
196InBlock.gif
197InBlock.gif                    response.ContentType = "image/gif";
198InBlock.gif                    break;
199InBlock.gif
200InBlock.gif                case "doc":
201InBlock.gif
202InBlock.gif                    response.ContentType = "application/msword";
203InBlock.gif
204InBlock.gif                    break;
205InBlock.gif                case "css":
206InBlock.gif
207InBlock.gif                    response.ContentType = "text/css";
208InBlock.gif                    break;
209InBlock.gif
210InBlock.gif                default:
211InBlock.gif
212InBlock.gif                    response.ContentType = "application/octet-stream";
213InBlock.gif                    break;
214InBlock.gif
215ExpandedSubBlockEnd.gif            }

216InBlock.gif            
217InBlock.gif
218InBlock.gif            response.WriteFile(file.FullName);
219InBlock.gif
220InBlock.gif            response.End();
221ExpandedSubBlockEnd.gif        }

222InBlock.gif
223InBlock.gif
224ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
225InBlock.gif        /// 确认字符串是否为空
226InBlock.gif        /// </summary>
227InBlock.gif        /// <param name="_value"></param>
228ExpandedSubBlockEnd.gif        /// <returns></returns>

229InBlock.gif        private bool Empty(string _value)
230ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
231InBlock.gif            if(_value == null | _value == string.Empty | _value == "")
232ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
233InBlock.gif                return true;
234ExpandedSubBlockEnd.gif            }

235InBlock.gif            else
236ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
237InBlock.gif                return false;
238ExpandedSubBlockEnd.gif            }

239ExpandedSubBlockEnd.gif        }

240InBlock.gif
241InBlock.gif
242ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
243InBlock.gif        /// 检查是否是非法链接
244InBlock.gif        /// </summary>
245InBlock.gif        /// <param name="context"></param>
246InBlock.gif        /// <param name="_myDomain"></param>
247ExpandedSubBlockEnd.gif        /// <returns></returns>

248InBlock.gif        private bool errorLink(HttpContext context,out string _myDomain)
249ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
250InBlock.gif            HttpResponse response = context.Response;
251InBlock.gif            string myDomain = context.Request.ServerVariables["SERVER_NAME"];
252InBlock.gif            _myDomain = myDomain ;
253InBlock.gif            string myDomainIp = context.Request.UserHostAddress;
254InBlock.gif
255InBlock.gif
256InBlock.gif            eWebapp_NoLink = ConfigurationSettings.AppSettings["eWebapp_NoLink"];
257InBlock.gif            eWebapp_AllowLink = ConfigurationSettings.AppSettings["eWebapp_AllowLink"];
258InBlock.gif
259InBlock.gif            try
260ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
261InBlock.gif                eWebapp_AllowOnlyFile = Convert.ToBoolean(ConfigurationSettings.AppSettings["eWebapp_AllowOnlyFile"]);
262ExpandedSubBlockEnd.gif            }

263InBlock.gif            catch
264ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
265InBlock.gif                eWebapp_AllowOnlyFile = true;
266ExpandedSubBlockEnd.gif            }

267InBlock.gif
268InBlock.gif
269InBlock.gif            if(context.Request.UrlReferrer != null)
270ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
271InBlock.gif
272InBlock.gif                
273InBlock.gif                //判定referDomain是否存在网站的IP或域名
274InBlock.gif                string referDomain = context.Request.UrlReferrer.AbsoluteUri.Replace(context.Request.UrlReferrer.AbsolutePath,"");
275InBlock.gif                string myPath  = context.Request.RawUrl;
276InBlock.gif
277InBlock.gif                if(referDomain.IndexOf(myDomainIp) >=0 | referDomain.IndexOf(myDomain)>=0)
278ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
279InBlock.gif                    return false;
280ExpandedSubBlockEnd.gif                }

281InBlock.gif                else
282ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
283InBlock.gif                    //这里使用正则表达对规则进行匹配
284InBlock.gif                    try
285ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
286InBlock.gif                        Regex myRegex ;
287InBlock.gif
288InBlock.gif                        //检查允许匹配
289InBlock.gif                        if(!Empty(eWebapp_AllowLink))
290ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
291InBlock.gif                            
292InBlock.gif                            myRegex = new Regex(eWebapp_AllowLink);
293InBlock.gif
294InBlock.gif                            if(myRegex.IsMatch(myPath))
295ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
296InBlock.gif                                return false;
297ExpandedSubBlockEnd.gif                            }

298InBlock.gif
299ExpandedSubBlockEnd.gif                        }

300InBlock.gif
301InBlock.gif
302InBlock.gif                        //检查禁止匹配
303InBlock.gif                        if(!Empty(eWebapp_NoLink))
304ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
305InBlock.gif
306InBlock.gif                            myRegex = new Regex(eWebapp_NoLink);
307InBlock.gif                            if(myRegex.IsMatch(myPath))
308ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
309InBlock.gif                                return true;
310ExpandedSubBlockEnd.gif                            }

311InBlock.gif                            else
312ExpandedSubBlockStart.gifContractedSubBlock.gif                            dot.gif{
313InBlock.gif                                return false;
314ExpandedSubBlockEnd.gif                            }

315InBlock.gif
316ExpandedSubBlockEnd.gif                        }

317InBlock.gif
318InBlock.gif                        return true;
319InBlock.gif
320ExpandedSubBlockEnd.gif                    }

321InBlock.gif                    catch
322ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
323InBlock.gif                        //如果匹配出错,链接错误
324InBlock.gif                        return true;
325ExpandedSubBlockEnd.gif                    }

326ExpandedSubBlockEnd.gif                }

327ExpandedSubBlockEnd.gif            }

328InBlock.gif            else
329ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
330InBlock.gif                //是否允许直接访问文件
331InBlock.gif                if(eWebapp_AllowOnlyFile)
332ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
333InBlock.gif                    return false;
334ExpandedSubBlockEnd.gif                }

335InBlock.gif                else
336ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
337InBlock.gif                    return true;
338ExpandedSubBlockEnd.gif                }

339ExpandedSubBlockEnd.gif            }

340InBlock.gif
341ExpandedSubBlockEnd.gif        }

342ExpandedSubBlockEnd.gif    }

343ExpandedBlockEnd.gif}

344 None.gif

转载于:https://www.cnblogs.com/coolylh/archive/2005/12/09/293759.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值