geoip.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. ipMutex sync.RWMutex
  93. }
  94. // NewGeoIP creates a new GeoIP data structure
  95. func NewGeoIP() (*GeoIP, error) {
  96. g := GeoIP{
  97. mutex: sync.RWMutex{},
  98. ipMutex: sync.RWMutex{},
  99. }
  100. err := g.Load()
  101. if err != nil {
  102. return nil, err
  103. }
  104. return &g, nil
  105. }
  106. // NewGeoIPNoLoad creates a new geoip type without loading the data
  107. func NewGeoIPNoLoad() (*GeoIP, error) {
  108. return &GeoIP{
  109. mutex: sync.RWMutex{},
  110. ipMutex: sync.RWMutex{},
  111. }, nil
  112. }
  113. // Load loads the GeoIP City Database from Maxmind
  114. func (g *GeoIP) Load() error {
  115. // Get MD5 sum for tar.gz file
  116. resp, err := http.Get(dbMd5URL)
  117. if err != nil {
  118. return err
  119. }
  120. md5Sum, err := ioutil.ReadAll(resp.Body)
  121. if err != nil {
  122. return err
  123. }
  124. resp.Body.Close()
  125. // Load the tar.gz file
  126. resp, err = http.Get(dbURL)
  127. if err != nil {
  128. return err
  129. }
  130. defer resp.Body.Close()
  131. bodyData, err := ioutil.ReadAll(resp.Body)
  132. if err != nil {
  133. return err
  134. }
  135. // Build the MD5 sum of the downloaded tar.gz
  136. hash := md5.New()
  137. if _, err := io.Copy(hash, bytes.NewReader(bodyData)); err != nil {
  138. return err
  139. }
  140. if string(md5Sum) != hex.EncodeToString(hash.Sum(nil)) {
  141. return fmt.Errorf("checksum mismatch: %s != %s", md5Sum, hash.Sum(nil))
  142. }
  143. // Extract the mmdb file
  144. gzReader, err := gzip.NewReader(bytes.NewReader(bodyData))
  145. if err != nil {
  146. return err
  147. }
  148. defer gzReader.Close()
  149. tarReader := tar.NewReader(gzReader)
  150. if err != nil {
  151. return err
  152. }
  153. for {
  154. header, err := tarReader.Next()
  155. if err == io.EOF {
  156. break
  157. }
  158. if err != nil {
  159. return err
  160. }
  161. // this is the mmdb database
  162. if header.Typeflag == tar.TypeReg && strings.HasSuffix(header.Name, "GeoLite2-City.mmdb") {
  163. tmpF, err := ioutil.TempFile("/tmp", "geoip-mmdb-")
  164. if err != nil {
  165. return err
  166. }
  167. _, err = io.Copy(tmpF, tarReader)
  168. if err != nil {
  169. return err
  170. }
  171. tmpF.Close()
  172. defer os.Remove(tmpF.Name())
  173. ggReader, err := gg.Open(tmpF.Name())
  174. if err != nil {
  175. return err
  176. }
  177. g.mutex.Lock()
  178. defer g.mutex.Unlock()
  179. if g.db != nil {
  180. g.db.Close()
  181. }
  182. g.db = ggReader
  183. return nil
  184. }
  185. }
  186. return nil
  187. }
  188. // Close closes the GeoIP database
  189. func (g *GeoIP) Close() {
  190. g.db.Close()
  191. }
  192. // LookupIP looks up the geo ip data for the given IP address
  193. func (g *GeoIP) LookupIP(ip string) (*GeoIP, error) {
  194. g.mutex.Lock()
  195. defer g.mutex.Unlock()
  196. x, err := NewGeoIPNoLoad()
  197. if err != nil {
  198. return nil, fmt.Errorf("failed to create new GeoIP type: %s", err)
  199. }
  200. x.IP = net.ParseIP(ip)
  201. if x.IP == nil {
  202. return nil, fmt.Errorf("%s is not a valid IP address", ip)
  203. }
  204. // ANONYMOUS IP
  205. //
  206. anon, err := g.db.AnonymousIP(x.IP)
  207. if err == nil {
  208. g.Anonymous = Anonymous{
  209. IsAnonymous: anon.IsAnonymous,
  210. IsAnonymousVPN: anon.IsAnonymousVPN,
  211. IsHostingProvider: anon.IsHostingProvider,
  212. IsPublicProxy: anon.IsPublicProxy,
  213. IsTorExitNode: anon.IsTorExitNode,
  214. }
  215. }
  216. // CITY
  217. //
  218. city, err := g.db.City(x.IP)
  219. if err == nil {
  220. subdivisions := make([]string, len(city.Subdivisions), len(city.Subdivisions))
  221. for i, sd := range city.Subdivisions {
  222. subdivisions[i] = sd.Names["en"]
  223. }
  224. x.City = City{
  225. AccuracyRadius: city.Location.AccuracyRadius,
  226. Continent: city.Continent.Names["en"],
  227. ContinentCode: city.Continent.Code,
  228. Country: city.Country.Names["en"],
  229. CountryCode: city.Country.IsoCode,
  230. IsAnonymousProxy: city.Traits.IsAnonymousProxy,
  231. IsSatelliteProvider: city.Traits.IsSatelliteProvider,
  232. Latitude: city.Location.Latitude,
  233. Longitude: city.Location.Longitude,
  234. MetroCode: city.Location.MetroCode,
  235. Name: city.City.Names["en"],
  236. Postcode: city.Postal.Code,
  237. RegisteredCountry: city.RegisteredCountry.Names["en"],
  238. RegisteredCountryCode: city.RegisteredCountry.IsoCode,
  239. RepresentedCountry: city.RepresentedCountry.Names["en"],
  240. RepresentedCountryCode: city.RepresentedCountry.IsoCode,
  241. RepresentedCountryType: city.RepresentedCountry.Type,
  242. Subdivisions: subdivisions,
  243. Timezone: city.Location.TimeZone,
  244. }
  245. } else {
  246. return nil, fmt.Errorf("failed to load city data for %s", ip)
  247. }
  248. // COUNTRY
  249. //
  250. country, err := g.db.Country(x.IP)
  251. if err == nil {
  252. x.Country = Country{
  253. Continent: country.Continent.Names["en"],
  254. ContinentCode: country.Continent.Code,
  255. Country: country.Country.Names["en"],
  256. CountryCode: country.Country.IsoCode,
  257. IsAnonymousProxy: country.Traits.IsAnonymousProxy,
  258. IsSatelliteProvider: country.Traits.IsSatelliteProvider,
  259. RegisteredCountry: country.RegisteredCountry.Names["en"],
  260. RegisteredCountryCode: country.RegisteredCountry.IsoCode,
  261. RepresentedCountry: country.RepresentedCountry.Names["en"],
  262. RepresentedCountryCode: country.RepresentedCountry.IsoCode,
  263. RepresentedCountryType: country.RepresentedCountry.Type,
  264. }
  265. } else {
  266. return nil, fmt.Errorf("failed to load country data for %s", ip)
  267. }
  268. return x, nil
  269. }
  270. // Lookup performs a geo ip lookup for ipAddr in the maxmind geoip database
  271. func (g *GeoIP) Lookup(ipAddr string) error {
  272. g.mutex.Lock()
  273. defer g.mutex.Unlock()
  274. g.IP = net.ParseIP(ipAddr)
  275. if g.IP == nil {
  276. return fmt.Errorf("%s is not a valid IP address", ipAddr)
  277. }
  278. // ANONYMOUS IP
  279. //
  280. anon, err := g.db.AnonymousIP(g.IP)
  281. if err == nil {
  282. g.Anonymous = Anonymous{
  283. IsAnonymous: anon.IsAnonymous,
  284. IsAnonymousVPN: anon.IsAnonymousVPN,
  285. IsHostingProvider: anon.IsHostingProvider,
  286. IsPublicProxy: anon.IsPublicProxy,
  287. IsTorExitNode: anon.IsTorExitNode,
  288. }
  289. }
  290. // CITY
  291. //
  292. city, err := g.db.City(g.IP)
  293. if err == nil {
  294. subdivisions := make([]string, len(city.Subdivisions), len(city.Subdivisions))
  295. for i, sd := range city.Subdivisions {
  296. subdivisions[i] = sd.Names["en"]
  297. }
  298. g.City = City{
  299. AccuracyRadius: city.Location.AccuracyRadius,
  300. Continent: city.Continent.Names["en"],
  301. ContinentCode: city.Continent.Code,
  302. Country: city.Country.Names["en"],
  303. CountryCode: city.Country.IsoCode,
  304. IsAnonymousProxy: city.Traits.IsAnonymousProxy,
  305. IsSatelliteProvider: city.Traits.IsSatelliteProvider,
  306. Latitude: city.Location.Latitude,
  307. Longitude: city.Location.Longitude,
  308. MetroCode: city.Location.MetroCode,
  309. Name: city.City.Names["en"],
  310. Postcode: city.Postal.Code,
  311. RegisteredCountry: city.RegisteredCountry.Names["en"],
  312. RegisteredCountryCode: city.RegisteredCountry.IsoCode,
  313. RepresentedCountry: city.RepresentedCountry.Names["en"],
  314. RepresentedCountryCode: city.RepresentedCountry.IsoCode,
  315. RepresentedCountryType: city.RepresentedCountry.Type,
  316. Subdivisions: subdivisions,
  317. Timezone: city.Location.TimeZone,
  318. }
  319. }
  320. // COUNTRY
  321. //
  322. country, err := g.db.Country(g.IP)
  323. if err == nil {
  324. g.Country = Country{
  325. Continent: country.Continent.Names["en"],
  326. ContinentCode: country.Continent.Code,
  327. Country: country.Country.Names["en"],
  328. CountryCode: country.Country.IsoCode,
  329. IsAnonymousProxy: country.Traits.IsAnonymousProxy,
  330. IsSatelliteProvider: country.Traits.IsSatelliteProvider,
  331. RegisteredCountry: country.RegisteredCountry.Names["en"],
  332. RegisteredCountryCode: country.RegisteredCountry.IsoCode,
  333. RepresentedCountry: country.RepresentedCountry.Names["en"],
  334. RepresentedCountryCode: country.RepresentedCountry.IsoCode,
  335. RepresentedCountryType: country.RepresentedCountry.Type,
  336. }
  337. }
  338. return nil
  339. }