From 3ef38350c60fed4fe3aede7fc419ef6084d2f584 Mon Sep 17 00:00:00 2001 From: ycc Date: Mon, 19 Sep 2022 13:26:27 +0200 Subject: [PATCH] Client to Server messaging --- .gitignore | 1 + client/server.go | 23 +++++++++++++++++-- endtoend_test.go | 60 +++++++++++++++++++++--------------------------- 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/.gitignore b/.gitignore index b5a24f4..70627d6 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ out/doc/server_deployment/server_deployment.png *.json test id.enc +invitation.png diff --git a/client/server.go b/client/server.go index e4b34d1..51b560b 100644 --- a/client/server.go +++ b/client/server.go @@ -29,6 +29,13 @@ func InternalServerFromUrl(url string) *InternalServer { return &is } +func InternalServerFromServer(server *meowlib.Server) *InternalServer { + var is InternalServer + is.ServerData = *server + is.Me = meowlib.NewKeyPair() + return &is +} + func (sl *InternalServerList) AddUrls(urls []string) { for _, url := range urls { sl.Servers = append(sl.Servers, *InternalServerFromUrl(url)) @@ -56,7 +63,7 @@ func (ints *InternalServer) AsymDecryptMessage(Message []byte, Signature []byte) } // Creates a basic message to server from a single packed user message and returns it as protobuf serialized byte array -func (ints *InternalServer) CreateMessageSendingMessage(usermsg *meowlib.PackedUserMessage) ([]byte, error) { +func (ints *InternalServer) BuildMessageSendingMessage(usermsg *meowlib.PackedUserMessage) ([]byte, error) { var msg meowlib.ToServerMessage msg.Type = "1" msg.From = ints.Me.Public @@ -69,7 +76,7 @@ func (ints *InternalServer) CreateMessageSendingMessage(usermsg *meowlib.PackedU } // Creates a basic message to server from a single packed user message and returns it as protobuf serialized byte array -func (ints *InternalServer) CreateMessageRequestMessage(lookupKeys []string) ([]byte, error) { +func (ints *InternalServer) BuildMessageRequestMessage(lookupKeys []string) ([]byte, error) { var msg meowlib.ToServerMessage msg.Type = "1" msg.From = ints.Me.Public @@ -80,3 +87,15 @@ func (ints *InternalServer) CreateMessageRequestMessage(lookupKeys []string) ([] } return out, nil } + +func (ints *InternalServer) PackServerMessage(payload []byte, signature []byte) (protoPackedMessage []byte, err error) { + var msg meowlib.PackedServerMessage + msg.From = ints.Me.Public + msg.Payload = payload + msg.Signature = signature + out, err := proto.Marshal(&msg) + if err != nil { + return nil, err + } + return out, nil +} diff --git a/endtoend_test.go b/endtoend_test.go index 3946647..f528931 100644 --- a/endtoend_test.go +++ b/endtoend_test.go @@ -48,15 +48,12 @@ func TestEndToEnd(t *testing.T) { ReceivedContact.ContactPublicKey = FirstFriendContactKp.Public ReceivedContact.EncryptionPublicKey = FirstFriendEncryptionKp.Public ReceivedContact.LookupPublicKey = FirstFriendLookupKp.Public - ReceivedContact.AddUrls([]string{"http://myfriend.org/meow/"}) + FriendServer1KP := meowlib.NewKeyPair() + FriendServer1 := meowlib.Server{Name: "FriendServer1", Url: "http://myfriend.org/meow/", PublicKey: FriendServer1KP.Public, Description: "Fancy description", ConfidenceLevel: 1} + ReceivedContact.PullServers = append(ReceivedContact.PullServers, &FriendServer1) // end Friend simulated invitation - // End simulating contact invitation response - // - - // // Finalize the contact with the invitation response - // Me.FinalizeInvitation(MyFirstFriend, &ReceivedContact) err = Me.Save("id.enc") if err != nil { @@ -69,44 +66,39 @@ func TestEndToEnd(t *testing.T) { // create message to simulated friend sentmessage := "Hello friend!" - EncMsg, Signature, Servers, err := MyFirstFriend.AsymEncryptMessage([]byte(sentmessage)) + EncMsg, EncMsgSignature, FriendServers, err := MyFirstFriend.AsymEncryptMessage([]byte(sentmessage)) if err != nil { fmt.Println(err.Error()) } + fmt.Println(len(FriendServers)) + packedMsg := MyFirstFriend.Pack(EncMsg, EncMsgSignature) - fmt.Println(len(Servers)) - // simulated friend decoding the message - //ReadMessage + srv := FriendServers[0] + intS1 := client.InternalServerFromServer(srv) - // simulates if peer can decrypt my message - //Message := "toto" - //Signature := "test" - decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), Signature) + // Creating Server message + toServerMessage, err := intS1.BuildMessageSendingMessage(&packedMsg) + // Encrypting it + encToServerMessage, encToServerMessageSignature, err := intS1.AsymEncryptMessage(toServerMessage) + // Packing it + protoPackedServerMsg, err := intS1.PackServerMessage(encToServerMessage, encToServerMessageSignature) + // Sending to server => fake action (network protocol trnasfer are not part of meowlib) + // You have to implement the netwok layer + println(len(protoPackedServerMsg)) + + // + // Simulating server side processing + // + + // + // Back to client, decoding server response + // + decMess, err2 := MyFirstFriend.AsymDecryptMessage([]byte(EncMsg), EncMsgSignature) if err2 != nil { fmt.Println(err2.Error()) } fmt.Println(decMess) - // simulates a new server to send a message to - var intS1 client.InternalServer - intS1.ServerData.Name = "My friend's Server 1" - intS1.ServerData.Description = "My friend's Server 1" - intS1.Me = meowlib.NewKeyPair() - intS1.ServerData.Url = "http://myfriend.org/meow/" - KP := meowlib.NewKeyPair() - intS1.ServerData.PublicKey = KP.Public - // sends a message to server 1 - SrvEncrypted, SrvSign, err3 := intS1.AsymEncryptMessage([]byte(sentmessage)) - if err3 != nil { - fmt.Println(err3.Error()) - } - - // tests simulated server decrypted message - SrvDecrypted, err4 := intS1.AsymDecryptMessage(SrvEncrypted, SrvSign) - if err4 != nil { - fmt.Println(err4.Error()) - } - fmt.Println("--- SrvDecryptedMess = ", string(SrvDecrypted)) } }