geoip.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package server
  20. import (
  21. "fmt"
  22. "io"
  23. "net"
  24. "os"
  25. "path/filepath"
  26. "strconv"
  27. "strings"
  28. "time"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  31. maxminddb "github.com/oschwald/maxminddb-golang"
  32. )
  33. const (
  34. GEOIP_UNKNOWN_VALUE = "None"
  35. GEOIP_DATABASE_TYPE_ISP = "GeoIP2-ISP"
  36. )
  37. // GeoIPData is GeoIP data for a client session. Individual client
  38. // IP addresses are neither logged nor explicitly referenced during a session.
  39. // The GeoIP country, city, and ISP corresponding to a client IP address are
  40. // resolved and then logged along with usage stats.
  41. type GeoIPData struct {
  42. Country string
  43. City string
  44. ISP string
  45. ASN string
  46. ASO string
  47. }
  48. // NewGeoIPData returns a GeoIPData initialized with the expected
  49. // GEOIP_UNKNOWN_VALUE values to be used when GeoIP lookup fails.
  50. func NewGeoIPData() GeoIPData {
  51. return GeoIPData{
  52. Country: GEOIP_UNKNOWN_VALUE,
  53. City: GEOIP_UNKNOWN_VALUE,
  54. ISP: GEOIP_UNKNOWN_VALUE,
  55. ASN: GEOIP_UNKNOWN_VALUE,
  56. ASO: GEOIP_UNKNOWN_VALUE,
  57. }
  58. }
  59. // SetClientLogFields adds the GeoIPData fields to LogFields, following
  60. // Psiphon field name and format conventions. For example, GeoIPData.Country
  61. // is logged as client_region.
  62. func (g GeoIPData) SetClientLogFields(logFields LogFields) {
  63. g.SetClientLogFieldsWithPrefix("", logFields)
  64. }
  65. // SetClientLogFieldsWithPrefix adds the GeoIPData fields to LogFields,
  66. // following Psiphon field name and format conventions and with the specified
  67. // prefix. For example, GeoIPData.Country is logged as
  68. // duplicate_authorization_client_region for the prefix "duplicate_authorization_".
  69. func (g GeoIPData) SetClientLogFieldsWithPrefix(prefix string, logFields LogFields) {
  70. g.SetLogFieldsWithPrefix(prefix, "client", logFields)
  71. }
  72. // SetLogFieldsWithPrefix adds the GeoIPData fields to LogFields, following
  73. // Psiphon field name and format conventions and with the specified prefix
  74. // and name. For example, GeoIPData.Country is logged as proxy_region for the
  75. // prefix "" and name "proxy".
  76. func (g GeoIPData) SetLogFieldsWithPrefix(prefix string, name string, logFields LogFields) {
  77. // In psi_web, the space replacement was done to accommodate space
  78. // delimited logging, which is no longer required; we retain the
  79. // transformation so that stats aggregation isn't impacted.
  80. logFields[fmt.Sprintf("%s%s_region", prefix, name)] = strings.Replace(g.Country, " ", "_", -1)
  81. logFields[fmt.Sprintf("%s%s_city", prefix, name)] = strings.Replace(g.City, " ", "_", -1)
  82. logFields[fmt.Sprintf("%s%s_isp", prefix, name)] = strings.Replace(g.ISP, " ", "_", -1)
  83. logFields[fmt.Sprintf("%s%s_asn", prefix, name)] = strings.Replace(g.ASN, " ", "_", -1)
  84. logFields[fmt.Sprintf("%s%s_aso", prefix, name)] = strings.Replace(g.ASO, " ", "_", -1)
  85. }
  86. // GeoIPService implements GeoIP lookup and session/GeoIP caching.
  87. // Lookup is via a MaxMind database; the ReloadDatabase function
  88. // supports hot reloading of MaxMind data while the server is
  89. // running.
  90. type GeoIPService struct {
  91. databases []*geoIPDatabase
  92. }
  93. type geoIPDatabase struct {
  94. common.ReloadableFile
  95. filename string
  96. tempFilename string
  97. tempFileSuffix int64
  98. isISPType bool
  99. maxMindReader *maxminddb.Reader
  100. }
  101. // NewGeoIPService initializes a new GeoIPService.
  102. func NewGeoIPService(databaseFilenames []string) (*GeoIPService, error) {
  103. geoIP := &GeoIPService{
  104. databases: make([]*geoIPDatabase, len(databaseFilenames)),
  105. }
  106. for i, filename := range databaseFilenames {
  107. database := &geoIPDatabase{
  108. filename: filename,
  109. }
  110. database.ReloadableFile = common.NewReloadableFile(
  111. filename,
  112. false,
  113. func(_ []byte, _ time.Time) error {
  114. // In order to safely mmap the database file, a temporary copy
  115. // is made and that copy is mmapped. The original file may be
  116. // repaved without affecting the mmap; upon hot reload, a new
  117. // temporary copy is made and once it is successful, the old
  118. // mmap is closed and previous temporary file deleted.
  119. //
  120. // On any reload error, database state remains the same.
  121. src, err := os.Open(database.filename)
  122. if err != nil {
  123. return errors.Trace(err)
  124. }
  125. tempFileSuffix := database.tempFileSuffix + 1
  126. tempFilename := fmt.Sprintf(
  127. "%s.%d",
  128. filepath.Join(os.TempDir(), filepath.Base(database.filename)),
  129. tempFileSuffix)
  130. dst, err := os.Create(tempFilename)
  131. if err != nil {
  132. src.Close()
  133. return errors.Trace(err)
  134. }
  135. _, err = io.Copy(dst, src)
  136. src.Close()
  137. dst.Close()
  138. if err != nil {
  139. _ = os.Remove(tempFilename)
  140. return errors.Trace(err)
  141. }
  142. maxMindReader, err := maxminddb.Open(tempFilename)
  143. if err != nil {
  144. _ = os.Remove(tempFilename)
  145. return errors.Trace(err)
  146. }
  147. if database.maxMindReader != nil {
  148. database.maxMindReader.Close()
  149. _ = os.Remove(database.tempFilename)
  150. }
  151. isISPType := (maxMindReader.Metadata.DatabaseType == GEOIP_DATABASE_TYPE_ISP)
  152. database.maxMindReader = maxMindReader
  153. database.isISPType = isISPType
  154. database.tempFilename = tempFilename
  155. database.tempFileSuffix = tempFileSuffix
  156. return nil
  157. })
  158. _, err := database.Reload()
  159. if err != nil {
  160. return nil, errors.Trace(err)
  161. }
  162. geoIP.databases[i] = database
  163. }
  164. return geoIP, nil
  165. }
  166. // Reloaders gets the list of reloadable databases in use
  167. // by the GeoIPService. This list is used to hot reload
  168. // these databases.
  169. func (geoIP *GeoIPService) Reloaders() []common.Reloader {
  170. reloaders := make([]common.Reloader, len(geoIP.databases))
  171. for i, database := range geoIP.databases {
  172. reloaders[i] = database
  173. }
  174. return reloaders
  175. }
  176. // Lookup determines a GeoIPData for a given string client IP address.
  177. func (geoIP *GeoIPService) Lookup(strIP string) GeoIPData {
  178. return geoIP.LookupIP(net.ParseIP(strIP))
  179. }
  180. // LookupIP determines a GeoIPData for a given client IP address.
  181. func (geoIP *GeoIPService) LookupIP(IP net.IP) GeoIPData {
  182. return geoIP.lookupIP(IP, false)
  183. }
  184. // LookupISPForIP determines a GeoIPData for a given client IP address. Only
  185. // ISP, ASN, and ASO fields will be populated. This lookup is faster than a
  186. // full lookup. Benchmarks show this lookup is <= ~1 microsecond against the
  187. // production geo IP database.
  188. func (geoIP *GeoIPService) LookupISPForIP(IP net.IP) GeoIPData {
  189. return geoIP.lookupIP(IP, true)
  190. }
  191. func (geoIP *GeoIPService) lookupIP(IP net.IP, ISPOnly bool) GeoIPData {
  192. result := NewGeoIPData()
  193. if IP == nil {
  194. return result
  195. }
  196. // Populate GeoIP fields.
  197. var geoIPFields struct {
  198. Country struct {
  199. ISOCode string `maxminddb:"iso_code"`
  200. } `maxminddb:"country"`
  201. City struct {
  202. Names map[string]string `maxminddb:"names"`
  203. } `maxminddb:"city"`
  204. ISP string `maxminddb:"isp"`
  205. ASN int `maxminddb:"autonomous_system_number"`
  206. ASO string `maxminddb:"autonomous_system_organization"`
  207. }
  208. geoIPFields.ASN = -1
  209. // Each database will populate geoIPFields with the values it contains. In the
  210. // current MaxMind deployment, the City database populates Country and City and
  211. // the separate ISP database populates ISP.
  212. for _, database := range geoIP.databases {
  213. database.ReloadableFile.RLock()
  214. var err error
  215. // Don't lookup the City database when only ISP fields are required;
  216. // skipping the City lookup is 5-10x faster.
  217. if !ISPOnly || database.isISPType {
  218. err = database.maxMindReader.Lookup(IP, &geoIPFields)
  219. }
  220. database.ReloadableFile.RUnlock()
  221. if err != nil {
  222. log.WithTraceFields(LogFields{"error": err}).Warning("GeoIP lookup failed")
  223. }
  224. }
  225. if geoIPFields.Country.ISOCode != "" {
  226. result.Country = geoIPFields.Country.ISOCode
  227. }
  228. name, ok := geoIPFields.City.Names["en"]
  229. if ok && name != "" {
  230. result.City = name
  231. }
  232. if geoIPFields.ISP != "" {
  233. result.ISP = geoIPFields.ISP
  234. }
  235. if geoIPFields.ASN != -1 {
  236. result.ASN = strconv.Itoa(geoIPFields.ASN)
  237. }
  238. if geoIPFields.ASO != "" {
  239. result.ASO = geoIPFields.ASO
  240. }
  241. return result
  242. }