2020-04-21 00:48:27 +05:30
|
|
|
package redis
|
2017-06-30 21:10:25 +05:30
|
|
|
|
|
|
|
import (
|
2017-08-10 05:44:28 +05:30
|
|
|
"bytes"
|
|
|
|
"compress/zlib"
|
2017-06-30 21:10:25 +05:30
|
|
|
"encoding/json"
|
2021-03-03 06:02:38 +05:30
|
|
|
"fmt"
|
2017-08-10 05:44:28 +05:30
|
|
|
"io"
|
2019-04-25 04:53:10 +05:30
|
|
|
"strconv"
|
2017-08-10 05:44:28 +05:30
|
|
|
"strings"
|
2017-08-15 03:33:02 +05:30
|
|
|
"time"
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-10 05:44:28 +05:30
|
|
|
"github.com/mediocregopher/radix.v2/pool"
|
2017-06-30 21:10:25 +05:30
|
|
|
"github.com/mediocregopher/radix.v2/redis"
|
|
|
|
"github.com/mediocregopher/radix.v2/util"
|
2017-08-10 05:44:28 +05:30
|
|
|
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/model"
|
2017-06-30 21:10:25 +05:30
|
|
|
)
|
|
|
|
|
2020-04-21 04:50:45 +05:30
|
|
|
var now = time.Now
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func New(addr string, poolSize int) (*Redis, error) {
|
|
|
|
conn, err := pool.New("tcp", addr, poolSize)
|
2017-08-10 05:44:28 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return &Redis{
|
|
|
|
pool: conn,
|
|
|
|
}, nil
|
2017-08-14 23:36:22 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 04:50:45 +05:30
|
|
|
const accountIdToUsernameKey = "hash:username-to-account-id" // TODO: this should be actually "hash:user-id-to-username"
|
2020-04-21 00:48:27 +05:30
|
|
|
const mojangUsernameToUuidKey = "hash:mojang-username-to-uuid"
|
2017-08-10 05:44:28 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
type Redis struct {
|
2019-05-06 19:42:37 +05:30
|
|
|
pool *pool.Pool
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) FindSkinByUsername(username string) (*model.Skin, error) {
|
2019-05-06 19:42:37 +05:30
|
|
|
conn, err := db.pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer db.pool.Put(conn)
|
2019-05-01 04:40:11 +05:30
|
|
|
|
|
|
|
return findByUsername(username, conn)
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func findByUsername(username string, conn util.Cmder) (*model.Skin, error) {
|
|
|
|
redisKey := buildUsernameKey(username)
|
|
|
|
response := conn.Cmd("GET", redisKey)
|
|
|
|
if response.IsType(redis.Nil) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 04:50:45 +05:30
|
|
|
encodedResult, _ := response.Bytes()
|
2020-04-21 00:48:27 +05:30
|
|
|
result, err := zlibDecode(encodedResult)
|
2019-05-06 19:42:37 +05:30
|
|
|
if err != nil {
|
2020-04-21 00:48:27 +05:30
|
|
|
return nil, err
|
2019-05-06 19:42:37 +05:30
|
|
|
}
|
2019-05-01 04:40:11 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
var skin *model.Skin
|
|
|
|
err = json.Unmarshal(result, &skin)
|
2019-05-06 19:42:37 +05:30
|
|
|
if err != nil {
|
2020-04-21 00:48:27 +05:30
|
|
|
return nil, err
|
2019-05-06 19:42:37 +05:30
|
|
|
}
|
2019-05-01 04:40:11 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
skin.OldUsername = skin.Username
|
|
|
|
|
|
|
|
return skin, nil
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) FindSkinByUserId(id int) (*model.Skin, error) {
|
2019-05-06 19:42:37 +05:30
|
|
|
conn, err := db.pool.Get()
|
|
|
|
if err != nil {
|
2020-04-21 00:48:27 +05:30
|
|
|
return nil, err
|
2019-05-06 19:42:37 +05:30
|
|
|
}
|
|
|
|
defer db.pool.Put(conn)
|
2019-05-01 04:40:11 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return findByUserId(id, conn)
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func findByUserId(id int, conn util.Cmder) (*model.Skin, error) {
|
|
|
|
response := conn.Cmd("HGET", accountIdToUsernameKey, id)
|
|
|
|
if response.IsType(redis.Nil) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
username, err := response.Str()
|
2019-05-06 19:42:37 +05:30
|
|
|
if err != nil {
|
2020-04-21 00:48:27 +05:30
|
|
|
return nil, err
|
2019-05-06 19:42:37 +05:30
|
|
|
}
|
2019-04-25 04:53:10 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return findByUsername(username, conn)
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) SaveSkin(skin *model.Skin) error {
|
2019-05-06 19:42:37 +05:30
|
|
|
conn, err := db.pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.pool.Put(conn)
|
2019-05-01 04:40:11 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return save(skin, conn)
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func save(skin *model.Skin, conn util.Cmder) error {
|
|
|
|
conn.Cmd("MULTI")
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
// If user has changed username, then we must delete his old username record
|
|
|
|
if skin.OldUsername != "" && skin.OldUsername != skin.Username {
|
|
|
|
conn.Cmd("DEL", buildUsernameKey(skin.OldUsername))
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
// If this is a new record or if the user has changed username, we set the value in the hash table
|
|
|
|
if skin.OldUsername != "" || skin.OldUsername != skin.Username {
|
|
|
|
conn.Cmd("HSET", accountIdToUsernameKey, skin.UserId, skin.Username)
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
str, _ := json.Marshal(skin)
|
|
|
|
conn.Cmd("SET", buildUsernameKey(skin.Username), zlibEncode(str))
|
|
|
|
|
|
|
|
conn.Cmd("EXEC")
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-17 05:17:35 +05:30
|
|
|
skin.OldUsername = skin.Username
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return nil
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) RemoveSkinByUserId(id int) error {
|
|
|
|
conn, err := db.pool.Get()
|
2020-04-20 17:46:15 +05:30
|
|
|
if err != nil {
|
2020-04-21 00:48:27 +05:30
|
|
|
return err
|
2020-04-20 17:46:15 +05:30
|
|
|
}
|
2020-04-21 00:48:27 +05:30
|
|
|
defer db.pool.Put(conn)
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return removeByUserId(id, conn)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
2017-08-10 05:44:28 +05:30
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
func removeByUserId(id int, conn util.Cmder) error {
|
|
|
|
record, err := findByUserId(id, conn)
|
|
|
|
if err != nil {
|
2020-04-20 17:46:15 +05:30
|
|
|
return err
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
conn.Cmd("MULTI")
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
conn.Cmd("HDEL", accountIdToUsernameKey, id)
|
|
|
|
if record != nil {
|
|
|
|
conn.Cmd("DEL", buildUsernameKey(record.Username))
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Cmd("EXEC")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) RemoveSkinByUsername(username string) error {
|
|
|
|
conn, err := db.pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.pool.Put(conn)
|
|
|
|
|
|
|
|
return removeByUsername(username, conn)
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
func removeByUsername(username string, conn util.Cmder) error {
|
|
|
|
record, err := findByUsername(username, conn)
|
|
|
|
if err != nil {
|
2019-05-06 19:42:37 +05:30
|
|
|
return err
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2020-04-20 17:46:15 +05:30
|
|
|
if record == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
conn.Cmd("MULTI")
|
|
|
|
|
|
|
|
conn.Cmd("DEL", buildUsernameKey(record.Username))
|
2019-05-06 19:42:37 +05:30
|
|
|
conn.Cmd("HDEL", accountIdToUsernameKey, record.UserId)
|
2018-01-23 21:13:37 +05:30
|
|
|
|
|
|
|
conn.Cmd("EXEC")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
func (db *Redis) GetUuid(username string) (string, bool, error) {
|
2020-04-21 00:48:27 +05:30
|
|
|
conn, err := db.pool.Get()
|
|
|
|
if err != nil {
|
2020-04-28 20:27:51 +05:30
|
|
|
return "", false, err
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
2020-04-21 00:48:27 +05:30
|
|
|
defer db.pool.Put(conn)
|
2017-08-17 05:17:35 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return findMojangUuidByUsername(username, conn)
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
func findMojangUuidByUsername(username string, conn util.Cmder) (string, bool, error) {
|
|
|
|
key := strings.ToLower(username)
|
|
|
|
response := conn.Cmd("HGET", mojangUsernameToUuidKey, key)
|
2019-04-25 04:53:10 +05:30
|
|
|
if response.IsType(redis.Nil) {
|
2020-04-28 20:27:51 +05:30
|
|
|
return "", false, nil
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
data, _ := response.Str()
|
|
|
|
parts := strings.Split(data, ":")
|
2021-03-03 06:02:38 +05:30
|
|
|
// https://github.com/elyby/chrly/issues/28
|
2021-02-07 21:17:21 +05:30
|
|
|
if len(parts) < 2 {
|
2021-03-03 06:02:38 +05:30
|
|
|
conn.Cmd("HDEL", mojangUsernameToUuidKey, key)
|
|
|
|
return "", false, fmt.Errorf("Got unexpected response from the mojangUsernameToUuid hash: \"%s\"", data)
|
2021-02-07 21:17:21 +05:30
|
|
|
}
|
|
|
|
|
2019-04-25 04:53:10 +05:30
|
|
|
timestamp, _ := strconv.ParseInt(parts[1], 10, 64)
|
|
|
|
storedAt := time.Unix(timestamp, 0)
|
2020-04-21 04:50:45 +05:30
|
|
|
if storedAt.Add(time.Hour * 24 * 30).Before(now()) {
|
2020-04-28 20:27:51 +05:30
|
|
|
conn.Cmd("HDEL", mojangUsernameToUuidKey, key)
|
|
|
|
return "", false, nil
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
return parts[0], true, nil
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) StoreUuid(username string, uuid string) error {
|
|
|
|
conn, err := db.pool.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer db.pool.Put(conn)
|
|
|
|
|
|
|
|
return storeMojangUuid(username, uuid, conn)
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:42:37 +05:30
|
|
|
func storeMojangUuid(username string, uuid string, conn util.Cmder) error {
|
2020-04-21 04:50:45 +05:30
|
|
|
value := uuid + ":" + strconv.FormatInt(now().Unix(), 10)
|
2019-05-06 19:42:37 +05:30
|
|
|
res := conn.Cmd("HSET", mojangUsernameToUuidKey, strings.ToLower(username), value)
|
|
|
|
if res.IsType(redis.Err) {
|
|
|
|
return res.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) Ping() error {
|
|
|
|
r := db.pool.Cmd("PING")
|
|
|
|
if r.Err != nil {
|
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 05:16:12 +05:30
|
|
|
func (db *Redis) Avail() int {
|
|
|
|
return db.pool.Avail()
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
func buildUsernameKey(username string) string {
|
2017-08-10 05:44:28 +05:30
|
|
|
return "username:" + strings.ToLower(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func zlibEncode(str []byte) []byte {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
writer := zlib.NewWriter(&buff)
|
2019-05-06 19:42:37 +05:30
|
|
|
_, _ = writer.Write(str)
|
|
|
|
_ = writer.Close()
|
2017-08-10 05:44:28 +05:30
|
|
|
|
|
|
|
return buff.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func zlibDecode(bts []byte) ([]byte, error) {
|
|
|
|
buff := bytes.NewReader(bts)
|
2020-04-21 00:48:27 +05:30
|
|
|
reader, err := zlib.NewReader(buff)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-08-10 05:44:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
resultBuffer := new(bytes.Buffer)
|
2019-05-06 19:42:37 +05:30
|
|
|
_, _ = io.Copy(resultBuffer, reader)
|
2020-04-21 00:48:27 +05:30
|
|
|
_ = reader.Close()
|
2017-08-10 05:44:28 +05:30
|
|
|
|
|
|
|
return resultBuffer.Bytes(), nil
|
|
|
|
}
|