better error management + shortcode overflow control

This commit is contained in:
ycc
2026-02-04 18:28:14 +01:00
parent bb3640c1c3
commit b1ecd04a28
6 changed files with 105 additions and 65 deletions

View File

@@ -12,6 +12,8 @@ import (
"github.com/go-redis/redis"
)
const MaxShortcodeLength = 64
func (r *RedisRouter) StoreInvitation(invitation []byte, timeout int, password string, serverTimeout int, urlLen int) (string, time.Time, error) {
id, err := r.createShortId(urlLen)
if err != nil {
@@ -84,10 +86,10 @@ func (r *RedisRouter) GetInvitation(id string, password string) ([]byte, error)
// deleteInvitation removes all invitation-related keys from Redis
func (r *RedisRouter) deleteInvitation(id string) {
r.Client.Del("mwiv:" + id) // invitation data
r.Client.Del("mwpw:" + id) // password
r.Client.Del("mwfa:" + id) // failed attempts
r.Client.Del("mwan:" + id) // answer to invitation
r.Client.Del("mwiv:" + id) // invitation data
r.Client.Del("mwpw:" + id) // password
r.Client.Del("mwfa:" + id) // failed attempts
r.Client.Del("mwan:" + id) // answer to invitation
}
func (r *RedisRouter) StoreAnswerToInvitation(id string, timeout int, invitation []byte, serverTimeout int) time.Time {
@@ -110,6 +112,9 @@ func (r *RedisRouter) createShortId(length int) (string, error) {
alphabet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
alphabetLen := big.NewInt(int64(len(alphabet)))
if length < 1 || length > MaxShortcodeLength {
return "", fmt.Errorf("invalid shortcode length: %d (must be 1-%d)", length, MaxShortcodeLength)
}
// for not in redis
for {
var id strings.Builder