setup.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package geoip
  2. import (
  3. "errors"
  4. "log"
  5. "net"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. "git.scraperwall.com/scw/asndb"
  10. "github.com/mholt/caddy"
  11. "github.com/mholt/caddy/caddyhttp/httpserver"
  12. "github.com/mmcloughlin/geohash"
  13. "github.com/oschwald/maxminddb-golang"
  14. )
  15. // GeoIP represents a middleware instance
  16. type GeoIP struct {
  17. Next httpserver.Handler
  18. DBHandler *maxminddb.Reader
  19. ASNDB *asndb.ASNDB
  20. Config Config
  21. }
  22. type GeoIPRecord struct {
  23. Country struct {
  24. ISOCode string `maxminddb:"iso_code"`
  25. IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
  26. Names map[string]string `maxminddb:"names"`
  27. GeoNameID uint64 `maxminddb:"geoname_id"`
  28. } `maxminddb:"country"`
  29. City struct {
  30. Names map[string]string `maxminddb:"names"`
  31. GeoNameID uint64 `maxminddb:"geoname_id"`
  32. } `maxminddb:"city"`
  33. Location struct {
  34. Latitude float64 `maxminddb:"latitude"`
  35. Longitude float64 `maxminddb:"longitude"`
  36. TimeZone string `maxminddb:"time_zone"`
  37. } `maxminddb:"location"`
  38. ASN asndb.ASN
  39. }
  40. // Init initializes the plugin
  41. func init() {
  42. caddy.RegisterPlugin("geoip", caddy.Plugin{
  43. ServerType: "http",
  44. Action: setup,
  45. })
  46. }
  47. func setup(c *caddy.Controller) error {
  48. config, err := parseConfig(c)
  49. if err != nil {
  50. return err
  51. }
  52. dbhandler, err := maxminddb.Open(config.DatabasePath)
  53. if err != nil {
  54. return c.Err("geoip: Can't open database: " + config.DatabasePath)
  55. }
  56. asndb, err := asndb.New()
  57. if err != nil {
  58. return c.Err("asndb: failed to load: " + err.Error)
  59. }
  60. // Create new middleware
  61. newMiddleWare := func(next httpserver.Handler) httpserver.Handler {
  62. return &GeoIP{
  63. Next: next,
  64. DBHandler: dbhandler,
  65. ASNDB: asndb,
  66. Config: config,
  67. }
  68. }
  69. // Add middleware
  70. cfg := httpserver.GetConfig(c)
  71. cfg.AddMiddleware(newMiddleWare)
  72. return nil
  73. }
  74. func (gip GeoIP) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
  75. gip.lookupLocation(w, r)
  76. return gip.Next.ServeHTTP(w, r)
  77. }
  78. func (gip GeoIP) lookupLocation(w http.ResponseWriter, r *http.Request) {
  79. record := gip.fetchGeoipData(r)
  80. replacer := newReplacer(r)
  81. replacer.Set("asn_asn", record.ASN.ASN)
  82. replacer.Set("asn_organization", record.ASN.Organization)
  83. replacer.Set("geoip_country_code", record.Country.ISOCode)
  84. replacer.Set("geoip_country_name", record.Country.Names["en"])
  85. replacer.Set("geoip_country_eu", strconv.FormatBool(record.Country.IsInEuropeanUnion))
  86. replacer.Set("geoip_country_geoname_id", strconv.FormatUint(record.Country.GeoNameID, 10))
  87. replacer.Set("geoip_city_name", record.City.Names["en"])
  88. replacer.Set("geoip_city_geoname_id", strconv.FormatUint(record.City.GeoNameID, 10))
  89. replacer.Set("geoip_latitude", strconv.FormatFloat(record.Location.Latitude, 'f', 6, 64))
  90. replacer.Set("geoip_longitude", strconv.FormatFloat(record.Location.Longitude, 'f', 6, 64))
  91. replacer.Set("geoip_geohash", geohash.Encode(record.Location.Latitude, record.Location.Longitude))
  92. replacer.Set("geoip_time_zone", record.Location.TimeZone)
  93. if rr, ok := w.(*httpserver.ResponseRecorder); ok {
  94. rr.Replacer = replacer
  95. }
  96. }
  97. func (gip GeoIP) fetchGeoipData(r *http.Request) GeoIPRecord {
  98. clientIP, _ := getClientIP(r, true)
  99. var record = GeoIPRecord{}
  100. err := gip.DBHandler.Lookup(clientIP, &record)
  101. if err != nil {
  102. log.Println(err)
  103. }
  104. record.ASN = gip.ASNDB.Lookup(clientIP)
  105. if record.Country.ISOCode == "" {
  106. record.Country.Names = make(map[string]string)
  107. record.City.Names = make(map[string]string)
  108. if clientIP.IsLoopback() {
  109. record.Country.ISOCode = "**"
  110. record.Country.Names["en"] = "Loopback"
  111. record.City.Names["en"] = "Loopback"
  112. } else {
  113. record.Country.ISOCode = "!!"
  114. record.Country.Names["en"] = "No Country"
  115. record.City.Names["en"] = "No City"
  116. }
  117. }
  118. return record
  119. }
  120. func getClientIP(r *http.Request, strict bool) (net.IP, error) {
  121. var ip string
  122. // Use the client ip from the 'X-Forwarded-For' header, if available.
  123. if fwdFor := r.Header.Get("X-Forwarded-For"); fwdFor != "" && !strict {
  124. ips := strings.Split(fwdFor, ", ")
  125. ip = ips[0]
  126. } else {
  127. // Otherwise, get the client ip from the request remote address.
  128. var err error
  129. ip, _, err = net.SplitHostPort(r.RemoteAddr)
  130. if err != nil {
  131. if serr, ok := err.(*net.AddrError); ok && serr.Err == "missing port in address" { // It's not critical try parse
  132. ip = r.RemoteAddr
  133. } else {
  134. log.Printf("Error when SplitHostPort: %v", serr.Err)
  135. return nil, err
  136. }
  137. }
  138. }
  139. // Parse the ip address string into a net.IP.
  140. parsedIP := net.ParseIP(ip)
  141. if parsedIP == nil {
  142. return nil, errors.New("unable to parse address")
  143. }
  144. return parsedIP, nil
  145. }
  146. func newReplacer(r *http.Request) httpserver.Replacer {
  147. return httpserver.NewReplacer(r, nil, "")
  148. }