geoip.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package geoip
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "compress/gzip"
  6. "crypto/md5"
  7. "encoding/hex"
  8. "fmt"
  9. "io"
  10. "io/ioutil"
  11. "net"
  12. "net/http"
  13. "os"
  14. "strings"
  15. "sync"
  16. gg "github.com/oschwald/geoip2-golang"
  17. )
  18. const (
  19. dbURL = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz"
  20. dbMd5URL = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz.md5"
  21. )
  22. // Anonymous contains information about whether an IP address is anonymous
  23. type Anonymous struct {
  24. // from geoip2-golang.AnonymousIP
  25. IsAnonymous bool `json:"is_anonymous"`
  26. IsAnonymousVPN bool `json:"is_anonymous_vpn"`
  27. IsHostingProvider bool `json:"is_hosting_provider"`
  28. IsPublicProxy bool `json:"is_public_proxy"`
  29. IsTorExitNode bool `json:"is_tor_exit_node"`
  30. }
  31. // City contains geographic data on a city level for a given IP address
  32. type City struct {
  33. Name string `json:"name"`
  34. Continent string `json:"continent"`
  35. ContinentCode string `json:"continent_code"`
  36. Country string `json:"country"`
  37. CountryCode string `json:"country_code"`
  38. AccuracyRadius uint16 `json:"accuracy_radius"`
  39. Latitude float64 `json:"latitude"`
  40. Longitude float64 `json:"longitude"`
  41. MetroCode uint `json:"metro_code"`
  42. Timezone string `json:"timezone"`
  43. Postcode string `json:"postcode"`
  44. RegisteredCountry string `json:"registered_country"`
  45. RegisteredCountryCode string `json:"registered_country_code"`
  46. RepresentedCountry string `json:"represented_country"`
  47. RepresentedCountryCode string `json:"represented_country_code"`
  48. RepresentedCountryType string `json:"represented_country_type"`
  49. Subdivisions []string `json:"subdivisions"`
  50. IsAnonymousProxy bool `json:"is_anonymous_proxy"`
  51. IsSatelliteProvider bool `json:"is_satellite_provider"`
  52. }
  53. // ConnectionType denotes the connection type for a given IP address
  54. type ConnectionType struct {
  55. Type string `json:"connection_type"`
  56. }
  57. // Country contains geographic data on a country level for a given IP address
  58. type Country struct {
  59. ContinentCode string `json:"continent_code"`
  60. Continent string `json:"continent"`
  61. CountryCode string `json:"country_code"`
  62. Country string `json:"country"`
  63. RegisteredCountryCode string `json:"registered_country_code"`
  64. RegisteredCountry string `json:"registered_country"`
  65. RepresentedCountryCode string `json:"represented_country_code"`
  66. RepresentedCountryType string `json:"represented_country_type"`
  67. RepresentedCountry string `json:"represented_country"`
  68. IsAnonymousProxy bool `json:"is_anonymous_proxy"`
  69. IsSatelliteProvider bool `json:"is_satellite_provider"`
  70. }
  71. // Domain denotes the domain for a given IP address
  72. type Domain struct {
  73. Domain string `json:"domain"`
  74. }
  75. // ISP contains information about the autonomous system for the given IP address
  76. type ISP struct {
  77. AutonomousSystemNumber uint `json:"autonomous_system_number"`
  78. AutonomousSystemOrganization string `json:"autonomous_system_organization"`
  79. ISP string `json:"isp"`
  80. Organization string `json:"organization"`
  81. }
  82. // GeoIP contains metadata for an IP address from the Maxmind GeoIP databases
  83. type GeoIP struct {
  84. db *gg.Reader
  85. IP net.IP `json:"ip" bson:"ip"`
  86. Anonymous Anonymous `json:"anonymous" bson:"anon"`
  87. City City `json:"city" bson:"city"`
  88. Country Country `json:"country" bson:"country"`
  89. Domain Domain `json:"domain" bson:"domain"`
  90. ISP ISP `json:"isp" bson:"isp"`
  91. mutex sync.RWMutex
  92. }
  93. // NewGeoIP creates a new GeoIP data structure
  94. func NewGeoIP() (*GeoIP, error) {
  95. g := GeoIP{
  96. mutex: sync.RWMutex{},
  97. }
  98. err := g.Load()
  99. if err != nil {
  100. return nil, err
  101. }
  102. return &g, nil
  103. }
  104. // Load loads the GeoIP City Database from Maxmind
  105. func (g *GeoIP) Load() error {
  106. // Get MD5 sum for tar.gz file
  107. resp, err := http.Get(dbMd5URL)
  108. if err != nil {
  109. return err
  110. }
  111. md5Sum, err := ioutil.ReadAll(resp.Body)
  112. if err != nil {
  113. return err
  114. }
  115. resp.Body.Close()
  116. // Load the tar.gz file
  117. resp, err = http.Get(dbURL)
  118. if err != nil {
  119. return err
  120. }
  121. defer resp.Body.Close()
  122. bodyData, err := ioutil.ReadAll(resp.Body)
  123. if err != nil {
  124. return err
  125. }
  126. // Build the MD5 sum of the downloaded tar.gz
  127. hash := md5.New()
  128. if _, err := io.Copy(hash, bytes.NewReader(bodyData)); err != nil {
  129. return err
  130. }
  131. if string(md5Sum) != hex.EncodeToString(hash.Sum(nil)) {
  132. return fmt.Errorf("checksum mismatch: %s != %s", md5Sum, hash.Sum(nil))
  133. }
  134. // Extract the mmdb file
  135. gzReader, err := gzip.NewReader(bytes.NewReader(bodyData))
  136. if err != nil {
  137. return err
  138. }
  139. defer gzReader.Close()
  140. tarReader := tar.NewReader(gzReader)
  141. if err != nil {
  142. return err
  143. }
  144. for {
  145. header, err := tarReader.Next()
  146. if err == io.EOF {
  147. break
  148. }
  149. if err != nil {
  150. return err
  151. }
  152. // this is the mmdb database
  153. if header.Typeflag == tar.TypeReg && strings.HasSuffix(header.Name, "GeoLite2-City.mmdb") {
  154. tmpF, err := ioutil.TempFile("/tmp", "geoip-mmdb-")
  155. if err != nil {
  156. return err
  157. }
  158. _, err = io.Copy(tmpF, tarReader)
  159. if err != nil {
  160. return err
  161. }
  162. tmpF.Close()
  163. defer os.Remove(tmpF.Name())
  164. ggReader, err := gg.Open(tmpF.Name())
  165. if err != nil {
  166. return err
  167. }
  168. g.mutex.Lock()
  169. defer g.mutex.Unlock()
  170. if g.db != nil {
  171. g.db.Close()
  172. }
  173. g.db = ggReader
  174. return nil
  175. }
  176. }
  177. return nil
  178. }
  179. // Close closes the GeoIP database
  180. func (g *GeoIP) Close() {
  181. g.db.Close()
  182. }
  183. // Lookup performs a geo ip lookup for ipAddr in the maxmind geoip database
  184. func (g *GeoIP) Lookup(ipAddr string) error {
  185. g.mutex.Lock()
  186. g.IP = net.ParseIP(ipAddr)
  187. g.mutex.Unlock()
  188. if g.IP == nil {
  189. return fmt.Errorf("%s is not a valid IP address", ipAddr)
  190. }
  191. // ANONYMOUS IP
  192. //
  193. anon, err := g.db.AnonymousIP(g.IP)
  194. if err == nil {
  195. g.mutex.Lock()
  196. g.Anonymous = Anonymous{
  197. IsAnonymous: anon.IsAnonymous,
  198. IsAnonymousVPN: anon.IsAnonymousVPN,
  199. IsHostingProvider: anon.IsHostingProvider,
  200. IsPublicProxy: anon.IsPublicProxy,
  201. IsTorExitNode: anon.IsTorExitNode,
  202. }
  203. g.mutex.Unlock()
  204. }
  205. // CITY
  206. //
  207. city, err := g.db.City(g.IP)
  208. if err == nil {
  209. subdivisions := make([]string, len(city.Subdivisions), len(city.Subdivisions))
  210. for i, sd := range city.Subdivisions {
  211. subdivisions[i] = sd.Names["en"]
  212. }
  213. g.mutex.Lock()
  214. g.City = City{
  215. AccuracyRadius: city.Location.AccuracyRadius,
  216. Continent: city.Continent.Names["en"],
  217. ContinentCode: city.Continent.Code,
  218. Country: city.Country.Names["en"],
  219. CountryCode: city.Country.IsoCode,
  220. IsAnonymousProxy: city.Traits.IsAnonymousProxy,
  221. IsSatelliteProvider: city.Traits.IsSatelliteProvider,
  222. Latitude: city.Location.Latitude,
  223. Longitude: city.Location.Longitude,
  224. MetroCode: city.Location.MetroCode,
  225. Name: city.City.Names["en"],
  226. Postcode: city.Postal.Code,
  227. RegisteredCountry: city.RegisteredCountry.Names["en"],
  228. RegisteredCountryCode: city.RegisteredCountry.IsoCode,
  229. RepresentedCountry: city.RepresentedCountry.Names["en"],
  230. RepresentedCountryCode: city.RepresentedCountry.IsoCode,
  231. RepresentedCountryType: city.RepresentedCountry.Type,
  232. Subdivisions: subdivisions,
  233. Timezone: city.Location.TimeZone,
  234. }
  235. g.mutex.Unlock()
  236. }
  237. // COUNTRY
  238. //
  239. country, err := g.db.Country(g.IP)
  240. if err == nil {
  241. g.mutex.Lock()
  242. g.Country = Country{
  243. Continent: country.Continent.Names["en"],
  244. ContinentCode: country.Continent.Code,
  245. Country: country.Country.Names["en"],
  246. CountryCode: country.Country.IsoCode,
  247. IsAnonymousProxy: country.Traits.IsAnonymousProxy,
  248. IsSatelliteProvider: country.Traits.IsSatelliteProvider,
  249. RegisteredCountry: country.RegisteredCountry.Names["en"],
  250. RegisteredCountryCode: country.RegisteredCountry.IsoCode,
  251. RepresentedCountry: country.RepresentedCountry.Names["en"],
  252. RepresentedCountryCode: country.RepresentedCountry.IsoCode,
  253. RepresentedCountryType: country.RepresentedCountry.Type,
  254. }
  255. g.mutex.Unlock()
  256. }
  257. return nil
  258. }