用于获取由于cdn无法获取用户真实IP的方法

今天研究log4net组件,想看下公司是怎么用的,遂看了下公司的源代码,发现获取IP的方法比较特别。原来公司搞的是用可以获取因用代理无法获取用户真实IP的方法。 公司的代码如下。 [code lang="csharp"] <pre>        public static string GetUserIp         {             get             {                 string userIP = "未获取用户IP";                 try                 {                     if (HttpContext.Current == null                 || HttpContext.Current.Request == null                 || HttpContext.Current.Request.ServerVariables == null)                         return "";                     string CustomerIP = "";                     //CDN加速后取到的IP                     CustomerIP = HttpContext.Current.Request.Headers["Cdn-Src-Ip"];                     if (!string.IsNullOrEmpty(CustomerIP))                     {                         return CustomerIP;                     }                     CustomerIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];                     if (!String.IsNullOrEmpty(CustomerIP))                         return CustomerIP;                     if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)                     {                         CustomerIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];                         if (CustomerIP == null)                             CustomerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];                     }                     else                     {                         CustomerIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];                     }                     if (string.Compare(CustomerIP, "unknown", true) == 0)                         return HttpContext.Current.Request.UserHostAddress;                     return CustomerIP;                 }                 catch { }                 return userIP;             }         }</pre> [/code] 关于代理的真实IP的说明 关键就在HTTP_X_FORWARDED_FOR 使用不同种类代理服务器,上面的信息会有所不同: 一、没有使用代理服务器的情况: REMOTE_ADDR = 您的 IP HTTP_VIA = 没数值或不显示 HTTP_X_FORWARDED_FOR = 没数值或不显示 二、使用透明代理服务器的情况:Transparent Proxies REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 代理服务器 IP HTTP_X_FORWARDED_FOR = 您的真实 IP 这类代理服务器还是将您的信息转发给您的访问对象,无法达到隐藏真实身份的目的。 三、使用普通匿名代理服务器的情况:Anonymous Proxies REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 代理服务器 IP HTTP_X_FORWARDED_FOR = 代理服务器 IP 隐藏了您的真实IP,但是向访问对象透露了您是使用代理服务器访问他们的。 四、使用欺骗性代理服务器的情况:Distorting Proxies REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 代理服务器 IP HTTP_X_FORWARDED_FOR = 随机的 IP 告诉了访问对象您使用了代理服务器,但编造了一个虚假的随机IP代替您的真实IP欺骗它。 五、使用高匿名代理服务器的情况:High Anonymity Proxies (Elite proxies) REMOTE_ADDR = 代理服务器 IP HTTP_VIA = 没数值或不显示 HTTP_X_FORWARDED_FOR = 没数值或不显示
posted @ 2013-03-08 10:58  keepnode  阅读(937)  评论(0编辑  收藏  举报