geoip.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. cache "github.com/patrickmn/go-cache"
  33. )
  34. const (
  35. GEOIP_SESSION_CACHE_TTL = 60 * time.Minute
  36. GEOIP_UNKNOWN_VALUE = "None"
  37. GEOIP_DATABASE_TYPE_ISP = "GeoIP2-ISP"
  38. )
  39. // GeoIPData is GeoIP data for a client session. Individual client
  40. // IP addresses are neither logged nor explicitly referenced during a session.
  41. // The GeoIP country, city, and ISP corresponding to a client IP address are
  42. // resolved and then logged along with usage stats.
  43. type GeoIPData struct {
  44. Country string
  45. City string
  46. ISP string
  47. ASN string
  48. ASO string
  49. }
  50. // NewGeoIPData returns a GeoIPData initialized with the expected
  51. // GEOIP_UNKNOWN_VALUE values to be used when GeoIP lookup fails.
  52. func NewGeoIPData() GeoIPData {
  53. return GeoIPData{
  54. Country: GEOIP_UNKNOWN_VALUE,
  55. City: GEOIP_UNKNOWN_VALUE,
  56. ISP: GEOIP_UNKNOWN_VALUE,
  57. ASN: GEOIP_UNKNOWN_VALUE,
  58. ASO: GEOIP_UNKNOWN_VALUE,
  59. }
  60. }
  61. // SetLogFields adds the GeoIPData fields to LogFields, following Psiphon
  62. // metric field name and format conventions.
  63. func (g GeoIPData) SetLogFields(logFields LogFields) {
  64. g.SetLogFieldsWithPrefix("", logFields)
  65. }
  66. func (g GeoIPData) SetLogFieldsWithPrefix(prefix string, logFields LogFields) {
  67. // In psi_web, the space replacement was done to accommodate space
  68. // delimited logging, which is no longer required; we retain the
  69. // transformation so that stats aggregation isn't impacted.
  70. logFields[prefix+"client_region"] = strings.Replace(g.Country, " ", "_", -1)
  71. logFields[prefix+"client_city"] = strings.Replace(g.City, " ", "_", -1)
  72. logFields[prefix+"client_isp"] = strings.Replace(g.ISP, " ", "_", -1)
  73. logFields[prefix+"client_asn"] = strings.Replace(g.ASN, " ", "_", -1)
  74. logFields[prefix+"client_aso"] = strings.Replace(g.ASO, " ", "_", -1)
  75. }
  76. // GeoIPService implements GeoIP lookup and session/GeoIP caching.
  77. // Lookup is via a MaxMind database; the ReloadDatabase function
  78. // supports hot reloading of MaxMind data while the server is
  79. // running.
  80. type GeoIPService struct {
  81. databases []*geoIPDatabase
  82. sessionCache *cache.Cache
  83. }
  84. type geoIPDatabase struct {
  85. common.ReloadableFile
  86. filename string
  87. tempFilename string
  88. tempFileSuffix int64
  89. isISPType bool
  90. maxMindReader *maxminddb.Reader
  91. }
  92. // NewGeoIPService initializes a new GeoIPService.
  93. func NewGeoIPService(databaseFilenames []string) (*GeoIPService, error) {
  94. geoIP := &GeoIPService{
  95. databases: make([]*geoIPDatabase, len(databaseFilenames)),
  96. sessionCache: cache.New(GEOIP_SESSION_CACHE_TTL, 1*time.Minute),
  97. }
  98. for i, filename := range databaseFilenames {
  99. database := &geoIPDatabase{
  100. filename: filename,
  101. }
  102. database.ReloadableFile = common.NewReloadableFile(
  103. filename,
  104. false,
  105. func(_ []byte, _ time.Time) error {
  106. // In order to safely mmap the database file, a temporary copy
  107. // is made and that copy is mmapped. The original file may be
  108. // repaved without affecting the mmap; upon hot reload, a new
  109. // temporary copy is made and once it is successful, the old
  110. // mmap is closed and previous temporary file deleted.
  111. //
  112. // On any reload error, database state remains the same.
  113. src, err := os.Open(database.filename)
  114. if err != nil {
  115. return errors.Trace(err)
  116. }
  117. tempFileSuffix := database.tempFileSuffix + 1
  118. tempFilename := fmt.Sprintf(
  119. "%s.%d",
  120. filepath.Join(os.TempDir(), filepath.Base(database.filename)),
  121. tempFileSuffix)
  122. dst, err := os.Create(tempFilename)
  123. if err != nil {
  124. src.Close()
  125. return errors.Trace(err)
  126. }
  127. _, err = io.Copy(dst, src)
  128. src.Close()
  129. dst.Close()
  130. if err != nil {
  131. _ = os.Remove(tempFilename)
  132. return errors.Trace(err)
  133. }
  134. maxMindReader, err := maxminddb.Open(tempFilename)
  135. if err != nil {
  136. _ = os.Remove(tempFilename)
  137. return errors.Trace(err)
  138. }
  139. if database.maxMindReader != nil {
  140. database.maxMindReader.Close()
  141. _ = os.Remove(database.tempFilename)
  142. }
  143. isISPType := (maxMindReader.Metadata.DatabaseType == GEOIP_DATABASE_TYPE_ISP)
  144. database.maxMindReader = maxMindReader
  145. database.isISPType = isISPType
  146. database.tempFilename = tempFilename
  147. database.tempFileSuffix = tempFileSuffix
  148. return nil
  149. })
  150. _, err := database.Reload()
  151. if err != nil {
  152. return nil, errors.Trace(err)
  153. }
  154. geoIP.databases[i] = database
  155. }
  156. return geoIP, nil
  157. }
  158. // Reloaders gets the list of reloadable databases in use
  159. // by the GeoIPService. This list is used to hot reload
  160. // these databases.
  161. func (geoIP *GeoIPService) Reloaders() []common.Reloader {
  162. reloaders := make([]common.Reloader, len(geoIP.databases))
  163. for i, database := range geoIP.databases {
  164. reloaders[i] = database
  165. }
  166. return reloaders
  167. }
  168. // Lookup determines a GeoIPData for a given string client IP address.
  169. func (geoIP *GeoIPService) Lookup(strIP string) GeoIPData {
  170. return geoIP.LookupIP(net.ParseIP(strIP))
  171. }
  172. // LookupIP determines a GeoIPData for a given client IP address.
  173. func (geoIP *GeoIPService) LookupIP(IP net.IP) GeoIPData {
  174. return geoIP.lookupIP(IP, false)
  175. }
  176. // LookupISPForIP determines a GeoIPData for a given client IP address. Only
  177. // ISP, ASN, and ASO fields will be populated. This lookup is faster than a
  178. // full lookup.
  179. func (geoIP *GeoIPService) LookupISPForIP(IP net.IP) GeoIPData {
  180. return geoIP.lookupIP(IP, true)
  181. }
  182. func (geoIP *GeoIPService) lookupIP(IP net.IP, ISPOnly bool) GeoIPData {
  183. result := NewGeoIPData()
  184. if IP == nil {
  185. return result
  186. }
  187. // Populate GeoIP fields.
  188. var geoIPFields struct {
  189. Country struct {
  190. ISOCode string `maxminddb:"iso_code"`
  191. } `maxminddb:"country"`
  192. City struct {
  193. Names map[string]string `maxminddb:"names"`
  194. } `maxminddb:"city"`
  195. ISP string `maxminddb:"isp"`
  196. ASN int `maxminddb:"autonomous_system_number"`
  197. ASO string `maxminddb:"autonomous_system_organization"`
  198. }
  199. geoIPFields.ASN = -1
  200. // Each database will populate geoIPFields with the values it contains. In the
  201. // current MaxMind deployment, the City database populates Country and City and
  202. // the separate ISP database populates ISP.
  203. for _, database := range geoIP.databases {
  204. database.ReloadableFile.RLock()
  205. var err error
  206. // Don't lookup the City database when only ISP fields are required;
  207. // skipping the City lookup is 5-10x faster.
  208. if !ISPOnly || database.isISPType {
  209. err = database.maxMindReader.Lookup(IP, &geoIPFields)
  210. }
  211. database.ReloadableFile.RUnlock()
  212. if err != nil {
  213. log.WithTraceFields(LogFields{"error": err}).Warning("GeoIP lookup failed")
  214. }
  215. }
  216. if geoIPFields.Country.ISOCode != "" {
  217. result.Country = geoIPFields.Country.ISOCode
  218. }
  219. name, ok := geoIPFields.City.Names["en"]
  220. if ok && name != "" {
  221. result.City = name
  222. }
  223. if geoIPFields.ISP != "" {
  224. result.ISP = geoIPFields.ISP
  225. }
  226. if geoIPFields.ASN != -1 {
  227. result.ASN = strconv.Itoa(geoIPFields.ASN)
  228. }
  229. if geoIPFields.ASO != "" {
  230. result.ASO = geoIPFields.ASO
  231. }
  232. return result
  233. }
  234. // SetSessionCache adds the sessionID/geoIPData pair to the
  235. // session cache. This value will not expire; the caller must
  236. // call MarkSessionCacheToExpire to initiate expiry.
  237. // Calling SetSessionCache for an existing sessionID will
  238. // replace the previous value and reset any expiry.
  239. func (geoIP *GeoIPService) SetSessionCache(sessionID string, geoIPData GeoIPData) {
  240. geoIP.sessionCache.Set(sessionID, geoIPData, cache.NoExpiration)
  241. }
  242. // MarkSessionCacheToExpire initiates expiry for an existing
  243. // session cache entry, if the session ID is found in the cache.
  244. // Concurrency note: SetSessionCache and MarkSessionCacheToExpire
  245. // should not be called concurrently for a single session ID.
  246. func (geoIP *GeoIPService) MarkSessionCacheToExpire(sessionID string) {
  247. geoIPData, found := geoIP.sessionCache.Get(sessionID)
  248. // Note: potential race condition between Get and Set. In practice,
  249. // the tunnel server won't clobber a SetSessionCache value by calling
  250. // MarkSessionCacheToExpire concurrently.
  251. if found {
  252. geoIP.sessionCache.Set(sessionID, geoIPData, cache.DefaultExpiration)
  253. }
  254. }
  255. // GetSessionCache returns the cached GeoIPData for the
  256. // specified session ID; a blank GeoIPData is returned
  257. // if the session ID is not found in the cache.
  258. func (geoIP *GeoIPService) GetSessionCache(sessionID string) GeoIPData {
  259. geoIPData, found := geoIP.sessionCache.Get(sessionID)
  260. if !found {
  261. return NewGeoIPData()
  262. }
  263. return geoIPData.(GeoIPData)
  264. }
  265. // InSessionCache returns whether the session ID is present
  266. // in the session cache.
  267. func (geoIP *GeoIPService) InSessionCache(sessionID string) bool {
  268. _, found := geoIP.sessionCache.Get(sessionID)
  269. return found
  270. }