The next generation of the Teknik Services. Written in ASP.NET. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestHelper.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. namespace Teknik.Helpers
  10. {
  11. public static class RequestHelper
  12. {
  13. /// <summary>
  14. /// method to get Client ip address
  15. /// </summary>
  16. /// <param name="GetLan"> set to true if want to get local(LAN) Connected ip address</param>
  17. /// <returns></returns>
  18. public static string GetVisitorIPAddress(bool GetLan = false)
  19. {
  20. string visitorIPAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  21. if (String.IsNullOrEmpty(visitorIPAddress))
  22. visitorIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  23. if (string.IsNullOrEmpty(visitorIPAddress))
  24. visitorIPAddress = HttpContext.Current.Request.UserHostAddress;
  25. if (string.IsNullOrEmpty(visitorIPAddress) || visitorIPAddress.Trim() == "::1")
  26. {
  27. GetLan = true;
  28. visitorIPAddress = string.Empty;
  29. }
  30. if (GetLan)
  31. {
  32. if (string.IsNullOrEmpty(visitorIPAddress))
  33. {
  34. //This is for Local(LAN) Connected ID Address
  35. string stringHostName = Dns.GetHostName();
  36. //Get Ip Host Entry
  37. IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName);
  38. //Get Ip Address From The Ip Host Entry Address List
  39. IPAddress[] arrIpAddress = ipHostEntries.AddressList;
  40. try
  41. {
  42. visitorIPAddress = arrIpAddress[arrIpAddress.Length - 2].ToString();
  43. }
  44. catch
  45. {
  46. try
  47. {
  48. visitorIPAddress = arrIpAddress[0].ToString();
  49. }
  50. catch
  51. {
  52. try
  53. {
  54. arrIpAddress = Dns.GetHostAddresses(stringHostName);
  55. visitorIPAddress = arrIpAddress[0].ToString();
  56. }
  57. catch
  58. {
  59. visitorIPAddress = "127.0.0.1";
  60. }
  61. }
  62. }
  63. }
  64. }
  65. return visitorIPAddress;
  66. }
  67. // based on http://www.grantburton.com/2008/11/30/fix-for-incorrect-ip-addresses-in-wordpress-comments/
  68. public static string ClientIPFromRequest(this HttpRequestBase request, bool skipPrivate)
  69. {
  70. foreach (var item in s_HeaderItems)
  71. {
  72. var ipString = request.Headers[item.Key];
  73. if (String.IsNullOrEmpty(ipString))
  74. continue;
  75. if (item.Split)
  76. {
  77. foreach (var ip in ipString.Split(','))
  78. if (ValidIP(ip, skipPrivate))
  79. return ip;
  80. }
  81. else
  82. {
  83. if (ValidIP(ipString, skipPrivate))
  84. return ipString;
  85. }
  86. }
  87. return request.UserHostAddress;
  88. }
  89. public static string DumpHeaders(this HttpRequestBase request)
  90. {
  91. var headers = string.Empty;
  92. foreach (var key in request.Headers.AllKeys)
  93. headers += key + "=" + request.Headers[key] + Environment.NewLine;
  94. return headers;
  95. }
  96. public static string DumpServerVariables(this HttpRequestBase request)
  97. {
  98. var variables = string.Empty;
  99. foreach (var key in request.ServerVariables.AllKeys)
  100. variables += key + "=" + request.ServerVariables[key] + Environment.NewLine;
  101. return variables;
  102. }
  103. private static bool ValidIP(string ip, bool skipPrivate)
  104. {
  105. IPAddress ipAddr;
  106. ip = ip == null ? String.Empty : ip.Trim();
  107. if (0 == ip.Length
  108. || false == IPAddress.TryParse(ip, out ipAddr)
  109. || (ipAddr.AddressFamily != AddressFamily.InterNetwork
  110. && ipAddr.AddressFamily != AddressFamily.InterNetworkV6))
  111. return false;
  112. if (skipPrivate && ipAddr.AddressFamily == AddressFamily.InterNetwork)
  113. {
  114. var addr = IpRange.AddrToUInt64(ipAddr);
  115. foreach (var range in s_PrivateRanges)
  116. {
  117. if (range.Encompasses(addr))
  118. return false;
  119. }
  120. }
  121. return true;
  122. }
  123. /// <summary>
  124. /// Provides a simple class that understands how to parse and
  125. /// compare IP addresses (IPV4) ranges.
  126. /// </summary>
  127. private sealed class IpRange
  128. {
  129. private readonly UInt64 _start;
  130. private readonly UInt64 _end;
  131. public IpRange(string startStr, string endStr)
  132. {
  133. _start = ParseToUInt64(startStr);
  134. _end = ParseToUInt64(endStr);
  135. }
  136. public static UInt64 AddrToUInt64(IPAddress ip)
  137. {
  138. var ipBytes = ip.GetAddressBytes();
  139. UInt64 value = 0;
  140. foreach (var abyte in ipBytes)
  141. {
  142. value <<= 8; // shift
  143. value += abyte;
  144. }
  145. return value;
  146. }
  147. public static UInt64 ParseToUInt64(string ipStr)
  148. {
  149. var ip = IPAddress.Parse(ipStr);
  150. return AddrToUInt64(ip);
  151. }
  152. public bool Encompasses(UInt64 addrValue)
  153. {
  154. return _start <= addrValue && addrValue <= _end;
  155. }
  156. public bool Encompasses(IPAddress addr)
  157. {
  158. var value = AddrToUInt64(addr);
  159. return Encompasses(value);
  160. }
  161. };
  162. private static readonly IpRange[] s_PrivateRanges =
  163. new IpRange[] {
  164. new IpRange("0.0.0.0","2.255.255.255"),
  165. new IpRange("10.0.0.0","10.255.255.255"),
  166. new IpRange("127.0.0.0","127.255.255.255"),
  167. new IpRange("169.254.0.0","169.254.255.255"),
  168. new IpRange("172.16.0.0","172.31.255.255"),
  169. new IpRange("192.0.2.0","192.0.2.255"),
  170. new IpRange("192.168.0.0","192.168.255.255"),
  171. new IpRange("255.255.255.0","255.255.255.255")
  172. };
  173. /// <summary>
  174. /// Describes a header item (key) and if it is expected to be
  175. /// a comma-delimited string
  176. /// </summary>
  177. private sealed class HeaderItem
  178. {
  179. public readonly string Key;
  180. public readonly bool Split;
  181. public HeaderItem(string key, bool split)
  182. {
  183. Key = key;
  184. Split = split;
  185. }
  186. }
  187. // order is in trust/use order top to bottom
  188. private static readonly HeaderItem[] s_HeaderItems =
  189. new HeaderItem[] {
  190. new HeaderItem("HTTP_CLIENT_IP",false),
  191. new HeaderItem("HTTP_X_FORWARDED_FOR",true),
  192. new HeaderItem("HTTP_X_FORWARDED",false),
  193. new HeaderItem("HTTP_X_CLUSTER_CLIENT_IP",false),
  194. new HeaderItem("HTTP_FORWARDED_FOR",false),
  195. new HeaderItem("HTTP_FORWARDED",false),
  196. new HeaderItem("HTTP_VIA",false),
  197. new HeaderItem("REMOTE_ADDR",false)
  198. };
  199. }
  200. }