From a2d4106d519c93e0be812573284deca4d2c2a75f Mon Sep 17 00:00:00 2001 From: ycc Date: Sun, 18 Dec 2022 20:59:27 +0100 Subject: [PATCH] refactor #3 --- client/message.go | 74 ++--------------------------------------------- client/peer.go | 29 +++++++++++++++++++ client/server.go | 34 ++++++++++++++++++++++ 3 files changed, 65 insertions(+), 72 deletions(-) diff --git a/client/message.go b/client/message.go index d68caf3..eec7016 100644 --- a/client/message.go +++ b/client/message.go @@ -1,29 +1,5 @@ package client -import ( - "fmt" - - "forge.redroom.link/yves/meowlib" - "google.golang.org/protobuf/proto" -) - -func ProcessOutboundUserMessage(peer *Peer, usermessage *meowlib.UserMessage) (*meowlib.PackedUserMessage, error) { - serializedMessage, err := peer.SerializeUserMessage(usermessage) - if err != nil { - return nil, err - } - // Encrypting it - EncMsg, EncMsgSignature, FriendServers, err := peer.AsymEncryptMessage(serializedMessage) - if err != nil { - return nil, err - } - fmt.Println(len(FriendServers)) - // Packing it - packedMsg := peer.PackUserMessage(EncMsg, EncMsgSignature) - - return packedMsg, nil -} - func ProcessOutboundTextMessage(peer *Peer, text string, srv *InternalServer) ([]byte, error) { // Creating User message usermessage, err := peer.BuildSimpleUserMessage([]byte(text)) @@ -31,57 +7,11 @@ func ProcessOutboundTextMessage(peer *Peer, text string, srv *InternalServer) ([ return nil, err } // Prepare cyphered + packed user message - packedMsg, err := ProcessOutboundUserMessage(peer, usermessage) + packedMsg, err := peer.ProcessOutboundUserMessage(usermessage) if err != nil { return nil, err } // Creating Server message for transporting the user message toServerMessage := srv.BuildToServerMessageFromUserMessage(packedMsg) - return ProcessOutboundMessage(toServerMessage, srv) -} - -func ProcessOutboundMessage(toServerMessage *meowlib.ToServerMessage, srv *InternalServer) ([]byte, error) { - byteToServerMessage, err := proto.Marshal(toServerMessage) - if err != nil { - return nil, err - } - // Encrypting it - encToServerMessage, encToServerMessageSignature, err := srv.AsymEncryptMessage(byteToServerMessage) - if err != nil { - return nil, err - } - // Packing it - protoPackedServerMsg, err := srv.PackServerMessage(encToServerMessage, encToServerMessageSignature) - if err != nil { - return nil, err - } - return protoPackedServerMsg, nil -} - -func ProcessInboundServerResponse(srv *InternalServer, msg []byte) (*meowlib.FromServerMessage, error) { - fsmsg := &meowlib.FromServerMessage{} - payload, signature, err := srv.UnPackServerMessage(msg) - if err != nil { - return nil, err - } - decrypted, err := srv.AsymDecryptMessage(payload, signature) - if err != nil { - return nil, err - } - if err := proto.Unmarshal(decrypted, fsmsg); err != nil { - return nil, err - } - return fsmsg, nil -} - -func ProcessInboundUserMessage(peer *Peer, message []byte, signature []byte) (*meowlib.UserMessage, error) { - dec, err := peer.AsymDecryptMessage(message, signature) - if err != nil { - return nil, err - } - msg, err := peer.DeserializeUserMessage(dec) - if err != nil { - return nil, err - } - return msg, nil + return srv.ProcessOutboundMessage(toServerMessage) } diff --git a/client/peer.go b/client/peer.go index 00ceea8..3fb50d5 100644 --- a/client/peer.go +++ b/client/peer.go @@ -197,6 +197,35 @@ func (p *Peer) GetConversationRequest() meowlib.ToServerMessage_ConversationRequ return cr } +func (p *Peer) ProcessOutboundUserMessage(usermessage *meowlib.UserMessage) (*meowlib.PackedUserMessage, error) { + serializedMessage, err := p.SerializeUserMessage(usermessage) + if err != nil { + return nil, err + } + // Encrypting it + EncMsg, EncMsgSignature, FriendServers, err := p.AsymEncryptMessage(serializedMessage) + if err != nil { + return nil, err + } + fmt.Println(len(FriendServers)) + // Packing it + packedMsg := p.PackUserMessage(EncMsg, EncMsgSignature) + + return packedMsg, nil +} + +func (p *Peer) ProcessInboundUserMessage(message []byte, signature []byte) (*meowlib.UserMessage, error) { + dec, err := p.AsymDecryptMessage(message, signature) + if err != nil { + return nil, err + } + msg, err := p.DeserializeUserMessage(dec) + if err != nil { + return nil, err + } + return msg, nil +} + func (p *Peer) StoreMessage(msg []byte) { } diff --git a/client/server.go b/client/server.go index aa2092a..45229a2 100644 --- a/client/server.go +++ b/client/server.go @@ -116,3 +116,37 @@ func (ints *InternalServer) UnPackServerMessage(protoPackedMessage []byte) (payl } return msg.Payload, msg.Signature, nil } + +func (srv *InternalServer) ProcessOutboundMessage(toServerMessage *meowlib.ToServerMessage) ([]byte, error) { + byteToServerMessage, err := proto.Marshal(toServerMessage) + if err != nil { + return nil, err + } + // Encrypting it + encToServerMessage, encToServerMessageSignature, err := srv.AsymEncryptMessage(byteToServerMessage) + if err != nil { + return nil, err + } + // Packing it + protoPackedServerMsg, err := srv.PackServerMessage(encToServerMessage, encToServerMessageSignature) + if err != nil { + return nil, err + } + return protoPackedServerMsg, nil +} + +func (srv *InternalServer) ProcessInboundServerResponse(msg []byte) (*meowlib.FromServerMessage, error) { + fsmsg := &meowlib.FromServerMessage{} + payload, signature, err := srv.UnPackServerMessage(msg) + if err != nil { + return nil, err + } + decrypted, err := srv.AsymDecryptMessage(payload, signature) + if err != nil { + return nil, err + } + if err := proto.Unmarshal(decrypted, fsmsg); err != nil { + return nil, err + } + return fsmsg, nil +}