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"
|
2023-12-14 06:45:59 +05:30
|
|
|
"context"
|
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
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
"github.com/mediocregopher/radix/v4"
|
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
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
func New(ctx context.Context, addr string, poolSize int) (*Redis, error) {
|
|
|
|
client, err := (radix.PoolConfig{Size: poolSize}).New(ctx, "tcp", addr)
|
2017-08-10 05:44:28 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return &Redis{
|
2023-12-14 06:45:59 +05:30
|
|
|
client: client,
|
|
|
|
context: ctx,
|
2020-04-21 00:48:27 +05:30
|
|
|
}, 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 {
|
2023-12-14 06:45:59 +05:30
|
|
|
client radix.Client
|
|
|
|
context context.Context
|
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) {
|
2023-12-14 06:45:59 +05:30
|
|
|
var skin *model.Skin
|
|
|
|
err := db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
|
|
|
var err error
|
|
|
|
skin, err = findByUsername(ctx, conn, username)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}))
|
2019-05-01 04:40:11 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
return skin, err
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
func findByUsername(ctx context.Context, conn radix.Conn, username string) (*model.Skin, error) {
|
2020-04-21 00:48:27 +05:30
|
|
|
redisKey := buildUsernameKey(username)
|
2023-12-14 06:45:59 +05:30
|
|
|
var encodedResult []byte
|
|
|
|
err := conn.Do(ctx, radix.Cmd(&encodedResult, "GET", redisKey))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(encodedResult) == 0 {
|
2020-04-21 00:48:27 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-12-14 06:45:59 +05:30
|
|
|
var skin *model.Skin
|
|
|
|
err := db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
|
|
|
var err error
|
|
|
|
skin, err = findByUserId(ctx, conn, id)
|
2019-05-01 04:40:11 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
return err
|
|
|
|
}))
|
2018-01-23 21:13:37 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
return skin, err
|
|
|
|
}
|
2020-04-21 00:48:27 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
func findByUserId(ctx context.Context, conn radix.Conn, id int) (*model.Skin, error) {
|
|
|
|
var username string
|
|
|
|
err := conn.Do(ctx, radix.FlatCmd(&username, "HGET", accountIdToUsernameKey, id))
|
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
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
if username == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return findByUsername(ctx, conn, username)
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) SaveSkin(skin *model.Skin) error {
|
2023-12-14 06:45:59 +05:30
|
|
|
return db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
|
|
|
return save(ctx, conn, skin)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(ctx context.Context, conn radix.Conn, skin *model.Skin) error {
|
|
|
|
err := conn.Do(ctx, radix.Cmd(nil, "MULTI"))
|
2019-05-06 19:42:37 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "DEL", buildUsernameKey(skin.OldUsername)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.FlatCmd(nil, "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)
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.FlatCmd(nil, "SET", buildUsernameKey(skin.Username), zlibEncode(str)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-21 00:48:27 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "EXEC"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 {
|
2023-12-14 06:45:59 +05:30
|
|
|
return db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
|
|
|
return removeByUserId(ctx, conn, id)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeByUserId(ctx context.Context, conn radix.Conn, id int) error {
|
|
|
|
record, err := findByUserId(ctx, conn, id)
|
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
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "MULTI"))
|
2018-01-23 21:13:37 +05:30
|
|
|
if err != nil {
|
2020-04-20 17:46:15 +05:30
|
|
|
return err
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.FlatCmd(nil, "HDEL", accountIdToUsernameKey, id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-17 05:17:35 +05:30
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
if record != nil {
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "DEL", buildUsernameKey(record.Username)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
return conn.Do(ctx, radix.Cmd(nil, "EXEC"))
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) RemoveSkinByUsername(username string) error {
|
2023-12-14 06:45:59 +05:30
|
|
|
return db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
|
|
|
return removeByUsername(ctx, conn, username)
|
|
|
|
}))
|
2020-04-21 00:48:27 +05:30
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
func removeByUsername(ctx context.Context, conn radix.Conn, username string) error {
|
|
|
|
record, err := findByUsername(ctx, conn, username)
|
2018-01-23 21:13:37 +05:30
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "MULTI"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-23 21:13:37 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "DEL", buildUsernameKey(record.Username)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-23 21:13:37 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.FlatCmd(nil, "HDEL", accountIdToUsernameKey, record.UserId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-23 21:13:37 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
return conn.Do(ctx, radix.Cmd(nil, "EXEC"))
|
2018-01-23 21:13:37 +05:30
|
|
|
}
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
func (db *Redis) GetUuid(username string) (string, bool, error) {
|
2023-12-14 06:45:59 +05:30
|
|
|
var uuid string
|
|
|
|
var found bool
|
|
|
|
err := db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
|
|
|
var err error
|
|
|
|
uuid, found, err = findMojangUuidByUsername(ctx, conn, username)
|
2017-08-17 05:17:35 +05:30
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
return err
|
|
|
|
}))
|
|
|
|
|
|
|
|
return uuid, found, err
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
func findMojangUuidByUsername(ctx context.Context, conn radix.Conn, username string) (string, bool, error) {
|
2020-04-28 20:27:51 +05:30
|
|
|
key := strings.ToLower(username)
|
2023-12-14 06:45:59 +05:30
|
|
|
var result string
|
|
|
|
err := conn.Do(ctx, radix.Cmd(&result, "HGET", mojangUsernameToUuidKey, key))
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result == "" {
|
2020-04-28 20:27:51 +05:30
|
|
|
return "", false, nil
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
parts := strings.Split(result, ":")
|
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 {
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "HDEL", mojangUsernameToUuidKey, key))
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", false, fmt.Errorf("got unexpected response from the mojangUsernameToUuid hash: \"%s\"", result)
|
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()) {
|
2023-12-14 06:45:59 +05:30
|
|
|
err = conn.Do(ctx, radix.Cmd(nil, "HDEL", mojangUsernameToUuidKey, key))
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
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 {
|
2023-12-14 06:45:59 +05:30
|
|
|
return db.client.Do(db.context, radix.WithConn("", func(ctx context.Context, conn radix.Conn) error {
|
|
|
|
return storeMojangUuid(ctx, conn, username, uuid)
|
|
|
|
}))
|
2020-04-21 00:48:27 +05:30
|
|
|
}
|
|
|
|
|
2023-12-14 06:45:59 +05:30
|
|
|
func storeMojangUuid(ctx context.Context, conn radix.Conn, username string, uuid string) error {
|
2020-04-21 04:50:45 +05:30
|
|
|
value := uuid + ":" + strconv.FormatInt(now().Unix(), 10)
|
2023-12-14 06:45:59 +05:30
|
|
|
err := conn.Do(ctx, radix.Cmd(nil, "HSET", mojangUsernameToUuidKey, strings.ToLower(username), value))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-05-06 19:42:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-04-25 04:53:10 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func (db *Redis) Ping() error {
|
2023-12-14 06:45:59 +05:30
|
|
|
return db.client.Do(db.context, radix.Cmd(nil, "PING"))
|
2020-05-01 05:16:12 +05:30
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|