keypair err mgt + shorturl random improve

This commit is contained in:
ycc
2026-02-02 15:11:41 +01:00
parent f8537aad6d
commit f498cfad1e
23 changed files with 778 additions and 1052 deletions

View File

@@ -16,7 +16,7 @@ const key = "3pw0c8#6ZG8{75b5;3?fe80$2"
type Identity struct {
ServerName string `json:"servername,omitempty"`
ServerDesc string `json:"serverdesc,omitempty"`
ServerKp meowlib.KeyPair `json:"server_kp,omitempty"`
ServerKp *meowlib.KeyPair `json:"server_kp,omitempty"`
Status string `json:"status,omitempty"`
OwnerName string `json:"owner_name,omitempty"`
OwnerPublicKey string `json:"owner_public_key,omitempty"`
@@ -27,9 +27,13 @@ type Identity struct {
func CreateIdentity(ServerName string, ServerDesc string) *Identity {
var id Identity
var err error
id.ServerName = ServerName
id.ServerDesc = ServerDesc
id.ServerKp = meowlib.NewKeyPair()
id.ServerKp, err = meowlib.NewKeyPair()
if err != nil {
return nil
}
return &id
}

View File

@@ -1,15 +1,21 @@
package server
import (
"crypto/rand"
"errors"
"math/rand"
"fmt"
"math/big"
"strings"
"time"
"github.com/go-redis/redis"
)
func (r *RedisRouter) StoreInvitation(invitation []byte, timeout int, password string, serverTimeout int, urlLen int) (string, time.Time) {
id := r.createShortId(urlLen)
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 {
return "", time.Time{}, fmt.Errorf("failed to create invitation ID: %w", err)
}
if timeout > serverTimeout {
timeout = serverTimeout
}
@@ -17,7 +23,7 @@ func (r *RedisRouter) StoreInvitation(invitation []byte, timeout int, password s
if len(password) > 0 {
r.Client.Set("mwpw:"+id, password, 0) //, time.Duration(timeout*1000000))
}
return id, time.Now().Add(time.Duration(timeout * 1000000)).UTC()
return id, time.Now().Add(time.Duration(timeout * 1000000)).UTC(), nil
}
func (r *RedisRouter) GetInvitation(id string, password string) ([]byte, error) {
@@ -54,17 +60,26 @@ func (r *RedisRouter) GetAnswerToInvitation(id string) ([]byte, error) {
return []byte(mwan), nil
}
func (r *RedisRouter) createShortId(length int) string {
id := ""
func (r *RedisRouter) createShortId(length int) (string, error) {
alphabet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
alphabetLen := big.NewInt(int64(len(alphabet)))
// for not in redis
for {
for i := 1; i <= length; i++ {
id += string(alphabet[rand.Intn(61)])
var id strings.Builder
id.Grow(length)
for i := 0; i < length; i++ {
n, err := rand.Int(rand.Reader, alphabetLen)
if err != nil {
return "", fmt.Errorf("random generation failed: %w", err)
}
id.WriteByte(alphabet[n.Int64()])
}
if r.Client.Get("mwiv:"+id).Err() == redis.Nil {
break
idStr := id.String()
if r.Client.Get("mwiv:"+idStr).Err() == redis.Nil {
return idStr, nil
}
}
return id
}

View File

@@ -272,7 +272,10 @@ func (r *RedisRouter) handleInvitation(msg *meowlib.ToServerMessage) (*meowlib.F
switch msg.Invitation.Step {
// create invitation => provide shortcode and expiry
case 1:
url, expiry := r.StoreInvitation(msg.Invitation.Payload, int(msg.Invitation.Timeout), msg.Invitation.Password, r.InvitationTimeout, int(msg.Invitation.ShortcodeLen))
url, expiry, err := r.StoreInvitation(msg.Invitation.Payload, int(msg.Invitation.Timeout), msg.Invitation.Password, r.InvitationTimeout, int(msg.Invitation.ShortcodeLen))
if err != nil {
return nil, err
}
from_server.Invitation = &meowlib.Invitation{}
from_server.Invitation.Shortcode = url
from_server.Invitation.Expiry = expiry.UTC().Unix()