2022-10-22 14:41:48 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-19 09:13:50 +02:00
|
|
|
"sync"
|
2022-10-22 14:41:48 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.redroom.link/yves/meowlib"
|
|
|
|
"github.com/go-redis/redis"
|
2022-10-22 22:40:03 +02:00
|
|
|
"google.golang.org/protobuf/proto"
|
2022-10-22 14:41:48 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type RedisRouter struct {
|
2023-08-31 23:38:03 +02:00
|
|
|
Name string
|
|
|
|
ServerIdentity *Identity
|
|
|
|
Client *redis.Client
|
|
|
|
InvitationTimeout int
|
|
|
|
Context context.Context
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 23:43:36 +02:00
|
|
|
func NewRedisRouter(server *Identity, redisUrl string, password string, db int, invitationTimeout int) *RedisRouter {
|
2022-10-22 14:41:48 +02:00
|
|
|
var r RedisRouter
|
|
|
|
r.ServerIdentity = server
|
|
|
|
r.Name = "Redis"
|
|
|
|
r.Client = redis.NewClient(&redis.Options{
|
2023-08-31 23:43:36 +02:00
|
|
|
Addr: redisUrl,
|
2022-10-22 14:41:48 +02:00
|
|
|
Password: password,
|
|
|
|
DB: db,
|
|
|
|
})
|
2023-08-31 23:43:36 +02:00
|
|
|
r.InvitationTimeout = invitationTimeout
|
2022-10-22 14:41:48 +02:00
|
|
|
r.Context = context.Background()
|
2023-12-08 16:52:18 +01:00
|
|
|
// set start for uptime
|
2023-12-09 20:01:19 +01:00
|
|
|
err := r.Client.Set("statistics:start", time.Now().UTC().Format(time.RFC3339), 0).Err()
|
2023-12-08 16:52:18 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-10-22 14:41:48 +02:00
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
2022-10-22 22:40:03 +02:00
|
|
|
func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) (*meowlib.FromServerMessage, error) {
|
2024-04-10 11:18:42 +02:00
|
|
|
var from_server *meowlib.FromServerMessage
|
2023-12-08 16:52:18 +01:00
|
|
|
// update messages counter
|
|
|
|
err := r.Client.Incr("statistics:messages:total").Err()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-03-30 22:44:59 +01:00
|
|
|
// user message => store
|
2023-08-31 23:38:03 +02:00
|
|
|
if len(msg.Messages) > 0 {
|
2024-06-04 22:02:18 +02:00
|
|
|
logger.Info().Msg("storing message")
|
2024-04-10 11:18:42 +02:00
|
|
|
from_server, err = r.storeMessage(msg)
|
2023-12-08 16:52:18 +01:00
|
|
|
if err != nil {
|
2024-04-10 11:18:42 +02:00
|
|
|
return nil, err
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-31 23:38:03 +02:00
|
|
|
// check for messages
|
2022-10-22 14:41:48 +02:00
|
|
|
if len(msg.PullRequest) > 0 {
|
2024-06-04 22:02:18 +02:00
|
|
|
logger.Info().Msg("checking for messages")
|
2024-04-10 11:18:42 +02:00
|
|
|
from_server, err = r.checkForMessage(msg)
|
2023-12-08 16:52:18 +01:00
|
|
|
if err != nil {
|
2024-04-10 11:18:42 +02:00
|
|
|
return nil, err
|
2023-12-08 16:52:18 +01:00
|
|
|
}
|
2024-04-19 09:13:50 +02:00
|
|
|
if msg.Timeout > 0 {
|
2024-06-04 22:02:18 +02:00
|
|
|
logger.Info().Msg("long poll, subscribing for messages")
|
2024-04-19 09:13:50 +02:00
|
|
|
// set timeout for the lookup
|
2024-04-24 20:12:03 +02:00
|
|
|
from_server, err = r.subscribe(msg, int(msg.Timeout))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-19 09:13:50 +02:00
|
|
|
}
|
2022-10-24 18:44:21 +02:00
|
|
|
}
|
2024-05-22 10:06:00 +02:00
|
|
|
// initiate video
|
|
|
|
if msg.VideoData != nil {
|
2024-06-04 22:02:18 +02:00
|
|
|
logger.Info().Msg("handling video")
|
2024-05-22 10:06:00 +02:00
|
|
|
from_server, err = r.handleVideo(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 23:38:03 +02:00
|
|
|
// manage Matriochka
|
2022-12-27 16:59:52 +01:00
|
|
|
if msg.MatriochkaMessage != nil {
|
2024-06-04 22:02:18 +02:00
|
|
|
logger.Info().Msg("handling matriochka")
|
2024-04-10 11:18:42 +02:00
|
|
|
from_server, err = r.handleMatriochka(msg)
|
2022-12-27 16:59:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-08-31 23:38:03 +02:00
|
|
|
// Server list exchange
|
2022-10-24 18:44:21 +02:00
|
|
|
if len(msg.KnownServers) > 0 {
|
|
|
|
|
2023-08-31 23:38:03 +02:00
|
|
|
}
|
|
|
|
// Through server invitation process
|
|
|
|
if msg.Invitation != nil {
|
2024-06-04 22:02:18 +02:00
|
|
|
logger.Info().Msg("handling invitation")
|
2024-04-10 11:18:42 +02:00
|
|
|
from_server, err = r.handleInvitation(msg)
|
2023-12-08 16:52:18 +01:00
|
|
|
if err != nil {
|
2024-04-10 11:18:42 +02:00
|
|
|
return nil, err
|
2023-08-31 23:38:03 +02:00
|
|
|
}
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
|
|
|
|
case "s": // servers list
|
2022-10-22 22:40:03 +02:00
|
|
|
breakmsgs
|
2022-10-22 14:41:48 +02:00
|
|
|
|
|
|
|
case "m": // matriochka
|
|
|
|
break
|
|
|
|
case "b": // broadcast
|
|
|
|
break
|
|
|
|
case "a": // admin
|
|
|
|
break
|
|
|
|
}
|
|
|
|
*/
|
2024-04-10 11:18:42 +02:00
|
|
|
return from_server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisRouter) storeMessage(msg *meowlib.ToServerMessage) (*meowlib.FromServerMessage, error) {
|
|
|
|
var from_server meowlib.FromServerMessage
|
|
|
|
// update messages counter
|
|
|
|
err := r.Client.Incr("statistics:messages:usermessages").Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, usrmsg := range msg.Messages {
|
|
|
|
// serialize the message to store it as byte array into redis
|
|
|
|
out, err := proto.Marshal(usrmsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-23 14:14:12 +02:00
|
|
|
r.Client.ZAdd("msg:"+usrmsg.Destination, redis.Z{Score: float64(time.Now().Unix()), Member: out})
|
|
|
|
r.Client.Publish("msgch:"+usrmsg.Destination, "!")
|
|
|
|
// if delivery tracking resquested, store the uid for the sender's key in delivery tracking
|
|
|
|
if usrmsg.ServerDeliveryUuid != "" {
|
|
|
|
r.Client.SAdd("dvyrq:"+usrmsg.ServerDeliveryUuid, redis.Z{Score: float64(time.Now().Unix()), Member: msg.From})
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:18:42 +02:00
|
|
|
}
|
|
|
|
from_server.UuidAck = msg.Uuid
|
|
|
|
return &from_server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RedisRouter) checkForMessage(msg *meowlib.ToServerMessage) (*meowlib.FromServerMessage, error) {
|
|
|
|
var from_server meowlib.FromServerMessage
|
|
|
|
//dataFound := false
|
|
|
|
// update messages counter
|
|
|
|
err := r.Client.Incr("statistics:messages:messagelookups").Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-23 14:14:12 +02:00
|
|
|
// todo check pull requests signature
|
|
|
|
// iterate over pull requests
|
2024-04-10 11:18:42 +02:00
|
|
|
for _, rq := range msg.PullRequest {
|
|
|
|
// get messages from redis
|
2024-05-23 14:14:12 +02:00
|
|
|
msgcnt, err := r.Client.ZCount("msg:"+rq.LookupKey, "-inf", "+inf").Result()
|
2024-04-10 11:18:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-23 14:14:12 +02:00
|
|
|
res, err := r.Client.ZPopMin("msg:"+rq.LookupKey, msgcnt).Result()
|
2024-04-10 11:18:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// iterate over messages
|
|
|
|
for _, redismsg := range res {
|
|
|
|
//println(redismsg.Score)
|
|
|
|
val := redismsg.Member
|
|
|
|
test := val.(string)
|
|
|
|
var usrmsg meowlib.PackedUserMessage
|
|
|
|
err := proto.Unmarshal([]byte(test), &usrmsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// add server timestamp
|
|
|
|
usrmsg.ServerTimestamp = append(usrmsg.ServerTimestamp, int64(redismsg.Score))
|
|
|
|
from_server.Chat = append(from_server.Chat, &usrmsg)
|
2024-05-23 14:14:12 +02:00
|
|
|
|
|
|
|
// if delivery requested, create, store and publish delivery message
|
|
|
|
res, err := r.Client.SPop("msg:" + usrmsg.ServerDeliveryUuid).Result()
|
|
|
|
if err != nil {
|
|
|
|
if err != redis.Nil { // exit only if real error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != redis.Nil {
|
|
|
|
// create a delivery record
|
|
|
|
r.Client.ZAdd("dvy:"+res, redis.Z{Score: float64(time.Now().Unix()), Member: usrmsg.ServerDeliveryUuid})
|
|
|
|
// publish it in case of listener
|
|
|
|
r.Client.Publish("dvych:"+usrmsg.ServerDeliveryUuid, "!")
|
|
|
|
}
|
2024-04-10 11:18:42 +02:00
|
|
|
}
|
|
|
|
// if no messages check for invitationanswer payload
|
|
|
|
if msgcnt == 0 {
|
|
|
|
// get invitation answer
|
|
|
|
var answer meowlib.Invitation
|
|
|
|
storedAnswer, _ := r.GetAnswerToInvitation(rq.LookupKey)
|
|
|
|
if storedAnswer != nil {
|
|
|
|
err := proto.Unmarshal(storedAnswer, &answer)
|
|
|
|
if err != nil {
|
|
|
|
from_server.Invitation.Payload = []byte("invitation answer corrupted")
|
|
|
|
}
|
|
|
|
from_server.Invitation = &answer
|
|
|
|
// exit loop if invitation found, cannot store several in a message
|
|
|
|
return &from_server, nil
|
|
|
|
}
|
|
|
|
// add invitation answer to the response
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return &from_server, nil
|
|
|
|
}
|
|
|
|
|
2024-04-19 09:13:50 +02:00
|
|
|
func goSubscribeAndListen(client *redis.Client, key string, messages chan<- string, wg *sync.WaitGroup, done <-chan struct{}) {
|
|
|
|
defer wg.Done()
|
2024-05-23 14:14:12 +02:00
|
|
|
pubsub := client.Subscribe("msgch:" + key)
|
2024-04-19 09:13:50 +02:00
|
|
|
defer pubsub.Close()
|
|
|
|
|
|
|
|
// Create a new channel for the messages from this subscription
|
|
|
|
myMessages := make(chan *redis.Message)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, err := pubsub.ReceiveMessage()
|
|
|
|
if err != nil {
|
|
|
|
close(myMessages)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
myMessages <- msg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for a message or for the done signal
|
|
|
|
select {
|
|
|
|
case msg := <-myMessages:
|
|
|
|
messages <- msg.Payload
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:48:37 +02:00
|
|
|
func (r *RedisRouter) subscribe(msg *meowlib.ToServerMessage, timeout int) (*meowlib.FromServerMessage, error) {
|
2024-04-19 09:13:50 +02:00
|
|
|
var from_server meowlib.FromServerMessage
|
|
|
|
// update messages counter
|
|
|
|
err := r.Client.Incr("statistics:messages:messagessubscription").Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
messages := make(chan string)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
done := make(chan struct{})
|
|
|
|
// extract lookup keys and subscribe
|
2024-05-23 14:14:12 +02:00
|
|
|
// iterate over pull requests
|
2024-04-19 09:13:50 +02:00
|
|
|
for _, rq := range msg.PullRequest {
|
|
|
|
wg.Add(1)
|
|
|
|
// subscribe to the lookup key
|
|
|
|
go goSubscribeAndListen(r.Client, rq.LookupKey, messages, &wg, done)
|
|
|
|
}
|
|
|
|
// wait for timeout or message
|
|
|
|
select {
|
|
|
|
case <-messages:
|
|
|
|
close(done)
|
|
|
|
return r.checkForMessage(msg)
|
2024-04-23 22:48:37 +02:00
|
|
|
case <-time.After(time.Duration(timeout) * time.Second): // 10 seconds timeout
|
2024-04-19 09:13:50 +02:00
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return &from_server, nil
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:18:42 +02:00
|
|
|
func (r *RedisRouter) handleInvitation(msg *meowlib.ToServerMessage) (*meowlib.FromServerMessage, error) {
|
|
|
|
var from_server meowlib.FromServerMessage
|
|
|
|
// update messages counter
|
|
|
|
err := r.Client.Incr("statistics:messages:invitation").Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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))
|
|
|
|
from_server.Invitation = &meowlib.Invitation{}
|
|
|
|
from_server.Invitation.Shortcode = url
|
|
|
|
from_server.Invitation.Expiry = expiry.UTC().Unix()
|
|
|
|
// get invitation => retrieve invitation from redis and send
|
|
|
|
case 2:
|
|
|
|
from_server.Invitation = &meowlib.Invitation{}
|
|
|
|
invitation, err := r.GetInvitation(msg.Invitation.Shortcode, msg.Invitation.Password)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "auth failed" {
|
|
|
|
from_server.Invitation.Payload = []byte("authentication failure")
|
|
|
|
} else {
|
|
|
|
from_server.Invitation.Payload = []byte("invitation expired")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
from_server.Invitation.Payload = invitation // protobuf invitation
|
|
|
|
}
|
|
|
|
|
|
|
|
// accept invitation => store accepted invitation for initiator
|
|
|
|
case 3:
|
|
|
|
var usermsg meowlib.PackedUserMessage
|
|
|
|
err := proto.Unmarshal(msg.Invitation.Payload, &usermsg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
data, err := proto.Marshal(msg.Invitation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
expiry := r.StoreAnswerToInvitation(usermsg.Destination, int(msg.Invitation.Timeout), data, r.InvitationTimeout)
|
|
|
|
from_server.Invitation = &meowlib.Invitation{}
|
|
|
|
from_server.Invitation.Expiry = expiry.UTC().Unix()
|
|
|
|
}
|
|
|
|
return &from_server, nil
|
|
|
|
}
|
|
|
|
|
2024-05-22 10:06:00 +02:00
|
|
|
func (r *RedisRouter) handleVideo(msg *meowlib.ToServerMessage) (*meowlib.FromServerMessage, error) {
|
|
|
|
var from_server meowlib.FromServerMessage
|
|
|
|
// update messages counter
|
|
|
|
err := r.Client.Incr("statistics:messages:video").Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
videoData, err := r.ServerIdentity.VideoServer.UpdateVideoData(msg.VideoData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
from_server.VideoData = videoData
|
|
|
|
from_server.UuidAck = msg.Uuid
|
|
|
|
return &from_server, nil
|
|
|
|
}
|
|
|
|
|
2024-04-10 11:18:42 +02:00
|
|
|
func (r *RedisRouter) handleMatriochka(msg *meowlib.ToServerMessage) (*meowlib.FromServerMessage, error) {
|
|
|
|
var from_server meowlib.FromServerMessage
|
|
|
|
// update messages counter
|
|
|
|
err := r.Client.Incr("statistics:messages:matriochka").Err()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.Client.ZAdd("mtk", redis.Z{Score: float64(time.Now().Unix()), Member: out})
|
|
|
|
if msg.MatriochkaMessage.LookupKey != "" {
|
|
|
|
//r.Client.ZAdd("trk:" + msg.MatriochkaMessage.Next.Uuid,{})
|
|
|
|
}
|
|
|
|
from_server.UuidAck = msg.Uuid
|
2022-10-22 22:40:03 +02:00
|
|
|
return &from_server, nil
|
2022-10-22 14:41:48 +02:00
|
|
|
}
|