geoip.go 8.6 KB

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