From fc3747a124f4881f43378d38f25259f91c0e34b7 Mon Sep 17 00:00:00 2001 From: ycc Date: Sat, 22 Oct 2022 14:41:48 +0200 Subject: [PATCH] encryption + helpers + contact card compression + server routing start --- asymcrypt.go | 4 +- asymcrypt_test.go | 3 + client/peer.go | 20 +++++-- client/server.go | 8 +++ contactcard.go | 27 +++++++++ contactcard_test.go | 21 +++++++ doc/act_01_send_msg.puml | 22 +++++++ doc/act_02_srv_recv_msg.puml | 22 +++++++ doc/act_03_srv_proc_msg.puml | 20 +++++++ endtoend_test.go | 37 ++++++++++-- go.mod | 5 +- go.sum | 112 +++++++++++++++++++++++++++++++++-- pb/messages.proto | 4 +- server/identity.go | 8 +-- server/router.go | 61 +++++++++++++++++++ 15 files changed, 350 insertions(+), 24 deletions(-) create mode 100644 contactcard_test.go create mode 100644 doc/act_01_send_msg.puml create mode 100644 doc/act_02_srv_recv_msg.puml create mode 100644 doc/act_03_srv_proc_msg.puml create mode 100644 server/router.go diff --git a/asymcrypt.go b/asymcrypt.go index 9175365..19bc333 100644 --- a/asymcrypt.go +++ b/asymcrypt.go @@ -117,11 +117,11 @@ func AsymEncryptAndSign(PublicEncryptionKey string, PrivateSignatureKey string, } func AsymDecryptAndCheck(MyPrivateEncryptionKey string, MyContactPublicKey string, data []byte, Signature []byte) (DecryptedMessage []byte, err error) { - pub, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey) + priv, err := base64.StdEncoding.DecodeString(MyPrivateEncryptionKey) if err != nil { log.Error().Msg("Message decryption and sign b64 failed") } - priv, err := base64.StdEncoding.DecodeString(MyContactPublicKey) + pub, err := base64.StdEncoding.DecodeString(MyContactPublicKey) if err != nil { log.Error().Msg("Message decryption and sign b64 failed") } diff --git a/asymcrypt_test.go b/asymcrypt_test.go index 167389c..6e31d4b 100644 --- a/asymcrypt_test.go +++ b/asymcrypt_test.go @@ -23,6 +23,9 @@ func TestGetKey(t *testing.T) { // fmt.Println(key.Armor()) Armpubkey, _ := key.GetArmoredPublicKey() pubkey := base64.StdEncoding.EncodeToString([]byte(Armpubkey)) + println(len([]byte(pubkey))) + binpubkey, _ := key.GetPublicKey() + println(len(binpubkey)) assert.Equal(t, kp.Public, pubkey, "The two public keys should be the same.") //if kp.Public != pubkey { // log.Fatal("error in public key") diff --git a/client/peer.go b/client/peer.go index 27ca4c9..ca79b1e 100644 --- a/client/peer.go +++ b/client/peer.go @@ -54,12 +54,16 @@ func (pl *PeerList) GetFromName(name string) *Peer { return nil } -func (p *Peer) BuildSimpleUserUMessage(message []byte) ([]byte, error) { +func (p *Peer) BuildSimpleUserUMessage(message []byte) (*meowlib.UserMessage, error) { var msg meowlib.UserMessage msg.From = p.Me.Public msg.Data = message msg.Type = "1" - out, err := proto.Marshal(&msg) + return &msg, nil +} + +func (p *Peer) SerializeUserMessage(msg *meowlib.UserMessage) ([]byte, error) { + out, err := proto.Marshal(msg) if err != nil { return nil, err } @@ -86,8 +90,8 @@ func (p *Peer) AsymDecryptMessage(Message []byte, Signature []byte) (DecryptedMe return DecryptedMessage, err } -// Pack will package the previously encrypted message for sending it to the peer in protobuff format -func (p *Peer) Pack(message []byte, signature []byte) meowlib.PackedUserMessage { +// PackUserMessage will package the previously encrypted message for sending it to the peer in protobuff format +func (p *Peer) PackUserMessage(message []byte, signature []byte) meowlib.PackedUserMessage { var msg meowlib.PackedUserMessage msg.Destination = p.Contact.LookupPublicKey msg.From = p.Me.Public @@ -96,6 +100,14 @@ func (p *Peer) Pack(message []byte, signature []byte) meowlib.PackedUserMessage return msg } +func (ints *InternalServer) UnPackUserMessage(protoPackedMessage []byte) (payload []byte, signature []byte, err error) { + msg := &meowlib.PackedServerMessage{} + if err := proto.Unmarshal(protoPackedMessage, msg); err != nil { + return nil, nil, err + } + return msg.Payload, msg.Signature, nil +} + func (p *Peer) GetConversationRequest() meowlib.ToServerMessage_ConversationRequest { var cr meowlib.ToServerMessage_ConversationRequest return cr diff --git a/client/server.go b/client/server.go index 181ecff..c07f3ff 100644 --- a/client/server.go +++ b/client/server.go @@ -102,3 +102,11 @@ func (ints *InternalServer) PackServerMessage(payload []byte, signature []byte) } return out, nil } + +func (ints *InternalServer) UnPackServerMessage(protoPackedMessage []byte) (payload []byte, signature []byte, err error) { + msg := &meowlib.PackedServerMessage{} + if err := proto.Unmarshal(protoPackedMessage, msg); err != nil { + return nil, nil, err + } + return msg.Payload, msg.Signature, nil +} diff --git a/contactcard.go b/contactcard.go index 04077ca..1745ecd 100644 --- a/contactcard.go +++ b/contactcard.go @@ -1,6 +1,8 @@ package meowlib import ( + "bytes" + "compress/gzip" "encoding/json" "fmt" "image" @@ -27,6 +29,31 @@ func (Contact *ContactCard) AddUrls(urls []string) { } } +func (contact *ContactCard) Serialize() ([]byte, error) { + out, err := proto.Marshal(contact) + if err != nil { + return nil, err + } + return out, nil +} + +func (contact *ContactCard) Compress() ([]byte, error) { + out, err := contact.Serialize() + if err != nil { + return nil, err + } + var b bytes.Buffer + gz := gzip.NewWriter(&b) + if _, err := gz.Write(out); err != nil { + log.Fatal(err) + } + if err := gz.Close(); err != nil { + log.Fatal(err) + } + fmt.Println(b.Bytes()) + return b.Bytes(), nil +} + func (contact *ContactCard) WritePng(filename string) { out, err := proto.Marshal(contact) if err != nil { diff --git a/contactcard_test.go b/contactcard_test.go new file mode 100644 index 0000000..4d2c749 --- /dev/null +++ b/contactcard_test.go @@ -0,0 +1,21 @@ +package meowlib + +import ( + "testing" +) + +func TestCompress(t *testing.T) { + kp1 := NewKeyPair() + kp2 := NewKeyPair() + kp3 := NewKeyPair() + var cc ContactCard + cc.Name = "My Full Name And It's loooong" + cc.ContactPublicKey = kp1.Public + cc.EncryptionPublicKey = kp2.Public + cc.LookupPublicKey = kp3.Public + cc.AddUrls([]string{"https://meow.myfirstdomain.com/services/meow:8080", "https://meow.myseconddomain.com/services/meow:8080", "http://meow.mythirddomain.com/services/meow:8080"}) + serialized, _ := cc.Serialize() + println(len(serialized)) + compressed, _ := cc.Compress() + println(len(compressed)) +} diff --git a/doc/act_01_send_msg.puml b/doc/act_01_send_msg.puml new file mode 100644 index 0000000..5eb0561 --- /dev/null +++ b/doc/act_01_send_msg.puml @@ -0,0 +1,22 @@ +@startuml +#PaleVioletRed:Write text; +fork + :Build user message containing text; +floating note left: Full process + :Serialize User message; + :Encrypt user message; + :Pack user message; + :Build server message containing user packed user message; + :Serialize server message; + :Encrypt server message; + :Pack server message; +fork again + :Build user message containing text; +floating note right: Simplified process with helper functions + :ProcessOutbound user message; + :Build server message containing user packed user message; + :ProcessOutbound server message; +end fork +#Palegreen:Send to server; + +@enduml \ No newline at end of file diff --git a/doc/act_02_srv_recv_msg.puml b/doc/act_02_srv_recv_msg.puml new file mode 100644 index 0000000..2294c25 --- /dev/null +++ b/doc/act_02_srv_recv_msg.puml @@ -0,0 +1,22 @@ +@startuml +#PaleVioletRed:Receive message from peer; +fork + :Unpack to_server message; + floating note left: Full process + :Decrypt to_server message; + :Deserialize to_server message; + :Enqueue to_server message for server processing; + :Build ack from_server message containing to_server message id; + :Serialize from_server message; + :Encrypt from_server message; + :Pack from_server message; +fork again + :ProcessInbound to_server message; + floating note right: Simplified process with helper functions + :Enqueue to_server message for server processing; + :Build ack from_server message containing to_server message id; + :ProcessOutbound from_server message; +end fork +#Palegreen:Send back to peer; + +@enduml \ No newline at end of file diff --git a/doc/act_03_srv_proc_msg.puml b/doc/act_03_srv_proc_msg.puml new file mode 100644 index 0000000..ad7303d --- /dev/null +++ b/doc/act_03_srv_proc_msg.puml @@ -0,0 +1,20 @@ +@startuml +#PaleVioletRed:Decoded to_server message; +fork + :Direct user message; + :Store message to provided lookup key; +fork again + :Matriochka user message; + :Forward to next server; +fork again + :Known servers message; + :Send back known servers list; +fork again + :Broadcast message; + :Store message to 255 lookup key; +fork again + :Admin message; + :Store message to 0 lookup key; +end fork +(*) +@enduml \ No newline at end of file diff --git a/endtoend_test.go b/endtoend_test.go index 4fc2f29..54e4838 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -58,10 +58,10 @@ func TestEndToEnd(t *testing.T) { // Finalize the contact with the invitation response // /////////////////////////////////////////////////////// Me.FinalizeInvitation(MyFirstFriend, &ReceivedContact) - err = Me.Save("id.enc") + /*err = Me.Save("id.enc") if err != nil { fmt.Println(err.Error()) - } + }*/ a, _ = json.Marshal(Me) ioutil.WriteFile("id.json", a, 0644) @@ -75,14 +75,18 @@ func TestEndToEnd(t *testing.T) { if err != nil { fmt.Println(err.Error()) } + serializedMessage, err := MyFirstFriend.SerializeUserMessage(usermessage) + if err != nil { + fmt.Println(err.Error()) + } // Encrypting it - EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage(usermessage) + EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage(serializedMessage) if err != nil { fmt.Println(err.Error()) } fmt.Println(len(FriendServers)) // Packing it - packedMsg := MyFirstFriend.Pack(EncMsg, EncMsgSignature) + packedMsg := MyFirstFriend.PackUserMessage(EncMsg, EncMsgSignature) srv := FriendServers[0] intS1 := client.InternalServerFromServer(srv) @@ -119,15 +123,36 @@ func TestEndToEnd(t *testing.T) { server1.ServerDesc = intS1.ServerData.Description // Unpack srv_from, srv_encmsg, srv_signature, err := server1.UnpackReceived(protoPackedServerMsg) + if err != nil { + fmt.Println(err.Error()) + } // Decrypt srv_clear, err := server1.AsymDecryptMessage(srv_from, srv_encmsg, srv_signature) + if err != nil { + fmt.Println(err.Error()) + } // Decode msg - srv_msg, err := server1.DecodeToServerMessage(srv_clear) + srv_msg, err := server1.DeserializeToServerMessage(srv_clear) + if err != nil { + fmt.Println(err.Error()) + } // Response : Ack received message srv_fromServerMessage, err := server1.BuildSimpleAckResponseMessage(srv_msg.Uuid) - encoded_srv_fromServerMessage, err := server1.EncodeFromServerMessage(srv_fromServerMessage) + if err != nil { + fmt.Println(err.Error()) + } + encoded_srv_fromServerMessage, err := server1.SerializeFromServerMessage(srv_fromServerMessage) + if err != nil { + fmt.Println(err.Error()) + } srv_resp, srv_respsig, err := server1.AsymEncryptMessage(srv_from, encoded_srv_fromServerMessage) + if err != nil { + fmt.Println(err.Error()) + } resp, err := server1.PackForSending(srv_resp, srv_respsig) + if err != nil { + fmt.Println(err.Error()) + } print(resp) ////////////////////////////////////////////// // Back to client, decoding server response // diff --git a/go.mod b/go.mod index 0ade208..1377974 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,13 @@ go 1.16 require ( github.com/ProtonMail/gopenpgp/v2 v2.2.4 + github.com/go-redis/redis v6.15.9+incompatible github.com/google/uuid v1.3.0 github.com/makiuchi-d/gozxing v0.1.1 + github.com/onsi/ginkgo v1.16.5 // indirect + github.com/onsi/gomega v1.22.1 // indirect github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.25.0 - github.com/stretchr/testify v1.4.0 + github.com/stretchr/testify v1.5.1 google.golang.org/protobuf v1.28.1 ) diff --git a/go.sum b/go.sum index e384c4f..2f4d737 100644 --- a/go.sum +++ b/go.sum @@ -5,20 +5,64 @@ github.com/ProtonMail/go-mime v0.0.0-20190923161245-9b5a4261663a h1:W6RrgN/sTxg1 github.com/ProtonMail/go-mime v0.0.0-20190923161245-9b5a4261663a/go.mod h1:NYt+V3/4rEeDuaev/zw1zCq8uqVEuPHzDPo3OZrlGJ4= github.com/ProtonMail/gopenpgp/v2 v2.2.4 h1:PEke+LAMLH9CplflEl8WqGyz2IiDoiiipKkB+3cEWFQ= github.com/ProtonMail/gopenpgp/v2 v2.2.4/go.mod h1:ygdaHbrbWFPhKjmXii0zOs3/xlSR/01GaVePKqv19Hc= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= +github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= +github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/makiuchi-d/gozxing v0.1.1 h1:xxqijhoedi+/lZlhINteGbywIrewVdVv2wl9r5O9S1I= github.com/makiuchi-d/gozxing v0.1.1/go.mod h1:eRIHbOjX7QWxLIDJoQuMLhuXg9LAuw6znsUtRkNw9DU= +github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= +github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= +github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c= +github.com/onsi/ginkgo/v2 v2.1.4/go.mod h1:um6tUpWM/cxCK3/FK8BXqEiUMUwRgSM4JXG47RKZmLU= +github.com/onsi/ginkgo/v2 v2.1.6/go.mod h1:MEH45j8TBi6u9BMogfbp0stKC5cdGjumZj5Y7AG4VIk= +github.com/onsi/ginkgo/v2 v2.3.0/go.mod h1:Eew0uilEqZmIEZr8JrvYlvOM7Rr6xzTmMV8AyFNU9d0= +github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= +github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= +github.com/onsi/gomega v1.20.1/go.mod h1:DtrZpjmvpn2mPm4YWQa0/ALMDj9v4YxLgojwPeREyVo= +github.com/onsi/gomega v1.21.1/go.mod h1:iYAIXgPSaDHak0LCMA+AWBpIKBr8WZicMxnE8luStNc= +github.com/onsi/gomega v1.22.1 h1:pY8O4lBfsHKZHM/6nrxkhVPUznOlIu3quZcKP/M20KI= +github.com/onsi/gomega v1.22.1/go.mod h1:x6n7VNe4hw0vkyYUM4mjIXx3JbLiPaBPNgB7PRQ1tuM= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -31,14 +75,20 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -47,41 +97,93 @@ golang.org/x/mobile v0.0.0-20200801112145-973feb4309de/go.mod h1:skQtrUTUwhdJvXM golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pb/messages.proto b/pb/messages.proto index 24a35e8..fc5d8b0 100644 --- a/pb/messages.proto +++ b/pb/messages.proto @@ -5,8 +5,8 @@ option go_package = "forge.redroom.link/yves/meowlib"; // structure definnig a message as received by a server in protobuf format message PackedServerMessage { string from = 1; // The client public key for that server to get an answer - bytes payload = 2; // The ToServerMessage encrypted with the server public key - bytes signature = 3; // The message signature with the client public key + bytes payload = 2; // The ToServerMessage encrypted with the server public key |or| symetrical encryption as agreed earlier + bytes signature = 3; // The message signature with the client public key |eo| the reference to teh symetrical key used } // structure defining a message for a server, that will be encrypted, then sent in a "packedmessage" payload diff --git a/server/identity.go b/server/identity.go index 25245a4..863a640 100644 --- a/server/identity.go +++ b/server/identity.go @@ -76,7 +76,7 @@ func (id *Identity) AsymDecryptMessage(ClientPublicKey string, Message []byte, S } // Decode decrypted ToServerMessage -func (id *Identity) DecodeToServerMessage(toServerMessage []byte) (*meowlib.ToServerMessage, error) { +func (id *Identity) DeserializeToServerMessage(toServerMessage []byte) (*meowlib.ToServerMessage, error) { tsmsg := &meowlib.ToServerMessage{} if err := proto.Unmarshal(toServerMessage, tsmsg); err != nil { return nil, err @@ -97,7 +97,7 @@ func (id *Identity) ProcessInbound(message []byte) (*meowlib.ToServerMessage, er return nil, err } // Decode msg - srv_msg, err := id.DecodeToServerMessage(srv_clear) + srv_msg, err := id.DeserializeToServerMessage(srv_clear) if err != nil { return nil, err } @@ -115,7 +115,7 @@ func (id *Identity) BuildSimpleAckResponseMessage(uuidToAck string) (fromServerM return &msg, nil } -func (id *Identity) EncodeFromServerMessage(msg *meowlib.FromServerMessage) (encodedFromServerMessage []byte, err error) { +func (id *Identity) SerializeFromServerMessage(msg *meowlib.FromServerMessage) (encodedFromServerMessage []byte, err error) { out, err := proto.Marshal(msg) if err != nil { return nil, err @@ -147,7 +147,7 @@ func (id *Identity) PackForSending(encryptedMessage []byte, signature []byte) (f // Outbound helper function func (id *Identity) ProcessOut(peerPublicKey string, msg *meowlib.FromServerMessage) (fromServerMessage []byte, err error) { - encoded_srv_fromServerMessage, err := id.EncodeFromServerMessage(msg) + encoded_srv_fromServerMessage, err := id.SerializeFromServerMessage(msg) if err != nil { return nil, err } diff --git a/server/router.go b/server/router.go new file mode 100644 index 0000000..0b16e0b --- /dev/null +++ b/server/router.go @@ -0,0 +1,61 @@ +package server + +import ( + "context" + "time" + + "forge.redroom.link/yves/meowlib" + "github.com/go-redis/redis" +) + +type RedisRouter struct { + Name string + ServerIdentity *Identity + Client *redis.Client + Context context.Context +} + +func NewRedisRouter(server *Identity, url string, password string, db int) *RedisRouter { + var r RedisRouter + r.ServerIdentity = server + r.Name = "Redis" + r.Client = redis.NewClient(&redis.Options{ + Addr: url, + Password: password, + DB: db, + }) + r.Context = context.Background() + return &r +} + +func (r *RedisRouter) Route(msg *meowlib.ToServerMessage) *meowlib.FromServerMessage { + var from_server meowlib.FromServerMessage + if len(msg.Messages) > 0 { // user message + for _, usrmsg := range msg.Messages { + r.Client.ZAdd(usrmsg.Destination, redis.Z{float64(time.Now().Unix()), usrmsg.Payload}) + } + from_server.UuidAck = msg.Uuid + } + if len(msg.PullRequest) > 0 { + for _, rq := range msg.PullRequest { + msgcnt := r.Client.ZCount(rq.LookupKey, "-inf", "+inf") + r.Client.ZPopMin(rq.LookupKey, msgcnt.Val()) + } + } + /* + case "q": // query messages + break + + case "s": // servers list + break + + case "m": // matriochka + break + case "b": // broadcast + break + case "a": // admin + break + } + */ + return &from_server +}