| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- /*
- * Copyright (c) 2016, Psiphon Inc.
- * All rights reserved.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- package server
- import (
- "encoding/json"
- "github.com/Psiphon-Inc/redigo/redis"
- "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
- )
- // UpdateRedisForLegacyPsiWeb sets the Psiphon session and discovery records for
- // a new SSH connection following the conventions of the legacy psi_web component.
- // This facility is used so psi_web can use the GeoIP values the SSH server has
- // resolved for the user connection.
- // The redis database indexes, expiry values, and record schemas all match the
- // legacy psi_web configuration.
- func UpdateRedisForLegacyPsiWeb(psiphonSessionID string, geoIPData GeoIPData) error {
- redisSessionDBIndex := 0
- // Discard sessions older than 60 minutes
- sessionExpireSeconds := 60 * 60
- sessionRecord, err := json.Marshal(
- struct {
- Country string `json:"region"`
- City string `json:"city"`
- ISP string `json:"isp"`
- }{geoIPData.Country, geoIPData.City, geoIPData.ISP})
- if err != nil {
- return psiphon.ContextError(err)
- }
- redisDiscoveryDBIndex := 1
- // Discard discovery records older than 5 minutes
- discoveryExpireSeconds := 60 * 5
- discoveryRecord, err := json.Marshal(
- struct {
- DiscoveryValue int `json:"client_ip_address_strategy_value"`
- }{geoIPData.DiscoveryValue})
- if err != nil {
- return psiphon.ContextError(err)
- }
- conn := redisPool.Get()
- // Note: using SET with NX (set if not exists) so as to not clobber
- // any existing records set by an upstream connection server (i.e.,
- // meek server). We allow expiry deadline extension unconditionally.
- conn.Send("MULTI")
- conn.Send("SELECT", redisSessionDBIndex)
- // http://redis.io/commands/set -- NX/EX options require Redis 2.6.12
- //conn.Send("SET", psiphonSessionID, string(sessionRecord), "NX", "EX", sessionExpireSeconds)
- conn.Send("SETNX", psiphonSessionID, string(sessionRecord))
- conn.Send("EXPIRE", psiphonSessionID, sessionExpireSeconds)
- conn.Send("SELECT", redisDiscoveryDBIndex)
- //conn.Send("SET", psiphonSessionID, string(discoveryRecord), "NX", "EX", discoveryExpireSeconds)
- conn.Send("SETNX", psiphonSessionID, string(discoveryRecord))
- conn.Send("EXPIRE", psiphonSessionID, discoveryExpireSeconds)
- _, err = conn.Do("EXEC")
- if err != nil {
- return psiphon.ContextError(err)
- }
- return nil
- }
- var redisPool *redis.Pool
- // InitRedis establishes a redis client connection pool and
- // also tests at least one single connection.
- func InitRedis(config *Config) error {
- redisPool = &redis.Pool{
- Dial: func() (redis.Conn, error) {
- return redis.Dial("tcp", config.RedisServerAddress)
- },
- MaxIdle: REDIS_POOL_MAX_IDLE,
- MaxActive: REDIS_POOL_MAX_ACTIVE,
- Wait: false,
- IdleTimeout: REDIS_POOL_IDLE_TIMEOUT,
- }
- // Exercise a connection to the configured redis server so
- // that Init fails if the configuration is incorrect or the
- // server is not responding.
- conn := redisPool.Get()
- _, err := conn.Do("PING")
- conn.Close()
- return err
- }
|