netutils.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package geodb
  2. import (
  3. "log"
  4. "net"
  5. "net/http"
  6. "strings"
  7. )
  8. // Utilities function
  9. func GetRequesterIP(r *http.Request) string {
  10. ip := r.Header.Get("X-Real-Ip")
  11. if ip == "" {
  12. ip = r.Header.Get("X-Forwarded-For")
  13. }
  14. if ip == "" {
  15. ip = r.RemoteAddr
  16. }
  17. /*
  18. Possible shits that might be extracted by this code
  19. 127.0.0.1:61001
  20. [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7]:61002
  21. 127.0.0.1
  22. 158.250.160.114,109.21.249.211
  23. [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7],109.21.249.211
  24. We need to extract just the first ip address
  25. */
  26. requesterRawIp := ip
  27. if strings.Contains(requesterRawIp, ",") {
  28. //Trim off all the forwarder IPs
  29. requesterRawIp = strings.Split(requesterRawIp, ",")[0]
  30. }
  31. //Trim away the port number
  32. if strings.Contains(requesterRawIp, "]:") {
  33. //e.g. [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7]:61002
  34. requesterRawIp = strings.Split(requesterRawIp, "]:")[0]
  35. requesterRawIp = strings.TrimSpace(requesterRawIp[1:])
  36. //The remaining part should be a ipv6 string
  37. //e.g. 15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7
  38. if !IsIPv6(requesterRawIp) {
  39. //WTF is this?
  40. log.Println("Unknown request IPv6 extracted: " + requesterRawIp)
  41. return ""
  42. }
  43. } else if strings.Contains(requesterRawIp, ":") && strings.Contains(requesterRawIp, ".") {
  44. //e.g. 127.0.0.1:61001
  45. requesterRawIp = strings.Split(requesterRawIp, ":")[0]
  46. requesterRawIp = strings.TrimSpace(requesterRawIp)
  47. if !IsIPv4(requesterRawIp) {
  48. log.Println("Unknown request IPv4 extracted: " + requesterRawIp)
  49. return ""
  50. }
  51. } else if strings.HasPrefix(requesterRawIp, "[") && strings.HasSuffix(requesterRawIp, "]") {
  52. //e.g. [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7]
  53. requesterRawIp = requesterRawIp[1 : len(requesterRawIp)-1]
  54. }
  55. return requesterRawIp
  56. }
  57. // Match the IP address with a wildcard string
  58. func MatchIpWildcard(ipAddress, wildcard string) bool {
  59. // Split IP address and wildcard into octets
  60. ipOctets := strings.Split(ipAddress, ".")
  61. wildcardOctets := strings.Split(wildcard, ".")
  62. // Check that both have 4 octets
  63. if len(ipOctets) != 4 || len(wildcardOctets) != 4 {
  64. return false
  65. }
  66. // Check each octet to see if it matches the wildcard or is an exact match
  67. for i := 0; i < 4; i++ {
  68. if wildcardOctets[i] == "*" {
  69. continue
  70. }
  71. if ipOctets[i] != wildcardOctets[i] {
  72. return false
  73. }
  74. }
  75. return true
  76. }
  77. // Match ip address with CIDR
  78. func MatchIpCIDR(ip string, cidr string) bool {
  79. // parse the CIDR string
  80. _, cidrnet, err := net.ParseCIDR(cidr)
  81. if err != nil {
  82. return false
  83. }
  84. // parse the IP address
  85. ipAddr := net.ParseIP(ip)
  86. // check if the IP address is within the CIDR range
  87. return cidrnet.Contains(ipAddr)
  88. }
  89. // Check if a ip is private IP range
  90. func IsPrivateIP(ipStr string) bool {
  91. ip := net.ParseIP(ipStr)
  92. if ip == nil {
  93. return false
  94. }
  95. return ip.IsPrivate()
  96. }
  97. // Check if an Ip string is ipv6
  98. func IsIPv6(ipStr string) bool {
  99. ip := net.ParseIP(ipStr)
  100. if ip == nil {
  101. return false
  102. }
  103. return ip.To4() == nil && ip.To16() != nil
  104. }
  105. // Check if an Ip string is ipv6
  106. func IsIPv4(ipStr string) bool {
  107. ip := net.ParseIP(ipStr)
  108. if ip == nil {
  109. return false
  110. }
  111. return ip.To4() != nil
  112. }