redis.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. "encoding/json"
  22. "github.com/Psiphon-Inc/redigo/redis"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  24. )
  25. // UpdateRedisForLegacyPsiWeb sets the Psiphon session and discovery records for
  26. // a new SSH connection following the conventions of the legacy psi_web component.
  27. // This facility is used so psi_web can use the GeoIP values the SSH server has
  28. // resolved for the user connection.
  29. // The redis database indexes, expiry values, and record schemas all match the
  30. // legacy psi_web configuration.
  31. func UpdateRedisForLegacyPsiWeb(psiphonSessionID string, geoIPData GeoIPData) error {
  32. redisSessionDBIndex := 0
  33. // Discard sessions older than 60 minutes
  34. sessionExpireSeconds := 60 * 60
  35. sessionRecord, err := json.Marshal(
  36. struct {
  37. Country string `json:"region"`
  38. City string `json:"city"`
  39. ISP string `json:"isp"`
  40. }{geoIPData.Country, geoIPData.City, geoIPData.ISP})
  41. if err != nil {
  42. return psiphon.ContextError(err)
  43. }
  44. redisDiscoveryDBIndex := 1
  45. // Discard discovery records older than 5 minutes
  46. discoveryExpireSeconds := 60 * 5
  47. discoveryRecord, err := json.Marshal(
  48. struct {
  49. DiscoveryValue int `json:"client_ip_address_strategy_value"`
  50. }{geoIPData.DiscoveryValue})
  51. if err != nil {
  52. return psiphon.ContextError(err)
  53. }
  54. conn := redisPool.Get()
  55. // Note: using SET with NX (set if not exists) so as to not clobber
  56. // any existing records set by an upstream connection server (i.e.,
  57. // meek server). We allow expiry deadline extension unconditionally.
  58. conn.Send("MULTI")
  59. conn.Send("SELECT", redisSessionDBIndex)
  60. // http://redis.io/commands/set -- NX/EX options require Redis 2.6.12
  61. //conn.Send("SET", psiphonSessionID, string(sessionRecord), "NX", "EX", sessionExpireSeconds)
  62. conn.Send("SETNX", psiphonSessionID, string(sessionRecord))
  63. conn.Send("EXPIRE", psiphonSessionID, sessionExpireSeconds)
  64. conn.Send("SELECT", redisDiscoveryDBIndex)
  65. //conn.Send("SET", psiphonSessionID, string(discoveryRecord), "NX", "EX", discoveryExpireSeconds)
  66. conn.Send("SETNX", psiphonSessionID, string(discoveryRecord))
  67. conn.Send("EXPIRE", psiphonSessionID, discoveryExpireSeconds)
  68. _, err = conn.Do("EXEC")
  69. if err != nil {
  70. return psiphon.ContextError(err)
  71. }
  72. return nil
  73. }
  74. var redisPool *redis.Pool
  75. // InitRedis establishes a redis client connection pool and
  76. // also tests at least one single connection.
  77. func InitRedis(config *Config) error {
  78. redisPool = &redis.Pool{
  79. Dial: func() (redis.Conn, error) {
  80. return redis.Dial("tcp", config.RedisServerAddress)
  81. },
  82. MaxIdle: REDIS_POOL_MAX_IDLE,
  83. MaxActive: REDIS_POOL_MAX_ACTIVE,
  84. Wait: false,
  85. IdleTimeout: REDIS_POOL_IDLE_TIMEOUT,
  86. }
  87. // Exercise a connection to the configured redis server so
  88. // that Init fails if the configuration is incorrect or the
  89. // server is not responding.
  90. conn := redisPool.Get()
  91. _, err := conn.Do("PING")
  92. conn.Close()
  93. return err
  94. }