geoip.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package main
  2. import (
  3. "github.com/abh/geodns/Godeps/_workspace/src/github.com/abh/geoip"
  4. "github.com/abh/geodns/countries"
  5. "log"
  6. "net"
  7. "strings"
  8. "time"
  9. )
  10. type GeoIP struct {
  11. country *geoip.GeoIP
  12. hasCountry bool
  13. countryLastLoad time.Time
  14. city *geoip.GeoIP
  15. cityLastLoad time.Time
  16. hasCity bool
  17. asn *geoip.GeoIP
  18. hasAsn bool
  19. asnLastLoad time.Time
  20. }
  21. var geoIP = new(GeoIP)
  22. func (g *GeoIP) GetCountry(ip net.IP) (country, continent string, netmask int) {
  23. if g.country == nil {
  24. return "", "", 0
  25. }
  26. country, netmask = geoIP.country.GetCountry(ip.String())
  27. if len(country) > 0 {
  28. country = strings.ToLower(country)
  29. continent = countries.CountryContinent[country]
  30. }
  31. return
  32. }
  33. func (g *GeoIP) GetCountryRegion(ip net.IP) (country, continent, regionGroup, region string, netmask int) {
  34. if g.city == nil {
  35. log.Println("No city database available")
  36. country, continent, netmask = g.GetCountry(ip)
  37. return
  38. }
  39. record := geoIP.city.GetRecord(ip.String())
  40. if record == nil {
  41. return
  42. }
  43. country = record.CountryCode
  44. region = record.Region
  45. if len(country) > 0 {
  46. country = strings.ToLower(country)
  47. continent = countries.CountryContinent[country]
  48. if len(region) > 0 {
  49. region = country + "-" + strings.ToLower(region)
  50. regionGroup = countries.CountryRegionGroup(country, region)
  51. }
  52. }
  53. return
  54. }
  55. func (g *GeoIP) GetASN(ip net.IP) (asn string, netmask int) {
  56. if g.asn == nil {
  57. log.Println("No asn database available")
  58. return
  59. }
  60. name, netmask := g.asn.GetName(ip.String())
  61. if len(name) > 0 {
  62. index := strings.Index(name, " ")
  63. if index > 0 {
  64. asn = strings.ToLower(name[:index])
  65. }
  66. }
  67. return
  68. }
  69. func (g *GeoIP) setDirectory() {
  70. if len(Config.GeoIP.Directory) > 0 {
  71. geoip.SetCustomDirectory(Config.GeoIP.Directory)
  72. }
  73. }
  74. func (g *GeoIP) setupGeoIPCountry() {
  75. if g.country != nil {
  76. return
  77. }
  78. g.setDirectory()
  79. gi, err := geoip.OpenType(geoip.GEOIP_COUNTRY_EDITION)
  80. if gi == nil || err != nil {
  81. log.Printf("Could not open country GeoIP database: %s\n", err)
  82. return
  83. }
  84. g.countryLastLoad = time.Now()
  85. g.hasCity = true
  86. g.country = gi
  87. }
  88. func (g *GeoIP) setupGeoIPCity() {
  89. if g.city != nil {
  90. return
  91. }
  92. g.setDirectory()
  93. gi, err := geoip.OpenType(geoip.GEOIP_CITY_EDITION_REV1)
  94. if gi == nil || err != nil {
  95. log.Printf("Could not open city GeoIP database: %s\n", err)
  96. return
  97. }
  98. g.cityLastLoad = time.Now()
  99. g.hasCity = true
  100. g.city = gi
  101. }
  102. func (g *GeoIP) setupGeoIPASN() {
  103. if g.asn != nil {
  104. return
  105. }
  106. g.setDirectory()
  107. gi, err := geoip.OpenType(geoip.GEOIP_ASNUM_EDITION)
  108. if gi == nil || err != nil {
  109. log.Printf("Could not open ASN GeoIP database: %s\n", err)
  110. return
  111. }
  112. g.asnLastLoad = time.Now()
  113. g.hasAsn = true
  114. g.asn = gi
  115. }