geoip.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. num, err := io.Copy(tmpF, tarReader)
  159. if err != nil {
  160. return err
  161. }
  162. tmpF.Close()
  163. fmt.Printf("output [%d]: %s\n", num, tmpF.Name())
  164. defer os.Remove(tmpF.Name())
  165. ggReader, err := gg.Open(tmpF.Name())
  166. if err != nil {
  167. return err
  168. }
  169. g.mutex.Lock()
  170. defer g.mutex.Unlock()
  171. if g.db != nil {
  172. g.db.Close()
  173. }
  174. g.db = ggReader
  175. return nil
  176. }
  177. }
  178. return nil
  179. }
  180. // Close closes the GeoIP database
  181. func (g *GeoIP) Close() {
  182. g.db.Close()
  183. }
  184. // Lookup performs a geo ip lookup for ipAddr in the maxmind geoip database
  185. func (g *GeoIP) Lookup(ipAddr string) error {
  186. g.mutex.Lock()
  187. g.IP = net.ParseIP(ipAddr)
  188. g.mutex.Unlock()
  189. if g.IP == nil {
  190. return fmt.Errorf("%s is not a valid IP address", ipAddr)
  191. }
  192. // ANONYMOUS IP
  193. //
  194. anon, err := g.db.AnonymousIP(g.IP)
  195. if err == nil {
  196. g.mutex.Lock()
  197. g.Anonymous = Anonymous{
  198. IsAnonymous: anon.IsAnonymous,
  199. IsAnonymousVPN: anon.IsAnonymousVPN,
  200. IsHostingProvider: anon.IsHostingProvider,
  201. IsPublicProxy: anon.IsPublicProxy,
  202. IsTorExitNode: anon.IsTorExitNode,
  203. }
  204. g.mutex.Unlock()
  205. }
  206. // CITY
  207. //
  208. city, err := g.db.City(g.IP)
  209. if err == nil {
  210. subdivisions := make([]string, len(city.Subdivisions), len(city.Subdivisions))
  211. for i, sd := range city.Subdivisions {
  212. subdivisions[i] = sd.Names["en"]
  213. }
  214. g.mutex.Lock()
  215. g.City = City{
  216. AccuracyRadius: city.Location.AccuracyRadius,
  217. Continent: city.Continent.Names["en"],
  218. ContinentCode: city.Continent.Code,
  219. Country: city.Country.Names["en"],
  220. CountryCode: city.Country.IsoCode,
  221. IsAnonymousProxy: city.Traits.IsAnonymousProxy,
  222. IsSatelliteProvider: city.Traits.IsSatelliteProvider,
  223. Latitude: city.Location.Latitude,
  224. Longitude: city.Location.Longitude,
  225. MetroCode: city.Location.MetroCode,
  226. Name: city.City.Names["en"],
  227. Postcode: city.Postal.Code,
  228. RegisteredCountry: city.RegisteredCountry.Names["en"],
  229. RegisteredCountryCode: city.RegisteredCountry.IsoCode,
  230. RepresentedCountry: city.RepresentedCountry.Names["en"],
  231. RepresentedCountryCode: city.RepresentedCountry.IsoCode,
  232. RepresentedCountryType: city.RepresentedCountry.Type,
  233. Subdivisions: subdivisions,
  234. Timezone: city.Location.TimeZone,
  235. }
  236. g.mutex.Unlock()
  237. }
  238. // COUNTRY
  239. //
  240. country, err := g.db.Country(g.IP)
  241. if err == nil {
  242. g.mutex.Lock()
  243. g.Country = Country{
  244. Continent: country.Continent.Names["en"],
  245. ContinentCode: country.Continent.Code,
  246. Country: country.Country.Names["en"],
  247. CountryCode: country.Country.IsoCode,
  248. IsAnonymousProxy: country.Traits.IsAnonymousProxy,
  249. IsSatelliteProvider: country.Traits.IsSatelliteProvider,
  250. RegisteredCountry: country.RegisteredCountry.Names["en"],
  251. RegisteredCountryCode: country.RegisteredCountry.IsoCode,
  252. RepresentedCountry: country.RepresentedCountry.Names["en"],
  253. RepresentedCountryCode: country.RepresentedCountry.IsoCode,
  254. RepresentedCountryType: country.RepresentedCountry.Type,
  255. }
  256. g.mutex.Unlock()
  257. }
  258. return nil
  259. }