C # real IP address retrieval and analysis

Source: Internet
Author: User
Tags servervariables

Let's talk about it. I also transferred it here, not to lie to PV, so it's easy for me to check it!

There are bugs in the so-called "getting real IP addresses" method on the Internet, and the multi-layer transparent proxy is not taken into account.
Most codes are similar:

String IPaddress = (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL
& Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = String. Empty)
? Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]
: Httpcontext. Current. Request. servervariables ["remote_addr"];


In fact, the above Code only tries to use a layer-1 proxy with the user. if the user has a layer-2, layer-3 http_x_forwarded_for value is: "The real IP address of the Local Machine, layer-1 proxy IP address, layer-2 proxy IP address ,..... ", if the length of the IP field stored in your data is very small (15 bytes), the database reports an error.

In practice, there are few users who use multi-layer transparent proxy.

In other cases, more and more websites are using proxy acceleration. For example, Sina and Sohu news all use squid as proxy and use multiple servers for traffic distribution. Squid itself is similar to a transparent proxy. It will send "http_x_forwarded_for". http_x_forwarded_for includes the customer's IP address. If the customer already uses a transparent proxy, the "http_x_forwarded_for" obtained by the program includes two IP addresses. (I have encountered 3 IP addresses and 4 have not)

Therefore, to obtain the "real" IP address, you should also determine whether "," comma "exists in" http_x_forwarded_for ", or whether the length is too long (more than 15 bytes of XXX. XXX ).

The Code should be as follows:

 

[CSHARP]View plaincopy
  1. /** // <Summary>
  2. /// Obtain the real IP address of the client. If a proxy exists, the first non-Intranet address is used.
  3. /// </Summary>
  4. Public static string IPaddress
  5. {
  6. Get
  7. {
  8. String result = string. empty;
  9. Result = httpcontext. Current. Request. servervariables ["http_x_forwarded_for"];
  10. If (result! = NULL & result! = String. Empty)
  11. {
  12. // A proxy may exist.
  13. If (result. indexof (".") =-1) // No "." is definitely not in IPv4 format
  14. Result = NULL;
  15. Else
  16. {
  17. If (result. indexof (",")! =-1)
  18. {
  19. // There are ",", multiple proxies are estimated. Obtain the first IP address that is not an intranet IP address.
  20. Result = result. Replace ("", ""). Replace (""","");
  21. String [] temparyip = result. Split (",;". tochararray ());
  22. For (INT I = 0; I <temparyip. length; I ++)
  23. {
  24. If (text. isipaddress (temparyip [I])
  25. & Temparyip [I]. substring (0, 3 )! = "10 ."
  26. & Temparyip [I]. substring (0, 7 )! = "192.168"
  27. & Temparyip [I]. substring (0, 7 )! = "172.16 .")
  28. {
  29. Return temparyip [I]; // locate an address that is not an intranet address
  30. }
  31. }
  32. }
  33. Else if (text. isipaddress (result) // The proxy is in the IP Format.
  34. Return result;
  35. Else
  36. Result = NULL; // the content in the proxy is not an IP address and the IP address is used.
  37. }
  38. }
  39. String IPaddress = (httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = NULL & httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]! = String. Empty )? Httpcontext. Current. Request. servervariables ["http_x_forwarded_for"]: httpcontext. Current. Request. servervariables ["remote_addr"];
  40. If (null = Result | result = string. Empty)
  41. Result = httpcontext. Current. Request. servervariables ["remote_addr"];
  42. If (result = NULL | result = string. Empty)
  43. Result = httpcontext. Current. Request. userhostaddress;
  44. Return result;
  45. }
  46. }


Disadvantages of "http_x_forwarded_for.

Http_x_forwarded_for is part of the HTTP header and does not affect TCP communication. In other words, the client can actually send http_x_forwarded_for any content, which is a counterfeit IP address. The simplest is the IP address record of the web program, which was originally intended to record the real IP address. Instead, it was cheated by hackers. When your application records the client's access IP address, denies or permits access from some IP addresses, error logs may occur, or even kill by mistake.

Therefore, the necessary security logs should record the complete "http_x_forwarded_for" (at least 3*15 + 2 bytes are allocated to the fields in the database to record at least 3 IP addresses) and "remote_addr ". It is also essential to check the IP Format of http_x_forwarded_for.

Appendix :( text is a custom class, And isipaddress is one of the methods to determine whether it is an IP address format)

[CSHARP]View plaincopy
  1. # Region bool isipaddress (str1) judge whether it is an IP Format
  2. /** // <Summary>
  3. /// Determine whether the IP address format is 0.0.0.0
  4. /// </Summary>
  5. /// <Param name = "str1"> IP address to be determined </param>
  6. /// <Returns> true or false </returns>
  7. Public static bool isipaddress (string str1)
  8. {
  9. If (str1 = NULL | str1 = string. Empty | str1.length <7 | str1.length> 15) return false;
  10. String regformat = @ "^ \ D {1, 3} [\.] \ D {1, 3} [\.] \ D {1, 3} [\.] \ D {1, 3} $ ";
  11. RegEx = new RegEx (regformat, regexoptions. ignorecase );
  12. Return RegEx. ismatch (str1 );
  13. }
  14. # Endregion


C # real IP address retrieval and analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.