netutils.go 2.5 KB

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