geoloader.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package geodb
  2. import (
  3. "bytes"
  4. "encoding/csv"
  5. "io"
  6. "net"
  7. "strings"
  8. )
  9. func (s *Store) search(ip string) string {
  10. if strings.Contains(ip, ",") {
  11. //This is a CF proxied request. We only need the front part
  12. //Example 219.71.102.145, 172.71.139.178
  13. ip = strings.Split(ip, ",")[0]
  14. ip = strings.TrimSpace(ip)
  15. }
  16. //See if there are cached country code for this ip
  17. ccc, ok := s.geoipCache.Load(ip)
  18. if ok {
  19. return ccc.(string)
  20. }
  21. for _, entry := range s.geodb {
  22. startIp := entry[0]
  23. endIp := entry[1]
  24. cc := entry[2]
  25. if isIPInRange(ip, startIp, endIp) {
  26. //Store results in cache
  27. s.geoipCache.Store(ip, cc)
  28. return cc
  29. }
  30. }
  31. //Not found
  32. return ""
  33. }
  34. // Construct the trie data structure for quick lookup
  35. func constrctTrieTree(data [][]string) *trie {
  36. tt := newTrie()
  37. for _, entry := range data {
  38. startIp := entry[0]
  39. endIp := entry[1]
  40. cc := entry[2]
  41. tt.insert(startIp, cc)
  42. tt.insert(endIp, cc)
  43. }
  44. return tt
  45. }
  46. // Parse the embedded csv as ipstart, ipend and country code entries
  47. func parseCSV(content []byte) ([][]string, error) {
  48. var records [][]string
  49. r := csv.NewReader(bytes.NewReader(content))
  50. for {
  51. record, err := r.Read()
  52. if err == io.EOF {
  53. break
  54. }
  55. if err != nil {
  56. return nil, err
  57. }
  58. records = append(records, record)
  59. }
  60. return records, nil
  61. }
  62. // Check if a ip string is within the range of two others
  63. func isIPInRange(ip, start, end string) bool {
  64. ipAddr := net.ParseIP(ip)
  65. if ipAddr == nil {
  66. return false
  67. }
  68. startAddr := net.ParseIP(start)
  69. if startAddr == nil {
  70. return false
  71. }
  72. endAddr := net.ParseIP(end)
  73. if endAddr == nil {
  74. return false
  75. }
  76. if ipAddr.To4() == nil || startAddr.To4() == nil || endAddr.To4() == nil {
  77. return false
  78. }
  79. return bytes.Compare(ipAddr.To4(), startAddr.To4()) >= 0 && bytes.Compare(ipAddr.To4(), endAddr.To4()) <= 0
  80. }