setup.go 4.8 KB

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